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

#include <queue>

#include <set>

#include <list>

#include <deque>

#include <vector>

#include <map>

#include <utility>

#include <algorithm>

#include <cmath>

#include <cstdio>

#include <string>

#include <iterator>

#define ll long long

#define pb push_back

#define pf push_front

#define ppb pop_back

#define ppf pop_front

#define powr(x,n,p) for(int axy=0 ; axy<p ; axy++){x=x*n;}

#define YES printf("YES\n")

#define Yes printf("Yes\n")

#define yes printf("yes\n")

#define NO printf("NO\n")

#define No printf("No\n")

#define no printf("no\n")

#define nl printf("\n")

#define fr0(n,q) for(q=0 ; q<n ; q++)

#define fr1(n,q) for(q=1 ; q<=n ; q++)

#define mp make_pair

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

#define sci(x) scanf("%d",&x)

#define lst(x) x[x.size()-1]

#define llst(x) x[x.size()-2]

#define md 1000000007

#define check(x,y) (x&(1<<y))

#define set(x,y) x=x|(1<<y)

using namespace std;

int n;

int arr[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 ret;

    if(j==0) ret=max(arr[i][j]+solve(i+1,1),arr[i][j]+solve(i+1,2));

    else if(j==1) ret=max(arr[i][j]+solve(i+1,0),arr[i][j]+solve(i+1,2));

    else ret=max(arr[i][j]+solve(i+1,0),arr[i][j]+solve(i+1,1));

    return dp[i][j]=ret;

}

int main()

{

        sci(n);

        int i,j;

        fr0(100005,i){

            fr0(3,j) dp[i][j]=-1;

        }

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

            for(j=0 ; j<3 ; j++) sci(arr[i][j]);

        }

        int ans=max(solve(1,0),solve(1,1));

        ans=max(ans,solve(1,2));

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

    return 0;

}

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

using namespace std;



int main()

{

	int n;

	scanf("%d",&n);

	

	int a,b,c;

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

	

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

	dp[0][0]=a,dp[0][1]=b,dp[0][2]=c;

	

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

	{

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

		

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

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

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

	}

	

	printf("%d\n",*max_element(dp[n-1].begin(),dp[n-1].end()));

	

	return 0;

}


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

using namespace std;

int a[100001][3];

int dp[100001][3];

int n;



int func(int ind, int flag) {



	if (ind == n) return 0;



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



	int maxi = 0;

	if (flag == 0) {

		maxi = max(maxi, a[ind][1] + func(ind + 1, 1));

		maxi = max(maxi, a[ind][2] + func(ind + 1, 2));

		maxi = max(maxi, a[ind][3] + func(ind + 1, 3));

	}

	else if (flag == 1) {

		maxi = max(maxi, a[ind][2] + func(ind + 1, 2));

		maxi = max(maxi, a[ind][3] + func(ind + 1, 3));

	}

	else if (flag == 2) {

		maxi = max(maxi, a[ind][1] + func(ind + 1, 1));

		maxi = max(maxi, a[ind][3] + func(ind + 1, 3));

	}

	else {

		maxi = max(maxi, a[ind][1] + func(ind + 1, 1));

		maxi = max(maxi, a[ind][2] + func(ind + 1, 2));

	}



	return dp[ind][flag] = maxi;

}



int main() {



	cin >> n;

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

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

			cin >> a[i][j];

		}

	}



	memset(dp, -1, sizeof dp);



	cout << func(0, 0);



	return 0;

}


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

using namespace std;



int main()

{

	int n;

	scanf("%d",&n);

	

	int a,b,c;

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

	

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

	dp[0][0]=a,dp[0][1]=b,dp[0][2]=c;

	

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

	{

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

		

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

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

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

	}

	

	printf("%d\n",*max_element(dp[n-1].begin(),dp[n-1].end()));

	

	return 0;

}


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

using namespace std;

#define INF INT_MAX

