stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include <bits/stdc++.h>

#define maxn (int)(2e5)

#define INF 1<<30

#define i64 long long

#define pii pair<i64,i64>

#define all(x) x.begin(),x.end()

#define LB(x,v) lower_bound(all(x), v) - x.begin()

#define fread() freopen("in.txt", "r", stdin);

using namespace std;



int mat[maxn+2][4], ans[maxn+2][4] , n;

const int k = 3;



void init(int n){

    for(int i = 0; i <= n; i++) 

        ans[i][0] = ans[i][1] = ans[i][2] = -1;

}



int solve(int day, int option){

    if(day >= n) return 0;



    if(ans[day][option] != -1) return ans[day][option];



    int res = -1;

    for(int next_option = 0; next_option < k; next_option++) {

        if(next_option != option) res = max(res, mat[day][option] + solve(day + 1, next_option));

    }



    return ans[day][option] = res;

}



int main() {

    

    while(scanf("%d",&n)==1){

        init(n);

        

        for(int i=0;i<n;i++) {

            for(int j = 0;j < k; j++) scanf("%d",&mat[i][j]);

        }



        int res = -1;

        for(int option = 0; option < k; option++) {

            res = max(res, solve(0, option));

        }



        printf("%d\n", res);

    }    

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.178039059592093x):
#include<stdio.h>

#include<algorithm>

using namespace std;

int dp[100001][3]={0};//第i天选了第j种

int a[100001],b[100001],c[100001];

