#include using namespace std; #define rep(i,n) for(int i=0;i<(int)(n);i++) #define ALL(v) v.begin(),v.end() typedef long long ll; template using V=vector; template using VV=V>; ll A[5050][3]; int main(){ ios::sync_with_stdio(false); std::cin.tie(nullptr); VV dp(5050,V(3,-1)); int n; cin>>n; rep(i,n) rep(j,3) cin>>A[i][j]; rep(j,3) dp[max(0,min(5000,(int)A[0][j]-1))][j]=A[0][j]; for(int i=0;i old(5050,V(3,-1)); rep(j,5001) rep(k,3) rep(l,3){ if(k==l) continue; if(dp[j][k]==-1) continue; int tmp=max(0,min(5000,j+(int)A[i+1][l]-1)); old[tmp][l]=max(old[tmp][l],dp[j][k]+j+A[i+1][l]); } swap(old,dp); } ll ans=0; rep(j,5001) rep(k,3) ans=max(ans,dp[j][k]); cout<