#include using namespace std; auto chmax = [](auto&& l, auto r) { return exchange(l, l < r ? r : l) < r; }; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); auto fn = [](vector a, vector b, vector c) { int n = a.size(); long long s = accumulate(begin(a), end(a), 0LL); map mp; for (int bt = 0; bt < 1 << n; bt += 2) { bool ok = true; for (int i = 1; i < n; ++i) { if (bt >> i & 1 and bt >> (i - 1) & 1) { ok = false; break; } } if (not ok) { continue; } long long sw = 0, sv = 0; for (int i = 0; i < n; ++i) { if (bt >> i & 1) { sw += a[i - 1] + a[i] + c[i]; sv += b[i]; } } if (sw <= s) { chmax(mp[sw], sv); } } for (auto it = begin(mp); it != end(mp); ++it) { if (it != begin(mp)) { chmax(it->second, prev(it)->second); } } return mp; }; 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]; } long long s = accumulate(begin(a), end(a), 0LL); long long res = 0; if (n >= 2) { for (int m : {n / 2, n / 2 + 1}) { auto mp0 = fn({begin(a), begin(a) + m}, {begin(b), begin(b) + m}, {begin(c), begin(c) + m}); auto mp1 = fn({begin(a) + m, end(a)}, {begin(b) + m, end(b)}, {begin(c) + m, end(c)}); for (auto e : mp0) { auto it = mp1.upper_bound(s - e.first); if (it != begin(mp1)) { chmax(res, e.second + prev(it)->second); } } } } cout << res << '\n'; }