#define N 100005

#define K 3

int n, k;

int arr[N][K];

int dp[N][K][K];

int solve(int idx, int last_k, int cur_k){

    int res, take = -1, leave = -1;

    if(idx == n)

        return 0;

    if(cur_k == K)

        return -1;

    if(~last_k && ~dp[idx][last_k][cur_k])

        return dp[idx][last_k][cur_k];

    if(cur_k == last_k)

        return dp[idx][last_k][cur_k] = solve(idx, last_k, cur_k+1);

    take = solve(idx+1, cur_k, 0);

    leave = solve(idx, last_k, cur_k+1);

    if(take == -1)

        return dp[idx][last_k][cur_k] = leave;

    take += arr[idx][cur_k];

    if(leave == -1)

        return dp[idx][last_k][cur_k] = take;

    if(idx == 1 && cur_k == 1 && last_k == 2)

        cout << take << ' ' << leave << '\n';

    return dp[idx][last_k][cur_k] = max(take, leave);

}

int main()

{

    cin >> n;

    memset(dp, -1, sizeof(dp[0][0][0]) * N * K * K);

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

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

            cin >> arr[i][j];

    cout << solve(0, -1, 0);

    return 0;

}

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

using namespace std;

const int N = 1e5 + 5;

int dp[N][3];

int main(){

	int n;

	cin >> n;

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

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

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

			cin >> arr[i][j];

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

		dp[n-1][j] = arr[n-1][j];

	for(int i = n-2; i >= 0; --i){

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

			dp[i][j] = max(dp[i+1][(j+1)%3] + arr[i][j], dp[i+1][(j+2)%3] + arr[i][j]);

	}

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

		dp[0][0] = max(dp[0][0], dp[0][j]);

	cout << dp[0][0];

	return 0;

}


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

using namespace std;

int n,arr[100009][3];

int dp[100009][3];

int bt(int x,int tur)

{

    if(x==n)

        return 0;

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

        return dp[x][tur];

    int &ret=dp[x][tur];

    ret=arr[x][tur];

    int mx=0;

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

    {

        if(i==tur)

            continue;

        mx=max(mx,bt(x+1,i));

    }

    ret+=mx;

    return ret;

}

int main()

{

    cin>>n;

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

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

    {

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

    }

    cout<<max(bt(0,0),max(bt(0,1),bt(0,2)));

}


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

using namespace std;

int n,k,dp[100009][3],a[100009][3],mx=1e9+8;

int main()

{

    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(k==j)continue;

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

            }

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

        }

    }

    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;

long long dp[100009][3],n,arr[100009][3];

int bt(int x,int b)

{

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

        return dp[x][b];

    if(x==n)

        return 0;

    long long &ret=dp[x][b]=-1;

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

    {

        if(i==b)

            continue;

        ret=max(bt(x+1,i)+arr[x+1][i],ret);

    }

    return ret;

}

int main()

{

    cin>>n;

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

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

    {

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

        {

            cin>>arr[i][j];

        }

    }

    cout<<max(bt(0,0)+arr[0][0],max(bt(0,1)+arr[0][1],bt(0,2)+arr[0][2]));

}


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

using namespace std;

int n,k,dp[100009][3],a[100009][3],mx=1e9+8;

int main()

{

    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(k==j)continue;

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

            }

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

        }

    }

    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;

int n,k;

int dp[200009][3],arr[200009][3];

int bt(int x,int pre)

{

    if(x==n)return 0;

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

    int &ret=dp[x][pre];

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

    {

        if(pre==i)continue;

        ret=max(ret,bt(x+1,i)+arr[x][i]);

    }

    return ret;

}

int main()

{

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

    cin>>n;

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

    cout<<max(bt(0,0),max(bt(0,1),bt(0,2)));

}


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

using namespace std;

int n,k,dp[100009][3],a[100009][3],mx=1e9+8;

int main()

