#include #include #include using namespace std; const int INF = 1e9; 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]; } vector> A;//(money, love) int ans = 0; auto dfs1 = [&](auto dfs1, int love, int money, int l, int r) -> void { if (l == r) { A.emplace_back(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); } }; auto dfs2 = [&](auto dfs2, int love, int money, int l, int r) -> void { if (l == r) { int tmp = lower_bound(A.begin(), A.end(), make_pair(-money, 0))->second; ans = max(ans, tmp + love); 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, 0, 0, 0, mid); A.emplace_back(INF, -INF); sort(A.begin(), A.end()); int N = A.size(); for (int i = N - 2; i >= 0; i--) { A[i].second = max(A[i].second, A[i + 1].second); } dfs2(dfs2, 0, 0, mid, W); cout << ans << endl; }