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

using namespace std;

#define db(x) cerr<<__LINE__<<": "<<#x<<" = "<<(x)<<'\n'

const int N = 1e5;

int A[N+7][5], mem[N+7][5];



int dp(int i, int j) {

	if(i==0) return 0;

	if(mem[i][j]!=-1) return mem[i][j];

	if(j==1) return mem[i][j] = A[i][j] + max(dp(i-1, 2), dp(i-1, 3));

	if(j==2) return mem[i][j] = A[i][j] + max(dp(i-1, 3), dp(i-1, 1));

	else return mem[i][j] = A[i][j] + max(dp(i-1, 1), dp(i-1, 2));

}

int main(){

	ios_base::sync_with_stdio(0);

	int a,b,n,T,cas=0;

	cin>>n;

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

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

	}

	memset(mem, -1, sizeof(mem));

	int res = max(dp(n,1), max(dp(n,2), dp(n,3)));

	cout<<res<<endl;

	return 0;

}

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

/**

*   author: j o s e m a g o

*   date:

**/

using namespace std;

#pragma GCC optimize ("Ofast")

#define f first

#define s second

#define mp make_pair

#define lb lower_bound

#define ub upper_bound

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

#define mset(a,v) memset(a,v,sizeof(a))

#define pb push_back

#define sz(x) ((int)(x).size())

#define rsz resize

#define ln "\n"



typedef double db;

typedef unsigned long long ull;

typedef long long ll;

typedef long double ld;

const ld PI = 2*acos(0.0);

const int MOD = 1000000007;





int matriz[100009][3];

int dp[100009][3];







int main(){



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

  //freopen("input.out","w",stdout);



    ios_base::sync_with_stdio(0); cin.tie(0);





    int n;

    cin >> n;

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

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

            cin >> matriz[i][j];

        }

    }

    int m = 0;

    dp[0][0] = matriz[0][0]; dp[0][1] = matriz[0][1]; dp[0][2] = matriz[0][2];

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



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



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

                if(k != j){

                    m = max(dp[i-1][k],m);

                }

            }

            dp[i][j]+=m;

            dp[i][j]+= matriz[i][j];

            m = 0;

        }

    }



    int sol = 0;

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

        sol = max(dp[n-1][i],sol);

    }

    cout<<sol;







    return 0;

}

/*

7

6 7 8

8 8 3

2 5 2

7 8 6

4 6 8

2 3 4

7 5 1







*/
// Slower C++ solution:
#include <cstdio>

#include <algorithm>

#include <cstring>

const int maxn = 3e5 + 5;

using namespace std;



int N;

int a[maxn][4];

int dp[maxn][4];

int main()

{

	scanf("%d", &N);

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

	{

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

		{

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

		}

	}

	memset(dp, 0, sizeof dp);

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

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

	for(int i = 2;i <= N;i++)

	{

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

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

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

	}

	int ans = 0;

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

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

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

	return 0;	

}

// Faster C++ solution for the same problem (measured speedup: 5.101643322981249x):
#include <iostream>



/* run this program using the console pauser or add your own getch, system("pause") or input loop */

using namespace std;





int maxRow(int dp[][3],bool calculated[][3],int M[][3],int selection,int row){

	if(calculated[row][selection])return dp[row][selection];

	if(row==0){

		calculated[row][selection]=true;

		return dp[row][selection]=M[row][selection];

		

		}

		int max=-1;

		int parent=-1;

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

			if(i!=selection){

				if(max<maxRow(dp,calculated,M,i,row-1)+M[row][selection]){

				max=maxRow(dp,calculated,M,i,row-1)+M[row][selection];	

				parent=i;

				}

				

				

			}

		}

		calculated[row][selection]=true;

	

		

		return	dp[row][selection]=max; 

				

	}



