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



using namespace std;



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

int memo[100007][4];

int n; 



int solve(int p, int last) {

    if(p == n) return 0;

    int & st = memo[p][last];

    if(st == -1) {

        st = max({

            last != 1? solve(p+1, 1) + a[p] : 0,

            last != 2? solve(p+1, 2) + b[p] : 0,

            last != 3? solve(p+1, 3) + c[p] : 0

        });

    }

    return st;

}

int main() {

    memset(memo, -1, sizeof memo);

    cin >> n;

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

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

    }



    cout << solve(0, 0);

}

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

#include <algorithm>

using std::max;



#define MAXN 100003

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

int main(void)

{

    int i,N;

    scanf("%d",&N);

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

    {

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

    }

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

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

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

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

    {

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

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

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

    }

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



    return 0;

}

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

#include<iostream>

#include<vector>

#include<algorithm>

#include<map>



using namespace std;



const int N=100500;

int OO=INT_MAX;

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

int mem[N][4];

int many=0;



int solve(int idx,int last)

{

    if(idx==n)

        return 0;

    if(mem[idx][last]!=-1)

        return mem[idx][last];

    int op1=-OO,op2=-OO;

    if(last==1)

    {

        op1=solve(idx+1,2)+b[idx];

        op2=solve(idx+1,3)+c[idx];

    }

    if(last==2)

    {

        op1=solve(idx+1,1)+a[idx];

        op2=solve(idx+1,3)+c[idx];

    }

    if(last==3)

    {

        op1=solve(idx+1,1)+a[idx];

        op2=solve(idx+1,2)+b[idx];

    }

    return mem[idx][last] = max(op1,op2);

}



int main() {



    cin>>n;

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

    {

        mem[i][1]=-1;

        mem[i][2]=-1;

        mem[i][3]=-1;

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

    }

    int fcall,scall,thcall;



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

    scall=solve(1,2)+b[0];

    thcall=solve(1,3)+c[0];



    cout<< max(fcall,max(scall,thcall));



	return 0;

}


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

#include <algorithm>

using std::max;



#define MAXN 100003

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

int main(void)

{

    int i,N;

    scanf("%d",&N);

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

    {

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

    }

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

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

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

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

    {

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

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

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

    }

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



    return 0;

}

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

#include <algorithm>



int arr[100005][3];

int val[100005][3];



int main()

{

	int n;

	std::cin >> n;

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

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

	val[0][0] = arr[0][0];

	val[0][1] = arr[0][1];

	val[0][2] = arr[0][2];

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

	{

		int* curr = val[i];

		int* temp = val[i - 1];

		curr[0] = std::max(temp[1], temp[2]) + arr[i][0];

		curr[1] = std::max(temp[0], temp[2]) + arr[i][1];

		curr[2] = std::max(temp[0], temp[1]) + arr[i][2];

	}

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

	return 0;

}

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

#include <algorithm>

using std::max;



#define MAXN 100003

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

int main(void)

{

    int i,N;

    scanf("%d",&N);

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

    {

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

    }

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

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

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

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

    {

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

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

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

    }

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



    return 0;

}

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

#include <iostream>

using namespace std;



int t;

int arr[100000+100][4] ;

long long dp[100000+100][4];



long long solve(int r ,int col=4)

{

    if(r>=t)

        return 0;

    long long &ret=dp[r][col] ;

    if(ret)

        return ret;

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

    {

        if(i==col) continue;

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

    }

     

    return  ret;

}

int main()

{

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

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

    

    cin>>t;

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

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

    printf("%lld\n",solve(0));

    return 0;

}

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

#include <algorithm>

using std::max;



#define MAXN 100003

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

int main(void)

{

    int i,N;

    scanf("%d",&N);

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

    {

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

    }

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

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

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

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

    {

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

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

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

    }

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



    return 0;

}

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

using namespace std;

const int maxn=100010;

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

int A[maxn][3];

int main(){

	int n;

	scanf("%d",&n); 

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

		scanf("%d%d%d",&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][0],dp[i-1][1])+A[i][2];

		

	}

	int temp=dp[n-1][0];

	if(temp<dp[n-1][1])	temp=dp[n-1][1];

	if(temp<dp[n-1][2])	temp=dp[n-1][2];

	printf("%d",temp);

	return 0;

}

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

#include <algorithm>

using std::max;



#define MAXN 100003

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

int main(void)

{

    int i,N;

    scanf("%d",&N);

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

    {

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

    }

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

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

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

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

    {

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

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

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

    }

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



    return 0;

}

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

#define inf 1200000

using namespace std;

int main()

