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

using namespace std;

int ara[100005][3];

int dp[100005][3];



int func(int pos,int des,int idx){

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

    int res;

    if(pos>des) res=0;

    else{

        int t1,t2;

        if(idx==0){

            t1=ara[pos][0]+func(pos+1,des,1);

            t2=ara[pos][0]+func(pos+1,des,2);

        }

        else if(idx==1){

             t1=ara[pos][1]+func(pos+1,des,0);

            t2=ara[pos][1]+func(pos+1,des,2);

        }

        else{

             t1=ara[pos][2]+func(pos+1,des,1);

            t2=ara[pos][2]+func(pos+1,des,0);

        }

        res=max(t1,t2);

    }

    dp[pos][idx]=res;

    return res;

}

int main(){

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

    int n,x,y,z;

    cin >> n;

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

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

    }

    x=func(1,n,0);

    y=func(1,n,1);

    z=func(1,n,2);

    int ans;

    

    ans=max(x,y);

    ans=max(ans,z);

    

    cout << ans << endl;

    

}

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

#include<stdio.h>

#include<algorithm>

#include<vector>

#include<cstring>

#include<set>

#include<queue>

#include<map>

#include<math.h>

#include<climits>



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

#define ll long long

#define dd double

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

#define scd(n)        scanf("%lf",&n)

#define scc(n)        scanf("%c",&n)

#define scs(n)        scanf("%s",&n)

#define ptl(n)        printf("%lld",n)

#define ptd(n)        printf("%f",n)

#define ptc(n)        printf("%c",n)

#define pts(n)        printf("%s",n)

#define nline         printf("\n")

#define pb            push_back

#define FOR(i,a,b)    for(ll i=a;i<=b;i++)

#define FORr(i,a,b)   for(ll i=a;i>=b;i--)

#define pi            2*acos(0.0)

#define pii           pair<ll,ll>

#define uu            first

#define vv            second



using namespace std;

ll n,a[100005],b[100005],c[100005],dp[100010][3],op0,op1,op2;

int main()

{

    scl(n);

    FOR(i,1,n)

    {

        scl(a[i]);

        scl(b[i]);

        scl(c[i]);

    }

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

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

    dp[n][2]=c[n];

    FORr(i,n-1,1)

    {

        if(i+1<=n)

        {

            op0=dp[i+1][0];

            op1=dp[i+1][1];

            op2=dp[i+1][2];

        }

        else

        {

            op0=LONG_MIN;

            op1=LONG_MIN;

            op2=LONG_MIN;

        }

        dp[i][0]=max(op1+a[i],op2+a[i]);

        dp[i][1]=max(op0+b[i],op2+b[i]);

        dp[i][2]=max(op0+c[i],op1+c[i]);

       // cout<<dp[i][0]<<endl;

    }

    ll ans=max(dp[1][0],dp[1][1]);

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

    ptl(ans);

    return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#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])));

}


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

#include<stdio.h>

#include<algorithm>

#include<vector>

#include<cstring>

#include<set>

#include<queue>

#include<map>

#include<math.h>

#include<climits>



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

#define ll long long

#define dd double

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

#define scd(n)        scanf("%lf",&n)

#define scc(n)        scanf("%c",&n)

#define scs(n)        scanf("%s",&n)

#define ptl(n)        printf("%lld",n)

#define ptd(n)        printf("%f",n)

#define ptc(n)        printf("%c",n)

#define pts(n)        printf("%s",n)

#define nline         printf("\n")

#define pb            push_back

#define FOR(i,a,b)    for(ll i=a;i<=b;i++)

#define FORr(i,a,b)   for(ll i=a;i>=b;i--)

#define pi            2*acos(0.0)

#define pii           pair<ll,ll>

#define uu            first

#define vv            second



using namespace std;

ll n,a[100005],b[100005],c[100005],dp[100010][3],op0,op1,op2;

int main()

{

    scl(n);

    FOR(i,1,n)

    {

        scl(a[i]);

        scl(b[i]);

        scl(c[i]);

    }

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

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

    dp[n][2]=c[n];

    FORr(i,n-1,1)

    {

        if(i+1<=n)

        {

            op0=dp[i+1][0];

            op1=dp[i+1][1];

            op2=dp[i+1][2];

        }

        else

        {

            op0=LONG_MIN;

            op1=LONG_MIN;

            op2=LONG_MIN;

        }

        dp[i][0]=max(op1+a[i],op2+a[i]);

        dp[i][1]=max(op0+b[i],op2+b[i]);

        dp[i][2]=max(op0+c[i],op1+c[i]);

       // cout<<dp[i][0]<<endl;

    }

    ll ans=max(dp[1][0],dp[1][1]);

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

    ptl(ans);

    return 0;

}


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

