#include #include using mint = atcoder::static_modint<998244353>; //using mint = atcoder::static_modint<1000000007>; using namespace std; using namespace atcoder; using ld = long double; using ll = long long; #define mp(a,b) make_pair(a,b) #define rep(i,s,n) for(int i=s; i dx{1,0,-1,0},dy{0,1,0,-1}; int main(){ int n;cin >> n; vector> a(n+1,vector(3)); rep(i,1,n+1)rep(j,0,3)cin >> a[i][j]; vector>> dp(n+1,vector>(3,vector(5001,-1))); vector> dpover(n+1,vector(3,-1)); rep(j,0,3)dp[0][j][0]=0; rep(i,1,n+1)rep(j,0,3){ rep(k,0,3)if(k!=j){ rep(l,0,5001)if(dp[i-1][j][l]>=0){ int newatk=l+a[i][k]; if(newatk-1<=5000)dp[i][k][max(0,newatk-1)]=max(dp[i][k][max(0,newatk-1)],dp[i-1][j][l]+newatk); else{ ll p=n-i+1; p*=(2*newatk-n+i); p/=2; dpover[i][k]=max(dpover[i][k],dpover[i-1][j]+p); } } if(dpover[i-1][j]>=0)dpover[i][k]=max(dpover[i][k],dpover[i-1][j]+(ll)a[i][k]*(n-i+1)); } } ll ans=0; rep(j,0,3)rep(k,0,5001)ans=max(ans,dp[n][j][k]); rep(j,0,3)ans=max(ans,dpover[n][j]); cout << ans; }