#include using namespace std; using ll = long long; #define rep(i, n) for (int i = 0; i < (n); i++) #define repr(i, n) for (int i = (n) - 1; i >= 0; i--) #define range(a) a.begin(), a.end() int main() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); int N; cin >> N; vector A(N), B(N), C(N); rep(i, N) cin >> A[i] >> B[i] >> C[i]; auto solve = [&](int n1, int n2) -> ll { if (n1 < 0 or n2 < 0) return LLONG_MIN; auto dfs = [&](auto dfs, int l, int r, ll money, ll like, bool promise, vector> &res) -> void { if (l == r) { if (r == N or not promise) { res.emplace_back(money, like); } return; } if (promise) { dfs(dfs, l + 1, r, money - C[l], like + B[l], false, res); } else { dfs(dfs, l + 1, r, money, like, true, res); dfs(dfs, l + 1, r, money + A[l], like, false, res); } }; vector> fst, snd; // (money, like) dfs(dfs, 0, n1, 0, 0, false, fst); dfs(dfs, n1, N, 0, 0, false, snd); sort(range(fst)); sort(range(snd)); auto normalize = [](vector> a) -> vector> { vector> res; for (auto p : a) { while (not res.empty() and res.back().second <= p.second) { res.pop_back(); } res.push_back(p); } return res; }; fst = normalize(fst); snd = normalize(snd); int k = (int)snd.size() - 1; ll ans = LLONG_MIN; for (auto p : fst) { while (k >= 1 and p.first + snd[k - 1].first >= 0) k--; if (p.first + snd[k].first >= 0) { ans = max(ans, p.second + snd[k].second); } } return ans; }; ll ans = max(solve(N / 2, (N + 1) / 2), solve(N / 2 + 1, (N + 1) / 2 - 1)); cout << ans << endl; }