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

using namespace std;

int main(){

    int n;

    cin>>n;

    long long dp[100005][3];

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

    dp[0][0]=0;

    dp[0][1]=0;

    dp[0][2]=0;

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

        long long a,b,c;

        cin>>a>>b>>c;

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

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

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

    }

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

    cout<<ans<<'\n';

}


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

#include <cstring>

#include <cstdio>

#include <vector>

#include <map>

#include <queue>

#include <algorithm>

#include <cmath>

#include <cstdio>

#include <utility>

using namespace std;

const int N=1e5+10;

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



int main()

{

	int n;

	scanf("%d",&n);

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

	{

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

	}

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

	{

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

		{

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

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

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

		}

	}

	printf("%d",max(max(dp[n][0],dp[n][1]),dp[n][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 double eps=1e-6,pi=acos(-1);

const int N=1e5+5,M=1e9+7,SEGM=4*N,OO=0x3f3f3f3f;

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

int mem[N][5];



int solve(int i=0,int bk=0){

    if(i==n) return 0;

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

    if(~ret) return ret;

    if(bk!=1)

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

    if(bk!=2)

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

    if(bk!=3)

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

    return ret;

}



int main(){

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

    scanf("%d",&n);

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

        scanf("%d%d%d",a+i,b+i,c+i);

    memset(mem,-1,sizeof mem);

    printf("%d",solve());

    return 0;

}


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

using namespace std;

const int maxn = 1e5+5;

int A[maxn], B[maxn], C[maxn];

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

{

	int n; scanf("%d", &n);

	

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

		scanf("%d %d %d", &A[i], &B[i], &C[i]);

	}



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

		int tmpA = A[i-1], tmpB = B[i-1], tmpC = C[i-1];



		A[i] += max(tmpB, tmpC);

		B[i] += max(tmpA, tmpC);

		C[i] += max(tmpA, tmpB);

	}

	printf("%d\n", max(max(A[n-1], B[n-1]), C[n-1]));

	

	return 0;

}

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

#define lld         long long int

#define llf         long double

#define pb          push_back

#define mp          make_pair

#define IN          insert

#define loopN(n)    for(lld i=0;i<n;i++)

#define loop(p,n)   for(lld i=p;i<=n;i++)

#define rloop(p,n)  for(lld i=n;i>=p;i--)

#define loop2(p,n)  for(lld j=p;j<=n;j++)

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

#define F1          first

#define F2          second

#define INF         2e18

#define FILE        freopen("input.txt","r",stdin);freopen("output.txt","w",stdout)

#define PI          2*acos(0.0)

#define ISO         ios::sync_with_stdio(false);cin.tie(0)

#define mem(a,b)    memset(a,b,sizeof(a))

#define case(z)     cout<<"Case "<<z<<": ";

using namespace std;

const lld mod=1e9+7;



lld on(lld m,lld p){return m | (1<<p);}

bool ison(lld m,lld p){return (bool)(m & (1<<p));}

lld flip(lld m,lld p){return m ^ (1<<p);}

lld allon(lld p){return (1<<p)-1;}

//______________________________________________________________________________________



lld n;

lld dp[100005][3];

lld a[100005][3];



lld fun(lld p, lld k)

{

    if(p>=n)return 0;

    if(dp[p][k]!=-1)

    {

        return dp[p][k];

    }

    lld ans=-INF;

    if(p==0)

    {

        loopN(3)

        {

            ans=max(ans,a[p][i]+fun(p+1,i));

        }

        return ans;

    }

    else

    {

        loopN(3)

        {

            if(i!=k)

            {

                ans=max(ans,a[p][i]+fun(p+1,i));

            }



        }



    }

    return dp[p][k]=ans;







}





int main()

{

    ISO;



    //lld t;cin>>t;while(t--)

    mem(dp,-1);

    cin>>n;

    loopN(n)loop2(0,2)cin>>a[i][j];

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





    return 0;

}






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

using namespace std;

const int maxn = 1e5+5;

int A[maxn], B[maxn], C[maxn];

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

{

	int n; scanf("%d", &n);

	

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

		scanf("%d %d %d", &A[i], &B[i], &C[i]);

	}



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

		int tmpA = A[i-1], tmpB = B[i-1], tmpC = C[i-1];



		A[i] += max(tmpB, tmpC);

		B[i] += max(tmpA, tmpC);

		C[i] += max(tmpA, tmpB);

	}

	printf("%d\n", max(max(A[n-1], B[n-1]), C[n-1]));

	

	return 0;

}

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