int main(int argc, char** argv) {

	int N;

	cin>>N;

   int M[N][3];	

   bool calculated[N][3];

   int dp[N][3];

   

   





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

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

			cin>>M[i][j];

		calculated[i][j]=false;



		}

	}

	

	//cout<<maxRow(dp,calculated,M,0,N-1)<<endl;	

		int max=-1;

		int head=-1;

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

		if(max<maxRow(dp,calculated,M,i,N-1))

		max=maxRow(dp,calculated,M,i,N-1);

		

		}

	

	cout<<max<<endl;

	

	return 0;

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

using namespace std;

#define db(x) cerr<<__LINE__<<": "<<#x<<" = "<<(x)<<'\n'

const int N = 1e5;

int A[N+7][5], mem[N+7][5];



int dp(int i, int j) {

	if(i==0) return 0;

	if(mem[i][j]!=-1) return mem[i][j];

	if(j==1) return mem[i][j] = A[i][j] + max(dp(i-1, 2), dp(i-1, 3));

	if(j==2) return mem[i][j] = A[i][j] + max(dp(i-1, 3), dp(i-1, 1));

	else return mem[i][j] = A[i][j] + max(dp(i-1, 1), dp(i-1, 2));

}

int main(){

	ios_base::sync_with_stdio(0);

	int a,b,n,T,cas=0;

	cin>>n;

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

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

	}

	memset(mem, -1, sizeof(mem));

	int res = max(dp(n,1), max(dp(n,2), dp(n,3)));

	cout<<res<<endl;

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 3.01005785304504x):
#include <iostream>



/* run this program using the console pauser or add your own getch, system("pause") or input loop */

using namespace std;





int maxRow(int dp[][3],bool calculated[][3],int M[][3],int selection,int row){

	if(calculated[row][selection])return dp[row][selection];

	if(row==0){

		calculated[row][selection]=true;

		return dp[row][selection]=M[row][selection];

		

		}

		int max=-1;

		int parent=-1;

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

			if(i!=selection){

				if(max<maxRow(dp,calculated,M,i,row-1)+M[row][selection]){

				max=maxRow(dp,calculated,M,i,row-1)+M[row][selection];	

				parent=i;

				}

				

				

			}

		}

		calculated[row][selection]=true;

	

		

		return	dp[row][selection]=max; 

				

	}



int main(int argc, char** argv) {

	int N;

	cin>>N;

   int M[N][3];	

   bool calculated[N][3];

   int dp[N][3];

   

   





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

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

			cin>>M[i][j];

		calculated[i][j]=false;



		}

	}

	

	//cout<<maxRow(dp,calculated,M,0,N-1)<<endl;	

		int max=-1;

		int head=-1;

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

		if(max<maxRow(dp,calculated,M,i,N-1))

		max=maxRow(dp,calculated,M,i,N-1);

		

		}

	

	cout<<max<<endl;

	

	return 0;

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

#include <algorithm>

#include <cstring>

const int maxn = 3e5 + 5;

using namespace std;



int N;

int a[maxn][4];

int dp[maxn][4];

int main()

{

	scanf("%d", &N);

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

	{

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

		{

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

		}

	}

	memset(dp, 0, sizeof dp);

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

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

	for(int i = 2;i <= N;i++)

	{

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

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

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

	}

	int ans = 0;

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

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

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

	return 0;	

}

// Faster C++ solution for the same problem (measured speedup: 2.3351086798822984x):
/*|In The Name Of Allah|*/



#include <bits/stdc++.h>



using namespace std;



const int N = 1e5+5;



int n;

int a[N][3];

int mem[N][3];



int go(int idx , int lst){

    if(idx == n + 1)

        return 0;

    if(~mem[idx][lst])

        return mem[idx][lst];

    int best = 0;

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

        if(idx == 1 || lst != i){

            best = max(best , go(idx + 1 , i) + a[idx][i]);

        }

    }

    return mem[idx][lst] = best;

}



int main(){

    scanf("%d" , &n);

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

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

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

        }

    }

    memset(mem , -1 , sizeof mem);

    printf("%d\n" , go(1 , 0));

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

using namespace std;

#define db(x) cerr<<__LINE__<<": "<<#x<<" = "<<(x)<<'\n'

const int N = 1e5;

int A[N+7][5], mem[N+7][5];



int dp(int i, int j) {

	if(i==0) return 0;

	if(mem[i][j]!=-1) return mem[i][j];

	if(j==1) return mem[i][j] = A[i][j] + max(dp(i-1, 2), dp(i-1, 3));

	if(j==2) return mem[i][j] = A[i][j] + max(dp(i-1, 3), dp(i-1, 1));

	else return mem[i][j] = A[i][j] + max(dp(i-1, 1), dp(i-1, 2));

}

int main(){

	ios_base::sync_with_stdio(0);

	int a,b,n,T,cas=0;

	cin>>n;

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

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

	}

	memset(mem, -1, sizeof(mem));

	int res = max(dp(n,1), max(dp(n,2), dp(n,3)));

	cout<<res<<endl;

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.3777545340990087x):
/*|In The Name Of Allah|*/



#include <bits/stdc++.h>



using namespace std;



const int N = 1e5+5;



int n;

int a[N][3];

int mem[N][3];



int go(int idx , int lst){

    if(idx == n + 1)

        return 0;

    if(~mem[idx][lst])

        return mem[idx][lst];

    int best = 0;

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

        if(idx == 1 || lst != i){

            best = max(best , go(idx + 1 , i) + a[idx][i]);

        }

    }

    return mem[idx][lst] = best;

}



int main(){

    scanf("%d" , &n);

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

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

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

        }

    }

    memset(mem , -1 , sizeof mem);

    printf("%d\n" , go(1 , 0));

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

#include <algorithm>

#include <cstring>

const int maxn = 3e5 + 5;

using namespace std;



int N;

int a[maxn][4];

int dp[maxn][4];

int main()

{

	scanf("%d", &N);

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

	{

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

		{

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

		}

	}

	memset(dp, 0, sizeof dp);

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

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

	for(int i = 2;i <= N;i++)

	{

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

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

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

	}

	int ans = 0;

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

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

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

	return 0;	

}

// Faster C++ solution for the same problem (measured speedup: 5.095865695994951x):
#include <functional>

#include <algorithm>

#include <iostream>

#include <cstring>

#include <cstdlib>

#include <iomanip>

#include <vector>

#include <cstdio>

#include <stack>

#include <queue>

#include <deque>

#include <cmath>

#include <list>

#include <set>

#include <map>



using namespace std;



long long a[100010],b[100010],c[100010],f[100010][10];



int main(void)

{

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

	//freopen("test.out","w",stdout);

	int n;

	cin >> n;

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

	{

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

	}

	f[1][1] = a[1];

	f[1][2] = b[1];

	f[1][3] = c[1];

	

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

	{

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

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

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

	}

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

	return 0;

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

using namespace std;

#define db(x) cerr<<__LINE__<<": "<<#x<<" = "<<(x)<<'\n'

const int N = 1e5;

int A[N+7][5], mem[N+7][5];



int dp(int i, int j) {

	if(i==0) return 0;

	if(mem[i][j]!=-1) return mem[i][j];

	if(j==1) return mem[i][j] = A[i][j] + max(dp(i-1, 2), dp(i-1, 3));

	if(j==2) return mem[i][j] = A[i][j] + max(dp(i-1, 3), dp(i-1, 1));

	else return mem[i][j] = A[i][j] + max(dp(i-1, 1), dp(i-1, 2));

}

int main(){

	ios_base::sync_with_stdio(0);

	int a,b,n,T,cas=0;

	cin>>n;

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

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

	}

	memset(mem, -1, sizeof(mem));

	int res = max(dp(n,1), max(dp(n,2), dp(n,3)));

	cout<<res<<endl;

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 3.0066489531316085x):
#include <functional>

#include <algorithm>

#include <iostream>

#include <cstring>

#include <cstdlib>

#include <iomanip>

#include <vector>

#include <cstdio>

#include <stack>

#include <queue>

#include <deque>

#include <cmath>

#include <list>

#include <set>

#include <map>



using namespace std;



long long a[100010],b[100010],c[100010],f[100010][10];



int main(void)

{

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

	//freopen("test.out","w",stdout);

	int n;

	cin >> n;

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

	{

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

	}

	f[1][1] = a[1];

	f[1][2] = b[1];

	f[1][3] = c[1];

	

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

	{

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

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

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

	}

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

	return 0;

}
// Slower C++ solution:
/*|In The Name Of Allah|*/



#include <bits/stdc++.h>



using namespace std;



const int N = 1e5+5;



int n;

int a[N][3];

int mem[N][3];



int go(int idx , int lst){

    if(idx == n + 1)

        return 0;

    if(~mem[idx][lst])

        return mem[idx][lst];

    int best = 0;

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

        if(idx == 1 || lst != i){

            best = max(best , go(idx + 1 , i) + a[idx][i]);

        }

    }

    return mem[idx][lst] = best;

}



int main(){

    scanf("%d" , &n);

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

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

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

        }

    }

    memset(mem , -1 , sizeof mem);

    printf("%d\n" , go(1 , 0));

}


// Faster C++ solution for the same problem (measured speedup: 2.182282023914968x):
#include <functional>

#include <algorithm>

#include <iostream>

#include <cstring>

#include <cstdlib>

#include <iomanip>

#include <vector>

#include <cstdio>

#include <stack>

#include <queue>

#include <deque>

#include <cmath>

#include <list>

#include <set>

#include <map>



using namespace std;



long long a[100010],b[100010],c[100010],f[100010][10];



int main(void)

{

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

	//freopen("test.out","w",stdout);

	int n;

	cin >> n;

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

	{

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

	}

	f[1][1] = a[1];

	f[1][2] = b[1];

	f[1][3] = c[1];

	

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

	{

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

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

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

	}

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

	return 0;

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

#include <algorithm>

#include <cstring>

const int maxn = 3e5 + 5;

using namespace std;



int N;

int a[maxn][4];

int dp[maxn][4];

int main()

{

	scanf("%d", &N);

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

	{

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

		{

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

		}

	}

	memset(dp, 0, sizeof dp);

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

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

	for(int i = 2;i <= N;i++)

	{

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

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

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

	}

	int ans = 0;

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

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

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

	return 0;	

}

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

#define eps 1e-3

#define pi acos(-1.0)

#define inf 0x3f3f3f3f

#define debug1 cout<<"&&";

#define debug2 cout<<"**";

#define pb push_back

#define ms(a, x) memset(a, x, sizeof(a))

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

#define for1(i, n) for(int i = 1; i <= n; ++i) 

typedef double db;

typedef long long ll;

typedef unsigned long long ull;

using namespace std;

const int mod = 1e9;

const int N = 1e5+10;

/*=======================================================================*/



int n;

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

// ll ab, ac, ba, bc, ca, cb;

ll aa, bb, cc;

ll ans = 0;



int main() {

   scanf("%d", &n);

   for1(i, n) {

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

   }

   for1(i, n) {

      if(i == 1) {

         aa += a[i];

         bb += b[i];

         cc += c[i];

      } else {

         int ta = aa, tb = bb, tc = cc;

         aa = max(tb + a[i], tc + a[i]);

         bb = max(ta + b[i], tc + b[i]);

         cc = max(ta + c[i], tb + c[i]);

      }

   }

   ans = max(ans, aa);

   ans = max(ans, cc);

   ans = max(ans, bb);

   cout << ans << endl;

   return 0;

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

using namespace std;

#define db(x) cerr<<__LINE__<<": "<<#x<<" = "<<(x)<<'\n'

const int N = 1e5;

int A[N+7][5], mem[N+7][5];



int dp(int i, int j) {

	if(i==0) return 0;

	if(mem[i][j]!=-1) return mem[i][j];

	if(j==1) return mem[i][j] = A[i][j] + max(dp(i-1, 2), dp(i-1, 3));

	if(j==2) return mem[i][j] = A[i][j] + max(dp(i-1, 3), dp(i-1, 1));

	else return mem[i][j] = A[i][j] + max(dp(i-1, 1), dp(i-1, 2));

}

int main(){

	ios_base::sync_with_stdio(0);

	int a,b,n,T,cas=0;

	cin>>n;

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

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

	}

	memset(mem, -1, sizeof(mem));

	int res = max(dp(n,1), max(dp(n,2), dp(n,3)));

	cout<<res<<endl;

	return 0;

}

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

#define eps 1e-3

#define pi acos(-1.0)

#define inf 0x3f3f3f3f

#define debug1 cout<<"&&";

#define debug2 cout<<"**";

#define pb push_back

#define ms(a, x) memset(a, x, sizeof(a))

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

#define for1(i, n) for(int i = 1; i <= n; ++i) 

typedef double db;

typedef long long ll;

typedef unsigned long long ull;

using namespace std;

const int mod = 1e9;

const int N = 1e5+10;

/*=======================================================================*/



int n;

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

// ll ab, ac, ba, bc, ca, cb;

ll aa, bb, cc;

ll ans = 0;



int main() {

   scanf("%d", &n);

   for1(i, n) {

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

   }

   for1(i, n) {

      if(i == 1) {

         aa += a[i];

         bb += b[i];

         cc += c[i];

      } else {

         int ta = aa, tb = bb, tc = cc;

         aa = max(tb + a[i], tc + a[i]);

         bb = max(ta + b[i], tc + b[i]);

         cc = max(ta + c[i], tb + c[i]);

      }

   }

   ans = max(ans, aa);

   ans = max(ans, cc);

   ans = max(ans, bb);

   cout << ans << endl;

   return 0;

}
// Slower C++ solution:
/*|In The Name Of Allah|*/



#include <bits/stdc++.h>



using namespace std;



const int N = 1e5+5;



int n;

int a[N][3];

int mem[N][3];



int go(int idx , int lst){

    if(idx == n + 1)

        return 0;

    if(~mem[idx][lst])

        return mem[idx][lst];

    int best = 0;

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

        if(idx == 1 || lst != i){

            best = max(best , go(idx + 1 , i) + a[idx][i]);

        }

    }

    return mem[idx][lst] = best;

}



int main(){

    scanf("%d" , &n);

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

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

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

        }

    }

    memset(mem , -1 , sizeof mem);

    printf("%d\n" , go(1 , 0));

}


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

#define eps 1e-3

#define pi acos(-1.0)

#define inf 0x3f3f3f3f

#define debug1 cout<<"&&";

#define debug2 cout<<"**";

#define pb push_back

#define ms(a, x) memset(a, x, sizeof(a))

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

#define for1(i, n) for(int i = 1; i <= n; ++i) 

typedef double db;

typedef long long ll;

typedef unsigned long long ull;

using namespace std;

const int mod = 1e9;

const int N = 1e5+10;

/*=======================================================================*/



int n;

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

// ll ab, ac, ba, bc, ca, cb;

ll aa, bb, cc;

ll ans = 0;



int main() {

   scanf("%d", &n);

   for1(i, n) {

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

   }

   for1(i, n) {

      if(i == 1) {

         aa += a[i];

         bb += b[i];

         cc += c[i];

      } else {

         int ta = aa, tb = bb, tc = cc;

         aa = max(tb + a[i], tc + a[i]);

         bb = max(ta + b[i], tc + b[i]);

         cc = max(ta + c[i], tb + c[i]);

      }

   }

   ans = max(ans, aa);

   ans = max(ans, cc);

   ans = max(ans, bb);

   cout << ans << endl;

   return 0;

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

#include <algorithm>

#include <cstring>

const int maxn = 3e5 + 5;

using namespace std;



int N;

int a[maxn][4];

int dp[maxn][4];

int main()

{

	scanf("%d", &N);

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

	{

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

		{

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

		}

	}

	memset(dp, 0, sizeof dp);

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

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

	for(int i = 2;i <= N;i++)

	{

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

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

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

	}

	int ans = 0;

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

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

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

	return 0;	

}

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

using namespace std;

typedef long long ll;

typedef long long int lli;

typedef long double ld;

const int oo = 1e9;

//const int mod = 1e9+9;

/*ll gcd(ll x, ll y){return (y ? gcd(y, x%y) : x); }

*ll lcm(ll x, ll y){return x*(y/gcd(x,y));}

*void exEuclid(int a, int b){

	if(b==0){x = 1; y=0; d=a; return ;}

	exEuclid(b, a%b);

	int x1 = y;

	int y1 = x-(a/b)*y;

	x = x1; 100000

	y = y1;

} 

* */

//===========================================================//

int n;

const int nx = 1e5+5;

int ac[nx][3];

int dp[3][nx];

int go(int a, int i){

	if(i == n) return 0;

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

	int& ans = dp[a][i];

	

	if(a==0){

		ans=max(ans, go(1, i+1)+ac[i][0]);

		ans=max(ans, go(2, i+1)+ac[i][0]);

	}

	if(a==1){

		ans=max(ans, go(0, i+1)+ac[i][1]);

		ans=max(ans, go(2, i+1)+ac[i][1]);

	}

	if(a==2){

		ans=max(ans, go(0, i+1)+ac[i][2]);

		ans=max(ans, go(1, i+1)+ac[i][2]);

	}

	

	return ans;

}

int main(){

	cin>>n;

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

	memset(dp, -1, sizeof dp);

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

	return 0;

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

using namespace std;

#define db(x) cerr<<__LINE__<<": "<<#x<<" = "<<(x)<<'\n'

const int N = 1e5;

int A[N+7][5], mem[N+7][5];



int dp(int i, int j) {

	if(i==0) return 0;

	if(mem[i][j]!=-1) return mem[i][j];

	if(j==1) return mem[i][j] = A[i][j] + max(dp(i-1, 2), dp(i-1, 3));

	if(j==2) return mem[i][j] = A[i][j] + max(dp(i-1, 3), dp(i-1, 1));

	else return mem[i][j] = A[i][j] + max(dp(i-1, 1), dp(i-1, 2));

}

int main(){

	ios_base::sync_with_stdio(0);

	int a,b,n,T,cas=0;

	cin>>n;

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

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

	}

	memset(mem, -1, sizeof(mem));

	int res = max(dp(n,1), max(dp(n,2), dp(n,3)));

	cout<<res<<endl;

	return 0;

}

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

using namespace std;

typedef long long ll;

typedef long long int lli;

typedef long double ld;

const int oo = 1e9;

//const int mod = 1e9+9;

/*ll gcd(ll x, ll y){return (y ? gcd(y, x%y) : x); }

*ll lcm(ll x, ll y){return x*(y/gcd(x,y));}

*void exEuclid(int a, int b){

	if(b==0){x = 1; y=0; d=a; return ;}

	exEuclid(b, a%b);

	int x1 = y;

	int y1 = x-(a/b)*y;

	x = x1; 100000

	y = y1;

} 

* */

//===========================================================//

int n;

const int nx = 1e5+5;

int ac[nx][3];

int dp[3][nx];

int go(int a, int i){

	if(i == n) return 0;

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

	int& ans = dp[a][i];

	

	if(a==0){

		ans=max(ans, go(1, i+1)+ac[i][0]);

		ans=max(ans, go(2, i+1)+ac[i][0]);

	}

	if(a==1){

		ans=max(ans, go(0, i+1)+ac[i][1]);

		ans=max(ans, go(2, i+1)+ac[i][1]);

	}

	if(a==2){

		ans=max(ans, go(0, i+1)+ac[i][2]);

		ans=max(ans, go(1, i+1)+ac[i][2]);

	}

	

	return ans;

}

int main(){

	cin>>n;

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

	memset(dp, -1, sizeof dp);

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

	return 0;

}