#include const long long inf = -(1LL << 60); void chmax(long long *a, long long b) { if (*a < b) *a = b; } int main() { int i, N, A[5001][3]; scanf("%d", &N); for (i = 1; i <= N; i++) scanf("%d %d %d", &(A[i][0]), &(A[i][1]), &(A[i][2])); int j, k, jj, kk, cur, prev; long long dp[2][3][5003], tmp, tmpp; for (j = 0; j < 3; j++) { for (k = 0; k <= N; k++) { dp[0][j][k] = inf; dp[1][j][k] = inf; } } for (i = 1, cur = 1, prev = 0, dp[0][0][0] = 0, dp[0][1][0] = 0, dp[0][2][0] = 0; i <= N; i++, cur ^= 1, prev ^= 1) { for (j = 0; j < 3; j++) { for (k = 0; k < N; k++) { tmp = dp[prev][j][k]; dp[prev][j][k] = inf; for (jj = 0; jj < 3; jj++) { if (jj == j) continue; kk = k + A[i][jj]; if (kk >= N) chmax(&(dp[cur][jj][N]), tmp + (long long)(kk + kk - (N - i)) * (N - i + 1) / 2); else if (kk > 0) chmax(&(dp[cur][jj][kk-1]), tmp + kk); else chmax(&(dp[cur][jj][kk]), tmp); } } tmp = dp[prev][j][N]; dp[prev][j][N] = inf; for (jj = 0; jj < 3; jj++) { if (jj != j) { for (k = i, tmpp = tmp; k <= N; k++) { if (A[i][jj] - (k - i) > 0) tmpp += A[i][jj] - (k - i); else break; } chmax(&(dp[cur][jj][N]), tmpp); } } } } long long ans = 0; for (j = 0; j < 3; j++) for (k = 0; k <= N; k++) chmax(&ans, dp[prev][j][k]); printf("%lld\n", ans); fflush(stdout); return 0; }