stablegradients/pie-gem5-pairs huggingface dataset:
// 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.356189266043134x):
//بِسْمِ اللَّهِ الرَّحْمَنِ الرَّحِيمِ

//لا إله إلا الله محمد رسول الله



#include <bits/stdc++.h>



#pragma GCC optimize("Ofast")



#define ll long long

#define pb push_back

#define fr first

#define sc second

#define in freopen("input.txt", "r" , stdin)

#define out freopen("snakes.out", "w", stdout)

using namespace std;



ll dp[100005][5];

ll n;

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

int main(){

    //in;

    //out;

    ios_base::sync_with_stdio(false);

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





}
// 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.7695198468895745x):
//بِسْمِ اللَّهِ الرَّحْمَنِ الرَّحِيمِ

//لا إله إلا الله محمد رسول الله



#include <bits/stdc++.h>



#pragma GCC optimize("Ofast")



#define ll long long

#define pb push_back

#define fr first

#define sc second

#define in freopen("input.txt", "r" , stdin)

#define out freopen("snakes.out", "w", stdout)

using namespace std;



ll dp[100005][5];

ll n;

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

int main(){

    //in;

    //out;

    ios_base::sync_with_stdio(false);

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





}
// 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.0745187380297025x):
#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;

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

        if(next_option != option) res = max(res, mat[day][option] + solve(day + 1, next_option));

    }



    return ans[day][option] = res;

}



int main() {

    

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

        init(n);

        

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

            for(int j = 0;j < k; j++) scanf("%d",&mat[i][j]);

        }



        int res = -1;

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

            res = max(res, solve(0, option));

        }



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

    }    

    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.40422675451726x):
#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;

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

        if(next_option != option) res = max(res, mat[day][option] + solve(day + 1, next_option));

    }



    return ans[day][option] = res;

}



int main() {

    

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

        init(n);

        

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

            for(int j = 0;j < k; j++) scanf("%d",&mat[i][j]);

        }



        int res = -1;

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

            res = max(res, solve(0, option));

        }



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

    }    

    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.7948471697881165x):
#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;

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

        if(next_option != option) res = max(res, mat[day][option] + solve(day + 1, next_option));

    }



    return ans[day][option] = res;

}



int main() {

    

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

        init(n);

        

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

            for(int j = 0;j < k; j++) scanf("%d",&mat[i][j]);

        }



        int res = -1;

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

            res = max(res, solve(0, option));

        }



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

    }    

    return 0;

}
// 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.123988840248965x):
#include<bits/stdc++.h>

using namespace std;

 

int main() {

	int Ndays;

	scanf("%d", &Ndays);

	int a, b, c;

	int A = 0, B = 0, C = 0;

	while (Ndays--) {

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

		int AA = max(B, C) + a;

		int BB = max(A, C) + b;

		int CC = max(A, B) + c;

		A = AA, B = BB, C = CC;

	}

	printf("%d", max(max(A, B), C));

}
// 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.4374136347353645x):
#include<bits/stdc++.h>

using namespace std;

 

int main() {

	int Ndays;

	scanf("%d", &Ndays);

	int a, b, c;

	int A = 0, B = 0, C = 0;

	while (Ndays--) {

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

		int AA = max(B, C) + a;

		int BB = max(A, C) + b;

		int CC = max(A, B) + c;

		A = AA, B = BB, C = CC;

	}

	printf("%d", max(max(A, B), C));

}
// 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.8123446464043618x):
#include<bits/stdc++.h>

using namespace std;

 

int main() {

	int Ndays;

	scanf("%d", &Ndays);

	int a, b, c;

	int A = 0, B = 0, C = 0;

	while (Ndays--) {

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

		int AA = max(B, C) + a;

		int BB = max(A, C) + b;

		int CC = max(A, B) + c;

		A = AA, B = BB, C = CC;

	}

	printf("%d", max(max(A, B), C));

}
// 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.091920298238545x):
#include<iostream>

#include<algorithm>

#define MAXN 100003

using namespace std;

int dp[MAXN][3], n, a[MAXN][3];

int main()

{

	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] = a[i][0] + max(dp[i-1][1], dp[i-1][2]);

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

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

	}

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

	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.415900542691365x):
#include<iostream>

#include<algorithm>

#define MAXN 100003

using namespace std;

int dp[MAXN][3], n, a[MAXN][3];

int main()

{

	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] = a[i][0] + max(dp[i-1][1], dp[i-1][2]);

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

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

	}

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

	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.8010020669720936x):
#include<iostream>

#include<algorithm>

#define MAXN 100003

using namespace std;

int dp[MAXN][3], n, a[MAXN][3];

int main()

{

	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] = a[i][0] + max(dp[i-1][1], dp[i-1][2]);

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

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

	}

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

	return 0;

}
// 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: 4.9912716016688945x):
#include <bits/stdc++.h>

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

using namespace std;

typedef long long ll;

const int inf = 1001001001;

 

int main(){

  int n;

  cin >> n;

  

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

  rep(i,n){

    rep(j,3){

      cin >> h.at(i).at(j);

    }

  }

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

  

  

  rep(i,n){

    

    if (i == 0){

      rep(l,3){dvector.at(0).at(l) = h.at(0).at(l);}

      continue;

    }

    

    rep(j,3){

      rep(k,3){

        

      if(k != j){

        dvector.at(i).at(j) = max(dvector.at(i).at(j), dvector.at(i-1).at(k)+h.at(i).at(j));

      }

      

      }

    }

  }

    

    int ans = 0;

    rep(i,3){

    ans = max(ans, dvector.at(n-1).at(i));

    }

    

    cout << ans << endl;

    

  }
// 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.3483806450699545x):
#include <bits/stdc++.h>

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

using namespace std;

typedef long long ll;

const int inf = 1001001001;

 

int main(){

  int n;

  cin >> n;

  

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

  rep(i,n){

    rep(j,3){

      cin >> h.at(i).at(j);

    }

  }

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

  

  

  rep(i,n){

    

    if (i == 0){

      rep(l,3){dvector.at(0).at(l) = h.at(0).at(l);}

      continue;

    }

    

    rep(j,3){

      rep(k,3){

        

      if(k != j){

        dvector.at(i).at(j) = max(dvector.at(i).at(j), dvector.at(i-1).at(k)+h.at(i).at(j));

      }

      

      }

    }

  }

    

    int ans = 0;

    rep(i,3){

    ans = max(ans, dvector.at(n-1).at(i));

    }

    

    cout << ans << endl;

    

  }
// 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.7654028234759427x):
#include <bits/stdc++.h>

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

using namespace std;

typedef long long ll;

const int inf = 1001001001;

 

int main(){

  int n;

  cin >> n;

  

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

  rep(i,n){

    rep(j,3){

      cin >> h.at(i).at(j);

    }

  }

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

  

  

  rep(i,n){

    

    if (i == 0){

      rep(l,3){dvector.at(0).at(l) = h.at(0).at(l);}

      continue;

    }

    

    rep(j,3){

      rep(k,3){

        

      if(k != j){

        dvector.at(i).at(j) = max(dvector.at(i).at(j), dvector.at(i-1).at(k)+h.at(i).at(j));

      }

      

      }

    }

  }

    

    int ans = 0;

    rep(i,3){

    ans = max(ans, dvector.at(n-1).at(i));

    }

    

    cout << ans << endl;

    

  }