int main(){

    int n;

    scanf("%d",&n);

    for(int i=1;i<=n;i++){

        scanf("%d%d%d",&a[i],&b[i],&c[i]);

    }

    dp[1][0] = a[1];

    dp[1][1] = b[1];

    dp[1][2] = c[1];

    for(int i=2;i<=n;i++){

        dp[i][0] = max(dp[i-1][1],dp[i-1][2])+a[i];

        dp[i][1] = max(dp[i-1][2],dp[i-1][0])+b[i];

        dp[i][2] = max(dp[i-1][0],dp[i-1][1])+c[i];

    }

    int ans = 0;

    for(int i=0;i<3;i++){

        ans = max(dp[n][i],ans);

    }

    printf("%d\n",ans);

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

 

int main() {

	int Ndays;

	scanf("%d", &Ndays);

	int a, b, c;

	int A = 0, B = 0, C = 0;

	while (Ndays--) {

		scanf("%d %d %d", &a, &b, &c);

		int AA = max(B, C) + a;

		int BB = max(A, C) + b;

		int CC = max(A, B) + c;

		A = AA, B = BB, C = CC;

	}

	printf("%d", max(max(A, B), C));

}

// Faster C++ solution for the same problem (measured speedup: 5.12804712370784x):
#include<stdio.h>

#include<algorithm>

using namespace std;

int dp[100001][3]={0};//第i天选了第j种

int a[100001],b[100001],c[100001];

int main(){

    int n;

    scanf("%d",&n);

    for(int i=1;i<=n;i++){

        scanf("%d%d%d",&a[i],&b[i],&c[i]);

    }

    dp[1][0] = a[1];

    dp[1][1] = b[1];

    dp[1][2] = c[1];

    for(int i=2;i<=n;i++){

        dp[i][0] = max(dp[i-1][1],dp[i-1][2])+a[i];

        dp[i][1] = max(dp[i-1][2],dp[i-1][0])+b[i];

        dp[i][2] = max(dp[i-1][0],dp[i-1][1])+c[i];

    }

    int ans = 0;

    for(int i=0;i<3;i++){

        ans = max(dp[n][i],ans);

    }

    printf("%d\n",ans);

}
// Slower C++ solution:
#include<iostream>

#include<algorithm>

#define MAXN 100003

using namespace std;

int dp[MAXN][3], n, a[MAXN][3];

int main()

{

	cin >> n;

	for (int i = 0; i < n; i++) {

		cin >> a[i][0] >> a[i][1] >> a[i][2];

	}

	dp[0][0] = a[0][0];

	dp[0][1] = a[0][1];

	dp[0][2] = a[0][2];

	    for(int i=1;i<n;i++)                               

	{

		dp[i][0] = a[i][0] + max(dp[i-1][1], dp[i-1][2]);

		dp[i][1] = a[i][1] + max(dp[i-1][0], dp[i-1][2]);

		dp[i][2] = a[i][2] + max(dp[i-1][0], dp[i-1][1]);

	}

	cout << max(dp[n-1][0], max(dp[n-1][1], dp[n-1][2]))<<endl;

	return 0;

}


// Faster C++ solution for the same problem (measured speedup: 5.160343189825554x):
#include<stdio.h>

#include<algorithm>

using namespace std;

int dp[100001][3]={0};//第i天选了第j种

int a[100001],b[100001],c[100001];

int main(){

    int n;

    scanf("%d",&n);

    for(int i=1;i<=n;i++){

        scanf("%d%d%d",&a[i],&b[i],&c[i]);

    }

    dp[1][0] = a[1];

    dp[1][1] = b[1];

    dp[1][2] = c[1];

    for(int i=2;i<=n;i++){

        dp[i][0] = max(dp[i-1][1],dp[i-1][2])+a[i];

        dp[i][1] = max(dp[i-1][2],dp[i-1][0])+b[i];

        dp[i][2] = max(dp[i-1][0],dp[i-1][1])+c[i];

    }

    int ans = 0;

    for(int i=0;i<3;i++){

        ans = max(dp[n][i],ans);

    }

    printf("%d\n",ans);

}
// Slower C++ solution:
#include <bits/stdc++.h>

#define rep(i,n) for (int i = 0; i < (n); i++)

using namespace std;

typedef long long ll;

const int inf = 1001001001;

 

int main(){

  int n;

  cin >> n;

  

  vector<vector<int>> h(n, vector<int>(3));

  rep(i,n){

    rep(j,3){

      cin >> h.at(i).at(j);

    }

  }

  vector<vector<int>> dvector(n, vector<int>(3,0));

  

  

  rep(i,n){

    

    if (i == 0){

      rep(l,3){dvector.at(0).at(l) = h.at(0).at(l);}

      continue;

    }

    

    rep(j,3){

      rep(k,3){

        

      if(k != j){

        dvector.at(i).at(j) = max(dvector.at(i).at(j), dvector.at(i-1).at(k)+h.at(i).at(j));

      }

      

      }

    }

  }

    

    int ans = 0;

    rep(i,3){

    ans = max(ans, dvector.at(n-1).at(i));

    }

    

    cout << ans << endl;

    

  }


// Faster C++ solution for the same problem (measured speedup: 5.264401204968299x):
#include<stdio.h>

#include<algorithm>

using namespace std;

int dp[100001][3]={0};//第i天选了第j种

int a[100001],b[100001],c[100001];

int main(){

    int n;

    scanf("%d",&n);

    for(int i=1;i<=n;i++){

        scanf("%d%d%d",&a[i],&b[i],&c[i]);

    }

    dp[1][0] = a[1];

    dp[1][1] = b[1];

    dp[1][2] = c[1];

    for(int i=2;i<=n;i++){

        dp[i][0] = max(dp[i-1][1],dp[i-1][2])+a[i];

        dp[i][1] = max(dp[i-1][2],dp[i-1][0])+b[i];

        dp[i][2] = max(dp[i-1][0],dp[i-1][1])+c[i];

    }

    int ans = 0;

    for(int i=0;i<3;i++){

        ans = max(dp[n][i],ans);

    }

    printf("%d\n",ans);

}
// Slower C++ solution:
#include <iostream>



#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)

#define repd(i,x,n) for(int i=x;i<=n;i++)



using namespace std;

typedef long long ll;

inline void getInt(int* p);

const int maxn=1000010;

const int inf=0x3f3f3f3f;

ll n;

ll a[maxn];

ll b[maxn];

ll c[maxn];

ll dp[maxn][5];



int main()

{

    gbtb;

    cin>>n;

    repd(i,1,n)

    {

        cin>>a[i]>>b[i]>>c[i];

    }

    dp[1][1]=a[1];

    dp[1][2]=b[1];

    dp[1][3]=c[1];

    repd(i,2,n)

    {

        dp[i][1]+=max(dp[i-1][2],dp[i-1][3])+a[i];

        dp[i][2]+=max(dp[i-1][1],dp[i-1][3])+b[i];

        dp[i][3]+=max(dp[i-1][1],dp[i-1][2])+c[i];

    }

    cout<<max(dp[n][3],max(dp[n][1],dp[n][2]));

    return 0;

}



inline void getInt(int* p) {

    char ch;

    do {

        ch = getchar();

    } while (ch == ' ' || ch == '\n');

    if (ch == '-') {

        *p = -(getchar() - '0');

        while ((ch = getchar()) >= '0' && ch <= '9') {

            *p = *p * 10 - ch + '0';

        }

    }

    else {

        *p = ch - '0';

        while ((ch = getchar()) >= '0' && ch <= '9') {

            *p = *p * 10 + ch - '0';

        }

    }

}

// Faster C++ solution for the same problem (measured speedup: 5.255163401656387x):
#include<stdio.h>

#include<algorithm>

using namespace std;

int dp[100001][3]={0};//第i天选了第j种

int a[100001],b[100001],c[100001];

int main(){

    int n;

    scanf("%d",&n);

    for(int i=1;i<=n;i++){

        scanf("%d%d%d",&a[i],&b[i],&c[i]);

    }

    dp[1][0] = a[1];

    dp[1][1] = b[1];

    dp[1][2] = c[1];

    for(int i=2;i<=n;i++){

        dp[i][0] = max(dp[i-1][1],dp[i-1][2])+a[i];

        dp[i][1] = max(dp[i-1][2],dp[i-1][0])+b[i];

        dp[i][2] = max(dp[i-1][0],dp[i-1][1])+c[i];

    }

    int ans = 0;

    for(int i=0;i<3;i++){

        ans = max(dp[n][i],ans);

    }

    printf("%d\n",ans);

}
// Slower C++ solution:
#include <bits/stdc++.h>



using namespace std;



    const int n = 1e5+5;

    int num;

    int act[n][3];

    int dyn[3][n];

    

    int init(int a, int i){

        

    	if(i == num) return 0;

    	

    	if(dyn[a][i] != -1) return dyn[a][i];

    	int& res = dyn[a][i];

    	

    	if(a==0){

    	    

    		res=max(res, init(1, i+1)+act[i][0]);

    		res=max(res, init(2, i+1)+act[i][0]);

    	}

    	

    	if(a==1){

    	    

    		res=max(res, init(0, i+1)+act[i][1]);

    		res=max(res, init(2, i+1)+act[i][1]);

    	}

    	

    	if(a==2){

    	    

    		res=max(res, init(0, i+1)+act[i][2]);

    		res=max(res, init(1, i+1)+act[i][2]);

    	}

    	

    	

    	return res;

    }

    

    int main(){

        

    	cin>>num;

    	for(int i=0; i<num; i++) cin>>act[i][0]>>act[i][1]>>act[i][2];

    	memset(dyn, -1, sizeof dyn);

    	cout<<max(init(0,0),max(init(1,0),init(2,0)))<<"\n";

    	return 0;

    	

    }

// Faster C++ solution for the same problem (measured speedup: 11.47336759769418x):
#include<stdio.h>

#include<algorithm>

using namespace std;

int dp[100001][3]={0};//第i天选了第j种

int a[100001],b[100001],c[100001];

int main(){

    int n;

    scanf("%d",&n);

    for(int i=1;i<=n;i++){

        scanf("%d%d%d",&a[i],&b[i],&c[i]);

    }

    dp[1][0] = a[1];

    dp[1][1] = b[1];

    dp[1][2] = c[1];

    for(int i=2;i<=n;i++){

        dp[i][0] = max(dp[i-1][1],dp[i-1][2])+a[i];

        dp[i][1] = max(dp[i-1][2],dp[i-1][0])+b[i];

        dp[i][2] = max(dp[i-1][0],dp[i-1][1])+c[i];

    }

    int ans = 0;

    for(int i=0;i<3;i++){

        ans = max(dp[n][i],ans);

    }

    printf("%d\n",ans);

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;



int dp[3];



int main(){

    int n,a,b,c,x,y,z;

    cin>>n;



    for(int i=0;i<n;i++){

        cin>>a>>b>>c;

        if(i==0){

            dp[0]=a;

            dp[1]=b;

            dp[2]=c;

            continue;

        }



        x=dp[0];

        y=dp[1];

        z=dp[2];



        dp[0]=a+max(y,z);

        dp[1]=b+max(x,z);

        dp[2]=c+max(x,y);







    }



    cout<<max(dp[0],max(dp[1],dp[2]))<<endl;





    return 0;







}


// Faster C++ solution for the same problem (measured speedup: 5.200254679698201x):
#include<stdio.h>

#include<algorithm>

using namespace std;

int dp[100001][3]={0};//第i天选了第j种

int a[100001],b[100001],c[100001];

int main(){

    int n;

    scanf("%d",&n);

    for(int i=1;i<=n;i++){

        scanf("%d%d%d",&a[i],&b[i],&c[i]);

    }

    dp[1][0] = a[1];

    dp[1][1] = b[1];

    dp[1][2] = c[1];

    for(int i=2;i<=n;i++){

        dp[i][0] = max(dp[i-1][1],dp[i-1][2])+a[i];

        dp[i][1] = max(dp[i-1][2],dp[i-1][0])+b[i];

        dp[i][2] = max(dp[i-1][0],dp[i-1][1])+c[i];

    }

    int ans = 0;

    for(int i=0;i<3;i++){

        ans = max(dp[n][i],ans);

    }

    printf("%d\n",ans);

}
// Slower C++ solution:
#include<iostream>

#include<cstdio>

#include<cstring> 

#include<string>

#include<map>

#include<vector>

#include<cmath>

#include<string>

#include<algorithm>

using namespace std;

int dp[100000][3];

int main()

{

	  int a,b,c,d;

	  cin>>a;

	  dp[0][0]=0;

	  dp[0][1]=0;

	  dp[0][2]=0;

	  for(int i=1;i<=a;i++)

	  {

	  	cin>>b;

	  	cin>>c;

	  	cin>>d;	 

	  	dp[i][0]=max(dp[i-1][2],dp[i-1][1])+b;

	  	dp[i][1]=max(dp[i-1][0],dp[i-1][2])+c;

	  	dp[i][2]=max(dp[i-1][0],dp[i-1][1])+d;

	  }	  

	  cout<<max(max(dp[a][0],dp[a][1]),dp[a][2])<<endl;

	  return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.198939769664748x):
#include<stdio.h>

#include<algorithm>

using namespace std;

int dp[100001][3]={0};//第i天选了第j种

int a[100001],b[100001],c[100001];

int main(){

    int n;

    scanf("%d",&n);

    for(int i=1;i<=n;i++){

        scanf("%d%d%d",&a[i],&b[i],&c[i]);

    }

    dp[1][0] = a[1];

    dp[1][1] = b[1];

    dp[1][2] = c[1];

    for(int i=2;i<=n;i++){

        dp[i][0] = max(dp[i-1][1],dp[i-1][2])+a[i];

        dp[i][1] = max(dp[i-1][2],dp[i-1][0])+b[i];

        dp[i][2] = max(dp[i-1][0],dp[i-1][1])+c[i];

    }

    int ans = 0;

    for(int i=0;i<3;i++){

        ans = max(dp[n][i],ans);

    }

    printf("%d\n",ans);

}
// Slower C++ solution:
#include <stdio.h>

#include <string.h>

#define MAXN 100003



int array_a[MAXN][3];

int array_d[MAXN][3];



int max(int a,int b){

	return a>b? a:b;

}



int main(){

	memset(array_a,-1,sizeof(int)*MAXN*3);

	memset(array_d,-1,sizeof(int)*MAXN*3);

	int N;

	scanf("%d",&N);



	for(int i=0;i<N;i++){

		scanf("%d %d %d",&array_a[i][0],&array_a[i][1],&array_a[i][2]);

	}



	array_d[0][0]=array_a[0][0];

	array_d[0][1]=array_a[0][1];

	array_d[0][2]=array_a[0][2];

	for(int i=1;i<N;i++){

		array_d[i][0]=array_a[i][0]+max(array_d[i-1][1],array_d[i-1][2]);

		array_d[i][1]=array_a[i][1]+max(array_d[i-1][0],array_d[i-1][2]);

		array_d[i][2]=array_a[i][2]+max(array_d[i-1][0],array_d[i-1][1]);

	}

	printf("%d",max(array_d[N-1][0],max(array_d[N-1][1],array_d[N-1][2])));

	return 0;



}

// Faster C++ solution for the same problem (measured speedup: 13.466107640735322x):
#include<stdio.h>

#include<algorithm>

using namespace std;

int dp[100001][3]={0};//第i天选了第j种

int a[100001],b[100001],c[100001];

int main(){

    int n;

    scanf("%d",&n);

    for(int i=1;i<=n;i++){

        scanf("%d%d%d",&a[i],&b[i],&c[i]);

    }

    dp[1][0] = a[1];

    dp[1][1] = b[1];

    dp[1][2] = c[1];

    for(int i=2;i<=n;i++){

        dp[i][0] = max(dp[i-1][1],dp[i-1][2])+a[i];

        dp[i][1] = max(dp[i-1][2],dp[i-1][0])+b[i];

        dp[i][2] = max(dp[i-1][0],dp[i-1][1])+c[i];

    }

    int ans = 0;

    for(int i=0;i<3;i++){

        ans = max(dp[n][i],ans);

    }

    printf("%d\n",ans);

}
// Slower C++ solution:
#include<iostream>

#include<string>

#include<cmath>

#include<algorithm>

#include<vector>

#include<map>

#include<stack>

#include<queue>

using namespace std;

#define MAXN 100001

int dp[MAXN*3];

int main(){

	int N;

	cin>>N;

	for(int i=0;i<N*3;++i){

		cin>>dp[i];

	}

	//dp[i]对应与本次选择i%3(A,B,C)的最大值，等于前面不选择i%3的最大值加上选择i%3的值

	for(int i=3;i<N*3;i++){

		if(i%3==0){

			dp[i]=dp[i]+max(dp[i-2],dp[i-1]);

		}else if(i%3==1){

			dp[i]=dp[i]+max(dp[i-4],dp[i-2]);

		}else{

			dp[i]=dp[i]+max(dp[i-4],dp[i-5]);

		}

	}

	int x;

	if(dp[3*N-1]>=dp[3*N-2]){

		x=dp[3*N-1];

	}else{

		x=dp[3*N-2];

	}

   cout<<(x>dp[3*N-3]?x:dp[3*N-3]);

   return 0;

} 

// Faster C++ solution for the same problem (measured speedup: 5.1929343853608465x):
#include<stdio.h>

#include<algorithm>

using namespace std;

int dp[100001][3]={0};//第i天选了第j种

int a[100001],b[100001],c[100001];

int main(){

    int n;

    scanf("%d",&n);

    for(int i=1;i<=n;i++){

        scanf("%d%d%d",&a[i],&b[i],&c[i]);

    }

    dp[1][0] = a[1];

    dp[1][1] = b[1];

    dp[1][2] = c[1];

    for(int i=2;i<=n;i++){

        dp[i][0] = max(dp[i-1][1],dp[i-1][2])+a[i];

        dp[i][1] = max(dp[i-1][2],dp[i-1][0])+b[i];

        dp[i][2] = max(dp[i-1][0],dp[i-1][1])+c[i];

    }

    int ans = 0;

    for(int i=0;i<3;i++){

        ans = max(dp[n][i],ans);

    }

    printf("%d\n",ans);

}
// Slower C++ solution:
#include<iostream>

#include<cstring>

#include<vector>

#include<stack>

#include<cstdio>

using namespace std;

int main()

{

	int n,a,b,c,flag=1,ans=0;

	int x[2][3];

	scanf("%d",&n);

	for(int i=0;i<n;++i)

	{

		scanf("%d%d%d",&a,&b,&c);

		if(i==0)

		{

			x[i][0]=a;

		    x[i][1]=b;

		    x[i][2]=c;

		    continue;

		}

		x[flag][0]=a+max(x[1-flag][1],x[1-flag][2]);

		x[flag][1]=b+max(x[1-flag][2],x[1-flag][0]);

		x[flag][2]=c+max(x[1-flag][1],x[1-flag][0]);

		flag=1-flag;

	}

	ans=max(x[1-flag][0],max(x[1-flag][1],x[1-flag][2]));

	printf("%d\n",ans);

	return 0;

}


// Faster C++ solution for the same problem (measured speedup: 5.130505100624955x):
#include<stdio.h>

#include<algorithm>

using namespace std;

int dp[100001][3]={0};//第i天选了第j种

int a[100001],b[100001],c[100001];

int main(){

    int n;

    scanf("%d",&n);

    for(int i=1;i<=n;i++){

        scanf("%d%d%d",&a[i],&b[i],&c[i]);

    }

    dp[1][0] = a[1];

    dp[1][1] = b[1];

    dp[1][2] = c[1];

    for(int i=2;i<=n;i++){

        dp[i][0] = max(dp[i-1][1],dp[i-1][2])+a[i];

        dp[i][1] = max(dp[i-1][2],dp[i-1][0])+b[i];

        dp[i][2] = max(dp[i-1][0],dp[i-1][1])+c[i];

    }

    int ans = 0;

    for(int i=0;i<3;i++){

        ans = max(dp[n][i],ans);

    }

    printf("%d\n",ans);

}
// Slower C++ solution:
//       Author: Mostafa Mounir Shehab

 

#include <bits/stdc++.h>

using namespace std;

 

#define PI  3.14159265  // PI = acos(-1)

#define EPS (1e-10)

#define endl "\n"

#define SZ(v) (int)(v.size())

typedef long long ll;

typedef long double ld;

//template <class Type>

 

 

void Fast(){

	std::ios_base::sync_with_stdio(0);

	cin.tie(NULL); cout.tie(NULL); cerr.tie(NULL);

}

 

inline int D(){

	int t;

	scanf("%d",&t);

	return t;

}

inline ll llD(){

	ll t;

	scanf("%lld",&t);

	return t;

}

 

 

ll MOD=1e9 + 7; 

const int N=1e5 + 5,M=50 + 1;

int arr[N][4];

ll dp[N][4];

ll OO=1e12; 

 

ll n,m,k,cnt,SU,idx,len,MN,MX;

ll l,r,mid;

ll a,b,c,d;



ll Solve(int idx,int row){

	

		

	if(idx == n)

		return 0;

	

	ll &ret = dp[idx][row];

	if(~ret)

		return ret;

	

	ret = -OO;

	

	for(int i=1;i<=3;++i)

		if(row != i)

			ret = max(ret,arr[idx][i] + Solve(idx+1,i));

	

	return ret;

}







 

 

int main(){

	

	memset(dp, -1 ,sizeof dp);

	cin>>n;

	for(int i=0;i<n;++i)

		cin>>arr[i][1]>>arr[i][2]>>arr[i][3];



	cout<<Solve(0,0)<<endl;

	



	





}

// Faster C++ solution for the same problem (measured speedup: 21.973816562316244x):
#include<stdio.h>

#include<algorithm>

using namespace std;

int dp[100001][3]={0};//第i天选了第j种

int a[100001],b[100001],c[100001];

int main(){

    int n;

    scanf("%d",&n);

    for(int i=1;i<=n;i++){

        scanf("%d%d%d",&a[i],&b[i],&c[i]);

    }

    dp[1][0] = a[1];

    dp[1][1] = b[1];

    dp[1][2] = c[1];

    for(int i=2;i<=n;i++){

        dp[i][0] = max(dp[i-1][1],dp[i-1][2])+a[i];

        dp[i][1] = max(dp[i-1][2],dp[i-1][0])+b[i];

        dp[i][2] = max(dp[i-1][0],dp[i-1][1])+c[i];

    }

    int ans = 0;

    for(int i=0;i<3;i++){

        ans = max(dp[n][i],ans);

    }

    printf("%d\n",ans);

}
// Slower C++ solution:
#define _CRT_SECURE_NO_WARNINGS

#include <bits/stdc++.h>

#include <unordered_map>

#define stop(x) {cout<<x<<endl;exit(0);}

#define all(x) x.begin(),x.end()

#define szz(s) int(s.size())

#define endl '\n'

int dx[] = { 0, 0, 1, -1, 1, -1, 1, -1 };

int dy[] = { 1, -1, 0, 0, -1, 1, 1, -1 };

typedef long long ll;

using namespace std;



void file() {

    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

#ifndef ONLINE_JUDGE

    if (freopen("input.txt", "r", stdin));

    if (freopen("output.txt", "w", stdout));

#endif

}





ll n;

ll mem[100004][4];

vector < vector < int > > v;



ll solve(int idx , int last) {

    if (idx == n)return 0;



    ll& ret = mem[idx][last];

    if (~ret)return ret;

    ret = 0;

    if (last != 0)ret = max(ret, solve(idx + 1, 0) + v[idx][0]);

    if (last != 1)ret = max(ret, solve(idx + 1, 1) + v[idx][1]);

    if (last != 2)ret = max(ret, solve(idx + 1, 2) + v[idx][2]);

    return ret;

}



int main() {

    //file();



    memset(mem, -1, sizeof mem);

    cin >> n;

    v = vector < vector < int > >(n, vector < int >(3));

    for (auto& e : v)

        for (auto& f : e)

            cin >> f;



    cout << solve(0,3) << endl;



}

// Faster C++ solution for the same problem (measured speedup: 22.02114238961273x):
#include<stdio.h>

#include<algorithm>

using namespace std;

int dp[100001][3]={0};//第i天选了第j种

int a[100001],b[100001],c[100001];

int main(){

    int n;

    scanf("%d",&n);

    for(int i=1;i<=n;i++){

        scanf("%d%d%d",&a[i],&b[i],&c[i]);

    }

    dp[1][0] = a[1];

    dp[1][1] = b[1];

    dp[1][2] = c[1];

    for(int i=2;i<=n;i++){

        dp[i][0] = max(dp[i-1][1],dp[i-1][2])+a[i];

        dp[i][1] = max(dp[i-1][2],dp[i-1][0])+b[i];

        dp[i][2] = max(dp[i-1][0],dp[i-1][1])+c[i];

    }

    int ans = 0;

    for(int i=0;i<3;i++){

        ans = max(dp[n][i],ans);

    }

    printf("%d\n",ans);

}
// Slower C++ solution:
/******

author :- Mahmoud Tarek.

*******/



#include<bits/stdc++.h>



using namespace std ;



#define rg register

#define dbg(v) cerr << #v << " " << v ;

typedef long long ll ;



int n;

struct doW{

int w[5] ;

};



vector<doW> a ;

int mem[100002][6] ;



int solve(int i, int ch){

if(i == n) return 0 ;

int & ret = mem[i][ch] ;

if(~ret) return ret ;

ret = 0 ;

for(int j = 2 ; j <= 4 ; j++){

    if(ch != j){

        ret = max(ret, a[i].w[j] + solve(i + 1, j)) ;

    }

}

return ret ;

}



int main() {

//  freopen("in.txt", "r", stdin) ;

    cin >> n ;

    a.resize(n) ;

    memset(mem, -1, sizeof mem) ;

    for(int i = 0 ; i < n ; i++){

        for(int j = 2 ; j <= 4 ; j++) cin >> a[i].w[j] ;

    }

    cout << solve(0, 1) ;



}



/*

  Empty set of troubles.

*/


// Faster C++ solution for the same problem (measured speedup: 17.690755567468383x):
#include<stdio.h>

#include<algorithm>

using namespace std;

int dp[100001][3]={0};//第i天选了第j种

int a[100001],b[100001],c[100001];

int main(){

    int n;

    scanf("%d",&n);

    for(int i=1;i<=n;i++){

        scanf("%d%d%d",&a[i],&b[i],&c[i]);

    }

    dp[1][0] = a[1];

    dp[1][1] = b[1];

    dp[1][2] = c[1];

    for(int i=2;i<=n;i++){

        dp[i][0] = max(dp[i-1][1],dp[i-1][2])+a[i];

        dp[i][1] = max(dp[i-1][2],dp[i-1][0])+b[i];

        dp[i][2] = max(dp[i-1][0],dp[i-1][1])+c[i];

    }

    int ans = 0;

    for(int i=0;i<3;i++){

        ans = max(dp[n][i],ans);

    }

    printf("%d\n",ans);

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;



const int N=1e5+30;

typedef long long ll;

int n;

int a[N],b[N],c[N];

ll dp[N][5];

ll solve(int i,int last){

    if(i==n)return 0;

    int path1=-1 ,path2=-1,path3=-1;

    ll &ret=dp[i][last];

    if(~ret)return ret;

    if(last!=1){

        path1=a[i]+solve(i+1,1);

    }

    if(last!=2){

        path2=b[i]+solve(i+1,2);

    }

    if(last!=3){

        path3=c[i]+solve(i+1,3);

    }

    return ret=max(path1,max(path2,path3));



}

int main()

{

    memset(dp,-1,sizeof dp);

    cin>>n;

    for(int i=0;i<n;i++)cin>>a[i]>>b[i]>>c[i];

    cout<<solve(0,4);



}




// Faster C++ solution for the same problem (measured speedup: 26.39807482868059x):
#include<stdio.h>

#include<algorithm>

using namespace std;

int dp[100001][3]={0};//第i天选了第j种

int a[100001],b[100001],c[100001];

int main(){

    int n;

    scanf("%d",&n);

    for(int i=1;i<=n;i++){

        scanf("%d%d%d",&a[i],&b[i],&c[i]);

    }

    dp[1][0] = a[1];

    dp[1][1] = b[1];

    dp[1][2] = c[1];

    for(int i=2;i<=n;i++){

        dp[i][0] = max(dp[i-1][1],dp[i-1][2])+a[i];

        dp[i][1] = max(dp[i-1][2],dp[i-1][0])+b[i];

        dp[i][2] = max(dp[i-1][0],dp[i-1][1])+c[i];

    }

    int ans = 0;

    for(int i=0;i<3;i++){

        ans = max(dp[n][i],ans);

    }

    printf("%d\n",ans);

}