#include #include #include using namespace std; using namespace atcoder; using mint = modint998244353; #define rep(i,n) for (int i = 0; i < (n); ++i) #define Inf32 1000000001 #define Inf64 1000000000000000000LL long long a[5005][3]; int main(){ int n; cin>>n; rep(i,n){ rep(j,3)cin>>a[i][j]; } vector dp(n+5,vector(3,-Inf64)); rep(i,3)dp[0][i] = 0; vector ndp(n+5,vector(3,-Inf64)); rep(i,n){ { ndp.assign(n+5,vector(3,-Inf64)); rep(j,n+5){ rep(k,3){ if(dp[j][k] == -Inf64)continue; rep(l,3){ if(k == l)continue; long long nj = j + a[i][l]; long long nv = dp[j][k]; if(nj>=n+5){ nv += ((long long)nj-(n+4)) * (n-i); nj = n+4; } ndp[nj][l] = max(ndp[nj][l],nv); } } } swap(dp,ndp); } { ndp.assign(n+5,vector(3,-Inf64)); rep(j,n+5){ rep(k,3){ if(dp[j][k] == -Inf64)continue; ndp[max(j-1,0)][k] = max(ndp[max(j-1,0)][k],dp[j][k] + j); } } swap(dp,ndp); } } long long ans = -Inf64; rep(i,n+5){ rep(j,3){ ans = max(ans,dp[i][j]); } } cout<