#include <iostream>

#include <iomanip>

#include <map>

#include <cmath>

#include <algorithm>

#include <string>

#include <vector>

#include <queue>

#include <set>

#include <deque>

#include <stack>

#include <bitset>

#include <fstream>

#include <map>

#include <unordered_map>



using namespace std;

typedef int me;

typedef long long ll;

typedef unsigned long long ull;



#define andrew ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)

#define endl '\n'

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

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

#define PI 3.1415926535

#define sin(i) (long double)sin(i *PI / 180.0)

#define cos(i) (long double)cos(i * PI/ 180.0)



ll gcd(ll x, ll y) { return (y == 0) ? x : gcd(y, x % y); }

ull lcm(ll x, ll y) { return x / gcd(x, y) * y; }



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

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

int id8[] = { 0, 1, 2, 3, 4, 5, 6, 7 };

struct happ

{

    int a, b, c;

};

int n;

vector<happ>v;

ll mem[100005][4];

ll solve(int i, int last)

{

    if (i == n+1)

        return 0;



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

    

    if (ret != -1)

        return ret;



    if (last != 1)

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

    if (last != 2)

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

    if (last != 3)

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



    return ret;

}

int main()

{

    andrew;

    cin >> n;

    v.resize(n + 1);



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

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



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

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

            mem[i][j] = -1;



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







}


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

using namespace std;

long long dp[100005][5];

int main()

{

     int n,x,y,z,p,q,i;

     cin>>n;

     cin>>dp[1][1]>>dp[1][2]>>dp[1][3];

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

     {

            cin>>x>>y>>z;

            dp[i][1]=x+max(dp[i-1][2],dp[i-1][3]);

            dp[i][2]=y+max(dp[i-1][1],dp[i-1][3]);

            dp[i][3]=z+max(dp[i-1][2],dp[i-1][1]);

     }

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

       return 0;

}


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

#include <ext/pb_ds/assoc_container.hpp>

#include <ext/pb_ds/tree_policy.hpp>



using namespace std;

using namespace __gnu_pbds;



template <typename T>

using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;



typedef long long ll;

typedef pair<ll, ll> pll;

typedef pair<int, int> pii;

typedef complex<double> point;



#define X real()

#define Y imag()

#define angle(a) (atan2((a).imag(), (a).real()))

#define vec(a, b) ((b) - (a))

#define same(p1, p2) (dp(vec(p1, p2), vec(p1, p2)) < EPS)

#define dp(a, b) ((conj(a) * (b)).real())

#define cp(a, b) ((conj(a) * (b)).imag())

#define length(a) (hypot((a).imag(), (a).real()))

#define normalize(a) (a) / length(a)

#define rotateO(p, ang) ((p)*exp(point(0, ang)))

#define rotateA(p, ang, about) (rotateO(vec(about, p), ang) + about)

#define reflectO(v, m) (conj((v) / (m)) * (m))

#define clr(a, b) memset(a, b, sizeof(a));



const ll N = 100005;

const int MOD = 1e9 + 7;

const double EPS = 1e-7;

const double PI = acos(-1.0);



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

ll memo[N][4];



ll solve(ll i, ll j)

{

	if(i == n)

		return 0;



	ll& ret = memo[i][j];



	if(~ret)

		return ret;



	ret = 0;



	if(j != 0)

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

	if(j != 1)

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

	if(j != 2)

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

	return ret;

}

int main()

{

    ios::sync_with_stdio(false);

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

    clr(memo, -1);

    cin >> n;

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

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

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

}

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



using namespace std;



typedef long long ll;

typedef pair<ll, ll> pll;

typedef pair<int, int> pii;



const ll N = 100005;

const ll INF = 1e9;

const int MOD = 1e9 + 7;

const double EPS = 1e-7;

const double PI = acos(-1.0);



struct E {

    ll a, b, c;

} arr[N];



ll n, dp[N][3];



