#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; } #define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl template ostream& operator << (ostream &s, pair P) { return s << '<' << P.first << ", " << P.second << '>'; } template ostream& operator << (ostream &s, vector P) { for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; } template ostream& operator << (ostream &s, vector > P) { for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; } #define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i) template ostream& operator << (ostream &s, map P) { EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; } return s << endl; } using pll = pair; const long long INF = 1LL<<60; int N; vector a, b, c; // 後半の各所持金差額に対する獲得好感度の差分ん最大値を求める 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); long long cur = -INF; map maxlatter; maxlatter[-INF] = -INF; for (auto it : latter) maxlatter[it.first] = max(cur, it.second); //COUT(former); COUT(latter); COUT(maxlatter); 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); //cout << pll(it.first, it.second) << ": " << pll(it2->first, it2->second) << endl; } return res; } int main() { while (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; } }