#include using ll = long long; #define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i) #define reps(i, n) for (int i = 1, i##_len = (n); i <= i##_len; ++i) #define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; --i) #define rreps(i, n) for (int i = ((int)(n)); i > 0; --i) #define rep2(i, s, n) for (int i = (s); i < (int)(n); i++) #define repc2(i, s, n) for (int i = (s); i <= (int)(n); i++) constexpr int inf = 2000'000'000; constexpr ll linf = 4000000000000000000ll; constexpr ll M7 = 1000000007ll; constexpr ll M09 = 1000000009ll; constexpr ll M9 = 998244353ll; #define all(v) begin(v), end(v) #define rall(v) rbegin(v), rend(v) using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; vector> a(n, vector(3)); rep(i, n) rep(j, 3) cin >> a[i][j]; using P = pair; vector>> dp(n + 1, vector>(3, vector

(n + 1, {-linf, -linf}))); dp[0][0][0] = dp[0][1][0] = dp[0][2][0] = {0, 0}; auto fmax = [&](P a, P b, int i) { ll t = n - 1 - i; auto [da, xa] = a; auto [db, xb] = b; if (da == -linf) return b; if (db == -linf) return a; if (xa == xb) return da > db ? a : b; ll sa = da + t * xa - (1 + t) * t / 2; ll sb = db + t * xb - (1 + t) * t / 2; return sa > sb ? a : b; }; rep(i, n) { rep(j, 3) { rep(k, n + 1) { auto [d, x] = dp[i][j][k]; rep(nj, 3) { if (j == nj) continue; ll aij = a[i][nj]; ll nx = max(0ll, x + aij - 1); ll nk = min(n, nx); ll nd = d + (x + aij); dp[i + 1][nj][nk] = fmax(dp[i + 1][nj][nk], {nd, nx}, i); } } } } ll ans = -linf; rep(j, 3) { rep(k, n + 1) { ans = max(ans, dp[n][j][k].first); } } cout << ans << endl; }