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



using namespace std;

typedef long long ll;



ll n,arr[100005][5],mem[100005][5];



ll dp(int pos, int last){

    if(pos==n)return 0;

    if(mem[pos][last]!=-1)return mem[pos][last];

    ll res=0;

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

        if(i!=last)res=max(res,dp(pos+1,i)+arr[pos][i]);

    }

    return mem[pos][last]=res;

}



int main(){

    ios_base::sync_with_stdio(0);

    cin.tie(0);cout.tie(0);

    cin>>n;

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

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

    }

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

    cout<<dp(0,3);

}

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



using namespace std;

typedef long long ll;



ll n,arr[100005][5],dp[100005][5];



int main(){

    ios_base::sync_with_stdio(0);

    cin.tie(0);cout.tie(0);

    cin>>n;

    for(int i=0;i<n;i++)cin>>arr[i][0]>>arr[i][1]>>arr[i][2];

    dp[0][0]=arr[0][0];dp[0][1]=arr[0][1];dp[0][2]=arr[0][2];

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

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

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

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

    }

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

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

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

#define debug(x)   cerr<<#x<<": "<<x<<endl;

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

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

#define endl       '\n'

#define MAX        1000000

typedef long long int ll;

using namespace std;



const ll mod = 1e9+7;





int n ;

vector<tuple<int,int,int>>arr;



int dp[100005][4];





int recur(int ind , int pre) {

    if(ind == n+1)return 0;



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



    int used_a =  0 , used_b = 0 ,  used_c = 0;

    if(pre!=1)

        used_a = get<0>(arr[ind]) + recur(ind + 1 , 1);

    if(pre!=2)

        used_b = get<1>(arr[ind]) + recur(ind + 1 , 2);

    if(pre!=3)

        used_c = get<2>(arr[ind]) + recur(ind + 1 , 3);

    return dp[ind][pre] = max({used_a , used_b , used_c});

}







void solve(){

    cin >>n;

    arr.resize(n + 1);

    for(int i=1;i<=n;i++)cin>>get<0>(arr[i])>>get<1>(arr[i])>>get<2>(arr[i]);



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



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

    return;

}











int main(){

    fastio

    int test=1;

    while(test--)

        solve();

    return 0;

}


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

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

typedef long long int ll;

using namespace std;



const ll     mod = 1e9+7;

const double eps = 1e-9;





int n;

const int N=1e5+10;

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



int rec(int ind,int p) {

    if(ind==n)

        return 0;

    int &cur=dp[ind][p+1];

    if(cur!=-1)return cur;

    int ans=0;

    ans=max({ans,(p!=0)*(a[ind][0]+rec(ind+1,0)),(p!=1)*(a[ind][1]+rec(ind+1,1)),(p!=2)*(a[ind][2]+rec(ind+1,2))});

    return cur=ans;

}



void solve(){

    cin >> n;

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

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

            dp[i][j]=-1;

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

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

    cout << rec(0 , -1) << "\n";

}





int main(){

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

    int t=1;

    while(t--)

        solve();

    return  0;

}
// Slower C++ solution:
//This is not my solution

#include<bits/stdc++.h>

using namespace std;

#define ll long long int

const int N=1e5+5;

const int inf=0x3f3f3f3f;

ll dp[3][N]={inf},v[3][N],k,n;



ll check(int index);





int main(){

    ios_base::sync_with_stdio(false);

    cin.tie(0); cout.tie(0);

ll i,j,m;

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



    cin>>n;

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

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

    for(auto it:{0,1,2})

        dp[it][0]=v[it][0];

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

        for(auto it:{0,1,2}){

            for(auto iit:{0,1,2}){

                if(it!=iit)

                    dp[it][i]=max(dp[it][i],v[it][i]+dp[iit][i-1]);

            }

        }

    }

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

    // for(auto it:{0,1,2})

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

    //         cout<<dp[it][i]<<" \n"[i==n-1];

    return 0;

}

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

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

typedef long long int ll;

using namespace std;



const ll     mod = 1e9+7;

const double eps = 1e-9;





int n;

const int N=1e5+10;

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



int rec(int ind,int p) {

    if(ind==n)

        return 0;

    int &cur=dp[ind][p+1];

    if(cur!=-1)return cur;

    int ans=0;

    ans=max({ans,(p!=0)*(a[ind][0]+rec(ind+1,0)),(p!=1)*(a[ind][1]+rec(ind+1,1)),(p!=2)*(a[ind][2]+rec(ind+1,2))});

    return cur=ans;

}



void solve(){

    cin >> n;

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

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

            dp[i][j]=-1;

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

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

    cout << rec(0 , -1) << "\n";

}





int main(){

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

    int t=1;

    while(t--)

        solve();

    return  0;

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

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

#define debug(x)   cerr<<#x<<": "<<x<<endl;

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

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

#define endl       '\n'

#define MAX        1000000

typedef long long int ll;

using namespace std;



const ll mod = 1e9+7;





int n ;

vector<tuple<int,int,int>>arr;



int dp[100005][4];





int recur(int ind , int pre) {

    if(ind == n+1)return 0;



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



    int used_a =  0 , used_b = 0 ,  used_c = 0;

    if(pre!=1)

        used_a = get<0>(arr[ind]) + recur(ind + 1 , 1);

    if(pre!=2)

        used_b = get<1>(arr[ind]) + recur(ind + 1 , 2);

    if(pre!=3)

        used_c = get<2>(arr[ind]) + recur(ind + 1 , 3);

    return dp[ind][pre] = max({used_a , used_b , used_c});

}







void solve(){

    cin >>n;

    arr.resize(n + 1);

    for(int i=1;i<=n;i++)cin>>get<0>(arr[i])>>get<1>(arr[i])>>get<2>(arr[i]);



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



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

    return;

}











int main(){

    fastio

    int test=1;

    while(test--)

        solve();

    return 0;

}


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

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

typedef long long int ll;

using namespace std;



const ll     mod = 1e9+7;

const double eps = 1e-9;





int n;

const int N=1e5+10;

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



int rec(int ind,int p) {

    if(ind==n)

        return 0;

    int &cur=dp[ind][p+1];

    if(cur!=-1)return cur;

    int ans=0;

    ans=max({ans,(p!=0)*(a[ind][0]+rec(ind+1,0)),(p!=1)*(a[ind][1]+rec(ind+1,1)),(p!=2)*(a[ind][2]+rec(ind+1,2))});

    return cur=ans;

}



void solve(){

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

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

    }

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

}





int main(){

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

    int t=1;

    while(t--)

        solve();

    return  0;

}
// Slower C++ solution:
//This is not my solution

#include<bits/stdc++.h>

using namespace std;

#define ll long long int

const int N=1e5+5;

const int inf=0x3f3f3f3f;

ll dp[3][N]={inf},v[3][N],k,n;



ll check(int index);





int main(){

    ios_base::sync_with_stdio(false);

    cin.tie(0); cout.tie(0);

ll i,j,m;

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



    cin>>n;

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

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

    for(auto it:{0,1,2})

        dp[it][0]=v[it][0];

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

        for(auto it:{0,1,2}){

            for(auto iit:{0,1,2}){

                if(it!=iit)

                    dp[it][i]=max(dp[it][i],v[it][i]+dp[iit][i-1]);

            }

        }

    }

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

    // for(auto it:{0,1,2})

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

    //         cout<<dp[it][i]<<" \n"[i==n-1];

    return 0;

}

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

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

typedef long long int ll;

using namespace std;



const ll     mod = 1e9+7;

const double eps = 1e-9;





int n;

const int N=1e5+10;

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



int rec(int ind,int p) {

    if(ind==n)

        return 0;

    int &cur=dp[ind][p+1];

    if(cur!=-1)return cur;

    int ans=0;

    ans=max({ans,(p!=0)*(a[ind][0]+rec(ind+1,0)),(p!=1)*(a[ind][1]+rec(ind+1,1)),(p!=2)*(a[ind][2]+rec(ind+1,2))});

    return cur=ans;

}



void solve(){

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

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

    }

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

}





int main(){

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

    int t=1;

    while(t--)

        solve();

    return  0;

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

#include <ext/pb_ds/assoc_container.hpp>

#define F first

#define S second

#define PB push_back

#define REP(i,a,b) for (int i = a ; i <= b ; i++)

#define REPI(i,a,b) for (int i = a ; i >= b ; i--)

using namespace __gnu_pbds;

using namespace std;



typedef long long ll;

typedef vector<int> vi;

typedef pair<int,int> pi;

typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> indexed_set;



const int OO = 1e9+7;

const int MOD = 1e9+7;

const int N = 1e5+7;



int mem[N][5];

int n;

int a[5][N];



int dp(int i,int t)

{

    int &ret = mem[i][t];

    if (ret != -1) return ret;

    if (i == n) return 0;

    ret = 0;

    REP(j,0,2)

    {

        if (j == t) continue;

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

    }

    return ret;

}



int main()

{

    //ios::sync_with_stdio(0);

    //cin.tie(0);cout.tie(0);

    //freopen("teleport.in","r",stdin);

    //freopen("teleport.out","w",stdout);



    memset(mem,-1,sizeof mem);

    scanf("%d",&n);

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

    printf("%d\n",dp(0,3));



    return 0;

}


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

#include <ext/pb_ds/assoc_container.hpp>

#define F first

#define S second

#define PB push_back

#define REP(i,a,b) for (int i = a ; i <= b ; i++)

#define REPI(i,a,b) for (int i = a ; i >= b ; i--)

using namespace __gnu_pbds;

using namespace std;



typedef long long ll;

typedef vector<int> vi;

typedef pair<int,int> pi;

typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> indexed_set;



const int OO = 1e9+7;

const int MOD = 1e9+7;

const int N = 1e5+7;



ll dp[N][5];

int n;

ll a[N][5];





int main()

{

    //ios::sync_with_stdio(0);

    //cin.tie(0);cout.tie(0);

    //freopen("teleport.in","r",stdin);

    //freopen("teleport.out","w",stdout);



    scanf("%d",&n);

    REP(i,0,n-1) scanf("%lld %lld %lld",&a[i][0], &a[i][1], &a[i][2]);



    REP(j,0,2) dp[0][j] = a[0][j];



    REP(i,1,n-1)

    {

        REP(j,0,2)

        {

            REP(k,0,2)

            {

                if (k==j) continue;

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

            }

        }

    }

    ll ans = 0;

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

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



    return 0;

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

using namespace std;

const int N = 1e5 + 1;

int n , a[N] , b[N] , c[N] , dp[N][4] , k;

int calc(int i , int op){

    if(i == n)

        return 0;

    int &ret = dp[i][op];

    if(ret != -1)

        return ret;

    ret = 0;

    if(op != 0){

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

    if(op != 1)

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

    if(op != 2)

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

    return ret;

    }

int main(){

    scanf("%d", &n );

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

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

    }

    memset(dp , -1 , sizeof dp);

    cout<<calc(0,3);

    return 0;

}

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

using namespace std;

const int N = 1e5 + 1;

int n , a[N] , b[N] , c[N] , dp[N][4] , k;

int main(){

    scanf("%d", &n );

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

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

    }

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

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

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

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

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

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

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

    }

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

    return 0;

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

using namespace std;

const int N = 1e5 + 1;

int n , a[N] , b[N] , c[N] , dp[N][4] , k;

int calc(int i , int op){

    if(i == n)

        return 0;

    int &ret = dp[i][op];

    if(ret != -1)

        return ret;

    ret = 0;

    if(op != 0){

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

    if(op != 1)

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

    if(op != 2)

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

    return ret;

    }

int main(){

    scanf("%d", &n );

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

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

    }

    memset(dp , -1 , sizeof dp);

    cout<<calc(0,3);

    return 0;

}

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



using namespace std;



const int N=1e5+10;

long long int n,arr[N][4],dp[N][4];



long long int calc(int day,int last){

    if (day>=n)

        return 0;

    if (dp[day][last])

        return dp[day][last];

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

        if (last==i)

            continue;

        dp[day][last]=max(arr[day][i]+calc(day+1,i),dp[day][last]);

    }

    return dp[day][last];

}



int main(){

    cin>>n;

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

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

            cin>>arr[i][j];

    cout<<(long long int)calc(0,-1);

    return 0;

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

using namespace std;

//vector<int> is passed by &

#define maxholidays 100000



int dp[maxholidays][3];

vector<vector<int>> joy;



int getMostJoyToDay(int D, int A)

{

	if(dp[D][A]!=-1)

		return dp[D][A];



	if(D==0)

		return joy[D][A];



	if(A==0)

		return dp[D][A] = joy[D][A] + max( getMostJoyToDay(D-1,1) , getMostJoyToDay(D-1,2) );

	else if(A==1)

		return dp[D][A] = joy[D][A] + max( getMostJoyToDay(D-1,0) , getMostJoyToDay(D-1,2) );

	else //(A==2)

		return dp[D][A] = joy[D][A] + max( getMostJoyToDay(D-1,0) , getMostJoyToDay(D-1,1) );

}





int main()

{

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

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

			dp[i][j]=-1;



	int n;	

	cin >> n ;

	int t;

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

	{

		vector<int> x;

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

		{

			cin >> t;

			x.push_back(t);

		}

		joy.push_back(x);

	}

	int a = getMostJoyToDay(n-1,0);

	int b = getMostJoyToDay(n-1,1);

	int c = getMostJoyToDay(n-1,2);

	cout << max(max(a,b),c) <<endl;



	return 0;

}

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

using namespace std;

//ask the right questions.		



int main() 

{

	int n,x;

	cin >> n;

	vector<vector<int>> v;

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

	{

		vector<int> p;

		cin >> x;

		p.push_back(x);

		cin >> x;

		p.push_back(x);

		cin >> x;

		p.push_back(x);

		v.push_back(p);

	}



	//max fun on day n obtainable if activity j=0,1,2 is done on that day 

	int dp[n+1][3];

	

	//trivial case:

	dp[0][0]=0;

	dp[0][1]=0;

	dp[0][2]=0;



	//base case:

	dp[1][0]=v[0][0];

	dp[1][1]=v[0][1];

	dp[1][2]=v[0][2];



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

	{

		//TIP: on ith day you could have done any of the 3 activites only, 

		//this fixes the activity done on prev day

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

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

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

	}

	

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

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

	cout << ans <<endl; 

	return 0;

	

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

#define ll long long int

#define mp make_pair

#define pb push_back

#define ff first 

#define ss second

#define mod 1000000007

#define lim 100005

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

using namespace std;



int main()

{

	int i,j,n,k,t,a[lim],b[lim],c[lim],dp[lim][3] = {};

	cin>>n;

	for(i=1; i<=n; i++) cin>>a[i]>>b[i]>>c[i];

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

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

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

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

	}

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

}

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

#define ull uint64_t

#define ll long long int

#define pb push_back

#define mp make_pair

#define ff first

#define ss second

#define mx5 100005

#define mx6 1000005

#define mod 1000000007

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

using namespace std;



int main() {

    nfs

    int i, j, n, fa, fb, fc, a, b, c;

    fa = fb = fc = 0;

    cin >> n;

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

        cin >> a >> b >> c;

        tie(fa, fb, fc) = make_tuple(a + max(fb, fc), b + max(fa, fc), c + max(fa, fb));

    }

    cout << max({fa, fb, fc});

}
// Slower C++ solution:
#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 PI acos(-1)