#define fr(i,a,n) for(ll i=a;i<n;i++)

#define YES cout<<"YES"<<endl

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

#define NO cout<<"NO"<<endl

#define pb(x) emplace_back(x)

#define fastio std::ios_base:: sync_with_stdio(false); cin.tie(0); cout.tie(0);

#define vi vector<ll>

#define mi map<ll,ll>

#define pi pair<ll,ll>

#define what(x) cout<<#x<<" ="<<x<<endl;

using namespace std;

using ll =long long;

ll dp[100005][4];

vi a(100005),b(100005),c(100005);

ll n;

ll solve(ll taken,ll i,ll j)

{

    if(i>=n)return 0;

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



    if(taken!=1) dp[i][1]=a[i]+max(solve(1,i+1,2),solve(1,i+1,3));

    if(taken!=2)dp[i][2]=b[i]+max(solve(2,i+1,1),solve(2,i+1,3));

    if(taken!=3)dp[i][3]=c[i]+max(solve(3,i+1,1),solve(3,i+1,2));



    if(taken==1)return max({dp[i][2],dp[i][3]});

    if(taken==2)return max({dp[i][1],dp[i][3]});

    if(taken==3)return max({dp[i][2],dp[i][1]});

    else return max({dp[i][2],dp[i][1],dp[i][3]});

}







int main()

{

    memset(dp,-1,sizeof dp);

    cin>>n;

    fr(i,0,n)cin>>a[i]>>b[i]>>c[i];



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





   /* fr(i,1,4)

    {

        fr(j,0,n)cout<<dp[j][i]<<" ";

        cout<<endl;

    }

*/



}




















































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

using namespace std;

#define endl '\n'

#define optimize() ios_base::sync_with_stdio(0);

int n;

int arr[100000][3];

int point[100000][3];



int p(int row,int col)

{



    if(row == n-1)

    {

        return arr[n-1][col];

    }

    if(point[row][col]>0)

    {

        return point[row][col];

    }



    else if(col==0)

    {

        return point[row][col]=arr[row][col]+max(p(row+1,1),p(row+1,2));

    }



    else if(col==1)

    {

        return point[row][col]=arr[row][col]+max(p(row+1,0),p(row+1,2));

    }

    else if(col==2)

    {

        return point[row][col]=arr[row][col]+max(p(row+1,0),p(row+1,1));

    }



}









int main()

{

    optimize();



    cin>>n;







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

    {

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

        {

            cin>>arr[i][j];

        }

    }



    int res=max(p(0,0),max(p(0,1),p(0,2)));

    cout<<res;



}




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

#include<bits/stdc++.h>

using namespace std;

#define ll long long int

#define p 1000000007



struct activity{

    int a;

    int b;

    int c;

};

int dp[100001][3];



int points(int i,int j,vector<activity> &v)

{

    if(i==1)

    {

        if(j==0)

        {

            return v[1].a;

        }

        else if(j==1)

        {

            return v[1].b;

        }

        else if(j==2)

        {

            return v[1].c;

        }

    }

    else

    {

        if(dp[i][j]!=-1)

        {

            return dp[i][j];

        }

        

        if(j==0)

        {

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

        }

        else if(j==1)

        {

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

        }

        else if(j==2)

        {

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

        }

    }

}



int solve(int n,vector<activity> &v)

{

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

     return max(max(points(n,0,v),points(n,1,v)),points(n,2,v));

}



int main()