{

    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(k==j)continue;

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

            }

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

        }

    }

    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;

 

#define lli long long int

#define REP(i,s,l) for(lli i=s;i<l;i++)

#define DEBUG 0

#define INF (1LL<<50)

#define MOD 1000000007



lli dp[3][100010];



signed main(){



	lli n;

	cin>>n;

	map<lli,vector<lli>> d;

	REP(i,0,n){

		lli a,b,c;

		cin>>a>>b>>c;

		d[0].push_back(a);

		d[1].push_back(b);

		d[2].push_back(c);

	}





	REP(i,0,3)REP(j,0,100010)dp[i][j]=0;



	REP(i,0,3){

		dp[i][0]=d[i][0];

		if(DEBUG)cout<<dp[i][0]<<" ";

	}



	if(DEBUG)cout<<endl;



	for(lli i=1;i<n;i++){/*深さ*/

		for(lli j=0;j<3;j++){//今の行動

			for(lli k=0;k<3;k++){//前の行動

				if(j==k)continue;

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

			}

			if(DEBUG)cout<<dp[j][i]<<" ";

		}

		if(DEBUG)cout<<endl;

	}



	lli ans=0;

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

	cout<<ans<<endl;



	return 0;

}


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

using namespace std;



#define lli long long int

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

#define MOD 1000000007

#define NUM 2520

#define INF (1LL<<50)

#define DEBUG 0

#define mp(a,b) make_pair(a,b)

#define SORT(V) sort(V.begin(),V.end())

#define PI (3.141592653589794)



//dp[i][j],i前の日にしたもの,j:index

lli dp[3][101000];

vector<lli> a[3];



int main(){



	lli n;

	cin>>n;



	REP(i,0,3)a[i].resize(n);

	REP(i,0,n)REP(j,0,3)cin>>a[j][i];



	REP(i,0,3)dp[i][0]=a[i][0];



	//j before,k,after

	REP(i,0,n-1)REP(j,0,3)REP(k,0,3){

		if(j==k)continue;

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

	}



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





    return 0;

}


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



using namespace std;



int n;

int cost[100001][3];

long long dp[100001][3];

long long rec(int pos , int type){

    if(pos >= n)return 0;

    if(~dp[pos][type])return dp[pos][type];

    long long ans = 0;

    if(type==0){

        ans += max(rec(pos + 1, 1) , rec(pos + 1, 2)) + cost[pos][type];

    }

    if(type == 1){

        ans += max(rec(pos + 1 , 2) , rec(pos + 1 , 0)) + cost[pos][type];

    }

    if(type == 2){

        ans += max(rec(pos + 1 , 1), rec(pos + 1, 0)) + cost[pos][type];

    }

    return dp[pos][type] = ans;



}



int main() {

    cin>>n;

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

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

            cin>>cost[i][j];

        }

    }

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

    long long ret0 = rec(0 , 0);

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

    long long ret1 = rec(0 , 1);

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

    long long ret2 = rec(0 , 2);

    cout<<max({ret0 , ret1 , ret2})<<"\n";

}

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



using namespace std;

int cost[100001][3];

long long dp[100001][3];



int main()

{

   int n;scanf("%d",&n);

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

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

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

       }

   }

   dp[0][0]=cost[0][0];

   dp[0][1]=cost[0][1];

   dp[0][2]=cost[0][2];

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

           }

           if(j==1){

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

           }

           if(j==2){

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

           }

       }

   }

   printf("%lld\n",max({dp[n][0],dp[n][1],dp[n][2]}));

   

   

   return 0;

}

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

using namespace std;

int n;

int dp[100001][4];

int point[100001][4];



int maxP(int i,  int last){

    if(i==n)

    return 0;

    

    int ret=INT_MIN;

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

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

        

        if(j!=last)

        {

            ret= max(ret, point[i][j]+maxP(i+1, j));

        }

        

        

     }

     return dp[i][last] = ret;

}