#define fscan1(n)  scanf("%d",&(n));

#define fscan2(n,m)  scanf("%d %d",&(n),&(m));

#define fscan3(n,m,k)  scanf("%d %d %d",&(n),&(m),&(k));



const int N = 1e5 + 100;

const int M = 2e5 + 100;

const ll oo = 1e18;



int n, m, k, t, q, h, a, b, c, x, y, z;

using namespace std;



int ar[N][3];

int dp[N][3];





int rec(int ind, int act){

	if(ind == n)return 0;



	int &ret = dp[ind][act];



	if(ret != -1)return ret;



	return ret = max(rec(ind + 1, (act + 1) % 3),rec(ind + 1, (act + 2) % 3)) + ar[ind][act];

}





int main(){

	

	cin >> n;

	memset(dp, -1, sizeof dp);

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

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

	}

	int ans = max(rec(0,0), max(rec(0, 1), rec(0, 2)));



	cout << ans << endl;

	



	return 0;

 }






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

#define fscan1(n)  scanf("%d",&(n));

#define fscan2(n,m)  scanf("%d %d",&(n),&(m));

#define fscan3(n,m,k)  scanf("%d %d %d",&(n),&(m),&(k));



const int N = 1e5 + 100;

const int M = 2e5 + 100;

const ll oo = 1e18;



int n, m, k, t, q, h, a, b, c, x, y, z;

using namespace std;



int ar[N][3];

int dp[N][3];





int main(){

	

	cin >> n;

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

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

	}

	

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

		if( i == 0){

			dp[0][0] = ar[0][0], dp[0][1] = ar[0][1], dp[0][2] = ar[0][2];

			continue;

		}

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

			dp[i][c] = max(dp[i- 1][(c + 1) % 3], dp[i - 1][(c + 2) % 3]) + ar[i][c];

		}

	}



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



	return 0;

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

using namespace std;



#define MAXN 100004



struct Day {

    int a, b, c;

};



vector<Day> vacation;

int dp[MAXN][4];



int solve(int n, int prev, int N) {

  if (n == N) return 0;

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

  int ans = 0;

  if (prev != 1) {

    ans = max(ans, solve(n+1, 1, N)+vacation[n].a);

  }

  if (prev != 2) {

    ans = max(ans, solve(n+1, 2, N)+vacation[n].b);

  }

  if (prev != 3) {

    ans = max(ans, solve(n+1, 3, N)+vacation[n].c);

  }

  return dp[n][prev] = ans;

}



int main() {

  ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);

#ifdef _DEBUG

  freopen("/Users/huogenx/Desktop/__RUN__/input.txt", "r", stdin);

#endif

  int N; cin >> N;

  vacation.resize(N);

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

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

  }

  memset(dp, -1, sizeof dp);

  cout << solve(0, 0, N) << "\n";

}

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

using namespace std;



#define MAXN 100004



struct Day {

    int a, b, c;

};



vector<Day> vacation;

int dp[MAXN][4];



int main() {

  ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);

#ifdef _DEBUG

  freopen("/Users/huogenx/Desktop/__RUN__/input.txt", "r", stdin);

#endif

  int N; cin >> N;

  vacation.resize(N);

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

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

  }

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

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

    dp[i][1] += vacation[i-1].a;



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

    dp[i][2] += vacation[i-1].b;



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

    dp[i][3] += vacation[i-1].c;

  }

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

}