stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include <iostream>

#include <queue>

#include <set>

#include <list>

#include <deque>

#include <vector>

#include <map>

#include <utility>

#include <algorithm>

#include <cmath>

#include <cstdio>

#include <string>

#include <iterator>

#define ll long long

#define pb push_back

#define pf push_front

#define ppb pop_back

#define ppf pop_front

#define powr(x,n,p) for(int axy=0 ; axy<p ; axy++){x=x*n;}

#define YES printf("YES\n")

#define Yes printf("Yes\n")

#define yes printf("yes\n")

#define NO printf("NO\n")

#define No printf("No\n")

#define no printf("no\n")

#define nl printf("\n")

#define fr0(n,q) for(q=0 ; q<n ; q++)

#define fr1(n,q) for(q=1 ; q<=n ; q++)

#define mp make_pair

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

#define sci(x) scanf("%d",&x)

#define lst(x) x[x.size()-1]

#define llst(x) x[x.size()-2]

#define md 1000000007

#define check(x,y) (x&(1<<y))

#define set(x,y) x=x|(1<<y)

using namespace std;

int n;

int arr[100005][3],dp[100005][3];

int solve(int i, int j){

    if(i>n) return 0;

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

    int ret;

    if(j==0) ret=max(arr[i][j]+solve(i+1,1),arr[i][j]+solve(i+1,2));

    else if(j==1) ret=max(arr[i][j]+solve(i+1,0),arr[i][j]+solve(i+1,2));

    else ret=max(arr[i][j]+solve(i+1,0),arr[i][j]+solve(i+1,1));

    return dp[i][j]=ret;

}

int main()

{

        sci(n);

        int i,j;

        fr0(100005,i){

            fr0(3,j) dp[i][j]=-1;

        }

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

            for(j=0 ; j<3 ; j++) sci(arr[i][j]);

        }

        int ans=max(solve(1,0),solve(1,1));

        ans=max(ans,solve(1,2));

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

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 2.189137395071049x):
 // /*

#include <bits/stdc++.h>

using namespace std;

int dp [100003][4];

int n;

int arr [100003];

int brr [100003];

int crr [100003];

int main ()

{



cin >>n;

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



{

cin >> arr [i]>>brr [i]>>crr [i];

}

dp [0][0] = arr [0];

dp [0][1] = brr [0];

dp [0][2] = crr [0];

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

{

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

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

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

}

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

}







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

using namespace std;

int a[100001][3];

int dp[100001][3];

int n;



int func(int ind, int flag) {



	if (ind == n) return 0;



	if (dp[ind][flag] != -1) return dp[ind][flag];



	int maxi = 0;

	if (flag == 0) {

		maxi = max(maxi, a[ind][1] + func(ind + 1, 1));

		maxi = max(maxi, a[ind][2] + func(ind + 1, 2));

		maxi = max(maxi, a[ind][3] + func(ind + 1, 3));

	}

	else if (flag == 1) {

		maxi = max(maxi, a[ind][2] + func(ind + 1, 2));

		maxi = max(maxi, a[ind][3] + func(ind + 1, 3));

	}

	else if (flag == 2) {

		maxi = max(maxi, a[ind][1] + func(ind + 1, 1));

		maxi = max(maxi, a[ind][3] + func(ind + 1, 3));

	}

	else {

		maxi = max(maxi, a[ind][1] + func(ind + 1, 1));

		maxi = max(maxi, a[ind][2] + func(ind + 1, 2));

	}



	return dp[ind][flag] = maxi;

}



int main() {



	cin >> n;

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

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

			cin >> a[i][j];

		}

	}



	memset(dp, -1, sizeof dp);



	cout << func(0, 0);



	return 0;

}


// Faster C++ solution for the same problem (measured speedup: 2.19530788810446x):
 // /*

#include <bits/stdc++.h>

using namespace std;

int dp [100003][4];

int n;

int arr [100003];

int brr [100003];

int crr [100003];

int main ()

{



cin >>n;

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



{

cin >> arr [i]>>brr [i]>>crr [i];

}

dp [0][0] = arr [0];

dp [0][1] = brr [0];

dp [0][2] = crr [0];

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

{

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

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

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

}

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

}







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

using namespace std;



long long w1,w2,hap=0,maxi,aa,bb,cc,N,i,j,k,a[100005][5];

long long M[100005][5];





int main()

{

    ios_base::sync_with_stdio(false);

    cin.tie(NULL);

    cin>>N;

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

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

            M[i][j]=0;



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

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

    M[N-1][0]=a[N-1][0];

    M[N-1][1]=a[N-1][1];

    M[N-1][2]=a[N-1][2];



    for(i=N-2; i>=0; i--)

    {

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

        {

            if(j==0)

            {

                w1=1;

                w2=2;

            }

            else if(j==1)

            {

                w1=0;

                w2=2;

            }

            else

            {

                w1=0;

                w2=1;

            }

        M[i][j]=a[i][j]+max(M[i+1][w1],M[i+1][w2]);

        }

    }

    aa=max(M[0][0],M[0][1]);

    aa=max(aa,M[0][2]);

    cout<<aa<<endl;

    return 0;

}






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

typedef long long ll;

using namespace std;

ll dpa[100005],dpb[100005],dpc[100005];



int main()

{

  ll n;

  cin>>n;

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

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

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

  }

  dpa[n-1]=a[n-1];

  dpb[n-1]=b[n-1];

  dpc[n-1]=c[n-1];

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

   dpa[i]=a[i]+max(dpb[i+1],dpc[i+1]);

   dpb[i]=b[i]+max(dpa[i+1],dpc[i+1]);

   dpc[i]=c[i]+max(dpb[i+1],dpa[i+1]);

  }

  cout<<max(dpa[0],max(dpb[0],dpc[0]));

    return 0;

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

using namespace std;

#define INF 100005

#define maxx 1000000000000



int n;

long long a[INF][3];

long long mem[INF][3];



long long rec(int level,int pre)

{

    if(level==n) return 0;

    if(mem[level][pre]!=-1) return mem[level][pre];

    long long now=0;

    if(pre==0)

    {

        now=max(now,max(a[level][0]+rec(level+1,1),a[level][0]+rec(level+1,2)));

    }else

    if(pre==1)

    {

        now=max(now,max(a[level][1]+rec(level+1,0),a[level][1]+rec(level+1,2)));

    }else

    if(pre==2)

    {

        now=max(now,max(a[level][2]+rec(level+1,0),a[level][2]+rec(level+1,1)));

    }

    return mem[level][pre]=now;

}



int main()

{

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

    scanf("%d",&n);

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

    {

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

    }

    long long ans=max(rec(0,0),max(rec(0,1),rec(0,2)));

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

}


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

typedef long long ll;

using namespace std;

ll dpa[100005],dpb[100005],dpc[100005];



int main()

{

  ll n;

  cin>>n;

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

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

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

  }

  dpa[n-1]=a[n-1];

  dpb[n-1]=b[n-1];

  dpc[n-1]=c[n-1];

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

   dpa[i]=a[i]+max(dpb[i+1],dpc[i+1]);

   dpb[i]=b[i]+max(dpa[i+1],dpc[i+1]);

   dpc[i]=c[i]+max(dpb[i+1],dpa[i+1]);

  }

  cout<<max(dpa[0],max(dpb[0],dpc[0]));

    return 0;

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

#include<algorithm>

#include<cstring>

using namespace std;

const int maxn = 1e5 + 5;



int a[maxn][4];

int dp[maxn][4];//d[i][j] i day choose j thing 's max happy



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]);

		}

	}



	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] ,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][2] ,dp[i - 1][1]) + a[i][3];

	}



	int ans = 0;

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

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

}

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

typedef long long ll;

using namespace std;

ll dpa[100005],dpb[100005],dpc[100005];



int main()

{

  ll n;

  cin>>n;

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

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

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

  }

  dpa[n-1]=a[n-1];

  dpb[n-1]=b[n-1];

  dpc[n-1]=c[n-1];

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

   dpa[i]=a[i]+max(dpb[i+1],dpc[i+1]);

   dpb[i]=b[i]+max(dpa[i+1],dpc[i+1]);

   dpc[i]=c[i]+max(dpb[i+1],dpa[i+1]);

  }

  cout<<max(dpa[0],max(dpb[0],dpc[0]));

    return 0;

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



using namespace std;



    const int n = 1e5+5;

    int num;

    int act[n][3];

    int dyn[3][n];

    

    int init(int a, int i){

        

    	if(i == num) return 0;

    	

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

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

    	

    	if(a==0){

    	    

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

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

    	}

    	

    	if(a==1){

    	    

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

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

    	}

    	

    	if(a==2){

    	    

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

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

    	}

    	

    	

    	return res;

    }

    

    int main(){

        

    	cin>>num;

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

    	memset(dyn, -1, sizeof dyn);

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

    	return 0;

    	

    }

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

typedef long long ll;

using namespace std;

ll dpa[100005],dpb[100005],dpc[100005];



int main()

{

  ll n;

  cin>>n;

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

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

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

  }

  dpa[n-1]=a[n-1];

  dpb[n-1]=b[n-1];

  dpc[n-1]=c[n-1];

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

   dpa[i]=a[i]+max(dpb[i+1],dpc[i+1]);

   dpb[i]=b[i]+max(dpa[i+1],dpc[i+1]);

   dpc[i]=c[i]+max(dpb[i+1],dpa[i+1]);

  }

  cout<<max(dpa[0],max(dpb[0],dpc[0]));

    return 0;

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

#include <string.h>

#define MAXN 100003



int array_a[MAXN][3];

int array_d[MAXN][3];



int max(int a,int b){

	return a>b? a:b;

}



int main(){

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

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

	int N;

	scanf("%d",&N);



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

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

	}



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

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

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

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

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

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

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

	}

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

	return 0;



}

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

typedef long long ll;

using namespace std;

ll dpa[100005],dpb[100005],dpc[100005];



int main()

{

  ll n;

  cin>>n;

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

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

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

  }

  dpa[n-1]=a[n-1];

  dpb[n-1]=b[n-1];

  dpc[n-1]=c[n-1];

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

   dpa[i]=a[i]+max(dpb[i+1],dpc[i+1]);

   dpb[i]=b[i]+max(dpa[i+1],dpc[i+1]);

   dpc[i]=c[i]+max(dpb[i+1],dpa[i+1]);

  }

  cout<<max(dpa[0],max(dpb[0],dpc[0]));

    return 0;

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

 

#include <bits/stdc++.h>

using namespace std;

 

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

#define EPS (1e-10)

#define endl "\n"

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

typedef long long ll;

typedef long double ld;

//template <class Type>

 

 

void Fast(){

	std::ios_base::sync_with_stdio(0);

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

}

 

inline int D(){

	int t;

	scanf("%d",&t);

	return t;

}

inline ll llD(){

	ll t;

	scanf("%lld",&t);

	return t;

}

 

 

ll MOD=1e9 + 7; 

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

int arr[N][4];

ll dp[N][4];

ll OO=1e12; 

 

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

ll l,r,mid;

ll a,b,c,d;



ll Solve(int idx,int row){

	

		

	if(idx == n)

		return 0;

	

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

	if(~ret)

		return ret;

	

	ret = -OO;

	

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

		if(row != i)

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

	

	return ret;

}







 

 

int main(){

	

	memset(dp, -1 ,sizeof dp);

	cin>>n;

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

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



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

	



	





}

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

typedef long long ll;

using namespace std;

ll dpa[100005],dpb[100005],dpc[100005];



int main()

{

  ll n;

  cin>>n;

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

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

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

  }

  dpa[n-1]=a[n-1];

  dpb[n-1]=b[n-1];

  dpc[n-1]=c[n-1];

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

   dpa[i]=a[i]+max(dpb[i+1],dpc[i+1]);

   dpb[i]=b[i]+max(dpa[i+1],dpc[i+1]);

   dpc[i]=c[i]+max(dpb[i+1],dpa[i+1]);

  }

  cout<<max(dpa[0],max(dpb[0],dpc[0]));

    return 0;

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

#include <bits/stdc++.h>

#include <unordered_map>

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

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

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

#define endl '\n'

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

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

typedef long long ll;

using namespace std;



void file() {

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

#ifndef ONLINE_JUDGE

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

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

#endif

}





ll n;

ll mem[100004][4];

vector < vector < int > > v;



ll solve(int idx , int last) {

    if (idx == n)return 0;



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

    if (~ret)return ret;

    ret = 0;

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

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

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

    return ret;

}



int main() {

    //file();



    memset(mem, -1, sizeof mem);

    cin >> n;

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

    for (auto& e : v)

        for (auto& f : e)

            cin >> f;



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



}

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

typedef long long ll;

using namespace std;

ll dpa[100005],dpb[100005],dpc[100005];



int main()

{

  ll n;

  cin>>n;

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

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

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

  }

  dpa[n-1]=a[n-1];

  dpb[n-1]=b[n-1];

  dpc[n-1]=c[n-1];

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

   dpa[i]=a[i]+max(dpb[i+1],dpc[i+1]);

   dpb[i]=b[i]+max(dpa[i+1],dpc[i+1]);

   dpc[i]=c[i]+max(dpb[i+1],dpa[i+1]);

  }

  cout<<max(dpa[0],max(dpb[0],dpc[0]));

    return 0;

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

author :- Mahmoud Tarek.

*******/



#include<bits/stdc++.h>



using namespace std ;



#define rg register

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

typedef long long ll ;



int n;

struct doW{

int w[5] ;

};



vector<doW> a ;

int mem[100002][6] ;



int solve(int i, int ch){

if(i == n) return 0 ;

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

if(~ret) return ret ;

ret = 0 ;

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

    if(ch != j){

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

    }

}

return ret ;

}



int main() {

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

    cin >> n ;

    a.resize(n) ;

    memset(mem, -1, sizeof mem) ;

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

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

    }

    cout << solve(0, 1) ;



}



/*

  Empty set of troubles.

*/


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

typedef long long ll;

using namespace std;

ll dpa[100005],dpb[100005],dpc[100005];



int main()

{

  ll n;

  cin>>n;

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

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

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

  }

  dpa[n-1]=a[n-1];

  dpb[n-1]=b[n-1];

  dpc[n-1]=c[n-1];

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

   dpa[i]=a[i]+max(dpb[i+1],dpc[i+1]);

   dpb[i]=b[i]+max(dpa[i+1],dpc[i+1]);

   dpc[i]=c[i]+max(dpb[i+1],dpa[i+1]);

  }

  cout<<max(dpa[0],max(dpb[0],dpc[0]));

    return 0;

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

using namespace std;



const int N=1e5+30;

typedef long long ll;

int n;

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

ll dp[N][5];

ll solve(int i,int last){

    if(i==n)return 0;

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

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

    if(~ret)return ret;

    if(last!=1){

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

    }

    if(last!=2){

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

    }

    if(last!=3){

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

    }

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



}

int main()

{

    memset(dp,-1,sizeof dp);

    cin>>n;

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

    cout<<solve(0,4);



}




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

typedef long long ll;

using namespace std;

ll dpa[100005],dpb[100005],dpc[100005];



int main()

{

  ll n;

  cin>>n;

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

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

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

  }

  dpa[n-1]=a[n-1];

  dpb[n-1]=b[n-1];

  dpc[n-1]=c[n-1];

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

   dpa[i]=a[i]+max(dpb[i+1],dpc[i+1]);

   dpb[i]=b[i]+max(dpa[i+1],dpc[i+1]);

   dpc[i]=c[i]+max(dpb[i+1],dpa[i+1]);

  }

  cout<<max(dpa[0],max(dpb[0],dpc[0]));

    return 0;

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

#include<bits/stdc++.h>

#define ll long long

using namespace std;

int n;

const int N = 100005;

vector<int>v;

ll dp[100005][4];

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

ll solve(int i,int lst) {

	if (i == n)return 0;

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

	if (~ret)return ret;

	if (lst != 1)ret = max(ret, solve(i + 1, 1) + A[i]);

	if (lst != 2)ret = max(ret, solve(i + 1, 2) + B[i]);

	if (lst != 3)ret = max(ret, solve(i + 1, 3) + C[i]);

	return ret;

}

int main() {

	cin >> n;

	for (int i = 0; i < n; i++)cin >> A[i] >> B[i] >> C[i];

	memset(dp, -1, sizeof dp);

	cout << solve(0, 0);

	

		 

}

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

typedef long long ll;

using namespace std;

ll dpa[100005],dpb[100005],dpc[100005];



int main()

{

  ll n;

  cin>>n;

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

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

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

  }

  dpa[n-1]=a[n-1];

  dpb[n-1]=b[n-1];

  dpc[n-1]=c[n-1];

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

   dpa[i]=a[i]+max(dpb[i+1],dpc[i+1]);

   dpb[i]=b[i]+max(dpa[i+1],dpc[i+1]);

   dpc[i]=c[i]+max(dpb[i+1],dpa[i+1]);

  }

  cout<<max(dpa[0],max(dpb[0],dpc[0]));

    return 0;

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

using namespace std;

int n;

int arr[100005][3];



int dp[100005][4];



int solve(int i, int prev){

	if(i == n) return 0;

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



	dp[i][prev] = -1e7;

	int a = 0, b = 0, c = 0;

	if(prev != 0)

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

	if(prev != 1)

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

	if(prev != 2)

		c = solve(i+1, 2) + arr[i][2];



	dp[i][prev] = max(a, max(b, c));

	return dp[i][prev];

}



int main(){

	cin>>n;

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

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



	memset(dp, -1, sizeof dp);

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

	return 0;

}

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

typedef long long ll;

using namespace std;

ll dpa[100005],dpb[100005],dpc[100005];



int main()

{

  ll n;

  cin>>n;

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

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

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

  }

  dpa[n-1]=a[n-1];

  dpb[n-1]=b[n-1];

  dpc[n-1]=c[n-1];

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

   dpa[i]=a[i]+max(dpb[i+1],dpc[i+1]);

   dpb[i]=b[i]+max(dpa[i+1],dpc[i+1]);

   dpc[i]=c[i]+max(dpb[i+1],dpa[i+1]);

  }

  cout<<max(dpa[0],max(dpb[0],dpc[0]));

    return 0;

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

#include<iostream>

#include<functional>

#include <bitset>

#include<algorithm>

#include<fstream>

#include<string>

#include <cstring>

#include<stdio.h>

#include<iomanip>

#include<sstream>

#include<cmath>

#include<vector>

#include<assert.h>

#include<queue>

#include<deque>

#include<stack>

#include<set>

#include<map>

// never lose hope ..

typedef unsigned long long ull;

typedef long long ll;

typedef long double ld;

#define eps 1e-8

using namespace std;

double const  PI = 3.141592653;

const int OO = 1000000000 + 9;



void fast() {

	ios_base::sync_with_stdio(0);

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

}

struct s {

	int x, y, z;

};



int n;

int v[(int)1e5][4];

int dp[(int)1e5][4];



int solve(int i = 0, int pre = 4) {

	if (i >= n)return 0;

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

	int ch1, ch2, ch3;

	ch1 = ch2 = ch3 = 0;

	if (pre != 1) {

		ch1 += solve(i + 1, 1) + v[i][1];

	}

	if (pre != 2) {

		ch2 += solve(i + 1, 2) + v[i][2];

	}

	if (pre != 3) {

		ch3 += solve(i + 1, 3) + v[i][3];

	}

	return dp[i][pre] = max({ ch1,ch2,ch3 });

}





int main()

{

	fast();



	cin >> n;

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

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

	}

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

	cout << solve();



	return 0;

}

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

typedef long long ll;

using namespace std;

ll dpa[100005],dpb[100005],dpc[100005];



int main()

{

  ll n;

  cin>>n;

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

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

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

  }

  dpa[n-1]=a[n-1];

  dpb[n-1]=b[n-1];

  dpc[n-1]=c[n-1];

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

   dpa[i]=a[i]+max(dpb[i+1],dpc[i+1]);

   dpb[i]=b[i]+max(dpa[i+1],dpc[i+1]);

   dpc[i]=c[i]+max(dpb[i+1],dpa[i+1]);

  }

  cout<<max(dpa[0],max(dpb[0],dpc[0]));

    return 0;

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

#include<bits/stdc++.h>

using namespace std;



typedef long long ll;



int n;

int a[100005][4];

ll dp[100005][4];



ll solve(int d,int l){

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

    if(dp[d][l]!=-1) return dp[d][l];

    ll ret=0;

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

        if(i!=l){

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

        }

    }

    return dp[d][l]=ret;



}





int main()

{

    cin>>n;

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

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

            cin>>a[i][j];

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

    cout<<solve(1,-1);



    return 0;

}


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

typedef long long ll;

using namespace std;

ll dpa[100005],dpb[100005],dpc[100005];



int main()

{

  ll n;

  cin>>n;

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

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

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

  }

  dpa[n-1]=a[n-1];

  dpb[n-1]=b[n-1];

  dpc[n-1]=c[n-1];

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

   dpa[i]=a[i]+max(dpb[i+1],dpc[i+1]);

   dpb[i]=b[i]+max(dpa[i+1],dpc[i+1]);

   dpc[i]=c[i]+max(dpb[i+1],dpa[i+1]);

  }

  cout<<max(dpa[0],max(dpb[0],dpc[0]));

    return 0;

}