int main() {

     cin>>n;

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

     {

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

     }

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

     cout<<maxP(0,0);

     return 0;

    

}


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

using namespace std;

// int n;

// int dp[100001][4];

// int point[100001][4];



// int maxP(int i,  int last){

//     if(i==n)

//     return 0;

    

//     int ret=INT_MIN;

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

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

        

//         if(j!=last)

//         {

//             ret= max(ret, point[i][j]+maxP(i+1, j));

//         }

        

        

//      }

//      return dp[i][last] = ret;

// }







int main() {

    int n;

     cin>>n;

     vector<int>val(3,0);

      vector<int>dp1(3,0);

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

         vector<int>dp2(3,0);

         cin>>val[0]>>val[1]>>val[2];

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

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

                 if(i!=j){

                     dp2[j] = max( dp2[j],dp1[i] + val[j]);

                 }

             }

             

         }

         dp1=dp2;

     }

     

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

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

    //  {

    //     cin>>point[i][1]>>point[i][2]>>point[i][3];

    //  }

    

     //cout<<maxP(0,0);

     return 0;

    

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
//https://atcoder.jp/contests/dp/tasks/dp_c

#include <bits/stdc++.h>

//#define long int64_t

#define cerr if(0)cerr

#define watch(x) cerr << (#x) << " is " << (x) << endl

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

#define endl "\n"

#define max3(a,b,c) (a>=b and a>=c ? a : b>=c and b>=a ? b : c)

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

#define min(a,b) (a<b?a:b)

#define MOD 1e8 + 7

#define INF 2e9

#define DESPACITO 1e18

#define PI acos(-1);

#define E 998244353

#define pb push_back

#define mp make_pair

#define ll long long



using namespace std;

const int N = 1e5 + 5;

ll a[N], b[N], c[N], cache[N][3];



ll dp (int idx, int opt) {

    if (idx == 0)

        return 0;

    ll &ans = cache[idx][opt];

    if (ans != -1) 

        return ans;

    else {

        switch(opt) {

            case 0:

                ans = max(dp(idx-1, 1), dp(idx-1, 2)) + a[idx];

                break;

            case 1:

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

                break;

            case 2:

                ans = max(dp(idx-1, 0), dp(idx-1, 1)) + c[idx];

                break;

        }

        //cerr << "cache[" << idx << "][" << opt << "] is " << cache[idx][opt] << endl;

        return ans;

    }

}



int main() {

    int i, n;

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

    cin >> n;

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

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

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

    return 0;

}

// Optimized solution to the same problem (faster):
//https://atcoder.jp/contests/dp/tasks/dp_c

#include <bits/stdc++.h>

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

using namespace std;

#define max3(x,y,z) max(x,max(y,z))

#define min(a,b) (a<b?a:b)

const int N = 1e5 + 5;

int n, a[N][3], cache[N][3];



int dp (int idx, int opt) {

    if (idx < 0)

        return 0;

    int &ans = cache[idx][opt];

    if (ans != -1)

        return ans;

    ans = a[idx][opt] + max(dp(idx-1, (opt+1)%3), dp(idx-1, (opt+2)%3));

    return ans;

}



int32_t main() {

    IOS;

    int i;

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

    cin >> n;

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

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

    cout << max3(dp(n, 0), dp(n, 1), dp(n, 2));

    return 0;

}

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

using namespace std;

using ll = long long;



const int N = 1e5 + 5;



int a[N][3];

ll dp[4][N];

int n;



ll solve(int p, int i) {

  if (i == n)

    return 0;

    if (dp[p][i] != -1)

      return dp[p][i];

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

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

  }

  return dp[p][i];

}



int main () {

  ios_base::sync_with_stdio(0);

  cin.tie(0);

  

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

  cin >> n;

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

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

  }

  cout << solve(3, 0) << '\n';

  return 0;

}

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

using namespace std;

using ll = long long;



const int N = 1e5 + 5;