{

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

    int n;

    int dp[110001][3];

    cin>>n;

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

    {

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



    }





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

    {

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

        {

            if(i==0)

            {

                if(j==0)

                    dp[i][j]=a[i];

                else if(j==1)

                    dp[i][j]=b[i];

                else

                    dp[i][j]=c[i];

            }

            else

            {

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

                {

                    if(j==k)

                        continue;

                     if(j==0)

                    {

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



                    }



                    else if(j==1)

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

                    else

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



                }

            }

        }

    }



   int r=0;

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



   r=max(r,dp[n-1][1]);



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



   cout<<r;



}


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

#include <algorithm>

using std::max;



#define MAXN 100003

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

int main(void)

{

    int i,N;

    scanf("%d",&N);

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

    {

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

    }

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

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

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

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

    {

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

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

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

    }

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



    return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#define _CRT_SECURE_NO_WARNINGS

#include<bits/stdc++.h>

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

#define per(i,a,n) for (int i=n-1;i>=a;i--)

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

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

#define clr(v, d) memset(v, d, sizeof(v))

#define fi first

#define se second

#define pp push_back

#define pb pop_back

#define pi 3.141592654

#define MOD 1000000007 

#define cons1 1e12

#define cons2 1e6

using namespace std;

typedef long long ll;

typedef vector<int> vi;

typedef pair<int, int> pa;

typedef unsigned long long int ull;

typedef double long dl;

const int N = 1e5+9,oo=1e9;

int n,k;

int v[N][3], ans[N][4];

int sol(int i,int l) {

	if (i == n)

		return 0;

	int mx = INT_MIN;

	if (!ans[i][l]) {

		if (l != 0)

			mx = max(mx, v[i][0] + sol(i + 1, 0));

		if (l != 1)

			mx = max(mx, v[i][1] + sol(i + 1, 1));

		if (l != 2)

			mx = max(mx, v[i][2] + sol(i + 1, 2));

		ans[i][l] = mx;

	}



	return ans[i][l];

	

}

void solve() {

	cin >> n;

	rep(i, 0, n)

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

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

	

}

int main() {

	ios::sync_with_stdio(0);

	cin.tie(NULL);

	cout.tie(NULL);

	int t = 1;

	//cin >> t;

	while (t--)

		solve();



#ifndef ONLINE_JUDGE

	- system("pause");

#endif



}

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

#include <algorithm>

using std::max;



#define MAXN 100003

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

int main(void)

{

    int i,N;

    scanf("%d",&N);

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

    {

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

    }

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

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

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

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

    {

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

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

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

    }

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



    return 0;

}

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



using namespace std;



int n;

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

bool found[100001][3];

long long int dp[100001][3];



long long int m(long long int a, long long int b){

    return ((a > b) ? a : b);

}



long long int f(int now, int k){

    if (now == n){

        if (k == 0){

            return max(b[now], c[now]);

        } else if (k == 1){

            return max(a[now], c[now]);

        } else if (k == 2) {

            return max(b[now], c[now]);

        } else {

            return max(a[now], max(b[now], c[now]));

        }

    } else {

        if (found[now][k]){

            return dp[now][k];

        } else {

            if (k == 0){

                dp[now][k] = m(b[now] + f(now+1, 1), c[now] + f(now+1, 2));

            } else if (k == 1){

                dp[now][k] = m(a[now] + f(now+1, 0), c[now] + f(now+1, 2));

            } else if (k == 2){

                dp[now][k] = m(b[now] + f(now+1, 1), a[now] + f(now+1, 0));

            } else {

                dp[now][k] = m(a[now] + f(now+1, 0), m(b[now] + f(now+1, 1), c[now] + f(now+1, 2)));

            }

            found[now][k] = true;

            return dp[now][k];

        }

    }

}



int main(){

    cin >> n;

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

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

    }

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

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

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

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

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

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

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

    }

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

}


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

#include <algorithm>

using std::max;



#define MAXN 100003

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

int main(void)

{

    int i,N;

    scanf("%d",&N);

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

    {

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

    }

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

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

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

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

    {

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

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

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

    }

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



    return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#define _CRT_SECURE_NO_WARNINGS

#include<bits/stdc++.h>

#include<iostream>

#include<algorithm>

#include<iomanip>

#include<string>

#include<cmath>

#include<vector>

#include<set>

#include<queue>

#include<stack>

#include<fstream>

#include<map>

#include<bitset>

#include<unordered_map>

#include<cstring>

#include<cctype>

using namespace std;

#define  ll long long

#define  ull unsigned long long

#define  ul unsigned long

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

#define sz(n) (int)n.size()

#define pb(s) push_back(s);

#define format(n) fixed<<setprecision(n)

#define finl "\n"

#define Mohammed_Atef_Hassan fast();

#define mod 1000000007

#define INF LLONG_MAX

#define PI acos(-1)

#define clr(dp,n) memset(dp,n,sizeof dp)

#define ex(n) return cout<<n<<finl,0;

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

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

ll fix_mod(ll x, ll y)

{

	return (y + x % y) % y;

}

void fast() {

	std::ios_base::sync_with_stdio(false);

	cin.tie(nullptr); cout.tie(nullptr);





}

int n;

vector<vector<ll>>v;

ll mem[(int)1e5 + 10][5];

ll solve(int i, int c)

{

	if (i == n)return 0;

	ll& ret = mem[i][c];

	if (~ret)return ret;

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

	{

		if (k == c)continue;

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

	}

	return ret;

}

int main()

{

	Mohammed_Atef_Hassan

		cin >> n;

	v = vector<vector<ll>>(n + 1, vector<ll>(3));

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

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

	clr(mem, -1);

	ex(solve(0, 4));

}

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

#include <algorithm>

using std::max;



#define MAXN 100003

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

int main(void)

{

    int i,N;

    scanf("%d",&N);

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

    {

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

    }

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

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

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

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

    {

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

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

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

    }

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



    return 0;

}

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

#define endl '\n'

#define ll long long

using namespace std;



const int MXN = 1e5 + 5;

int n, k;

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

int dp[MXN][4];



int Dp(int d, int lst) {

    if(d == n) return 0;

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

    int ans = 0;

    if(lst != 0) ans = Dp(d + 1, 0) + a[d];

    if(lst != 1) ans = max(ans, Dp(d + 1, 1) + b[d]);

    if(lst != 2) ans = max(ans, Dp(d + 1, 2) + c[d]);

    return dp[d][lst] = ans;

}



int main() {

    ios_base::sync_with_stdio(false); cin.tie(NULL);

    cin >> n;

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

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

    }

    memset(dp, -1, sizeof dp);

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

    return 0;

}

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

#include <cstdio>

using namespace std;

#define max(a, b) ((a>b)?a:b)

const int N = 1e5+1;

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

int n;



int main(){

	int aa, bb, cc;

	scanf("%d", &n);

	A[0] = B[0] = C[0] = 0;

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

		scanf("%d%d%d", &aa, &bb, &cc);

		A[i] = aa + max(B[i-1], C[i-1]);

		B[i] = bb + max(A[i-1], C[i-1]);

		C[i] = cc + max(A[i-1], B[i-1]);

	}

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

}

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



using namespace std;

#define tam 100001

int A[tam][3],n;

int dp[tam][3];

int solver(int y, int x)

{

    if(dp[y][x]!=-1)

        return dp[y][x];

    if(y == 0 )

    {

    	dp[y][x] = A[y][x];

        return A[y][x];

    }

    return dp[y][x] = max(solver((y-1),(x+1)%3)+ A[y][x],

	solver((y-1),(x+2)%3) + A[y][x]);

}

int main()

{

    int bas;

    cin>>n;

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

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

            cin >>A[i][j];

    memset(dp,-1,sizeof dp);

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

    	bas = solver(n-1,i);

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

    return 0;

}

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

#include <cstdio>

using namespace std;

#define max(a, b) ((a>b)?a:b)

const int N = 1e5+1;

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

int n;



int main(){

	int aa, bb, cc;

	scanf("%d", &n);

	A[0] = B[0] = C[0] = 0;

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

		scanf("%d%d%d", &aa, &bb, &cc);

		A[i] = aa + max(B[i-1], C[i-1]);

		B[i] = bb + max(A[i-1], C[i-1]);

		C[i] = cc + max(A[i-1], B[i-1]);

	}

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

}

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

 

#define pb push_back

#define fst first

#define snd second

#define INF 0x7fffffff

#define INFLL 0x7FFFFFFFFFFFFFFF

#define lenA(ar) sizeof(ar)/sizeof(*ar)

#define lenrows(mat) sizeof(mat)/sizeof(*mat)

#define lencols(mat) sizeof(*mat)/sizeof(**mat)

#define fill(a,c) memset(&a, c, sizeof(a))

#define fore(i,x,y) for(ll i=x;i<y;i++)

#define foreI(i,x,y) for(ll i=x-1;i>=y;i--)

#define max2(a,b) max(a, b)

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

#define min2(a,b) min(a, b)

#define min3(a,b,c) min2(min2(a,b),c)

#define MOD 1000000009

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

#define rall(x) (x).rbegin(), (x).rend()

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

 

typedef long long ll;

typedef unsigned long long ull;

 

using namespace std;

ll arr[100010][3];

ll dp[100010][3];

int n;

ll solve(int i, int j){

    if(i==n-1) return arr[i][j];

    ll &r = dp[i][j];

    if(r>-1) return r;

    ll a = 0; ll b = 0;ll c = 0; ll act=arr[i][j];

    if(j!=0) a=solve(i+1, 0)+act;

    if(j!=1) b=solve(i+1, 1)+act;

    if(j!=2) c=solve(i+1, 2)+act;

   return r=max3(a,b,c);

}

 

int main() {

     

    cin>>n;

    arr[n][3];

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

    fore(i,0,n){

        fore(j,0,3){

            cin>>arr[i][j];

        }

    }

 

    cout<<max3(solve(0,0),solve(0,1),solve(0,2))<<"\n";

 

    return 0;

}

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

#include <cstdio>

using namespace std;

#define max(a, b) ((a>b)?a:b)

const int N = 1e5+1;

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

int n;



int main(){

	int aa, bb, cc;

	scanf("%d", &n);

	A[0] = B[0] = C[0] = 0;

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

		scanf("%d%d%d", &aa, &bb, &cc);

		A[i] = aa + max(B[i-1], C[i-1]);

		B[i] = bb + max(A[i-1], C[i-1]);

		C[i] = cc + max(A[i-1], B[i-1]);

	}

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

}

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

#include <iostream>

using namespace std;

const int MAXN=1e5+7;

int n,ans;

int h[MAXN][5],f[MAXN][5][5];

int main()

{

	cin>>n;

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

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

	memset(f,127,sizeof(f));

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

		for(int j=1;j<=3;j++) f[1][i][j]=h[1][i];

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

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

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

			{

				int maxx=0;

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

					if(j!=k&&k!=l) maxx=max(maxx,f[i-1][k][l]);

				f[i][j][k]=maxx+h[i][j];

			}

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

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

	cout<<ans<<endl;

	return 0;

}

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

using namespace std;

#define  ll long long int



int main()

{

    ll n,k;

    cin>>n;

    ll a[n+7],b[n+6],c[n+6];

    ll dp[n+5][4];

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

    }

    cout<<max({dp[n][1],dp[n][2],dp[n][3]})<<endl;

}


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

using namespace std;

#define sz 100005

int n,a[sz],b[sz],c[sz];

int dp[sz][5];



int res(int ind,int ofStatus=0)

{

    if(ind==n)return 0;

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

    int r=INT_MIN;

    if(ofStatus!=1)

    {

        r=max(r,a[ind]+res(ind+1,1));

    }

    if(ofStatus!=2)

    {

        r=max(r,b[ind]+res(ind+1,2));

    }

    if(ofStatus!=3)

    {

        r=max(r,c[ind]+res(ind+1,3));

    }

    return dp[ind][ofStatus]=r;

}



int main()

{

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

    cin>>n;

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

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



    cout<<res(0)<<endl;



   return 0;

}


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

using namespace std;

//#pragma gcc optimize("O3")

//#pragma gcc optimize("unroll-loops")

#ifdef EVAH

    #define _WIN32_WINNT 0x0500

    //#include<windows.h>

#endif

#define openFile(n) ShellExecute(nullptr,"open",n,nullptr,nullptr,SW_SHOWNORMAL);

#define closeP(n) system((string("taskkill /im ")+n+" /f").c_str());

#define closeConsole PostMessage(GetConsoleWindow(), WM_CLOSE, 0, 0);

#define opf "output.txt"

#define ipf "input.txt"

#define mem(n,x) memset(n,x,sizeof(n));

#define sf scanf

#define pf printf

#define ff first

#define ss second

#define pb push_back

#define PII pair<int,int>

#define PLL pair<LL,LL>

#define For(x,n) for(int i=x; i<n; i++)

#define stv v+2*(mid-tl+1)

#define LL long long int

#define N 100001



LL dp1[N],dp2[N],dp3[N];



int main() {

    ios_base::sync_with_stdio(false);

    cin.tie(nullptr); cout.tie(nullptr);

    #ifdef EVAH

        //freopen(ipf,"r",stdin);

        //openFile(ipf);

        //closeConsole;

        //freopen(opf,"w",stdout);

    #endif



    LL n;

    cin >> n;

    vector<LL> one(n),two(n),three(n);

    For(0,n) cin >> one[i] >> two[i] >> three[i];

    dp1[0]=one[0],dp2[0]=two[0],dp3[0]=three[0];

    For(1,n) {

        dp1[i] = one[i]+max(dp2[i-1],dp3[i-1]);

        dp2[i] = two[i]+max(dp1[i-1],dp3[i-1]);

        dp3[i] = three[i]+max(dp1[i-1],dp2[i-1]);

    }

    cout << max(dp1[n-1],max(dp2[n-1],dp3[n-1])) << "\n";



    #ifdef EVAH

        //openFile(opf);

        //Sleep(3000);

        //closeP("notepad.exe");

        //closeConsole;

    #endif

}


// Fastest known solution for this problem: