n=int(input()) dp=[[-1]*3 for _ in range(n+1)] dp[0]=[0]*3 for v in range(1,n+1)[::-1]: ndp=[[-1]*3 for _ in range(n+1)] a=list(map(int,input().split())) for i in range(n): for j in range(3): if dp[i][j]==-1: continue for k in range(3): if j==k: continue p=min(n,max(0,i+a[k]-1)) if p!=n: ndp[p][k]=max(ndp[p][k],dp[i][j]+i+a[k]) else: ndp[n][k]=max(ndp[n][k],dp[i][j]+(i+a[k]-1)*v+1) for j in range(3): if dp[n][j]==-1: continue for k in range(3): if j==k: continue ndp[n][k]=max(ndp[n][k],dp[n][j]+(a[k]-1)*v+1) dp=ndp ans=0 for i in range(n+1): ans=max(ans,max(dp[i])) print(ans)