#define _USE_MATH_DEFINES #include "bits/stdc++.h" using namespace std; #define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i)) #define rep(i,j) FOR(i,0,j) #define each(x,y) for(auto &(x):(y)) #define mp make_pair #define MT make_tuple #define all(x) (x).begin(),(x).end() #define debug(x) cout<<#x<<": "<<(x)<; using vi = vector; using vll = vector; struct P{ ll a,b,c; }; void solve() { int W; cin >> W; if (W == 1) { cout << 0 << endl; return; } vll A(W), B(W), C(W); rep(i, W) { cin >> A[i] >> B[i] >> C[i]; } auto g = [&](vector

p) { vi S{ 0 }; for (int i = 1; i < sz(p); ++i) { int n = sz(S); rep(j, n) { int U = S[j]; if (!(U >> j & (i - 1))) { S.push_back(U | 1 << i); } } } int n = sz(S); // (money, likes) vector > res(n); rep(i, n) { int U = S[i]; if (sz(p) > 0) { res[i].first += p[0].a; } for (int j = 1; j < sz(p); ++j) { if (U >> j & 1) { // 前回はアルバイトができなかった res[i].first -= p[j - 1].a + p[j].c; res[i].second += p[j].b; } else { res[i].first += p[j].a; } } } sort(all(res)); // (money, likes) vector > so; each(r, res) { while (sz(so) && so.back().second <= r.second) { so.pop_back(); } so.push_back(r); } return so; }; ll ans = 0; auto f = [&](int n) { // [0, n), [n, W) vector

pre, pos; rep(i, n) { pre.push_back(P{ A[i],B[i],C[i] }); } FOR(i, n, W) { pos.push_back(P{ A[i],B[i],C[i] }); } auto L = g(pre); auto R = g(pos); each(le, L) { auto itri = lower_bound(all(R), pair(-le.first, 0)); if (itri == R.end()) { continue; } assert(le.first + itri->first >= 0); smax(ans, le.second + itri->second); } }; f(W / 2); f(W / 2 - 1); cout << ans << endl; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(15); solve(); return 0; }