int a[N][3];

ll dp[N][3];

int n;



int main () {

  ios_base::sync_with_stdio(0);

  cin.tie(0);

  

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

  cin >> n;

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

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

  }

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

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

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

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

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

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

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

  }

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

  return 0;

}

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

using namespace std;



typedef long long int ll;

typedef long double ld;



#define Pb push_back

#define Mp make_pair



#define ff first

#define ss second



#define Max(x,y,z) max(x,max(y,z))

#define Min(x,y,z) min(x,min(y,z))



#define INF LONG_LONG_MAX

#define MINF LONG_LONG_MIN



#define endl "\n"

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



#define db1(x) cout<<#x<<" : "<<x<<endl;

#define db2(x,y) cout<<#x<<" : "<<x<<"\t"<<#y<<" : "<<y<<endl;

#define db3(x,y,z) cout<<#x<<" : "<<x<<"\t"<<#y<<" : "<<y<<"\t"<<#z<<" : "<<z<<endl;



const int N = 1e5+5;

ll dp[N][3];

ll n;

ll v[N][3];



ll sol(int d,int a)

{

  if(d == n)

  {

    return 0;

  }

  ll &ans = dp[d][a];

  if(ans != -1)

  {

    return ans;

  }

  if(a == 0)

  {

    ans = v[d][a] + max(sol(d+1,1),sol(d+1,2));

  }

  else if(a == 1)

  {

    ans = v[d][a] + max(sol(d+1,0),sol(d+1,2));

  }

  else

  {

    ans = v[d][a] + max(sol(d+1,0),sol(d+1,1));

  }

  return ans;

}



void solve()

{

  cin >> n;

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

  {

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

  }

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

  ll ans = Max(sol(0,0),sol(0,1),sol(0,2));

  cout << ans;

}



signed main()

{

  IOS;

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

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

  int t=1;

  //cin >> t;

  while(t--)

  {

    solve();

  }

  return 0;

}

/*==============================================================================================================================================================*/


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

using namespace std;



typedef long long int ll;

typedef long double ld;



#define Pb push_back

#define Mp make_pair



#define ff first

#define ss second



#define Max(x,y,z) max(x,max(y,z))

#define Min(x,y,z) min(x,min(y,z))



#define INF LONG_LONG_MAX

#define MINF LONG_LONG_MIN



#define endl "\n"

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



#define db1(x) cout<<#x<<" : "<<x<<endl;

#define db2(x,y) cout<<#x<<" : "<<x<<"\t"<<#y<<" : "<<y<<endl;

#define db3(x,y,z) cout<<#x<<" : "<<x<<"\t"<<#y<<" : "<<y<<"\t"<<#z<<" : "<<z<<endl;



const int N = 1e5+5;

ll dp[N][3];

ll n;

ll v[N][3];



void solve()

{

  cin >> n;

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

  {

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

  }

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

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

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

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

  {

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

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

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

  }

  ll ans = Max(dp[n-1][0],dp[n-1][1],dp[n-1][2]);

  cout << ans;

}



signed main()

{

  IOS;

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

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

  int t=1;

  //cin >> t;

  while(t--)

  {

    solve();

  }

  return 0;

}

/*==============================================================================================================================================================*/


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

using namespace std;

#include<string.h>

#include<bits/stdc++.h>



int dp[100001][3] = {};



int rec(int mat[][3] , int type, int idx);



int main(){

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

	int n;

	cin >> n;

	int mat[n][3];

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

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

	}

	dp[0][0] = mat[0][0];

	dp[0][1] = mat[0][1];

	dp[0][2] = mat[0][2];



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

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

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

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

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

		}

	}

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

}

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

using namespace std;

#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);



int main(){

	IOS;

	int n;

	cin >> n;

	int a[n][3];



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

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

	}



	int dp[n][3];

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

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

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



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

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

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

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

	}

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

}

// Fastest known solution for this problem: