#ifdef NACHIA #define _GLIBCXX_DEBUG #else #define NDEBUG #endif #include #include #include #include using i64 = long long; using u64 = unsigned long long; #define rep(i,n) for(int i=0; i void chmin(A& l, const A& r){ if(r < l) l = r; } template void chmax(A& l, const A& r){ if(l < r) l = r; } using namespace std; #include using Modint = atcoder::static_modint<998244353>; void testcase(){ int N; cin >> N; vector> A(N); rep(i,N){ A[i].resize(3); rep(j,3) cin >> A[i][j]; } vector> dp(3, vector(N+1, -INF)); rep(x,3) dp[x][0] = 0; i64 t_left = N; for(auto& a : A){ vector> nx(3, vector(N+1, -INF)); rep(i,3){ i64 b = a[i]; rep(x,N){ if(x+b >= N){ chmax(nx[i][N], dp[i][x] + (x+b) * t_left - (t_left * (t_left-1) / 2)); } else { chmax(nx[i][x+b], dp[i][x]); } } chmax(nx[i][N], dp[i][N] + b * t_left); } vector> nx2(3, vector(N+1, -INF)); rep(t,3) rep(s,3) if(s != t){ chmax(nx2[t][0], nx[s][0]); rep(i,N-1) chmax(nx2[t][i], nx[s][i+1] + (i+1)); chmax(nx2[t][N], nx[s][N]); } t_left--; swap(dp, nx2); } i64 ans = -INF; rep(j,3) rep(i,N+1) chmax(ans, dp[j][i]); cout << ans << endl; } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); testcase(); return 0; }