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



using namespace std;

const int N = 1e5 + 3 , OO = 0x3f3f3f3f ;

int arr1[N]  , arr2[N] , arr3[N], mem[N][5];

int n  ;

int solve(int idx , int pre){

    if(idx == n)

        return 0;

    int &ret = mem[idx][pre];

    if(~ret)

        return ret;

        if(pre == 0){

            ret = max(ret , arr1[idx] + solve(idx + 1 , 1));

            ret = max(ret , arr1[idx] + solve(idx + 1 , 2));

        }

         if(pre == 1){

            ret = max(ret , arr2[idx] + solve(idx + 1 , 0));

            ret = max(ret , arr2[idx] + solve(idx + 1 , 2));

        }

         if(pre == 2){

            ret = max(ret , arr3[idx] + solve(idx + 1 , 1));

            ret = max(ret , arr3[idx] + solve(idx + 1 , 0));

        }

    return ret;

}

int main()

{

    cin >> n ;

    for(int i = 0 ; i < n ; ++i) cin >> arr1[i] >> arr2[i] >> arr3[i];

    memset(mem , -1 , sizeof mem);

    cout << max({solve(0 , 0) , solve(0 , 1) , solve(0 , 2)});

    return 0;

}


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

using namespace std;

typedef long long ll;

const int N = 1e5 + 9 ;

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

int mem[N][5];

int dp[N][5];

int solve(int idx , int pre){

    if(idx == n)

        return 0;

    int &ret = mem[idx][pre];

    if(~ret)

        return ret;

    if(pre == 0){

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

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

    }

    if(pre == 1){

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

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

    }

    if(pre == 2){

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

        ret = max(ret , c[idx] + solve(idx + 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 = max(solve(0 , 0) , max(solve(0 , 1) , solve(0 , 2)));

//   cout << ans ;

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

     }

     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 + 3 , OO = 0x3f3f3f3f ;

int arr1[N]  , arr2[N] , arr3[N], mem[N][5];

int n  ;

int solve(int idx , int pre){

    if(idx == n)

        return 0;

    int &ret = mem[idx][pre];

    if(~ret)

        return ret;

        if(pre == 0){

            ret = max(ret , arr1[idx] + solve(idx + 1 , 1));

            ret = max(ret , arr1[idx] + solve(idx + 1 , 2));

        }

         if(pre == 1){

            ret = max(ret , arr2[idx] + solve(idx + 1 , 0));

            ret = max(ret , arr2[idx] + solve(idx + 1 , 2));

        }

         if(pre == 2){

            ret = max(ret , arr3[idx] + solve(idx + 1 , 1));

            ret = max(ret , arr3[idx] + solve(idx + 1 , 0));

        }

    return ret;

}

int main()

{

    cin >> n ;

    for(int i = 0 ; i < n ; ++i) cin >> arr1[i] >> arr2[i] >> arr3[i];

    memset(mem , -1 , sizeof mem);

    cout << max({solve(0 , 0) , solve(0 , 1) , solve(0 , 2)});

    return 0;

}


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

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

}


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

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

}

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

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

}


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

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

}




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

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

}

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

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

}

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

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

    }

}




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

    }

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

}

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

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



}

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



}