#include using namespace std; using i64 = long long; using pii = pair; i64 w, mid, a[55], b[55], c[55]; i64 maxl = 0; vector ans[55]; void dfs1(int l, int r, i64 x, i64 y) { if(l >= r) { ans[l].push_back({x, y}); return; } dfs1(l + 1, r, x + a[l], y); dfs1(l + 2, r, x - c[l + 1], y + b[l + 1]); } void dfs2(int r, i64 x, i64 y) { if(r < mid - 1) return; if(ans[r + 1].size()) { maxl = max(maxl, lower_bound(ans[r + 1].begin(), ans[r + 1].end(), pii{-x, 0})->second + y); } dfs2(r - 1, x + a[r], y); dfs2(r - 2, x - c[r], y + b[r]); } int main() { cin >> w; for(int i = 0; i < w; i ++) cin >> a[i] >> b[i] >> c[i]; mid = w / 2; dfs1(0, mid, 0, 0); for(auto t : {mid, mid + 1}) { sort(ans[t].begin(), ans[t].end()); ans[t].push_back({1e18, -1e18}); for(int i = (int)ans[t].size() - 2; i >= 0; i --) ans[t][i].second = max(ans[t][i].second, ans[t][i + 1].second); } dfs2(w - 1, 0, 0); cout << maxl; }