#include #include #include #include using namespace std; template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } using pll = pair; const long long INF = 1LL<<60; int N; vector a, b, c; // ma[所持金 down 分] = 好感度 up 分 void rec(int n, int finish, map &ma, long long syoji = 0, long long love = 0) { if (n == finish) { chmax(ma[-syoji], love); return; } // アルバイト rec(n + 1, finish, ma, syoji + a[n], love); // 学校からのデート if (n + 2 <= finish) rec(n + 2, finish, ma, syoji - c[n+1], love + b[n+1]); } long long solve(int mid) { // 前半と後半 map former, latter; rec(0, mid, former); rec(mid, N, latter); // 後半の累積 max long long cur = -INF; map maxlatter; maxlatter[-INF] = -INF; for (auto it : latter) { maxlatter[it.first] = max(cur, it.second); chmax(cur, it.second); } // まとめ long long res = 0; for (auto it : former) { long long need = -(it.first); auto it2 = maxlatter.upper_bound(need); --it2; chmax(res, it.second + it2->second); } return res; } int main() { cin >> N; a.resize(N), b.resize(N), c.resize(N); for (int i = 0; i < N; ++i) cin >> a[i] >> b[i] >> c[i]; cout << max(solve(N/2), solve(N/2+1)) << endl; }