#include #include #include #include using namespace std; long long INF = 1e18; int main() { int W; cin >> W; vector a(W), b(W), c(W); for (int i = 0; i < W; i++) { cin >> a[i] >> b[i] >> c[i]; } map m;//m[money] = love auto dfs1 = [&](auto dfs1, long long love, long long money, int l, int r) -> void { if (l == r) { m[money] = max(m[money], love); return; } dfs1(dfs1, love, money + a[l], l + 1, r); if (l + 2 <= r) { dfs1(dfs1, love + b[l + 1], money - c[l + 1], l + 2, r); } }; long long ans = 0; map m2; auto dfs2 = [&](auto dfs2, long long love, long long money, int l, int r) -> void { if (l == r) { long long tmp = m2.lower_bound(-money)->second + love; ans = max(ans, tmp); return; } dfs2(dfs2, love, money + a[l], l + 1, r); if (l + 2 <= r) { dfs2(dfs2, love + b[l + 1], money - c[l + 1], l + 2, r); } }; int mid = W / 2; dfs1(dfs1, 0LL, 0LL, 0, mid); m[INF] = -INF; auto itr = m.end(); long long tmp = -INF; while (itr != m.begin()) { itr = prev(itr); m2[itr->first] = max(itr->second, tmp); tmp = max(tmp, m[itr->first]); } dfs2(dfs2, 0LL, 0LL, mid, W); m.clear(); m2.clear(); tmp = -INF; dfs1(dfs1, 0LL, 0LL, 0, mid - 1); m[INF] = -INF; itr = m.end(); while (itr != m.begin()) { itr = prev(itr); m2[itr->first] = max(itr->second, tmp); tmp = max(tmp, m[itr->first]); } dfs2(dfs2, 0LL, 0LL, mid - 1, W); cout << ans << endl; }