int main() {

    ios::sync_with_stdio(false);

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

    cin >> n;

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

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

    

    dp[0][0] = arr[0].a;

    dp[0][1] = arr[0].b;

    dp[0][2] = arr[0].c;



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

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

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

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

    }

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

}






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

using namespace std;



int n, a[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 c = 0;

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

        if (k != j)

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

    return dp[i][j] = c;

}



int main()

{

    memset(dp, -1, sizeof dp);

    cin >> n;

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

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

        cin >> a[i][j];

    cout << solve(0, 4);

    return 0;

}

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

using namespace std;



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

#define wtf(s) freopen((s), "w", stdout)

#define rff(s) freopen((s), "r", stdin)

#define pi (2 * acos(0))

typedef long double ld;

typedef long long ll;

typedef complex<ld> point;

#define endl '\n'

#define matrix vector<vector<ll>>

#define EPS 1e-9

#define X real()

#define Y imag()

#define vec(a, b) ((b) - (a))

#define dot(a, b) ((conj(a) * (b)).real())

#define cross(a, b) ((conj(a) *(b)).imag())

#define lengthsqr(v) (dot(v, v))

#define length(v) ((ld)hypot((v).Y, (v).X))

#define same(a, b) (lengthsqr(vec(a, b)) < EPS)



const int N = 1e5 + 5, inf = 1e9 + 5;

const ll mod = 1e9 + 7;



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



int main()

{

    VIC

    int n;

    cin >> n;

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

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

            cin >> a[i][j];

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

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

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

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

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

            {

                if (j == k)

                    continue;

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

            }

    int ans = 0;

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

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

    cout << ans;

    return 0;

}


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

using namespace std;



int dp[100001][4];



int n;

void fillarray()

{

	int i,j;

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

	{

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

		dp[i][j] = -1;

	}

}

int solve(int a[] , int b[], int c[],  int i , int last)

{

	if(i == n)

	return 0;



	

	if(dp[i][last] >= 0)

	return dp[i][last];

	

	int val = 0;

	

	if(last != 1)

	val = max(val ,  a[i] + solve(a , b , c, i + 1 , 1));

	

	if(last != 2)

	val = max( val , b[i] + solve(a ,b ,c  , i + 1 , 2));

	

	if(last != 3)

	val = max( val ,c[i] + solve(a , b , c , i + 1 , 3));

	

	dp[i][last] = val;

	return val;

}

main()

{

	cin>>n;

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

	int i,j;

	

	fillarray();

	

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

	{

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

	}

	

	cout<<solve(a , b , c, 0,0);

	

}

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

#include<vector>

#include<limits.h>

#define ll long long

using namespace std;

int main()

{

	int n;

	cin>>n;

	

	

	int i,j,k;

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

	

	int dp[n][3];

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

	{

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

	}

	

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

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

	dp[0][2] = c[0];

	

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

	{

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

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

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

	}

	

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

	

}

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

using namespace std;



const int MAXN = 1e5+5;

const int INF = 1e9+7;

int dp[MAXN][4],a[MAXN][4];

// dp[i][j] ==> The maximum we can get if we do [1..i] and do type j activity on day i



int main()

{

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

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

            dp[i][j] = INF;

        }

    }

    int n;cin >> n;

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

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

            cin >> a[i][j];

        }

    }

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

        // Choose Swimming

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

    }

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

}


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

using namespace std;



const int MAXN = 1e5+5;

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



int main()

{

    int n;cin >> n;

    vector<int> dp(3);



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

        vector<int> new_dp(3);

        vector<int> c(3);

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

            cin >> c[j];

        }

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

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

                if(i==j) continue;

                new_dp[i] = max(new_dp[i],dp[j]);

            }

            new_dp[i] += c[i];

        }

        dp = new_dp;

    }



    int maxs=0;

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

        maxs = max(maxs,dp[i]);

    }

    cout << maxs;

}


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

#include <stdio.h>

#include <vector>

#include <string>

#include <set>

#include <queue>

#include <cmath>

#include <map>

#include <algorithm>

#include <stack>

#include <cstring>

 

#define ll long long

#define MP make_pair

#define PB push_back

#define P push

#define f first

#define se second

#define S size()

#define B begin()

#define E end()

#define rB rbegin()

#define rE rend()

#define PI acos(-1)



 

