#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include 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; } constexpr long long MAX = 5100000; constexpr long long INF = 1LL << 60; constexpr int inf = 1 << 28; //constexpr long long mod = 1000000007LL; //constexpr long long mod = 998244353LL; using namespace std; typedef unsigned long long ull; typedef long long ll; ll W; vector a, b, c; vector> vp[2]; //好感度, お金 void dfs(ll now, ll end, ll love, ll money, int t) { if (now + 1 == end) { dfs(now + 1, end, love, money + a[now], t); return; } if (now == end) { vp[t].emplace_back(money, love); return; } dfs(now + 1, end, love, money + a[now], t); dfs(now + 2, end, love + b[now + 1], money - c[now + 1], t); } ll solve(ll h) { vp[0].clear(); vp[1].clear(); dfs(0, h, 0, 0, 0); dfs(h, W, 0, 0, 1); for (int i = 0; i < 2; i++) { sort(vp[i].begin(), vp[i].end()); for (int j = (int)vp[i].size() - 2; j >= 0; j--) { chmax(vp[i][j].second, vp[i][j + 1].second); } } ll res = 0; for (int i = 0; i < vp[0].size(); i++) { auto itr = lower_bound(vp[1].begin(), vp[1].end(), make_pair(-vp[0][i].first,-INF)); if (itr == vp[1].end()) continue; chmax(res, vp[0][i].second + itr->second); } return res; } int main() { /* cin.tie(nullptr); ios::sync_with_stdio(false); */ scanf("%lld", &W); a.resize(W), b.resize(W), c.resize(W); for (int i = 0; i < W; i++) scanf("%lld %lld %lld", &a[i], &b[i], &c[i]); cout << max(solve(W / 2), solve(W / 2 + 1)) << endl; return 0; /* おまじないを使ったらscanfとprintf関連注意!!!!!!!!!!!! */ }