stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#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);

}

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

#define INF 0x3f3f3f3f

using namespace std;

const int maxn = 1e6+10;

int a[maxn][3];

int dp[maxn][3];

int main()

{

    int n;

    scanf("%d",&n);

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

    {

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

        {

            scanf("%d",a[i]+j);

        }

    }

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

    {

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

        {

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

            {

                if(j!=k)

                {

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

                }

            }

        }

    }

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

    {

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

        {

            printf("%d ",dp[i][j]);

        }

        printf("\n");

    }*/

    int ans=0;

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

    {

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

    }

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

    return 0;

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

}

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

#define INF 0x3f3f3f3f

using namespace std;

const int maxn = 1e6+10;

int a[maxn][3];

int dp[maxn][3];

int main()

{

    int n;

    scanf("%d",&n);

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

    {

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

        {

            scanf("%d",a[i]+j);

        }

    }

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

    {

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

        {

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

            {

                if(j!=k)

                {

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

                }

            }

        }

    }

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

    {

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

        {

            printf("%d ",dp[i][j]);

        }

        printf("\n");

    }*/

    int ans=0;

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

    {

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

    }

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

    return 0;

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

}

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

#define INF 0x3f3f3f3f

using namespace std;

const int maxn = 1e6+10;

int a[maxn][3];

int dp[maxn][3];

int main()

{

    int n;

    scanf("%d",&n);

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

    {

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

        {

            scanf("%d",a[i]+j);

        }

    }

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

    {

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

        {

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

            {

                if(j!=k)

                {

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

                }

            }

        }

    }

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

    {

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

        {

            printf("%d ",dp[i][j]);

        }

        printf("\n");

    }*/

    int ans=0;

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

    {

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

    }

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

    return 0;

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

}

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

#define INF 0x3f3f3f3f

using namespace std;

const int maxn = 1e6+10;

int a[maxn][3];

int dp[maxn][3];

int main()

{

    int n;

    scanf("%d",&n);

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

    {

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

        {

            scanf("%d",a[i]+j);

        }

    }

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

    {

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

        {

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

            {

                if(j!=k)

                {

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

                }

            }

        }

    }

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

    {

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

        {

            printf("%d ",dp[i][j]);

        }

        printf("\n");

    }*/

    int ans=0;

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

    {

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

    }

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

    return 0;

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

#include <algorithm>

#include <cstring>

const int maxn = 3e5 + 5;

using namespace std;



int N;

int a[maxn][4];

int dp[maxn][4];

int main()

{

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

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

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

	}

	int ans = 0;

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

		ans = max(dp[N][i], ans);

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

	return 0;	

}

// Faster C++ solution for the same problem (measured speedup: 1.5003874898235863x):
#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;

}
// Slower C++ solution:
/*

** author:mahmoud magdy

*/

#include <bits/stdc++.h>

using namespace std;



#define ll long long

#define endl "\n"



inline int D()

{

    int m ;

    cin>>m;

    return m ;

}

inline ll lD()

{

    ll m ;

    cin>>m;

    return m ;

}

inline double dD()

{

    double m;

    cin>>m;

    return m;

}

void fast()

{

    ios_base :: sync_with_stdio( 0 ) ;

    cin.tie( 0 ) ;

    cout.tie( 0 ) ;

}

int n;

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

ll dp[100005][5];

ll solve(int i,int prv){

 if(i==n)return 0;

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

 ll x=0;

 if(prv!=1){

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

 }

 ll y=0;

 if(prv!=2)

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

 ll z=0;

 if(prv!=3)

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

 return dp[i][prv]=max({x,y,z});

}

int main()

{

    memset(dp,-1,sizeof dp);

    fast();

    n=D();

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

        a[i]=D();b[i]=D();c[i]=D();

    }

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

}

// Faster C++ solution for the same problem (measured speedup: 1.497598502529832x):
#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;

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

}

// Faster C++ solution for the same problem (measured speedup: 1.4985136163445538x):
#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;

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

#include <algorithm>

#include <cstring>

const int maxn = 3e5 + 5;

using namespace std;



int N;

int a[maxn][4];

int dp[maxn][4];

int main()

{

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

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

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

	}

	int ans = 0;

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

		ans = max(dp[N][i], ans);

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

	return 0;	

}

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

#define ll long long

using namespace std;

#define inf 0x3f3f3f3f 

int main(){

	ll t , a , b , c , dp[3] = {0};

    cin >> t ;

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

        cin >> a >> b >> c;

        ll aa , bb , cc;

        aa = max(dp[1] + a , dp[2] + a);

        bb = max(dp[0] + b , dp[2] + b);

        cc = max(dp[0] + c , dp[1] + c);

        dp[0] = aa; dp [ 1] = bb ; dp[2] = cc;

    }

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

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

using namespace std;

#define db(x) cerr<<__LINE__<<": "<<#x<<" = "<<(x)<<'\n'

const int N = 1e5;

int A[N+7][5], mem[N+7][5];



int dp(int i, int j) {

	if(i==0) return 0;

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

	if(j==1) return mem[i][j] = A[i][j] + max(dp(i-1, 2), dp(i-1, 3));

	if(j==2) return mem[i][j] = A[i][j] + max(dp(i-1, 3), dp(i-1, 1));

	else return mem[i][j] = A[i][j] + max(dp(i-1, 1), dp(i-1, 2));

}

int main(){

	ios_base::sync_with_stdio(0);

	int a,b,n,T,cas=0;

	cin>>n;

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

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

	}

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

	int res = max(dp(n,1), max(dp(n,2), dp(n,3)));

	cout<<res<<endl;

	return 0;

}

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

#define ll long long

using namespace std;

#define inf 0x3f3f3f3f 

int main(){

	ll t , a , b , c , dp[3] = {0};

    cin >> t ;

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

        cin >> a >> b >> c;

        ll aa , bb , cc;

        aa = max(dp[1] + a , dp[2] + a);

        bb = max(dp[0] + b , dp[2] + b);

        cc = max(dp[0] + c , dp[1] + c);

        dp[0] = aa; dp [ 1] = bb ; dp[2] = cc;

    }

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

}
// Slower C++ solution:
/*|In The Name Of Allah|*/



#include <bits/stdc++.h>



using namespace std;



const int N = 1e5+5;



int n;

int a[N][3];

int mem[N][3];



int go(int idx , int lst){

    if(idx == n + 1)

        return 0;

    if(~mem[idx][lst])

        return mem[idx][lst];

    int best = 0;

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

        if(idx == 1 || lst != i){

            best = max(best , go(idx + 1 , i) + a[idx][i]);

        }

    }

    return mem[idx][lst] = best;

}



int main(){

    scanf("%d" , &n);

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

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

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

        }

    }

    memset(mem , -1 , sizeof mem);

    printf("%d\n" , go(1 , 0));

}


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

#define ll long long

using namespace std;

#define inf 0x3f3f3f3f 

int main(){

	ll t , a , b , c , dp[3] = {0};

    cin >> t ;

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

        cin >> a >> b >> c;

        ll aa , bb , cc;

        aa = max(dp[1] + a , dp[2] + a);

        bb = max(dp[0] + b , dp[2] + b);

        cc = max(dp[0] + c , dp[1] + c);

        dp[0] = aa; dp [ 1] = bb ; dp[2] = cc;

    }

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

}
// 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;

//const int mod = 1e9+9;

/*ll gcd(ll x, ll y){return (y ? gcd(y, x%y) : x); }

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

*void exEuclid(int a, int b){

	if(b==0){x = 1; y=0; d=a; return ;}

	exEuclid(b, a%b);

	int x1 = y;

	int y1 = x-(a/b)*y;

	x = x1; 100000

	y = y1;

} 

* */

//===========================================================//

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

#define ll long long

using namespace std;

#define inf 0x3f3f3f3f 

int main(){

	ll t , a , b , c , dp[3] = {0};

    cin >> t ;

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

        cin >> a >> b >> c;

        ll aa , bb , cc;

        aa = max(dp[1] + a , dp[2] + a);

        bb = max(dp[0] + b , dp[2] + b);

        cc = max(dp[0] + c , dp[1] + c);

        dp[0] = aa; dp [ 1] = bb ; dp[2] = cc;

    }

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

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

using namespace std;



int m;

const int mx = 1e5+5;

int tasks[mx][3];

int diario[3][mx];

int cosa(int a, int i){

	if(i == m){

     return 0;

     }



	if(diario[a][i] != -1) {

        return diario[a][i];

    }

	int& ans = diario[a][i];



        if(a==0){

		ans=max(ans, cosa(1, i+1)+tasks[i][0]);

		ans=max(ans, cosa(2, i+1)+tasks[i][0]);

	}

        if(a==1){

		ans=max(ans, cosa(0, i+1)+tasks[i][1]);

		ans=max(ans, cosa(2, i+1)+tasks[i][1]);

        }

	if(a==2){

		ans=max(ans, cosa(0, i+1)+tasks[i][2]);

		ans=max(ans, cosa(1, i+1)+tasks[i][2]);

	}



	return ans;

}

int main(){

	cin>>m;

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

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

	}

	memset(diario, -1, sizeof diario);

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

	return 0;

}


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

#define ll long long

using namespace std;

#define inf 0x3f3f3f3f 

int main(){

	ll t , a , b , c , dp[3] = {0};

    cin >> t ;

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

        cin >> a >> b >> c;

        ll aa , bb , cc;

        aa = max(dp[1] + a , dp[2] + a);

        bb = max(dp[0] + b , dp[2] + b);

        cc = max(dp[0] + c , dp[1] + c);

        dp[0] = aa; dp [ 1] = bb ; dp[2] = cc;

    }

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

}
// 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 nn;

const int nxx = 1e5+5;

int acc[nxx][3];

int dpp[3][nxx];

int go(int aa, int ii)

{

	if(ii==nn) return 0;

	if(dpp[aa][ii] != -1) return dpp[aa][ii];

	int& ans = dpp[aa][ii];

	if(aa==0)

	{

		ans=max(ans, go(1, ii+1)+acc[ii][0]);

		ans=max(ans, go(2, ii+1)+acc[ii][0]);

	}

	if(aa==1)

	{

		ans=max(ans, go(0, ii+1)+acc[ii][1]);

		ans=max(ans, go(2, ii+1)+acc[ii][1]);

	}

	if(aa==2)

	{

		ans=max(ans, go(0, ii+1)+acc[ii][2]);

		ans=max(ans, go(1, ii+1)+acc[ii][2]);

	}



	return ans;



}

int main()

{

    cin>>nn;

    for(int ii=0; ii<nn; ii++) cin>>acc[ii][0]>>acc[ii][1]>>acc[ii][2];

    memset(dpp, -1, sizeof dpp);

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

#define ll long long

using namespace std;

#define inf 0x3f3f3f3f 

int main(){

	ll t , a , b , c , dp[3] = {0};

    cin >> t ;

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

        cin >> a >> b >> c;

        ll aa , bb , cc;

        aa = max(dp[1] + a , dp[2] + a);

        bb = max(dp[0] + b , dp[2] + b);

        cc = max(dp[0] + c , dp[1] + c);

        dp[0] = aa; dp [ 1] = bb ; dp[2] = cc;

    }

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

}