const int N = 1e5 + 100;

const int M = 1e6 + 100;

const ll oo = 1e18;

 

ll n, m, k, t, q, h, a, b, d, c, e, w, x, y, z;

using namespace std;





int ai[N], bi[N], ci[N], dp[N][3];





int calc(int n, int las) {



	if(n == m) return 0;



	int &r = dp[n][max(las, 0)];



	if(r != -1) return r;





	int x = -1, y = -1, f = -1;



	if(las != 0){

		x = calc(n + 1, 0) + ai[n];

	}

	if(las != 1){

		y = calc(n + 1, 1) + bi[n];

	}

	if(las != 2){

		f = calc(n + 1, 2) + ci[n];

	}



	return r = max({x, y, f});

}





int main() {



	memset(dp, -1, sizeof dp);



	cin >> m;

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

		cin >> ai[i] >> bi[i] >> ci[i];

	}

	

	cout << calc(0, -1);







    

    return 0;

}






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

#include <stdio.h>

#include <vector>

#include <string>

#include <set>

#include <queue>

#include <cmath>

#include <map>

#include <algorithm>

#include <stack>

#include <cstring>

 

#define ll long long

#define MP make_pair

#define PB push_back

#define P push

#define f first

#define se second

#define S size()

#define B begin()

#define E end()

#define rB rbegin()

#define rE rend()

#define PI acos(-1)



 

const int N = 1e5 + 100;

const int M = 1e6 + 100;

const ll oo = 1e18;

 

ll n, m, k, t, q, h, a, b, d, c, e, w, x, y, z;

using namespace std;





int ai[N], bi[N], ci[N], dp[N][3];









int main() {



	cin >> n;

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

		cin >> ai[i] >> bi[i] >> ci[i];

	}







	

	dp[0][0] = ai[0], dp[0][1] = bi[0], dp[0][2] = ci[0];





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

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



			if(j == 0){

				dp[i][j] = max(dp[i - 1][1] + ai[i], dp[i - 1][2] + ai[i]);

			}

			if(j == 1){

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

			}

			if(j == 2){

				dp[i][j] = max(dp[i - 1][0] + ci[i], dp[i - 1][1] + ci[i]);

			}

			

		}

	}





	



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

    

    return 0;

}






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



using namespace std;



typedef long long ll;

typedef vector <int> vi;

typedef vector <ll> vl;

typedef pair <ll, ll> dl;

typedef pair <int, int> ii;

typedef vector <ii> vii;

typedef vector <vi> graph;



int n;

ll mat[100010][3];

ll dp[100010][3];



ll solve(int r, int c) {

	if (r == n - 1) {

		return mat[r][c];

	}

	if (dp[r][c] != -1) return dp[r][c];

	ll temp, mayor = -1;

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

	{

		if (i == c) continue;

		temp = solve(r + 1, i);

		mayor = temp > mayor ? temp : mayor;

	}

	return dp[r][c] = mat[r][c] + mayor;

}



int main() {

    ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);



	cin >> n;

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

	{

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

		{

			cin >> mat[i][j];

		}

	}

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

	{

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

		{

			dp[i][j] = -1;

		}

	}

	ll temp, mayor = -1;

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

	{

		temp = solve(0, i);

		mayor = temp > mayor ? temp : mayor;

	}

	cout << mayor << "\n";

    return 0;

}


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



using namespace std;



typedef long long ll;

typedef vector <int> vi;

typedef vector <ll> vl;

typedef pair <ll, ll> dl;

typedef pair <int, int> ii;

typedef vector <ii> vii;

typedef vector <vi> graph;



int n;

ll mat[100010][3];



/*ll solve(int r, int c) {

	if (r == n - 1) {

		return mat[r][c];

	}

	if (dp[r][c] != -1) return dp[r][c];

	ll temp, mayor = -1;

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

	{

		if (i == c) continue;

		temp = solve(r + 1, i);

		mayor = temp > mayor ? temp : mayor;

	}

	return dp[r][c] = mat[r][c] + mayor;

}*/



int main() {

    ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);



	cin >> n;

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

	{

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

		{

			cin >> mat[i][j];

		}

	}

	vector <long long> arr1(3), arr2(3);

	arr1[0] = mat[0][0];

	arr1[1] = mat[0][1];

	arr1[2] = mat[0][2];

	arr2 = arr1;

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

	{

		arr2[0] = mat[i][0] + max(arr1[1], arr1[2]);

		arr2[1] = mat[i][1] + max(arr1[0], arr1[2]);

		arr2[2] = mat[i][2] + max(arr1[1], arr1[0]);

		if (i < n - 1) arr1.swap(arr2);

	}

	ll mayor = max(arr2[0], max(arr2[1], arr2[2]));

	cout << mayor << "\n";

    return 0;

}


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

#include<string>

#include<cstdio>

#include<algorithm>

#include<cstdlib>

#include<cmath>

#include<vector>

#include<utility>

#include<queue>

#include<stack>

#include<set>

#include<map>

#include<unordered_set>

#include<bitset>

using namespace std;

//#define MODE 1

#ifdef MODE

#define DEB(X) cout<< #X <<": "<<X<<" ";

#define ARDEB(i,X) cout<< #X <<"["<<i<<"]: "<<X[i]<<" ";

#define END cout<<endl;

#else

#define DEB(X) {}

#define ARDEB(i,X) {}

#define END {}

#endif

typedef long long int ll;

#define int long long

typedef pair<int,int> P;

struct edge{int to,cost;};

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

const int INF=100000000000000000;



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

int dp[100010][3];



int DP(int a,int k){

	if(dp[a][k]!=-1)return dp[a][k];

	int res=0,res2=0,res3=0;

	if(a==N){

		return 0;

	}

	DEB(k)END

	while(0);

	if(k==0){

		res2=DP(a+1,1)+B[a+1];

		res3=DP(a+1,2)+C[a+1];

	}

	else if(k==1){

		res=DP(a+1,0)+A[a+1];

		res3=DP(a+1,2)+C[a+1];

	}

	else if(k==2){

		res=DP(a+1,0)+A[a+1];

		res2=DP(a+1,1)+B[a+1];

	}

	return dp[a][k]=max({res,res2,res3});

}



signed main(){

	cin>>N;

	REP(i,N){

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

	}

	REP(i,100010)REP(j,3){

		dp[i][j]=-1;

	}

	cout<<max({DP(0,0)+A[0],DP(0,1)+B[0],DP(0,2)+C[0]})<<endl;

	return 0;

}


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

#include<string>

#include<cstdio>

#include<algorithm>

#include<cstdlib>

#include<cmath>

#include<vector>

#include<utility>

#include<queue>

#include<stack>

#include<set>

#include<map>

#include<unordered_set>

#include<unordered_map>

#include<bitset>

#include<iomanip>

#include<list>

#include<deque>

using namespace std;

//#define MODE 1

#ifdef MODE

#define DEB(X) cout<< #X <<": "<<X<<" ";

#define END cout<<endl;

#else

#define DEB(X) {}

#define END {}

#endif

typedef long long ll;

#define int ll

#define uset unordered_set

#define umap unordered_map

typedef std::pair<int,int> P;

struct edge{int to,cost;};

const int INF=100000000000000000;

const int INF2=9223372036854775807;

const int MOD=1000000007;

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

#define min(X,Y) (((int)(X)<(int)(Y))?(X):(Y))

#define max(X,Y) (((int)(X)>(int)(Y))?(X):(Y))

#define MAX(X,Y) (*max_element(X,Y))

#define MIN(X,Y) (*min_element(X,Y))

#define NP(X,Y) next_permutation(X,Y)

#define setp(X,Y) cout<<fixed<<setprecision(Y)<<X;

int ceil2(int a,int b){if(a%b==0){return a/b;}else{return a/b+1;}}

int pow2(int a,int b){int r=1;for(int i=1;i<=b;i++){r*=a;}return r;}

int Log2(int a){int t=0;while(1){if(a==0||a==1){break;}a/=2;t++;}return t;}



int N;

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

int dp[100010];

int dp2[100010];

int dp3[100010];



signed main(){

	cin>>N;

	REP(i,N){

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

	}

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

		dp[i]=max(dp2[i-1],dp3[i-1])+A[i];

		dp2[i]=max(dp[i-1],dp3[i-1])+B[i];

		dp3[i]=max(dp[i-1],dp2[i-1])+C[i];

	}

	cout<<max(dp[N-1],max(dp2[N-1],dp3[N-1]))<<endl;

	return 0;

}


// Fastest known solution for this problem: