stablegradients/pie-gem5-bysrc huggingface dataset:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;



typedef signed long long ll;

const int MX = 1e5 + 5;

const int INF = 1e9 + 5;

int n, A[MX][3], dp[MX][4];



int solve(int pos, int prev) {

    if(pos >= n) return 0;

    if(dp[pos][prev] != -1) return dp[pos][prev];

    int ans = 0;

    if(prev == 1) 

        ans = max(ans, max(A[pos][1] + solve(pos + 1, 2), A[pos][2] + solve(pos + 1, 3)));

    else if(prev == 2) 

        ans = max(ans, max(A[pos][0] + solve(pos + 1, 1), A[pos][2] + solve(pos + 1, 3)));

    else if(prev == 3) 

        ans = max(ans, max(A[pos][1] + solve(pos + 1, 2), A[pos][0] + solve(pos + 1, 1)));

    else

        ans = max(A[pos][0] + solve(pos + 1, 1), max(A[pos][1] + solve(pos + 1, 2), A[pos][2] + solve(pos + 1, 3)));

    return dp[pos][prev] = ans;

}



int main() {

    scanf("%d", &n);



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

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

    }

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

    int ans = solve(0, 0);

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

    return 0;

}

// Optimized solution to the same problem (faster):
#include <stdio.h>

#include<algorithm>

using namespace std;

int dp[2][3];

int memo[3];

int k[2] = { 0,1 };



int main(){

    int day;

    scanf("%d", &day); 

    int i = 1;

    while (day--) {

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

        dp[k[i % 2]][0] = memo[0] + max(dp[k[(i + 1) % 2]][1], dp[k[(i + 1) % 2]][2]);

        dp[k[i % 2]][1] = memo[1] + max(dp[k[(i + 1) % 2]][0], dp[k[(i + 1) % 2]][2]);

        dp[k[i % 2]][2] = memo[2] + max(dp[k[(i + 1) % 2]][1], dp[k[(i + 1) % 2]][0]);

        i++;

    }

    printf("%d\n", max(max(dp[k[(i + 1) % 2]][0], dp[k[(i + 1) % 2]][1]), dp[k[(i + 1) % 2]][2]));

    return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>

#include <cstdio>

#include <algorithm>



#define rep(i, a, b) for(int i = a; i <= b; i ++)

#define pre(i, a, b) for(int i = a; i >= b; i --)



using namespace std;



typedef long long ll;



const int maxn = 1e5 + 10;

const int inf = 0x3f3f3f3f;

const int mod = 1e9 + 7;



int a[maxn][4], dp[maxn][4], n;



int read()

{

	int x;

	scanf("%d", &x);

	return x;

}



int main()

{

	n = read();

	rep(i, 1, n)

	{

		rep(j, 1, 3)

		{

			a[i][j] = read();

		}

	}

	rep(i, 1, 3)

	{

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

	}

	rep(i, 2, n)

	{

		rep(j, 1, 3)

		{

			rep(k, 1, 3)

			{

				if(k == j)continue; //Á½¸ö»î¶¯²»ÄÜÏàÍ¬ 

				dp[i][k] = max(dp[i][k], dp[i - 1][j] + a[i][k]);

			}

		}

	}

	int ans = 0;

	rep(i, 1, 3)

	{

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

	}

	cout << ans << endl;

	return 0;

}

// Optimized solution to the same problem (faster):
#include <stdio.h>

#include<algorithm>

using namespace std;

int dp[2][3];

int memo[3];

int k[2] = { 0,1 };



int main(){

    int day;

    scanf("%d", &day); 

    int i = 1;

    while (day--) {

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

        dp[k[i % 2]][0] = memo[0] + max(dp[k[(i + 1) % 2]][1], dp[k[(i + 1) % 2]][2]);

        dp[k[i % 2]][1] = memo[1] + max(dp[k[(i + 1) % 2]][0], dp[k[(i + 1) % 2]][2]);

        dp[k[i % 2]][2] = memo[2] + max(dp[k[(i + 1) % 2]][1], dp[k[(i + 1) % 2]][0]);

        i++;

    }

    printf("%d\n", max(max(dp[k[(i + 1) % 2]][0], dp[k[(i + 1) % 2]][1]), dp[k[(i + 1) % 2]][2]));

    return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

#define ll long long

using namespace std;

ll n,a[100009][4],dp[100009][4],k;

int main(){

    cin >> n;

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

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

    }

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

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

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

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

    }//ashsijk

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

}




// Optimized solution to the same problem (faster):
#include <stdio.h>

#include<algorithm>

using namespace std;

int dp[2][3];

int memo[3];

int k[2] = { 0,1 };



int main(){

    int day;

    scanf("%d", &day); 

    int i = 1;

    while (day--) {

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

        dp[k[i % 2]][0] = memo[0] + max(dp[k[(i + 1) % 2]][1], dp[k[(i + 1) % 2]][2]);

        dp[k[i % 2]][1] = memo[1] + max(dp[k[(i + 1) % 2]][0], dp[k[(i + 1) % 2]][2]);

        dp[k[i % 2]][2] = memo[2] + max(dp[k[(i + 1) % 2]][1], dp[k[(i + 1) % 2]][0]);

        i++;

    }

    printf("%d\n", max(max(dp[k[(i + 1) % 2]][0], dp[k[(i + 1) % 2]][1]), dp[k[(i + 1) % 2]][2]));

    return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

const int N = 1e5+5;

int dp[N][3];

int grid[N][3];

int main()

{

     int n;cin>>n;

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

     {

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

     	  {

     	  	cin>>grid[i][k];

     	  }

     }

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

     {

     	for(int k=1;k<=3;k++) dp[i][k] = 0;

     }

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

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

     		dp[i][k] = grid[i][k];

     //cout<<dp[1][1]<<" "<<dp[1][2]<<" "<<dp[1][3]<<endl;

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

     {

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

     	{

     		if(k==1)

     		{

     			dp[i+1][k+1] = max(dp[i+1][k+1],dp[i][k]+grid[i+1][k+1]);

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

     		}

     		else if(k==2)

     		{

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

     			dp[i+1][k+1] = max(dp[i+1][k+1],dp[i][k]+grid[i+1][k+1]);

     		}

     		else 

     		{

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

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

     		}

     	}

     }

     int res = -1;

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

     { 

     	  //cout<<dp[n][i]<<endl;

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

     }

     cout<<res<<"\n";

     return 0;



}

// Optimized solution to the same problem (faster):
#include <stdio.h>

#include<algorithm>

using namespace std;

int dp[2][3];

int memo[3];

int k[2] = { 0,1 };



int main(){

    int day;

    scanf("%d", &day); 

    int i = 1;

    while (day--) {

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

        dp[k[i % 2]][0] = memo[0] + max(dp[k[(i + 1) % 2]][1], dp[k[(i + 1) % 2]][2]);

        dp[k[i % 2]][1] = memo[1] + max(dp[k[(i + 1) % 2]][0], dp[k[(i + 1) % 2]][2]);

        dp[k[i % 2]][2] = memo[2] + max(dp[k[(i + 1) % 2]][1], dp[k[(i + 1) % 2]][0]);

        i++;

    }

    printf("%d\n", max(max(dp[k[(i + 1) % 2]][0], dp[k[(i + 1) % 2]][1]), dp[k[(i + 1) % 2]][2]));

    return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using ll = long long;



int main(){

	std::cin.tie(0);

	std::ios::sync_with_stdio(false);



	int N;

	std::cin >> N;



	std::vector<ll> Hap[3];

	std::vector<ll> dp[3];

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

		Hap[i].resize(N+1, 0);

		dp[i].resize(N+1, 0);

	}

	

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

		std::cin >> Hap[0][i] >> Hap[1][i] >> Hap[2][i];

	}





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

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

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

				if( j == k ) continue;

				dp[j][i] = std::max(dp[j][i], dp[k][i-1] + Hap[j][i] );

			}

		}

	}

	std::cout << std::max( dp[0][N], std::max(dp[1][N], dp[2][N]) ) << '\n';

	return 0;

}

// Optimized solution to the same problem (faster):
#include <stdio.h>

#include<algorithm>

using namespace std;

int dp[2][3];

int memo[3];

int k[2] = { 0,1 };



int main(){

    int day;

    scanf("%d", &day); 

    int i = 1;

    while (day--) {

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

        dp[k[i % 2]][0] = memo[0] + max(dp[k[(i + 1) % 2]][1], dp[k[(i + 1) % 2]][2]);

        dp[k[i % 2]][1] = memo[1] + max(dp[k[(i + 1) % 2]][0], dp[k[(i + 1) % 2]][2]);

        dp[k[i % 2]][2] = memo[2] + max(dp[k[(i + 1) % 2]][1], dp[k[(i + 1) % 2]][0]);

        i++;

    }

    printf("%d\n", max(max(dp[k[(i + 1) % 2]][0], dp[k[(i + 1) % 2]][1]), dp[k[(i + 1) % 2]][2]));

    return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
//下一个状态选上一个状态不与自己相同的另外两个的最大值 

#include<cstdio>

#include<cmath>

#include<cstring>

#include<cstdlib>

#include<iostream>

#include<algorithm>

#define ll long long



using namespace std;

const int MaxN = 100005;



ll n, i;

ll a[MaxN], b[MaxN], c[MaxN];

ll dp[MaxN][5];



int max(int x, int y){

	x>y?x:y;

} 



int main(){

	cin >> n;

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

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

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

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

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

        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( max(dp[n][1], dp[n][2]), dp[n][3] );

    return 0; 

}

// Optimized solution to the same problem (faster):
#include <stdio.h>

#include<algorithm>

using namespace std;

int dp[2][3];

int memo[3];

int k[2] = { 0,1 };



int main(){

    int day;

    scanf("%d", &day); 

    int i = 1;

    while (day--) {

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

        dp[k[i % 2]][0] = memo[0] + max(dp[k[(i + 1) % 2]][1], dp[k[(i + 1) % 2]][2]);

        dp[k[i % 2]][1] = memo[1] + max(dp[k[(i + 1) % 2]][0], dp[k[(i + 1) % 2]][2]);

        dp[k[i % 2]][2] = memo[2] + max(dp[k[(i + 1) % 2]][1], dp[k[(i + 1) % 2]][0]);

        i++;

    }

    printf("%d\n", max(max(dp[k[(i + 1) % 2]][0], dp[k[(i + 1) % 2]][1]), dp[k[(i + 1) % 2]][2]));

    return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <stdio.h>

#include <iostream>

#include <algorithm>



using namespace std;



int main(){

    int n;

    scanf("%d", &n);

    int mat[n][3];

    cin >> mat[0][0] >> mat[0][1] >> mat[0][2];

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

        int a, b, c;

        cin >> a >> b >> c;

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

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

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

    }

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

    

    return 0;

}

// Optimized solution to the same problem (faster):
#include <stdio.h>

#include<algorithm>

using namespace std;

int dp[2][3];

int memo[3];

int k[2] = { 0,1 };



int main(){

    int day;

    scanf("%d", &day); 

    int i = 1;

    while (day--) {

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

        dp[k[i % 2]][0] = memo[0] + max(dp[k[(i + 1) % 2]][1], dp[k[(i + 1) % 2]][2]);

        dp[k[i % 2]][1] = memo[1] + max(dp[k[(i + 1) % 2]][0], dp[k[(i + 1) % 2]][2]);

        dp[k[i % 2]][2] = memo[2] + max(dp[k[(i + 1) % 2]][1], dp[k[(i + 1) % 2]][0]);

        i++;

    }

    printf("%d\n", max(max(dp[k[(i + 1) % 2]][0], dp[k[(i + 1) % 2]][1]), dp[k[(i + 1) % 2]][2]));

    return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

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

const int N = 1e6;

const int inf = 1e9;



int main(){

	ios_base::sync_with_stdio(0);

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

	cin>>n;

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

	for(vector<int> &x: A) {

		cin>>x[0]>>x[1]>>x[2];

	}

	vector< vector<int> > dp(n+1, vector<int>(3));

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

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

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

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

	}

	cout<<max(dp[n][0], max(dp[n][1], dp[n][2]))<<'\n';

	return 0;

}

// Optimized solution to the same problem (faster):
#include <stdio.h>

#include<algorithm>

using namespace std;

int dp[2][3];

int memo[3];

int k[2] = { 0,1 };



int main(){

    int day;

    scanf("%d", &day); 

    int i = 1;

    while (day--) {

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

        dp[k[i % 2]][0] = memo[0] + max(dp[k[(i + 1) % 2]][1], dp[k[(i + 1) % 2]][2]);

        dp[k[i % 2]][1] = memo[1] + max(dp[k[(i + 1) % 2]][0], dp[k[(i + 1) % 2]][2]);

        dp[k[i % 2]][2] = memo[2] + max(dp[k[(i + 1) % 2]][1], dp[k[(i + 1) % 2]][0]);

        i++;

    }

    printf("%d\n", max(max(dp[k[(i + 1) % 2]][0], dp[k[(i + 1) % 2]][1]), dp[k[(i + 1) % 2]][2]));

    return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<iostream>



using namespace std;



int main(){

	

	int N;

	cin >> N;

	

	int A[N] = {}, B[N] = {}, C[N] = {};

	

	int custo[N][3] = {};

	

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

		cin>>A[i]>>B[i]>>C[i];

	}

	

	

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

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

			if(i == 0){

				switch(j){

					case 0: custo[i][j] = A[i]; break; 

					case 1:	custo[i][j] = B[i]; break;

					case 2: custo[i][j] = C[i]; break;

				}

			}

			else{

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

					if(j == k) continue;

					switch(j){

						case 0: custo[i][j] = max(custo[i][j], custo[i-1][k] + A[i]);break;

						case 1:	custo[i][j] = max(custo[i][j], custo[i-1][k] + B[i]);break;

						case 2: custo[i][j] = max(custo[i][j], custo[i-1][k] + C[i]);break;

					}

				}

				

			}

		

		}

	}

	

	int maximo = 0;

	

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

		if(custo[N-1][i] > maximo)

			maximo = custo[N-1][i];

	}

	cout<< maximo << endl;

	return 0;

}

// Optimized solution to the same problem (faster):
#include <stdio.h>

#include<algorithm>

using namespace std;

int dp[2][3];

int memo[3];

int k[2] = { 0,1 };



int main(){

    int day;

    scanf("%d", &day); 

    int i = 1;

    while (day--) {

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

        dp[k[i % 2]][0] = memo[0] + max(dp[k[(i + 1) % 2]][1], dp[k[(i + 1) % 2]][2]);

        dp[k[i % 2]][1] = memo[1] + max(dp[k[(i + 1) % 2]][0], dp[k[(i + 1) % 2]][2]);

        dp[k[i % 2]][2] = memo[2] + max(dp[k[(i + 1) % 2]][1], dp[k[(i + 1) % 2]][0]);

        i++;

    }

    printf("%d\n", max(max(dp[k[(i + 1) % 2]][0], dp[k[(i + 1) % 2]][1]), dp[k[(i + 1) % 2]][2]));

    return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

#include <iostream>

using namespace std;



int main() {

  long long n;



  cin>>n;



  long long A[n],B[n],C[n];

  long long agendaDP[n][3];



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

    agendaDP[0][i] = 0;

  }



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

      cin>>A[i]>>B[i]>>C[i];

  }



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

      agendaDP[i+1][0] = max(agendaDP[i][1], agendaDP[i][2]) + A[i];

      agendaDP[i+1][1] = max(agendaDP[i][0], agendaDP[i][2]) + B[i];

      agendaDP[i+1][2] = max(agendaDP[i][0], agendaDP[i][1]) + C[i];

  }



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



}


// Optimized solution to the same problem (faster):
#include <stdio.h>

#include<algorithm>

using namespace std;

int dp[2][3];

int memo[3];

int k[2] = { 0,1 };



int main(){

    int day;

    scanf("%d", &day); 

    int i = 1;

    while (day--) {

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

        dp[k[i % 2]][0] = memo[0] + max(dp[k[(i + 1) % 2]][1], dp[k[(i + 1) % 2]][2]);

        dp[k[i % 2]][1] = memo[1] + max(dp[k[(i + 1) % 2]][0], dp[k[(i + 1) % 2]][2]);

        dp[k[i % 2]][2] = memo[2] + max(dp[k[(i + 1) % 2]][1], dp[k[(i + 1) % 2]][0]);

        i++;

    }

    printf("%d\n", max(max(dp[k[(i + 1) % 2]][0], dp[k[(i + 1) % 2]][1]), dp[k[(i + 1) % 2]][2]));

    return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;

#define f first

#define s second

#define ll long long

#define ld long double

#define pii pair <int,int>

#define pll pair <ll,ll>

#define pil pair <int,ll>

#define pli pair <ll,int>

#define ull unsigned long long

#define pdd pair <double,double>

#define pb push_back

#define mp make_pair

#define puu pair<ull,ull>

#define pvv pair<vector<int>, vector<int> >

#define _bp __builtin_popcount

#define ptt pair <Tree*,Tree*>

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

const long double pi = acos(-1);

void boos()

{

     ios_base :: sync_with_stdio(false);

     cin.tie(0);

     cout.tie(0);

}

void file()

{

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

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

}

const ll INF = 1e9 + 7;

const ll INF1 = 998244353;

const ll LLINF = (ll)1e18;

const int  INTmx = (int)1e9;

const int N = 2e5 + 100;

ll dp[N][3];

ll a[N];

ll b[N];

ll c[N];

int main()

{

    //file();

    boos();

    int n;

    cin >> n;

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

    {

        cin >> 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][0],dp[i - 1][2]) + b[i];

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

    }

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

}

// Optimized solution to the same problem (faster):
#include <stdio.h>

#include<algorithm>

using namespace std;

int dp[2][3];

int memo[3];

int k[2] = { 0,1 };



int main(){

    int day;

    scanf("%d", &day); 

    int i = 1;

    while (day--) {

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

        dp[k[i % 2]][0] = memo[0] + max(dp[k[(i + 1) % 2]][1], dp[k[(i + 1) % 2]][2]);

        dp[k[i % 2]][1] = memo[1] + max(dp[k[(i + 1) % 2]][0], dp[k[(i + 1) % 2]][2]);

        dp[k[i % 2]][2] = memo[2] + max(dp[k[(i + 1) % 2]][1], dp[k[(i + 1) % 2]][0]);

        i++;

    }

    printf("%d\n", max(max(dp[k[(i + 1) % 2]][0], dp[k[(i + 1) % 2]][1]), dp[k[(i + 1) % 2]][2]));

    return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>                                           

 

#define int long long

//#pragma GCC optimize("Ofast")

//#pragma comment(linker, "/stack:200000000")

//#pragma GCC target("sse,sse2,sse3,ssse3,sse4")

 

 

#define file(s) freopen(s".in","r",stdin); freopen(s".out","w",stdout);

#define forev(i, b, a) for(int i = (b); i >= (a); --i)

#define forn(i, a, b) for(int i = (a); i <= (b); ++i)

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

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

#define pb push_back

#define ppb pop_back

#define mp make_pair

#define s second

#define f first

 

 

using namespace std;

 

 

typedef pair < long long, long long > pll;    

typedef pair < int, int > pii;

typedef unsigned long long ull;         

typedef vector < pii > vpii;

typedef vector < int > vi;

typedef long double ldb; 

typedef long long ll;  

typedef double db;                         

 

 

const int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1}, block = 555;

const pii base = mp(1171, 3307), Mod = mp(1e9 + 7, 1e9 + 9);

const int inf = 1e9, N = 2e5 + 48, mod = 1e9 + 7;

const db eps = 1e-12, pi = 3.14159265359;

const ll INF = 1e18;





int n, a, b, c, dp[N][3];



main () {

	cin >> n;

	forn (i, 1, n) {

		cin >> a >> b >> c;

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

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

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

	}





	cout << max({dp[n][0], dp[n][1], dp[n][2]});	

} 

// Optimized solution to the same problem (faster):
#include <stdio.h>

#include<algorithm>

using namespace std;

int dp[2][3];

int memo[3];

int k[2] = { 0,1 };



int main(){

    int day;

    scanf("%d", &day); 

    int i = 1;

    while (day--) {

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

        dp[k[i % 2]][0] = memo[0] + max(dp[k[(i + 1) % 2]][1], dp[k[(i + 1) % 2]][2]);

        dp[k[i % 2]][1] = memo[1] + max(dp[k[(i + 1) % 2]][0], dp[k[(i + 1) % 2]][2]);

        dp[k[i % 2]][2] = memo[2] + max(dp[k[(i + 1) % 2]][1], dp[k[(i + 1) % 2]][0]);

        i++;

    }

    printf("%d\n", max(max(dp[k[(i + 1) % 2]][0], dp[k[(i + 1) % 2]][1]), dp[k[(i + 1) % 2]][2]));

    return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
//#pragma GCC target ("avx2,sse2")

//#pragma GCC optimization ("Ofast")

//#pragma GCC optimization ("unroll-loops")

#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>

#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;



#define ordered_set tree <pair <ll, int>, null_type, less <pair <ll, int> >, rb_tree_tag, tree_order_statistics_node_update>

#define ll long long

#define ull unsigned long long

#define db long double

#define pb push_back

#define pf push_front

#define ppb pop_back

#define ppf pop_front

#define fi first

#define se second

#define mp make_pair

#define up_b upper_bound

#define low_b lower_bound

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

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

#define endl "\n"



#define left fsdsdfoisf

#define sum dpsdfioppsf

#define assign xcvjlkdjfio

#define trie fksdfkjkfnjuiv

#define next sidlfjsfkl

#define merge sdfksdkfsldf



#define int long long



using namespace std;



void dout() {

    cerr << endl;

}

template <typename Head, typename... Tail>

void dout(Head H, Tail... T) {

    cerr << H << ' ';

    dout(T...);

}



//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

typedef pair <int, int> pii;



const int N = 1e5 + 7;

int n, c[3][N], dp[3][N];



void solve(int tc) {

    // check for (int i = 0; i < n; j++)

    cin >> n;

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

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

            cin >> c[j][i];

        }

    }

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

        dp[i][1] = c[i][1];

    }

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

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

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

        }

    }

    int res = 0;

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

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

    }

    cout << res;

}



signed main() {

    ios_base::sync_with_stdio(false);

    cin.tie(NULL);

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

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

    int tc = 1;

//    cin >> tc;

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

        solve(i);

//        cleanup();

    }

}


// Optimized solution to the same problem (faster):
#include <stdio.h>

#include<algorithm>

using namespace std;

int dp[2][3];

int memo[3];

int k[2] = { 0,1 };



int main(){

    int day;

    scanf("%d", &day); 

    int i = 1;

    while (day--) {

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

        dp[k[i % 2]][0] = memo[0] + max(dp[k[(i + 1) % 2]][1], dp[k[(i + 1) % 2]][2]);

        dp[k[i % 2]][1] = memo[1] + max(dp[k[(i + 1) % 2]][0], dp[k[(i + 1) % 2]][2]);

        dp[k[i % 2]][2] = memo[2] + max(dp[k[(i + 1) % 2]][1], dp[k[(i + 1) % 2]][0]);

        i++;

    }

    printf("%d\n", max(max(dp[k[(i + 1) % 2]][0], dp[k[(i + 1) % 2]][1]), dp[k[(i + 1) % 2]][2]));

    return 0;

}


// Fastest known solution for this problem: