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

#define F first

#define S second

#define T second.first

#define C second.second

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

using namespace std;

typedef long long ll;

typedef pair<int, int> pii;

typedef pair<int,pair<int,int>> piii;



const int N= 1e5+5, L = 14, OO = 0x3f3f3f3f;

const ll M=10 ;

const double ESP = 1e-8;



int n, k;

piii A[N];

int dp[N][5];

int solve(int idx, int prev){

    if(idx == n)

        return 0;



    int& ret = dp[idx][prev];

    if(~ret)

        return ret;



    ret = max(ret, (prev != 1)? solve(idx+1, 1)+A[idx].F : -1);

    ret = max(ret, (prev != 2)? solve(idx+1, 2)+A[idx].T : -1);

    return ret = max(ret, (prev != 3)? solve(idx+1, 3)+A[idx].C : -1);



}



int main(){

    FIO

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

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

    cin>>n;

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

    memset(dp, -1, sizeof dp);

    cout<<solve(0,4);

}


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

#include<algorithm>

using namespace std;

const int maxn=100001;

int a[maxn][3],dp[maxn][3];



int main(){

	int n;

	cin>>n;

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

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

	}

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

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

	dp[0][2]=a[0][2];

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

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

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

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

	}



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

   return 0;

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

using namespace std;

#define print printf("==================\n")

#define ll long long

#define pi acos(-1.0)

#define eps 1e-16

#define max3(a,b,c) max(a,max(b,c))

const ll INF = 1 << 30;

typedef pair<ll, ll  > payar;

typedef struct

{

    ll  x, y;

} point;

priority_queue<payar, vector<payar>,  greater<payar> > pq; ///accending

vector< pair<ll,payar  > > vpp;

vector < payar >  vp;



ll n ;

int ara[100005][4];

ll dp[100005][4];

ll cal(int day, int itm)

{

    if(day>n)

    {

        return 0;

    }



    if(dp[day][itm]!=-1)

        return dp[day][itm];

    ll maxi = -1;

    if(day==1)

    {

   return   maxi = dp[day][itm]=max3( cal(day+1,1)+ara[day][1], cal(day+1,2)+ara[day][2], cal(day+1,3)+ara[day][3] );



    }

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

    {

            if(i==itm)

                continue;

            maxi=max( cal(day+1,i)+ara[day][i],maxi);





    }

    return dp[day][itm]=maxi;

}



int main()

{



    cin>>n;

    memset(dp,-1,sizeof dp);

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

    {

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

        {

            cin>>ara[i][j];

        }

    }

    cout<<cal(1,1);







    return  0 ;

}


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



using namespace std;



const int N=1e5;

int n;

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

int mem[N][5];



int solve(int i,int lst){

    if(i==n)

        return 0;

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

    int op1=lst!=0?solve(i+1,0)+a[i]:-1000000000;

    int op2=lst!=1?solve(i+1,1)+b[i]:-1000000000;

    int op3=lst!=2?solve(i+1,2)+c[i]:-1000000000;

    return mem[i][lst]=max(op1,max(op2,op3));

}



int main() {

    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",max(solve(0,0),max(solve(0,1),solve(0,2))));

    return 0;

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

using namespace std;

#define N (int)2e5

int n,a[N],b[N],c[N],memo[N][10];



// a = 0, b = 1, c = 2

int dp(int curr , int last) {

    if (curr > n) return 0;

    int &sol = memo[curr][last];

    if (sol != -1) return sol;

    if (last == 0) {

        sol = max(

            dp(curr + 1 , 1) + b[curr] ,

            dp(curr + 1 , 2) + c[curr]

        );

    } else if (last == 1) {

        sol = max(

            dp(curr + 1 , 0) + a[curr] ,

            dp(curr + 1 , 2) + c[curr]

        );

    } else {

        sol = max(

            dp(curr + 1 , 0) + a[curr] ,

            dp(curr + 1 , 1) + b[curr]

        );

    }

    return sol;

}



int main() {

    scanf("%d" , &n);

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

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

    memset (memo , -1 , sizeof memo);

    printf("%d\n" , max(dp(2 , 0) + a[1] , max(dp(2 , 1) + b[1] , dp(2 , 2) + c[1])));

}


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



using namespace std;



const int N=1e5;

int n;

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

int mem[N][5];



int solve(int i,int lst){

    if(i==n)

        return 0;

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

    int op1=lst!=0?solve(i+1,0)+a[i]:-1000000000;

    int op2=lst!=1?solve(i+1,1)+b[i]:-1000000000;

    int op3=lst!=2?solve(i+1,2)+c[i]:-1000000000;

    return mem[i][lst]=max(op1,max(op2,op3));

}



int main() {

    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",max(solve(0,0),max(solve(0,1),solve(0,2))));

    return 0;

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

#include <stdlib.h>

#include <string.h>

#include <math.h>

#include <queue>

#include <stack>

#include <vector>

#include <algorithm>

#include <iostream>



using namespace std;



const int MAXN = 1e5 + 5;

int n,m;

int a,b,c;

int d,e,f;

vector <int> A;

vector <int> B;

vector <int> C;

vector <int> dp[MAXN];



int main()

{

	scanf("%d",&n);

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

	{

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

		A.push_back(a);//第一天的a现在是a[0]

		B.push_back(b);

		C.push_back(c);

	}

	dp[0].push_back(A[0]);

	dp[0].push_back(B[0]);

	dp[0].push_back(C[0]);

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

	{

		d = max(dp[i - 1][1],dp[i - 1][2]);

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

		f = max(dp[i - 1][0],dp[i - 1][1]);

		dp[i].push_back(d + A[i]);

		dp[i].push_back(e + B[i]);

		dp[i].push_back(f + C[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];

	}

	m = max(max(dp[n - 1][1],dp[n - 1][2]),dp[n - 1][0]);

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

	return 0;

}

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



using namespace std;



const int N=1e5;

int n;

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

int mem[N][5];



int solve(int i,int lst){

    if(i==n)

        return 0;

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

    int op1=lst!=0?solve(i+1,0)+a[i]:-1000000000;

    int op2=lst!=1?solve(i+1,1)+b[i]:-1000000000;

    int op3=lst!=2?solve(i+1,2)+c[i]:-1000000000;

    return mem[i][lst]=max(op1,max(op2,op3));

}



int main() {

    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",max(solve(0,0),max(solve(0,1),solve(0,2))));

    return 0;

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

using namespace std;

typedef long long int ll;

const int MAX=1e5+10;

int N,A[MAX],B[MAX],C[MAX],dp[MAX][3];

int solve(int idx,int pr)

{

    if(idx>N)return 0;

    if(dp[idx][pr]!=-1)return dp[idx][pr];

    int res=0;

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

        if(i==pr)continue;

        int val=0;

        if(i==1)val=A[idx];

        if(i==2)val=B[idx];

        if(i==3)val=C[idx];

        res = max(res,solve(idx+1,i)+val);

    }

    return dp[idx][pr]=res;

}

int  main ()

{

    memset(dp,-1,sizeof dp);

    cin >> N;

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

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

    }

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

    return 0;

}






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

#include <cstdio>

#include <cmath>

#include <queue>

using namespace std;



int dp_H[100003][3];//用dp_H[i][k]表示第i天选择第k项活动的最大快乐值

int Happy[100003][3];//



int BiggestHappy(int N){

    dp_H[1][1]=Happy[1][1];

    dp_H[1][2]=Happy[1][2];

    dp_H[1][3]=Happy[1][3];

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

        dp_H[i][1]=Happy[i][1]+max(dp_H[i-1][2],dp_H[i-1][3]);

        dp_H[i][2]=Happy[i][2]+max(dp_H[i-1][1],dp_H[i-1][3]);

        dp_H[i][3]=Happy[i][3]+max(dp_H[i-1][1],dp_H[i-1][2]);

    }

    int answer=max(dp_H[N][1],max(dp_H[N][2],dp_H[N][3]));

    return answer;

}



int main()

{

    int N;

    while(cin>>N){

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

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

        }

        cout<<BiggestHappy(N)<<endl;

    }

    return 0;

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

using namespace std;

#define print printf("==================\n")

#define ll long long

#define pi acos(-1.0)

#define eps 1e-16

#define max3(a,b,c) max(a,max(b,c))

const ll INF = 1 << 30;

typedef pair<ll, ll  > payar;

typedef struct

{

    ll  x, y;

} point;

priority_queue<payar, vector<payar>,  greater<payar> > pq; ///accending

vector< pair<ll,payar  > > vpp;

vector < payar >  vp;



ll n ;

int ara[100005][4];

ll dp[100005][4];

ll cal(int day, int itm)

{

    if(day>n)

    {

        return 0;

    }



    if(dp[day][itm]!=-1)

        return dp[day][itm];

    ll maxi = -1;

    if(day==1)

    {

   return   maxi = dp[day][itm]=max3( cal(day+1,1)+ara[day][1], cal(day+1,2)+ara[day][2], cal(day+1,3)+ara[day][3] );



    }

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

    {

            if(i==itm)

                continue;

            maxi=max( cal(day+1,i)+ara[day][i],maxi);





    }

    return dp[day][itm]=maxi;

}



int main()

{



    cin>>n;

    memset(dp,-1,sizeof dp);

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

    {

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

        {

            cin>>ara[i][j];

        }

    }

    cout<<cal(1,1);







    return  0 ;

}


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

#include <cstdio>

#include <cmath>

#include <queue>

using namespace std;



int dp_H[100003][3];//用dp_H[i][k]表示第i天选择第k项活动的最大快乐值

int Happy[100003][3];//



int BiggestHappy(int N){

    dp_H[1][1]=Happy[1][1];

    dp_H[1][2]=Happy[1][2];

    dp_H[1][3]=Happy[1][3];

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

        dp_H[i][1]=Happy[i][1]+max(dp_H[i-1][2],dp_H[i-1][3]);

        dp_H[i][2]=Happy[i][2]+max(dp_H[i-1][1],dp_H[i-1][3]);

        dp_H[i][3]=Happy[i][3]+max(dp_H[i-1][1],dp_H[i-1][2]);

    }

    int answer=max(dp_H[N][1],max(dp_H[N][2],dp_H[N][3]));

    return answer;

}



int main()

{

    int N;

    while(cin>>N){

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

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

        }

        cout<<BiggestHappy(N)<<endl;

    }

    return 0;

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

#include <cstdint>

#include <cinttypes>

#include <cstring>

#include <algorithm>



using i32 = std::int_fast32_t;



const int MaxN = 100001;



int A[MaxN][3], dp[MaxN][3];



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

	i32 n;

	std::scanf("%" SCNdFAST32, &n);

	

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

		std::scanf("%" SCNdFAST32 "%" SCNdFAST32 "%" SCNdFAST32, A[i], A[i] + 1, A[i] + 2);

	}

	

	memset(dp, 0, sizeof dp);

	

	dp[0][0] = A[0][0];

	dp[0][1] = A[0][1];

	dp[0][2] = A[0][2];

	

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

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

			for (i32 a = 0; a < 3; ++ a) {

				if (a == j) {

					continue;

				}

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

			}

		}

	}

	

	std::printf("%" PRIdFAST32, std::max(dp[n - 1][0], std::max(dp[n - 1][1], dp[n - 1][2])));

	return 0;

}

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

#include <cstdio>

#include <cmath>

#include <queue>

using namespace std;



int dp_H[100003][3];//用dp_H[i][k]表示第i天选择第k项活动的最大快乐值

int Happy[100003][3];//



int BiggestHappy(int N){

    dp_H[1][1]=Happy[1][1];

    dp_H[1][2]=Happy[1][2];

    dp_H[1][3]=Happy[1][3];

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

        dp_H[i][1]=Happy[i][1]+max(dp_H[i-1][2],dp_H[i-1][3]);

        dp_H[i][2]=Happy[i][2]+max(dp_H[i-1][1],dp_H[i-1][3]);

        dp_H[i][3]=Happy[i][3]+max(dp_H[i-1][1],dp_H[i-1][2]);

    }

    int answer=max(dp_H[N][1],max(dp_H[N][2],dp_H[N][3]));

    return answer;

}



int main()

{

    int N;

    while(cin>>N){

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

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

        }

        cout<<BiggestHappy(N)<<endl;

    }

    return 0;

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

using namespace std;

#define N (int)2e5

int n,a[N],b[N],c[N],memo[N][10];



// a = 0, b = 1, c = 2

int dp(int curr , int last) {

    if (curr > n) return 0;

    int &sol = memo[curr][last];

    if (sol != -1) return sol;

    if (last == 0) {

        sol = max(

            dp(curr + 1 , 1) + b[curr] ,

            dp(curr + 1 , 2) + c[curr]

        );

    } else if (last == 1) {

        sol = max(

            dp(curr + 1 , 0) + a[curr] ,

            dp(curr + 1 , 2) + c[curr]

        );

    } else {

        sol = max(

            dp(curr + 1 , 0) + a[curr] ,

            dp(curr + 1 , 1) + b[curr]

        );

    }

    return sol;

}



int main() {

    scanf("%d" , &n);

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

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

    memset (memo , -1 , sizeof memo);

    printf("%d\n" , max(dp(2 , 0) + a[1] , max(dp(2 , 1) + b[1] , dp(2 , 2) + c[1])));

}


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

#include <cstdio>

#include <cmath>

#include <queue>

using namespace std;



int dp_H[100003][3];//用dp_H[i][k]表示第i天选择第k项活动的最大快乐值

int Happy[100003][3];//



int BiggestHappy(int N){

    dp_H[1][1]=Happy[1][1];

    dp_H[1][2]=Happy[1][2];

    dp_H[1][3]=Happy[1][3];

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

        dp_H[i][1]=Happy[i][1]+max(dp_H[i-1][2],dp_H[i-1][3]);

        dp_H[i][2]=Happy[i][2]+max(dp_H[i-1][1],dp_H[i-1][3]);

        dp_H[i][3]=Happy[i][3]+max(dp_H[i-1][1],dp_H[i-1][2]);

    }

    int answer=max(dp_H[N][1],max(dp_H[N][2],dp_H[N][3]));

    return answer;

}



int main()

{

    int N;

    while(cin>>N){

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

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

        }

        cout<<BiggestHappy(N)<<endl;

    }

    return 0;

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

using namespace std;

typedef long long LL;



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

int dp[100007][5];



int Solve(int idx, int n, int prev){

	

	if(idx == n)return 0;

	

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

	

	int ans = -1;

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

		if(i == prev)continue;

		if(i == 1)ans = max(ans, a[idx] + Solve(idx+1, n, i));

		else if(i == 2)ans = max(ans, b[idx] + Solve(idx+1, n, i));

		else ans = max(ans, c[idx] + Solve(idx+1, n, i));		

	}

	return dp[idx][prev] = ans;

}



int main(){

	int n; cin>>n;

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

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

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

	}

	int ret = -1;

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

		ret = max(ret, Solve(0,n,i));

	}

	cout<<ret<<endl;



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

	return 0;

}

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

#include <cstdio>

#include <cmath>

#include <queue>

using namespace std;



int dp_H[100003][3];//用dp_H[i][k]表示第i天选择第k项活动的最大快乐值

int Happy[100003][3];//



int BiggestHappy(int N){

    dp_H[1][1]=Happy[1][1];

    dp_H[1][2]=Happy[1][2];

    dp_H[1][3]=Happy[1][3];

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

        dp_H[i][1]=Happy[i][1]+max(dp_H[i-1][2],dp_H[i-1][3]);

        dp_H[i][2]=Happy[i][2]+max(dp_H[i-1][1],dp_H[i-1][3]);

        dp_H[i][3]=Happy[i][3]+max(dp_H[i-1][1],dp_H[i-1][2]);

    }

    int answer=max(dp_H[N][1],max(dp_H[N][2],dp_H[N][3]));

    return answer;

}



int main()

{

    int N;

    while(cin>>N){

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

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

        }

        cout<<BiggestHappy(N)<<endl;

    }

    return 0;

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

#include <stdlib.h>

#include <string.h>

#include <math.h>

#include <queue>

#include <stack>

#include <vector>

#include <algorithm>

#include <iostream>



using namespace std;



const int MAXN = 1e5 + 5;

int n,m;

int a,b,c;

int d,e,f;

vector <int> A;

vector <int> B;

vector <int> C;

vector <int> dp[MAXN];



int main()

{

	scanf("%d",&n);

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

	{

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

		A.push_back(a);//第一天的a现在是a[0]

		B.push_back(b);

		C.push_back(c);

	}

	dp[0].push_back(A[0]);

	dp[0].push_back(B[0]);

	dp[0].push_back(C[0]);

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

	{

		d = max(dp[i - 1][1],dp[i - 1][2]);

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

		f = max(dp[i - 1][0],dp[i - 1][1]);

		dp[i].push_back(d + A[i]);

		dp[i].push_back(e + B[i]);

		dp[i].push_back(f + C[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];

	}

	m = max(max(dp[n - 1][1],dp[n - 1][2]),dp[n - 1][0]);

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

	return 0;

}

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

#include <cstdio>

#include <cmath>

#include <queue>

using namespace std;



int dp_H[100003][3];//用dp_H[i][k]表示第i天选择第k项活动的最大快乐值

int Happy[100003][3];//



int BiggestHappy(int N){

    dp_H[1][1]=Happy[1][1];

    dp_H[1][2]=Happy[1][2];

    dp_H[1][3]=Happy[1][3];

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

        dp_H[i][1]=Happy[i][1]+max(dp_H[i-1][2],dp_H[i-1][3]);

        dp_H[i][2]=Happy[i][2]+max(dp_H[i-1][1],dp_H[i-1][3]);

        dp_H[i][3]=Happy[i][3]+max(dp_H[i-1][1],dp_H[i-1][2]);

    }

    int answer=max(dp_H[N][1],max(dp_H[N][2],dp_H[N][3]));

    return answer;

}



int main()

{

    int N;

    while(cin>>N){

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

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

        }

        cout<<BiggestHappy(N)<<endl;

    }

    return 0;

}
// Slower C++ solution:
#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;

}

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

#include <cstdio>

#include <cmath>

#include <queue>

using namespace std;



int dp_H[100003][3];//用dp_H[i][k]表示第i天选择第k项活动的最大快乐值

int Happy[100003][3];//



int BiggestHappy(int N){

    dp_H[1][1]=Happy[1][1];

    dp_H[1][2]=Happy[1][2];

    dp_H[1][3]=Happy[1][3];

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

        dp_H[i][1]=Happy[i][1]+max(dp_H[i-1][2],dp_H[i-1][3]);

        dp_H[i][2]=Happy[i][2]+max(dp_H[i-1][1],dp_H[i-1][3]);

        dp_H[i][3]=Happy[i][3]+max(dp_H[i-1][1],dp_H[i-1][2]);

    }

    int answer=max(dp_H[N][1],max(dp_H[N][2],dp_H[N][3]));

    return answer;

}



int main()

{

    int N;

    while(cin>>N){

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

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

        }

        cout<<BiggestHappy(N)<<endl;

    }

    return 0;

}
// Slower C++ solution:
#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;

}

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

#include <cstdio>

#include <cmath>

#include <queue>

using namespace std;



int dp_H[100003][3];//用dp_H[i][k]表示第i天选择第k项活动的最大快乐值

int Happy[100003][3];//



int BiggestHappy(int N){

    dp_H[1][1]=Happy[1][1];

    dp_H[1][2]=Happy[1][2];

    dp_H[1][3]=Happy[1][3];

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

        dp_H[i][1]=Happy[i][1]+max(dp_H[i-1][2],dp_H[i-1][3]);

        dp_H[i][2]=Happy[i][2]+max(dp_H[i-1][1],dp_H[i-1][3]);

        dp_H[i][3]=Happy[i][3]+max(dp_H[i-1][1],dp_H[i-1][2]);

    }

    int answer=max(dp_H[N][1],max(dp_H[N][2],dp_H[N][3]));

    return answer;

}



int main()

{

    int N;

    while(cin>>N){

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

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

        }

        cout<<BiggestHappy(N)<<endl;

    }

    return 0;

}
// Slower C++ solution:
#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;

}

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

#include <cstdio>

#include <cmath>

#include <queue>

using namespace std;



int dp_H[100003][3];//用dp_H[i][k]表示第i天选择第k项活动的最大快乐值

int Happy[100003][3];//



int BiggestHappy(int N){

    dp_H[1][1]=Happy[1][1];

    dp_H[1][2]=Happy[1][2];

    dp_H[1][3]=Happy[1][3];

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

        dp_H[i][1]=Happy[i][1]+max(dp_H[i-1][2],dp_H[i-1][3]);

        dp_H[i][2]=Happy[i][2]+max(dp_H[i-1][1],dp_H[i-1][3]);

        dp_H[i][3]=Happy[i][3]+max(dp_H[i-1][1],dp_H[i-1][2]);

    }

    int answer=max(dp_H[N][1],max(dp_H[N][2],dp_H[N][3]));

    return answer;

}



int main()

{

    int N;

    while(cin>>N){

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

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

        }

        cout<<BiggestHappy(N)<<endl;

    }

    return 0;

}