{

    ios_base::sync_with_stdio(0);

    cin.tie(0);

    cout.tie(0);

    

    int n;

    cin>>n;

    

    vector<activity> v(n+1);

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

    {

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

    }

    cout<<solve(n,v);

    return 0;

}

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

#include<bits/stdc++.h>

using namespace std;

#define ll long long int

#define p 1000000007



struct activity{

    int a;

    int b;

    int c;

};



int solve(int n,vector<activity> &v)

{

    int dp[n+1][3];

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

    dp[1][1]=v[1].b;

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

    

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

    {

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

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

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

    }

    

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

}



int main()

{

    ios_base::sync_with_stdio(0);

    cin.tie(0);

    cout.tie(0);

    

    int n;

    cin>>n;

    

    vector<activity> v(n+1);

    

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

    {

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

    }

    cout<<solve(n,v);

    return 0;

}

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

#include <algorithm>

#include <cstring>



using namespace std;



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

int dp_a[100100],dp_b[100100],dp_c[100100];



int vac(int i,int n,char estado){

    if(i>n) return 0;



    int resp;



    if(i==0){

        resp=max((a[i]+vac(i+1,n,'a')),(b[i]+vac(i+1,n,'b')));

        resp=max(resp,c[i]+vac(i+1,n,'c'));

    }else{

        switch(estado){

            case 'a':

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

                return dp_a[i] =max(b[i]+vac(i+1,n,'b'),c[i]+vac(i+1,n,'c')); break;

            case 'b':

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

                return dp_b[i] =max(a[i]+vac(i+1,n,'a'),c[i]+vac(i+1,n,'c')); break;

            case 'c':

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

                return dp_c[i] =max(a[i]+vac(i+1,n,'a'),b[i]+vac(i+1,n,'b')); break;

        }

    }

    return resp;

}



int main(){



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

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

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



    int n;

    cin>>n;



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

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

    }



    cout<<vac(0,n-1,'d');

}


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

#include <algorithm>

#include <cstring>



using namespace std;



int n;

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

int dp_a[100100],dp_b[100100],dp_c[100100];



int main(){

    cin>>n;



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

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

    }



    dp_a[0]=0;

    dp_b[0]=0;

    dp_c[0]=0;



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

        dp_a[i]=max(b[i-1]+dp_b[i-1],c[i-1]+dp_c[i-1]);

        dp_b[i]=max(a[i-1]+dp_a[i-1],c[i-1]+dp_c[i-1]);

        dp_c[i]=max(a[i-1]+dp_a[i-1],b[i-1]+dp_b[i-1]); 

    }

    /*

    cout<<dp_a[n]<<" ";

    cout<<dp_b[n]<<" ";

    cout<<dp_c[n];



    cout<<endl;

    */

    int dpa =dp_a[n-1]+a[n-1];

    int dpb =dp_b[n-1]+b[n-1];

    int dpc =dp_c[n-1]+c[n-1];

    /*

    cout<<dpa<<" ";

    cout<<dpb<<" ";

    cout<<dpc;

    */

    int maiorPath=max(dpa,dpb);

    maiorPath=max(maiorPath,dpc);

    cout<<maiorPath;



}


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

#include <algorithm>

#include <cstring>



using namespace std;



int n;

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

int dp_a[100100],dp_b[100100],dp_c[100100];





int vac(int i,char estado){



    if(i>n) return 0;



    int resp;



    if(i==0){

        resp=max(a[i]+vac(i+1,'a'),b[i]+vac(i+1,'b'));

        return resp=max(resp, c[i]+vac(i+1,'c'));

    }

    else{

        switch(estado){

            case 'a':

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

                return dp_a[i] =max(b[i]+vac(i+1,'b'),c[i]+vac(i+1,'c')); break;

            case 'b':

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

                return dp_b[i] =max(a[i]+vac(i+1,'a'),c[i]+vac(i+1,'c')); break;

            case 'c':

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

                return dp_c[i] =max(a[i]+vac(i+1,'a'),b[i]+vac(i+1,'b')); break;

        }

    }

}



int main(){



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

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

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



    cin>>n;



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

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

    }



    cout<<vac(0,'d');

}


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

#include <algorithm>

#include <cstring>



using namespace std;



int n;

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

int dp_a[100100],dp_b[100100],dp_c[100100];



int main(){

    cin>>n;



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

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

    }



    dp_a[0]=0;

    dp_b[0]=0;

    dp_c[0]=0;



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

        dp_a[i]=max(b[i-1]+dp_b[i-1],c[i-1]+dp_c[i-1]);

        dp_b[i]=max(a[i-1]+dp_a[i-1],c[i-1]+dp_c[i-1]);

        dp_c[i]=max(a[i-1]+dp_a[i-1],b[i-1]+dp_b[i-1]); 

    }

    /*

    cout<<dp_a[n]<<" ";

    cout<<dp_b[n]<<" ";

    cout<<dp_c[n];



    cout<<endl;

    */

    int dpa =dp_a[n-1]+a[n-1];

    int dpb =dp_b[n-1]+b[n-1];

    int dpc =dp_c[n-1]+c[n-1];

    /*

    cout<<dpa<<" ";

    cout<<dpb<<" ";

    cout<<dpc;

    */

    int maiorPath=max(dpa,dpb);

    maiorPath=max(maiorPath,dpc);

    cout<<maiorPath;



}


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

#include <algorithm>

#include <cstring>



using namespace std;



int n;

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

int dp_a[100100],dp_b[100100],dp_c[100100];





int vac(int i,char estado){



    if(i>n) return 0;



    int resp;



    if(i==0){

        resp=max(a[i]+vac(i+1,'a'),b[i]+vac(i+1,'b'));

        return resp=max(resp, c[i]+vac(i+1,'c'));

    }

    else{

        switch(estado){

            case 'a':

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

                return dp_a[i] =max(b[i]+vac(i+1,'b'),c[i]+vac(i+1,'c')); break;

            case 'b':

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

                return dp_b[i] =max(a[i]+vac(i+1,'a'),c[i]+vac(i+1,'c')); break;

            case 'c':

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

                return dp_c[i] =max(a[i]+vac(i+1,'a'),b[i]+vac(i+1,'b')); break;

        }

    }

}



int main(){



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

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

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



    cin>>n;



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

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

    }



    cout<<vac(0,'d');

}

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

#include <algorithm>

#include <cstring>



using namespace std;



int n;

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

int dp_a[100100],dp_b[100100],dp_c[100100];



int main(){

    cin>>n;



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

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

    }



    dp_a[0]=0;

    dp_b[0]=0;

    dp_c[0]=0;



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

        dp_a[i]=max(b[i-1]+dp_b[i-1],c[i-1]+dp_c[i-1]);

        dp_b[i]=max(a[i-1]+dp_a[i-1],c[i-1]+dp_c[i-1]);

        dp_c[i]=max(a[i-1]+dp_a[i-1],b[i-1]+dp_b[i-1]); 

    }

    /*

    cout<<dp_a[n]<<" ";

    cout<<dp_b[n]<<" ";

    cout<<dp_c[n];



    cout<<endl;

    */

    int dpa =dp_a[n-1]+a[n-1];

    int dpb =dp_b[n-1]+b[n-1];

    int dpc =dp_c[n-1]+c[n-1];

    /*

    cout<<dpa<<" ";

    cout<<dpb<<" ";

    cout<<dpc;

    */

    int maiorPath=max(dpa,dpb);

    maiorPath=max(maiorPath,dpc);

    cout<<maiorPath;



}


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

using namespace std;



#define INF 0x3f3f3f3f

#define MAXN 100010

#define endl "\n"

#define abs(a, b) ((a > b) ? a-b : b-a)



typedef long long int ll;



/* End */



int n;

ll a[MAXN];

ll b[MAXN];

ll c[MAXN];

ll pd[MAXN][3];



