#include #include using namespace std; template long long trisum(T a, T b){ if(a > b) return T(0); long long res = ((b - a + 1) * (a + b)) / 2; return res; } const long long INF = 1LL << 60; long long dp[5001][5001][3]; int main(){ int n; cin >> n; vector> a(n, vector(3)); for(int i = 0; i < n; i++){ for(int j = 0; j < 3; j++){ cin >> a[i][j]; } } for(int i = 0; i <= n; i++){ for(int j = 0; j <= n; j++){ for(int k = 0; k < 3; k++){ dp[i][j][k] = -INF; } } } dp[0][0][0] = dp[0][0][1] = dp[0][0][2] = 0; for(int i = 0; i < n; i++){ for(int j = 0; j <= n - i; j++){ long long d = n - i - j; for(int k1 = 0; k1 < 3; k1++){ if(dp[i][j][k1] == -INF) continue; for(int k2 = 0; k2 < 3; k2++){ if(k1 == k2) continue; int nj = min(n - i, j + a[i][k2]); dp[i + 1][max(0, nj - 1)][k2] = max(dp[i + 1][nj - 1][k2], dp[i][j][k1] + trisum(j + 1LL, j + min(d, (long long) a[i][k2])) + max(0LL, a[i][k2] - d) * (n - i)); } } } } long long ans = 0; for(int i = 0; i <= n; i++){ for(int j = 0; j < 3; j++){ ans = max(ans, dp[n][i][j]); } } cout << ans << endl; }