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

using namespace std;

typedef long long ll;

const int N = 1e5 + 9 ;

int a[N] , b[N] ,c[N] , mem[N][5];

int n;

int solve(int day , int curr){

    if(day == n)

        return 0;

    int &ret = mem[day][curr];

    if(~ret)

        return ret ;

    if(curr == 0){

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

        ret = max(ret , a[day] + solve(day + 1 , 2)) ;

    }

    if(curr == 1){

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

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

    }

    if(curr == 2){

        ret = max(ret , c[day] + solve(day + 1 , 0)) ;

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

    }

    return ret ;



}

int main()

{



    memset(mem ,-1 , sizeof mem);

    cin >> n ;

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

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

    }

    int ans = 0 ;

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

        ans = max(ans , solve(0 , i));

    cout << ans << endl ;



    return 0;

}


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

using namespace std;

typedef long long ll;

const int N = 1e5 + 9 ;

int a[N] , b[N] ,c[N] , mem[N][5];

int n;

int dp[N][5];

int solve(int day , int curr){          // top down

    if(day == n)

        return 0;

    int &ret = mem[day][curr];

    if(~ret)

        return ret ;

    if(curr == 0){

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

        ret = max(ret , a[day] + solve(day + 1 , 2)) ;

    }

    if(curr == 1){

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

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

    }

    if(curr == 2){

        ret = max(ret , c[day] + solve(day + 1 , 0)) ;

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

    }

    return ret ;



}

int main()

{



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

    cin >> n ;

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

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

    }

    int ans = 0 ;

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

//        ans = max(ans , solve(0 , i));

//    cout << ans << endl ;

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

     }

     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):
//Inbornhandsome

#include <bits/stdc++.h>

using namespace std;

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

#define FIO freopen("input.txt","r",stdin); freopen("output.txt","w",stdout)

#define endl '\n'

#define int long long



const int N = 1e5 + 7;



int arr[N][3], ans[N][3], n;



int dp(int a, int b, int k)

{



    if(a == n) return arr[n][b];



    if(a > n) return -1e10;



    if(ans[a][k] != 0) return ans[a][k];



    ans[a][k] = max(arr[a][b] + dp(a+1, (b+1) % 3, (k+1) % 3), arr[a][b] + dp(a+1, (b+2) % 3, (k+2) % 3));



    return ans[a][k];

    

}



int32_t main()

{

    IOS; //FIO;



    cin >> n;



    memset(ans, 0, N);



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

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



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



    cout << ans[0][0];



    return 0;

}

// Optimized solution to the same problem (faster):
//Inbornhandsome

#include <bits/stdc++.h>

using namespace std;

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

#define FIO freopen("input.txt","r",stdin); freopen("output.txt","w",stdout)

#define endl '\n'

#define int long long



const int N = 1e5 + 7;



int arr[N][3], ans[N][3], n;



int dp(int a, int b)

{



    if(a == n) return arr[n][b];



    if(ans[a][b] != 0) return ans[a][b];



    ans[a][b] = max(arr[a][b] + dp(a+1, (b+1) % 3), arr[a][b] + dp(a+1, (b+2) % 3));



    return ans[a][b];

    

}



int32_t main()

{

    IOS; //FIO;



    cin >> n;



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

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



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



    cout << ans[0][0];



    return 0;

}

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

int a[100005][4];

int dp[100005][4];



using namespace std;

int n;

int ans(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]+ans(ind+1,1));

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

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

    }

    else if(flag==1)

    {

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

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

    }

    else if(flag==2)

    {

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

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

    }

    else if(flag==3)

    {

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

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

    }    

    

    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<<ans(0,0);

    return 0;

}


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



using namespace std;

int arr[100001][3];

int ans[100001][3];

int main()

{

    int n;

    cin>>n;

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

    {

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

        {

            cin>>arr[i][j];

        }

    }

    ans[0][0] = arr[0][0];

    ans[0][1] = arr[0][1];

    ans[0][2] = arr[0][2];

    

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

    {

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

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

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

    }

    int answer = max({ans[n-1][0],ans[n-1][1],ans[n-1][2]});

    cout<<answer<<endl;

    return 0;

}


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

#define ll long long

#define pb push_back

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

#define mp make_pair

#define ff first

#define ss second

#define MAXN  100005

#define endl "\n"

using namespace std;

ll n,a,b,c;

vector<int>v[3];

ll dp[MAXN][4];

ll rec(int i, int p){

    if(i==n)

        return 0;

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

        return dp[i][p];

    ll ans=0;

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

        if(h!=p){

            ans=max(ans,rec(i+1,h)+v[h][i]);

        }

    }

    //cout<<i<<" "<<p<<" "<<ans<<endl;

    return dp[i][p]=ans;

}



int main(){

    ios::sync_with_stdio(false);

    cin.tie(0);

    cin>>n;

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

        cin>>a>>b>>c;

        v[0].pb(a);

        v[1].pb(b);

        v[2].pb(c);

    }

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

    ll ans=max(rec(0,0),rec(0,1));

    cout<<ans;





    return 0;

}




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

#define ll long long

#define pb push_back

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

#define mp make_pair

#define ff first

#define ss second

#define MAXN  1000005

#define endl "\n"

using namespace std;



int main(){

    ios::sync_with_stdio(false);

    cin.tie(0);

    int n, r;

    cin>>n;

    vector<int>dp(3,0);

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

        vector<int>v(3);

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

            cin>>v[j];

            r = INT_MIN;

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

                if(k != j){

                    r = max(r, dp[k]);

                }

            }

            v[j] += r;

        }

        dp = v;

    }

    cout<<*max_element(all(dp));



    return 0;

}




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

#include<algorithm>

using namespace std;



int dp[100001][4];

long long int solve(int **a,int i,int prev,int n)

{

	if(i==n)

	{

		return(0);

	}

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

	{

		return(dp[i][prev]);

	}

	if(prev==0)

	{

		long long int op1=solve(a,i+1,1,n)+a[i][1];

		long long int op2=solve(a,i+1,2,n)+a[i][2];

		return(dp[i][prev]=max(op1,op2));

	}

	else if(prev==1)

	{

		long long int op1=solve(a,i+1,0,n)+a[i][0];

		long long int op2=solve(a,i+1,2,n)+a[i][2];

		return(dp[i][prev]=max(op1,op2));

	}

	else if(prev==2)

	{

		long long int op1=solve(a,i+1,1,n)+a[i][1];

		long long int op2=solve(a,i+1,0,n)+a[i][0];

		return(dp[i][prev]=max(op1,op2));

	}

	else

	{

		long long int op1=solve(a,i+1,1,n)+a[i][1];

		long long int op2=solve(a,i+1,2,n)+a[i][2];

		long long int op3=solve(a,i+1,0,n)+a[i][0];

		return(dp[i][prev]=max(op1,max(op2,op3)));

	}

}

int main()

{

	long long int n,i,j;

	cin>>n;

	int **a=new int*[n];

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

	{

		a[i]=new int[3];

	}

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

	{

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

	}

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

	{

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

		{

			dp[i][j]=-1;

		}

	}

	cout<<solve(a,0,3,n);

}

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

#include<algorithm>

#include<climits>

using namespace std;

#define ll long long





ll dp[100001][4];



int solve(ll a[][4],ll i,ll prev,ll n)

{

	if(i==n)

	{

		return(0);

	}

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

	{

		return(dp[i][prev]);

	}

    ll ans=INT_MIN,j;

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

	{

		if(prev!=j)

		{

			ans=max(ans,solve(a,i+1,j,n)+a[i][j]);

		}

	}

	return(dp[i][prev]=ans);

}



int main()

{

	ll n,i;

	cin>>n;

	ll a[n][4];

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

	{

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

		dp[i][0]=-1;

		dp[i][1]=-1;

		dp[i][2]=-1;

		dp[i][3]=-1;

	}

	cout<<solve(a,0,0,n);

}

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

#include <vector>

#include <cmath>

#include <algorithm>

#include <iomanip>

#include <cstring>

#define rep(i, N) for (int i = 0; i < (int)N; i++)

using namespace std;

typedef long long ll;

const ll INF = 9223372036854775807/2;

const int MOD = 1000000007;

const int N_MAX = pow(10,5);



int main() {

  int N; cin >> N;

  int a[N+1][3]; for(int i=1; i<=N; i++) rep(j, 3) cin >> a[i][j];



  ll dp[N_MAX+1][3] = {};

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

    rep(j, 3) rep(k, 3) {

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

    }

  }

  // rep(i, N+1) rep(j, 3) cout << dp[i][j] << " "; cout << endl;

  ll result = 0; rep(i, 3) result = max(result, dp[N][i]);

  cout << result << endl;

  return 0;

}

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



using namespace std;

typedef long long ll;

typedef pair<ll, ll> p_ll;



template<class T>

void debug(T itr1, T itr2) { auto now = itr1; while(now<itr2) { cout << *now << " "; now++; } cout << endl; }

#define repr(i,from,to) for (int i=(int)from; i<(int)to; i++)

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

#define rep(i,N) repr(i,0,N)

#define per(i,N) for (int i=(int)N-1; i>=0; i--)



const ll MOD = pow(10,9)+7;

const ll LLINF = pow(2,61)-1;

const int INF = pow(2,30)-1;



vector<ll> fac;

void c_fac(int x=pow(10,6)+10) { fac.resize(x,true); rep(i,x) fac[i] = i ? (fac[i-1]*i)%MOD : 1; }

ll inv(ll a, ll m=MOD) { ll b = m, x = 1, y = 0; while (b!=0) { int d = a/b; a -= b*d; swap(a,b); x -= y*d; swap(x,y); } return (x+m)%m; }

ll nck(ll n, ll k) { return fac[n]*inv(fac[k]*fac[n-k]%MOD)%MOD; }

ll gcd(ll a, ll b) { if (a<b) swap(a,b); return b==0 ? a : gcd(b, a%b); }

ll lcm(ll a, ll b) { return a/gcd(a,b)*b; }



struct day { int a, b, c; };



int main() {

  int N; cin >> N;

  day d[N]; rep(i,N) { int a, b, c; cin >> a >> b >> c; d[i]={a,b,c}; }

  int dp[N+1][3] = {};

  rep(i,N) {

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

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

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

  }

  int result = 0; rep(i,3) result = max(result, dp[N][i]);

  cout << result << endl;

  return 0;

}

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

using namespace std;

#define ll long long

#define vi vector<int>

#define pb push_back

#define pii pair<int , int>

#define fast ios_base::sync_with_stdio(0)

#define mx 100005

#define inf 100000000000000000LL

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

/// 1     2       3

int n;

ll dp[mx][4];

ll fun(int pos, int prev)

{

    if(pos>n) return 0;

    ll& ret = dp[pos][prev];

    if(ret!=-1) return ret;

    if(prev!=1) ret = max(ret, a[pos] + fun(pos+1, 1));

    if(prev!=2) ret = max(ret, b[pos] + fun(pos+1, 2));

    if(prev!=3) ret = max(ret, c[pos] + fun(pos+1, 3));

    return ret;

}

int main()

{

    while(scanf("%d",&n)){

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

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

        memset(dp, -1, sizeof dp);

        printf("%lld\n",fun(1,0));

        return 0;

    }

}




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

using namespace std;

#define ll long long

#define vi vector<int>

#define pb push_back

#define pii pair<int , int>

#define fast ios_base::sync_with_stdio(0)

#define mx 100005

#define inf 100000000000000000LL

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

/// 0     1       2

int n;

ll dp[mx][3];

int main()

{

    while(scanf("%d",&n)){

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

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

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

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

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

        for(int i=2; 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]);

        }

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

        return 0;

    }

}




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

#include <fstream>

using namespace std;

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

#define rep2(r, aa, bb) for (ll r = (aa)-1; r >= (bb); r--)

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

#define pb(t) push_back(t)

#define ll long long

int n;

int dp[100005][4];

vector<vector<int>> A;

int minicost(int v, int pre)

{

    if (v >= n)

        return 0;

    if (dp[v][pre] != -1)

        return dp[v][pre];

    int maxi = 0;

    if (pre != 0)

    {

        maxi = max(maxi, A[v][0] + minicost(v + 1, 0));

    }

    if (pre != 1)

    {

        maxi = max(maxi, A[v][1] + minicost(v + 1, 1));

    }

    if (pre != 2)

    {

        maxi = max(maxi, A[v][2] + minicost(v + 1, 2));

    }

    return dp[v][pre]=maxi;

}

int main()

{

    cin >> n;

    A.resize(n);

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

    rep(i, 0, n)

    {

        A[i].resize(3);

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

    }

    cout << minicost(0, 3);

}

// Optimized solution to the same problem (faster):
//we'll get thru it believe me 

#include <bits/stdc++.h>

using namespace std;

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

#define rep2(r, aa, bb) for (int r = (aa)-1; r >= (bb); r--)

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

#define pb(t) push_back(t)

#define ll long long

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

void max_self(int& a, int b){

	a=max(a, b);

}

int main(){

	ios_base::sync_with_stdio(false);

    cin.tie(NULL);

    cout.tie(NULL);

    int n;

    cin>>n;

    const int INF=INT_MAX;

    vector<vector<int>>dp(n, vector<int>(3, -INF)), A(n, vector<int>(3));

    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=0; i<n-1; i++){

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

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

				if(x==j)continue;

				max_self(dp[i+1][x], dp[i][j]+A[i+1][x]);

			}

		}

	}

	cout<<max({dp[n-1][0], 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 ll long long

ll dp[100009][4]= {-1};

ll inf = -1*1e9;

ll n;

ll arr[100009][4];

ll dfs(int index,int j)

{

  ll choice1,choice2;

  if(index>=n-1)

  return 0;

if(dp[index][j]!=-1)

return dp[index][j];

if(j==1)

{

  choice1=arr[index+1][0]+dfs(index+1,0);

    choice2=arr[index+1][2]+dfs(index+1,2);

     

}

else if(j==2)

{

  choice1=arr[index+1][0]+dfs(index+1,0);

    choice2=arr[index+1][1]+dfs(index+1,1);

}

else

{

   choice1=arr[index+1][2]+dfs(index+1,2);

    choice2=arr[index+1][1]+dfs(index+1,1);

}

return dp[index][j] =max(choice1,choice2);



}



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(arr[0][0] +dfs(0,0),max(arr[0][1] +dfs(0,1),arr[0][2] +dfs(0,2)));



}

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

using namespace std;

#define ll long long

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

ll dp[100009][3]= {-1};

ll inf = -1*1e9;

ll n;

ll arr[100009][3];





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

  }  

  

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

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

{

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

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

if(k!=j)

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



}

cout<<max3(dp[n-1][0],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 ll long long



const ll INF= 1LL << 60;



//入力

ll N;

ll K;

ll a[100010][3];

//dp

ll dp[100010][3];





void chmin(ll& a, ll b){

  if(a>b){

    a=b;

  }

}





void chmax(ll& a, ll b){

  if(a<b){

    a=b;

  }

}



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<100010;i++){

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

          dp[i][j]=0;

    } 

  }

  

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

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

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

        if(j==k) continue;

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

      }

    }

  }

  

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

    chmax(dp[N][0],dp[N][i]);

  }

  cout << dp[N][0] << endl;

}

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



using namespace std;



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

#define rep2(i,m,n) for(int (i)=(int)(n)-1;i>=(int)(m);i--)

#define REP(i,n) rep(i,0,n)

#define REP2(i,n) rep2(i,0,n)

#define FOR(i,c) for(decltype((c).begin())i=(c).begin();i!=(c).end();++i)

#define ll long long

#define ull unsigned long long

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

#define en '\n'

template <class T> using vec = vector<T>;

template <class T> using vvec = vector<vec<T>>;

typedef pair<ll, ll> P;

constexpr long long INF = 1LL << 60;

constexpr int INF_INT = 1 << 25;

constexpr long long MOD = (ll) 1e9 + 7;

typedef vector<ll> Array;

typedef vector<Array> Matrix;

constexpr int loose = 0;

constexpr int tight = 1;





template<class T> inline bool chmin(T& a, T b) {

    if (a > b) {

        a = b;

        return true;

    }

    return false;

}

template<class T> inline bool chmax(T& a, T b) {

    if (a < b) {

        a = b;

        return true;

    }

    return false;

}



struct Edge {

	ll to, cap, rev;

	Edge(ll _to, ll _cap, ll _rev) {

	to = _to; cap = _cap; rev = _rev;

	}

};

typedef vector<Edge> Edges;

typedef vector<Edges> Graph;



void add_edge(Graph& G, ll from, ll to, ll cap, bool revFlag, ll revCap) {

	G[from].push_back(Edge(to, cap, (ll)G[to].size()));

	if (revFlag)G[to].push_back(Edge(from, revCap, (ll)G[from].size() - 1));

}



void solve(){

	ll n;

	cin>>n;

	Matrix a(n,Array(3));

	REP(i,n) cin>>a[i][0]>>a[i][1]>>a[i][2];



	Matrix dp(n+1,Array(3,0));//iこまでみて、最後にjした

	REP(i,n){

		REP(j,3){

			REP(k,3){

				if(j==k) continue;

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

			}

		}		

	}

	ll ans=0;

	REP(i,3) chmax(ans,dp[n][i]);

	cout<< ans<<en;

}



int main() {

    ios::sync_with_stdio(false);

    cin.tie(0);

    cout.tie(0);



    solve();

    return 0;

}

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

    // typedef __int128 int128;

    // typedef __float128 float128;

     

    using namespace std;

    using namespace rel_ops;

    typedef long long ll;

    typedef long double ld;

    typedef complex<ld> pt;

    typedef vector<pt> pol;

    typedef vector<ll> vi;

    typedef vector<vi> vvi;

    typedef vector<vvi> vvvi;

    typedef vector<ld> vd;

    typedef vector<vd> vvd;

    typedef pair<ll, ll> pii;

    typedef vector<pii> vpii;

    #define pb push_back

     

     

    int main(){

     

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

     

      ll N;

      cin >> N;

     

     

      ll abc[100010][4];

     

      ll f[100010][4];

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

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

        if(i == 1) f[i][1] = abc[i][1], f[i][2] = abc[i][2], f[i][3] = abc[i][3];

        else f[i][1] = max(abc[i][1] + f[i-1][2], abc[i][1] + f[i-1][3]),

               f[i][2] = max(abc[i][2] + f[i-1][1], abc[i][2] + f[i-1][3]),

               f[i][3] = max(abc[i][3] + f[i-1][1], abc[i][3] + f[i-1][2]);

      }

     

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

     

     

    }

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

using namespace std;



#define ll  int

#define intmax INT_MAX

#define pi 3.14159265358979323846

#define ff first 

#define ss second

#define pb push_back

#define watch(x) cerr<<(#x)<<"  "<<x<<"\n";

const int MOD=998244353;

const int MAXN = 3e5+4;

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



int main(){	

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



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++){

 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][2],max(dp[n][0],dp[n][1]))<<"\n";





return 0;

}


// Fastest known solution for this problem: