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

using namespace std;

 

#define ll long long int

#define tc(t) int t;cin >> t; while(t--)

#define pb push_back

#define fi first

#define se second

#define debug1(x) cerr << #x << " = " << x << '\n';

#define debug2(x, y) cerr << #x << " = " << x << "   " << #y << " = " << y << "\n";

#define _ ios_base::sync_with_stdio(0);cin.tie(NULL);

 

int main() {_

    ll n;

    cin >> n;

    ll dp[n][3] = {};

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

        ll a, b, c;

        cin >> a >> b >> c;

        if(i == 0) {

            dp[i][0] = a;

            dp[i][1] = b;

            dp[i][2] = c;

        }

        else {

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

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

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

        }

    }

    cout << max(dp[n-1][0], max(dp[n-1][1], dp[n-1][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>

using namespace std;

const int Max= 1e5+10;

long long dp[Max][4];

long long a[Max][4];

int main()

{

	int n;

	scanf("%d",&n);

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

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

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

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

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

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

	{

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

		{

			if(j==1)

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

			else if(j==2)

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

			else

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

		}

	}

	printf("%lld\n",max(dp[n][1],max(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 <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;

}

// 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 <vector>

#include <math.h>

using namespace std;

int main()

{

    int n;

    cin >> n;

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

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

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

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

    int i;

    dp[0][0] = v[0][0];

    dp[0][1] = v[0][1];

    dp[0][2] = v[0][2];

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

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

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

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

    }

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

using namespace std;

typedef long long ll;

const int inf=0x3f3f3f3f;

const int N=2e5+5;

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

int main(){

	cin>>n;

	for(int 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(int 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(dp[n][3],max(dp[n][1],dp[n][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 <iostream>

#include <algorithm>

#include <cmath>

#include <cstdlib>

#include <cstdio>

#include <cstring>

#include <iomanip>

#include <string>

#include <set>

#include <vector>

#include <queue>

#include <stack>

using namespace std;

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

#define fore(i, a, n) for (long long i = a; i < n; i++)

#define ford(i, a, n) for (long long i = n - 1; i >= a; i--)

#define si(a) scanf("%d", &a)

#define sii(a, b) scanf("%d%d", &a, &b)

#define debug(a) cout << a << endl

#define endl '\n'

#define pi acos(-1.0)

#define tr t[root]

#define lson t[root << 1]

#define rson t[root << 1 | 1]

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

const double eps = 1e-8;

inline int sgn(const double &x) { return x < -eps ? -1 : x > eps; }

typedef long long ll;

const int inf = 0x3f3f3f3f;

const int maxn=100005;



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

int a[maxn][4];

int dp[maxn][4];

int n,k;

void init() {

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

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

            a[i][j]=0;

            dp[i][j]=0;

        }

    }

}

int main()

{

    IO;

    cin>>n;

    init();

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

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

            cin>>a[i][j];

        }

    }

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

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

            for(int k=1;k<4;k++) {

                if(j!=k)

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

            }

        }

    }

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

using namespace std;

typedef long long ll;

const int inf=0x3f3f3f3f;

const int maxn=2e5+5;

int a[maxn][5];

int dp[maxn][5];

int main(){

	int n;

	cin>>n;

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

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

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

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

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

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

		{

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

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

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

		}

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

using namespace std;

const int N=100005;

int a[N][4],dp[N][4];

int main()

{

	int n;

	scanf("%d",&n);

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

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

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

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

	{

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

		{

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

			{

				if(j==k)

					continue;

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

			}

		}

	}

	printf("%d\n",max(dp[n][1],max(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 <bits/stdc++.h>



using namespace std;

typedef long long ll;

typedef long long int lli;

typedef long double ld;

const int oo = 1e9;





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;

}

// Optimized solution to the same problem (faster):
#include <cstdio>

#include <algorithm>

using std::max;



#define MAXN 100003

int d[MAXN][3],a[MAXN][3];

int main(void)

{

    int i,N;

    scanf("%d",&N);

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

    {

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

    }

    d[0][0]=a[0][0];

    d[0][1]=a[0][1];

    d[0][2]=a[0][2];

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

    {

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

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

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

    }

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



    return 0;

}

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

using namespace std;

typedef long long ll;

typedef long long int lli;

typedef long double ld;

const int oo = 1e9; 



int m;

const int mx = 1e5+5;

int ac[mx][3];

int dp[3][mx];

int go(int a, int i){

	if(i == m) 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>>m;

	for(int i=0; i<m; 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;

}

// Optimized solution to the same problem (faster):
#include <cstdio>

#include <algorithm>

using std::max;



#define MAXN 100003

int d[MAXN][3],a[MAXN][3];

int main(void)

{

    int i,N;

    scanf("%d",&N);

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

    {

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

    }

    d[0][0]=a[0][0];

    d[0][1]=a[0][1];

    d[0][2]=a[0][2];

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

    {

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

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

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

    }

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



    return 0;

}

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

#define MAXN 100010

using namespace std;



int n, ans, happy[MAXN][3], dp[MAXN][3];



void init(){

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

}



int main(){



    ios_base::sync_with_stdio(0);

    cin.tie(0);

    cout.tie(0);



    cin>>n;



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

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

            cin>>happy[i][j];

        }

    }



    init();



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

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

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

                if(k==j) continue;

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

            }

        }

    }



    for(int i=1; i<=3; i++) ans=max(ans, dp[n][i]);



    cout<<ans;



return 0;

}


// Optimized solution to the same problem (faster):
#include <cstdio>

#include <algorithm>

using std::max;



#define MAXN 100003

int d[MAXN][3],a[MAXN][3];

int main(void)

{

    int i,N;

    scanf("%d",&N);

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

    {

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

    }

    d[0][0]=a[0][0];

    d[0][1]=a[0][1];

    d[0][2]=a[0][2];

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

    {

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

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

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

    }

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



    return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

    if(option == 0){

        res = mat[day][option] + max(solve(day+1, 1), solve(day+1, 2));

    }

    else if(option == 1){

        res = mat[day][option] + max(solve(day+1, 0), solve(day+1, 2));

    }

    else {

        res = mat[day][option] + max(solve(day+1, 0), solve(day+1, 1));

    }

    return ans[day][option] = res;

}



int main() {

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

        init(n);

        for(int i=0;i<n;i++) scanf("%d %d %d",&mat[i][0], &mat[i][1], &mat[i][2]);

        printf("%d\n", max(solve(0,0), max(solve(0,1), solve(0,2)) ));

    }    

    return 0;

}

// Optimized solution to the same problem (faster):
#include <cstdio>

#include <algorithm>

using std::max;



#define MAXN 100003

int d[MAXN][3],a[MAXN][3];

int main(void)

{

    int i,N;

    scanf("%d",&N);

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

    {

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

    }

    d[0][0]=a[0][0];

    d[0][1]=a[0][1];

    d[0][2]=a[0][2];

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

    {

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

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

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

    }

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



    return 0;

}

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

using namespace std;

 

#define ll long long int



int main() {

    ll N;

    cin >> N;

    ll dp[N][3] = {};

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

        ll a, b, c;

        cin >> a >> b >> c;

        if(i == 0) {

            dp[i][0] = a;

            dp[i][1] = b;

            dp[i][2] = c;

        }

        else {

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

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

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

        }

    }

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

}

// Optimized solution to the same problem (faster):
#include <cstdio>

#include <algorithm>

using std::max;



#define MAXN 100003

int d[MAXN][3],a[MAXN][3];

int main(void)

{

    int i,N;

    scanf("%d",&N);

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

    {

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

    }

    d[0][0]=a[0][0];

    d[0][1]=a[0][1];

    d[0][2]=a[0][2];

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

    {

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

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

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

    }

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



    return 0;

}

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

using namespace std;

typedef long long ll;

typedef long long int lli;

typedef long double ld;

const int oo = 1e9;



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";

}

	

// Optimized solution to the same problem (faster):
#include <cstdio>

#include <algorithm>

using std::max;



#define MAXN 100003

int d[MAXN][3],a[MAXN][3];

int main(void)

{

    int i,N;

    scanf("%d",&N);

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

    {

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

    }

    d[0][0]=a[0][0];

    d[0][1]=a[0][1];

    d[0][2]=a[0][2];

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

    {

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

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

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

    }

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



    return 0;

}

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

using namespace std;

const int mx=100010;

int arr[mx][3],n;

int dp[mx][3];

int f(int pos,int ty){

	if(pos==n)return 0;

	int &ans=dp[pos][ty];

	if(ans^-1)return ans;

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

		if(ty^i || !pos){

			ans=max(ans,f(pos+1,i)+arr[pos][i]);

		}

	}

	return ans;

}



int main(int argc, char const *argv[])

{

	memset(dp,-1,sizeof dp);

	cin>>n;

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

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

			cin>>arr[i][j];

		}

	}

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



	return 0;

}

// Optimized solution to the same problem (faster):
#include<iostream>

#include<stdio.h>

#include<algorithm>

#include<vector>

#include<cstring>

#include<set>

#include<queue>

#include<map>

#include<math.h>

#include<climits>



#define FASTREAD      ios_base::sync_with_stdio(0);cin.tie(nullptr);

#define ll long long

#define dd double

#define scl(n)        scanf("%lld",&n)

#define scd(n)        scanf("%lf",&n)

#define scc(n)        scanf("%c",&n)

#define scs(n)        scanf("%s",&n)

#define ptl(n)        printf("%lld",n)

#define ptd(n)        printf("%f",n)

#define ptc(n)        printf("%c",n)

#define pts(n)        printf("%s",n)

#define nline         printf("\n")

#define pb            push_back

#define FOR(i,a,b)    for(ll i=a;i<=b;i++)

#define FORr(i,a,b)   for(ll i=a;i>=b;i--)

#define pi            2*acos(0.0)

#define pii           pair<ll,ll>

#define uu            first

#define vv            second



using namespace std;

ll n,a[100005],b[100005],c[100005],dp[100010][3],op0,op1,op2;

int main()

{

    scl(n);

    FOR(i,1,n)

    {

        scl(a[i]);

        scl(b[i]);

        scl(c[i]);

    }

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

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

    dp[n][2]=c[n];

    FORr(i,n-1,1)

    {

        if(i+1<=n)

        {

            op0=dp[i+1][0];

            op1=dp[i+1][1];

            op2=dp[i+1][2];

        }

        else

        {

            op0=LONG_MIN;

            op1=LONG_MIN;

            op2=LONG_MIN;

        }

        dp[i][0]=max(op1+a[i],op2+a[i]);

        dp[i][1]=max(op0+b[i],op2+b[i]);

        dp[i][2]=max(op0+c[i],op1+c[i]);

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

    }

    ll ans=max(dp[1][0],dp[1][1]);

    ans=max(ans,dp[1][2]);

    ptl(ans);

    return 0;

}


// Fastest known solution for this problem: