#include #include #include #include using namespace std; int main() { int n; cin >> n; vector> dp(3, vector(n + 1, -(1LL << 60))); dp[0][0] = dp[1][0] = dp[2][0] = 0; for (int t = 0; t < n; ++t) { vector A(3); for (int i = 0; i < 3; ++i) { cin >> A[i]; } vector> ndp(3, vector(n + 1, -(1LL << 60))); for (int i1 = 0; i1 < 3; ++i1) { for (int i2 = 0; i2 < 3; ++i2) { if (i1 == i2) continue; for (int j = 0; j < n; ++j) { int nj = j + A[i2]; long long nv = dp[i1][j]; if (nj > n) { nv += (n - t) * (nj - n); nj = n; } nv += nj; nj = max(nj - 1, 0); ndp[i2][nj] = max(ndp[i2][nj], nv); } } } dp = ndp; } long long result = -(1LL << 60); for (const auto& row : dp) { result = max(result, *max_element(row.begin(), row.end())); } cout << result << endl; return 0; }