import std.conv, std.functional, std.range, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons; import core.bitop; class EOFException : Throwable { this() { super("EOF"); } } string[] tokens; string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; } int readInt() { return readToken.to!int; } long readLong() { return readToken.to!long; } real readReal() { return readToken.to!real; } bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } } bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } } int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; } int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); } int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); } enum INF = 10L^^18; void main() { try { for (; ; ) { const W = readInt(); auto A = new long[W]; auto B = new long[W]; auto C = new long[W]; foreach (i; 0 .. W) { A[i] = readLong(); B[i] = readLong(); C[i] = readLong(); } long ans; foreach (m; W / 2 .. W / 2 + 2) { alias Entry = Tuple!(long, "x", long, "y"); auto ess = new Entry[][2]; void dfs(int s, int r, int i, long x, long y) { if (i == r) { ess[s] ~= Entry(x, y); return; } if (i + 1 <= r) { dfs(s, r, i + 1, x + A[i], y); } if (i + 2 <= r) { dfs(s, r, i + 2, x - C[i + 1], y + B[i + 1]); } } dfs(0, m, 0, 0, 0); dfs(1, W, m, 0, 0); ess[0].sort; ess[1].sort; debug { writeln("m = ", m); writeln("ess[0] = ", ess[0]); writeln("ess[1] = ", ess[1]); } const len0 = cast(int)(ess[0].length); const len1 = cast(int)(ess[1].length); long mx = -INF; for (int j = 0, k = len1; j < len0; ++j) { for (; k > 0 && ess[0][j].x + ess[1][k - 1].x >= 0; ) { chmax(mx, ess[1][--k].y); } chmax(ans, ess[0][j].y + mx); } } writeln(ans); } } catch (EOFException e) { } }