#include "bits/stdc++.h" using namespace std; using i64 = long long; signed main(){ int n; cin >> n; vector a(n), b(n), c(n); for(int i = 0; i < n; ++i) cin >> a[i] >> b[i] >> c[i]; array m{n / 2, (n + 1) / 2}; // v[k][flag] = (money, val) vector>> v(2); vector>> special(2); for(int k = 0; k < 2; ++k){ for(int i = 0; i < (1 << m[k]); ++i){ i64 money = 0; i64 val = 0; bool flag = false; bool ignore = false; for(int j = 0; j < m[k]; ++j){ if((i >> j) & 1){ if(flag){ ignore = true; break; } flag = true; } else{ if(flag){ val += b[j + m[0] * k]; money -= c[j + m[0] * k]; } else{ money += a[j + m[0] * k]; } flag = false; } } if(!ignore){ v[k].emplace_back(money, val); if(!k && flag) special[k].emplace_back(money, val); if(k && !(i & 1)) special[k].emplace_back(money - a[m[0]] - c[m[0]], val + b[m[0]]); } } } vector> v_keys(2), special_keys(2); vector> v_vals(2), special_vals(2); for(int i = 0; i < 2; ++i){ for(auto& p : v[i]) v_keys[i].emplace_back(p.first); for(auto& p : special[i]) special_keys[i].emplace_back(p.first); sort(v_keys[i].begin(), v_keys[i].end()); sort(special_keys[i].begin(), special_keys[i].end()); v_keys[i].erase(unique(v_keys[i].begin(), v_keys[i].end()), v_keys[i].end()); special_keys[i].erase(unique(special_keys[i].begin(), special_keys[i].end()), special_keys[i].end()); v_vals[i].assign(v_keys[i].size(), -1e18); special_vals[i].assign(special_keys[i].size(), -1e18); for(auto& p : v[i]){ int idx = distance(v_keys[i].begin(), lower_bound(v_keys[i].begin(), v_keys[i].end(), p.first)); v_vals[i][idx] = max(v_vals[i][idx], p.second); } for(auto& p : special[i]){ int idx = distance(special_keys[i].begin(), lower_bound(special_keys[i].begin(), special_keys[i].end(), p.first)); special_vals[i][idx] = max(special_vals[i][idx], p.second); } for(int j = v_keys[i].size() - 1; j > 0; --j) v_vals[i][j - 1] = max(v_vals[i][j - 1], v_vals[i][j]); for(int j = special_vals[i].size() - 1; j > 0; --j) special_vals[i][j - 1] = max(special_vals[i][j - 1], special_vals[i][j]); } i64 ans = 0; for(int i = 0; i < v_keys[0].size(); ++i){ auto it = lower_bound(v_keys[1].begin(), v_keys[1].end(), -v_keys[0][i]); if(it == v_keys[1].end()){ continue; } int idx = distance(v_keys[1].begin(), it); ans = max(ans, v_vals[0][i] + v_vals[1][idx]); } for(int i = 0; i < special_keys[0].size(); ++i){ auto it = lower_bound(special_keys[1].begin(), special_keys[1].end(), -special_keys[0][i]); if(it == special_keys[1].end()){ continue; } int idx = distance(special_keys[1].begin(), it); ans = max(ans, special_vals[0][i] + special_vals[1][idx]); } /* for(int i = 0; i < 2; ++i){ for(auto &x : v_keys[i]) cout << x << " "; cout << endl; for(auto &x : v_vals[i]) cout << x << " "; cout << endl; cout << endl; } */ cout << ans << endl; }