ll solve(int i, int m) { //0 - a, 1 - b, 2 - c

	if (i == n+1) return 0;

	if (pd[i][m] != -1) return pd[i][m];

	if (m == 0)

		return pd[i][m] = max(b[i] + solve(i+1, 1), c[i] + solve(i+1, 2));

	else if (m == 1)

		return pd[i][m] = max(a[i] + solve(i+1, 0), c[i] + solve(i+1, 2));

	else

		return pd[i][m] = max(a[i] + solve(i+1, 0), b[i] + solve(i+1, 1));

}



int main() {

	cin.tie(NULL);

	ios_base::sync_with_stdio(false);

	

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



	cin >> n;

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



	cout << max(solve(1, 0), max(solve(1, 1), solve(1, 2))) << endl;



	return 0;

}


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

using namespace std;



#define INF 0x3f3f3f3f

#define MAXN 100010

#define endl "\n"

#define abs(a, b) ((a > b) ? a-b : b-a)



typedef long long int ll;



/* End */



int n;

ll a[MAXN];

ll b[MAXN];

ll c[MAXN];

ll pd[MAXN][3];



int main() {

	cin.tie(NULL);

	ios_base::sync_with_stdio(false);

	

	cin >> n;

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

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



	pd[n][0] = max(b[n], c[n]);

	pd[n][1] = max(a[n], c[n]);

	pd[n][2] = max(a[n], b[n]);



	for (int i = n-1; i >= 0; i--) {

	        pd[i][0] = max(b[i] + pd[i+1][1], c[i] + pd[i+1][2]);

		pd[i][1] = max(a[i] + pd[i+1][0], c[i] + pd[i+1][2]);

		pd[i][2] = max(a[i] + pd[i+1][0], b[i] + pd[i+1][1]);

	}



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



	return 0;

}


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

#define ll long long

using namespace std;

ll dp[100005][4];

ll n,a[100005][4];



ll helper(ll day, ll chose)

{

    if(day == n) return 0;



    if(dp[day][chose] != -1) return dp[day][chose];



    ll maxi = 0;

    if(chose == 0)

    {

        maxi = max(maxi, a[day][1]+ helper(day+1, 1));

        maxi = max(maxi, a[day][2]+ helper(day+1, 2));

        maxi = max(maxi, a[day][3]+ helper(day+1, 3));

    }

    else if(chose == 1)

    {

        maxi = max(maxi, a[day][2] + helper(day+1, 2));  

        maxi = max(maxi, a[day][3] + helper(day+1, 3));

    }

    else if(chose == 1)

    {

        maxi = max(maxi, a[day][2]+ helper(day+1, 2));  

        maxi = max(maxi, a[day][3]+ helper(day+1, 3));

    }

    else if(chose == 2)

    {

        maxi = max(maxi, a[day][1]+ helper(day+1, 1));  

        maxi = max(maxi, a[day][3]+ helper(day+1, 3));

    }

    else 

    {

        maxi = max(maxi, a[day][2]+ helper(day+1, 2));  

        maxi = max(maxi, a[day][1]+ helper(day+1, 1));

    }

    return dp[day][chose] = maxi;

}

int main() {



    cin >> n;

    memset(dp, -1, sizeof dp);



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

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

            cin >> a[i][j];



    cout<<helper(0,0);

}

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

#define ll long long

using namespace std;

ll dp[100001][3];

ll n,a[100001][3];



ll helper(ll day, ll chose)

{

    if(day == n) return 0;



    if(dp[day][chose] != -1) return dp[day][chose];



    ll maxi = 0;

    if(chose == 0)

    {

        maxi = max(maxi, a[day][1]+ helper(day+1, 1));

        maxi = max(maxi, a[day][2]+ helper(day+1, 2));

        maxi = max(maxi, a[day][3]+ helper(day+1, 3));

    }

    else if(chose == 1)

    {

        maxi = max(maxi, a[day][2] + helper(day+1, 2));  

        maxi = max(maxi, a[day][3] + helper(day+1, 3));

    }

    else if(chose == 1)

    {

        maxi = max(maxi, a[day][2]+ helper(day+1, 2));  

        maxi = max(maxi, a[day][3]+ helper(day+1, 3));

    }

    else if(chose == 2)

    {

        maxi = max(maxi, a[day][1]+ helper(day+1, 1));  

        maxi = max(maxi, a[day][3]+ helper(day+1, 3));

    }

    else 

    {

        maxi = max(maxi, a[day][2]+ helper(day+1, 2));  

        maxi = max(maxi, a[day][1]+ helper(day+1, 1));

    }

    return dp[day][chose] = maxi;

}

int main() {



    cin >> n;

    memset(dp, -1, sizeof dp);



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

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

            cin >> a[i][j];



    cout<<helper(0,0);

}

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

using namespace std;



int main () {

  int n;

  cin>>n;

  

  int g[100001][3];

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

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

      cin>>g[i][j];

    }

  }

  

  int dp[100001][3]={0};

  dp[0][0]=0;

  dp[0][1]=0;

  dp[0][2]=0;

  

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

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

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

        if (j==k) {

          continue;

        }

        else {

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

        }

      }

    }

  }

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

  

}

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

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

#define chmax(x,y) x = max(x,y)

#define chmin(x,y) x = min(x,y)

using namespace std;

typedef long long ll;

typedef pair<int,int> P;

typedef tuple<ll,ll,ll> T;

const long long INF = 1LL<<60;

const int MOD = 1000000000+7;

#define rev(s) (string((s).rbegin(), (s).rend()))

template < typename T > inline string toString( const T &a ) { ostringstream oss; oss << a; return oss.str(); };

// cout << fixed << setprecision(10) << ans << endl;

// *min_element(c + l, c + r) *max_element(c + l, c + r)

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

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

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

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

// struct Edge {

//   int to, id;

//   Edge(int to, int id): to(to), id(id) {}

// };



int main() {

  int N;cin>>N;

  vector<vector<int>> happy(N,vector<int>(3,0));

  rep(i,N) cin >> happy[i][0] >> happy[i][1] >> happy[i][2];

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

  rep(i,N) {

    rep(j,3) {

      chmax(dp[i + 1][(j + 1) % 3], dp[i][j] + happy[i][(j + 1) % 3]);

      chmax(dp[i + 1][(j + 2) % 3], dp[i][j] + happy[i][(j + 2) % 3]);

    }

  }

  int ans = 0;

  rep(i,3) chmax(ans, dp[N][i]);



  cout << ans << endl;

}


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

using namespace std;

#define INF 1000000000

int dp(int i,int last);

int n;

int a[9][100009],memo[100009][9];



int main(){

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

  cin>>n;

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

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

      cin>>a[j][i];

    }

  }

  cout<<dp(0,5)<<endl;

  return(0);

}



int dp(int i,int last){

  int rec=0;

  if(memo[i][last]!=-1)return memo[i][last];

  if(i==n)return 0;

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

    if(last!=j){

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

    }

  }

  return memo[i][last]=rec;

}


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

using namespace std;

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

#define rep2(i, s, n) for (int i = (s); i < (int)(n); ++i)

#define fi first

#define se second

#define INF 1000000009

#define LLINF 1000000000000000009LL

#define chmax(a, b) a = max(a, b)

using ll = long long;



int dp[100009][3];



int main(){

  int n;

  cin>>n;

  vector<int>a(n);

  vector<int>b(n);

  vector<int>c(n);

  rep(i,n)cin>>a[i]>>b[i]>>c[i];

  rep(i,n){

    rep(j,3){

      if(j==0){

        chmax(dp[i+1][1],dp[i][0]+b[i]);

        chmax(dp[i+1][2],dp[i][0]+c[i]);

      }

      if(j==1){

        chmax(dp[i+1][2],dp[i][1]+c[i]);

        chmax(dp[i+1][0],dp[i][1]+a[i]);

      }

      if(j==2){

        chmax(dp[i+1][0],dp[i][2]+a[i]);

        chmax(dp[i+1][1],dp[i][2]+b[i]);

      }

    }

  }

  int ans=0;

  rep(i,3){

    chmax(ans,dp[n][i]);

  }

  cout<<ans<<endl;

  return(0);

}

// Fastest known solution for this problem: