#include using namespace std; const int64_t INF = INT64_C(1) << 60; template bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } int main() { ios::sync_with_stdio(false); cin.tie(0); 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]; } function &)> dfs = [&](int now, int end, int64_t money, int64_t love, map &mp) { if (now == end) { chmax(mp[money], love); return; } dfs(now + 1, end, money + A[now], love, mp); if (now + 1 < end) dfs(now + 2, end, money - C[now + 1], love + B[now + 1], mp); }; auto solve = [&](int w) { map A, B; // B[INF] = -INF; // B[-INF] = -INF; dfs(0, w, 0, 0, A); dfs(w, W, 0, 0, B); int64_t res = 0; for (const auto &[key, value] : A) { auto itr = B.lower_bound(-key); if (itr != B.end()) res = max(res, itr->second + value); } return res; }; cout << max(solve(W / 2), solve(W / 2 + 1)) << '\n'; return 0; }