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

#define fr(i,a,n) for(ll i=a;i<n;i++)

#define YES cout<<"YES"<<endl

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

#define NO cout<<"NO"<<endl

#define pb(x) emplace_back(x)

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

#define vi vector<ll>

#define mi map<ll,ll>

#define pi pair<ll,ll>

#define what(x) cout<<#x<<" ="<<x<<endl;

using namespace std;

using ll =long long;

ll dp[100005][4];

vi a(100005),b(100005),c(100005);

ll n;

ll solve(ll taken,ll i,ll j)

{

    if(i>=n)return 0;

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



    if(taken!=1) dp[i][1]=a[i]+max(solve(1,i+1,2),solve(1,i+1,3));

    if(taken!=2)dp[i][2]=b[i]+max(solve(2,i+1,1),solve(2,i+1,3));

    if(taken!=3)dp[i][3]=c[i]+max(solve(3,i+1,1),solve(3,i+1,2));



    if(taken==1)return max({dp[i][2],dp[i][3]});

    if(taken==2)return max({dp[i][1],dp[i][3]});

    if(taken==3)return max({dp[i][2],dp[i][1]});

    else return max({dp[i][2],dp[i][1],dp[i][3]});

}







int main()

{

    memset(dp,-1,sizeof dp);

    cin>>n;

    fr(i,0,n)cin>>a[i]>>b[i]>>c[i];



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





   /* fr(i,1,4)

    {

        fr(j,0,n)cout<<dp[j][i]<<" ";

        cout<<endl;

    }

*/



}




















































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

using namespace std;

typedef long long int ll;



int main(){

    ios::sync_with_stdio(false);

    cin.tie(NULL);

    cout.tie(NULL);





    int n;

    cin>>n;

    vector<int> a(n), b(n), c(n);



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

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

    }



    vector<int> dpA(n), dpB(n), dpC(n);         /// dpA[i] = A was done in i-th day



    dpA[0] = a[0];

    dpB[0] = b[0];

    dpC[0] = c[0];



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

        dpA[i] = a[i] + max(dpB[i-1], dpC[i-1]);

        dpB[i] = b[i] + max(dpA[i-1], dpC[i-1]);

        dpC[i] = c[i] + max(dpA[i-1], dpB[i-1]);

    }





    int ans = max({dpA[n-1], dpB[n-1], dpC[n-1]});



    cout<<ans<<endl;





    return 0;

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

#include<bits/stdc++.h>

using namespace std;

#define ll long long int

#define p 1000000007



struct activity{

    int a;

    int b;

    int c;

};

int dp[100001][3];



int points(int i,int j,vector<activity> &v)

{

    if(i==1)

    {

        if(j==0)

        {

            return v[1].a;

        }

        else if(j==1)

        {

            return v[1].b;

        }

        else if(j==2)

        {

            return v[1].c;

        }

    }

    else

    {

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

        {

            return dp[i][j];

        }

        

        if(j==0)

        {

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

        }

        else if(j==1)

        {

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

        }

        else if(j==2)

        {

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

        }

    }

}



int solve(int n,vector<activity> &v)

{

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

     return max(max(points(n,0,v),points(n,1,v)),points(n,2,v));

}



int main()

{

    ios_base::sync_with_stdio(0);

    cin.tie(0);

    cout.tie(0);

    

    int n;

    cin>>n;

    

    vector<activity> v(n+1);

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

    {

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

    }

    cout<<solve(n,v);

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 2.174720937018188x):
#include <iostream>

#include<bits/stdc++.h>

using namespace std;

#define ll long long int

#define p 1000000007



struct activity{

    int a;

    int b;

    int c;

};



int solve(int n,vector<activity> &v)

{

    int dp[n+1][3];

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

    dp[1][1]=v[1].b;

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

    

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

    {

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

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

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

    }

    

    return max(max(dp[n][0],dp[n][1]),dp[n][2]);

}



int main()

{

    ios_base::sync_with_stdio(0);

    cin.tie(0);

    cout.tie(0);

    

    int n;

    cin>>n;

    

    vector<activity> v(n+1);

    

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

    {

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

    }

    cout<<solve(n,v);

    return 0;

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

#include <algorithm>

#include <cstring>



using namespace std;



int a[100100],b[100100],c[100100];

int dp_a[100100],dp_b[100100],dp_c[100100];



int vac(int i,int n,char estado){

    if(i>n) return 0;



    int resp;



    if(i==0){

        resp=max((a[i]+vac(i+1,n,'a')),(b[i]+vac(i+1,n,'b')));

        resp=max(resp,c[i]+vac(i+1,n,'c'));

    }else{

        switch(estado){

            case 'a':

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

                return dp_a[i] =max(b[i]+vac(i+1,n,'b'),c[i]+vac(i+1,n,'c')); break;

            case 'b':

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

                return dp_b[i] =max(a[i]+vac(i+1,n,'a'),c[i]+vac(i+1,n,'c')); break;

            case 'c':

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

                return dp_c[i] =max(a[i]+vac(i+1,n,'a'),b[i]+vac(i+1,n,'b')); break;

        }

    }

    return resp;

}



int main(){



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

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

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



    int n;

    cin>>n;



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

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

    }



    cout<<vac(0,n-1,'d');

}


// Faster C++ solution for the same problem (measured speedup: 2.207745461751006x):
#include <iostream>

#include <algorithm>

#include <cstring>



using namespace std;



int n;

int a[100100],b[100100],c[100100];

int dp_a[100100],dp_b[100100],dp_c[100100];



int main(){

    cin>>n;



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

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

    }



    dp_a[0]=0;

    dp_b[0]=0;

    dp_c[0]=0;



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

        dp_a[i]=max(b[i-1]+dp_b[i-1],c[i-1]+dp_c[i-1]);

        dp_b[i]=max(a[i-1]+dp_a[i-1],c[i-1]+dp_c[i-1]);

        dp_c[i]=max(a[i-1]+dp_a[i-1],b[i-1]+dp_b[i-1]); 

    }

    /*

    cout<<dp_a[n]<<" ";

    cout<<dp_b[n]<<" ";

    cout<<dp_c[n];



    cout<<endl;

    */

    int dpa =dp_a[n-1]+a[n-1];

    int dpb =dp_b[n-1]+b[n-1];

    int dpc =dp_c[n-1]+c[n-1];

    /*

    cout<<dpa<<" ";

    cout<<dpb<<" ";

    cout<<dpc;

    */

    int maiorPath=max(dpa,dpb);

    maiorPath=max(maiorPath,dpc);

    cout<<maiorPath;



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

#include <algorithm>

#include <cstring>



using namespace std;



int n;

int a[100100],b[100100],c[100100];

int dp_a[100100],dp_b[100100],dp_c[100100];





int vac(int i,char estado){



    if(i>n) return 0;



    int resp;



    if(i==0){

        resp=max(a[i]+vac(i+1,'a'),b[i]+vac(i+1,'b'));

        return resp=max(resp, c[i]+vac(i+1,'c'));

    }

    else{

        switch(estado){

            case 'a':

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

                return dp_a[i] =max(b[i]+vac(i+1,'b'),c[i]+vac(i+1,'c')); break;

            case 'b':

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

                return dp_b[i] =max(a[i]+vac(i+1,'a'),c[i]+vac(i+1,'c')); break;

            case 'c':

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

                return dp_c[i] =max(a[i]+vac(i+1,'a'),b[i]+vac(i+1,'b')); break;

        }

    }

}



int main(){



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

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

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



    cin>>n;



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

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

    }



    cout<<vac(0,'d');

}


// Faster C++ solution for the same problem (measured speedup: 2.2075793073321015x):
#include <iostream>

#include <algorithm>

#include <cstring>



using namespace std;



int n;

int a[100100],b[100100],c[100100];

int dp_a[100100],dp_b[100100],dp_c[100100];



int main(){

    cin>>n;



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

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

    }



    dp_a[0]=0;

    dp_b[0]=0;

    dp_c[0]=0;



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

        dp_a[i]=max(b[i-1]+dp_b[i-1],c[i-1]+dp_c[i-1]);

        dp_b[i]=max(a[i-1]+dp_a[i-1],c[i-1]+dp_c[i-1]);

        dp_c[i]=max(a[i-1]+dp_a[i-1],b[i-1]+dp_b[i-1]); 

    }

    /*

    cout<<dp_a[n]<<" ";

    cout<<dp_b[n]<<" ";

    cout<<dp_c[n];



    cout<<endl;

    */

    int dpa =dp_a[n-1]+a[n-1];

    int dpb =dp_b[n-1]+b[n-1];

    int dpc =dp_c[n-1]+c[n-1];

    /*

    cout<<dpa<<" ";

    cout<<dpb<<" ";

    cout<<dpc;

    */

    int maiorPath=max(dpa,dpb);

    maiorPath=max(maiorPath,dpc);

    cout<<maiorPath;



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

#include <algorithm>

#include <cstring>



using namespace std;



int n;

int a[100100],b[100100],c[100100];

int dp_a[100100],dp_b[100100],dp_c[100100];





int vac(int i,char estado){



    if(i>n) return 0;



    int resp;



    if(i==0){

        resp=max(a[i]+vac(i+1,'a'),b[i]+vac(i+1,'b'));

        return resp=max(resp, c[i]+vac(i+1,'c'));

    }

    else{

        switch(estado){

            case 'a':

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

                return dp_a[i] =max(b[i]+vac(i+1,'b'),c[i]+vac(i+1,'c')); break;

            case 'b':

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

                return dp_b[i] =max(a[i]+vac(i+1,'a'),c[i]+vac(i+1,'c')); break;

            case 'c':

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

                return dp_c[i] =max(a[i]+vac(i+1,'a'),b[i]+vac(i+1,'b')); break;

        }

    }

}



int main(){



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

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

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



    cin>>n;



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

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

    }



    cout<<vac(0,'d');

}

// Faster C++ solution for the same problem (measured speedup: 2.2075793073321015x):
#include <iostream>

#include <algorithm>

#include <cstring>



using namespace std;



int n;

int a[100100],b[100100],c[100100];

int dp_a[100100],dp_b[100100],dp_c[100100];



int main(){

    cin>>n;



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

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

    }



    dp_a[0]=0;

    dp_b[0]=0;

    dp_c[0]=0;



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

        dp_a[i]=max(b[i-1]+dp_b[i-1],c[i-1]+dp_c[i-1]);

        dp_b[i]=max(a[i-1]+dp_a[i-1],c[i-1]+dp_c[i-1]);

        dp_c[i]=max(a[i-1]+dp_a[i-1],b[i-1]+dp_b[i-1]); 

    }

    /*

    cout<<dp_a[n]<<" ";

    cout<<dp_b[n]<<" ";

    cout<<dp_c[n];



    cout<<endl;

    */

    int dpa =dp_a[n-1]+a[n-1];

    int dpb =dp_b[n-1]+b[n-1];

    int dpc =dp_c[n-1]+c[n-1];

    /*

    cout<<dpa<<" ";

    cout<<dpb<<" ";

    cout<<dpc;

    */

    int maiorPath=max(dpa,dpb);

    maiorPath=max(maiorPath,dpc);

    cout<<maiorPath;



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

using namespace std;



#define INF 0x3f3f3f3f

#define MAXN 100010

#define endl "\n"

#define abs(a, b) ((a > b) ? a-b : b-a)



typedef long long int ll;



/* End */



int n;

ll a[MAXN];

ll b[MAXN];

ll c[MAXN];

ll pd[MAXN][3];



ll solve(int i, int m) { //0 - a, 1 - b, 2 - c

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

	if (pd[i][m] != -1) return pd[i][m];

	if (m == 0)

		return pd[i][m] = max(b[i] + solve(i+1, 1), c[i] + solve(i+1, 2));

	else if (m == 1)

		return pd[i][m] = max(a[i] + solve(i+1, 0), c[i] + solve(i+1, 2));

	else

		return pd[i][m] = max(a[i] + solve(i+1, 0), b[i] + solve(i+1, 1));

}



int main() {

	cin.tie(NULL);

	ios_base::sync_with_stdio(false);

	

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



	cin >> n;

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



	cout << max(solve(1, 0), max(solve(1, 1), solve(1, 2))) << endl;



	return 0;

}


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

using namespace std;



#define INF 0x3f3f3f3f

#define MAXN 100010

#define endl "\n"

#define abs(a, b) ((a > b) ? a-b : b-a)



typedef long long int ll;



/* End */



int n;

ll a[MAXN];

ll b[MAXN];

ll c[MAXN];

ll pd[MAXN][3];



int main() {

	cin.tie(NULL);

	ios_base::sync_with_stdio(false);

	

	cin >> n;

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

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



	pd[n][0] = max(b[n], c[n]);

	pd[n][1] = max(a[n], c[n]);

	pd[n][2] = max(a[n], b[n]);



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

	        pd[i][0] = max(b[i] + pd[i+1][1], c[i] + pd[i+1][2]);

		pd[i][1] = max(a[i] + pd[i+1][0], c[i] + pd[i+1][2]);

		pd[i][2] = max(a[i] + pd[i+1][0], b[i] + pd[i+1][1]);

	}



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



	return 0;

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

#define ll long long

using namespace std;

ll dp[100005][4];

ll n,a[100005][4];



ll helper(ll day, ll chose)

{

    if(day == n) return 0;



    if(dp[day][chose] != -1) return dp[day][chose];



    ll maxi = 0;

    if(chose == 0)

    {

        maxi = max(maxi, a[day][1]+ helper(day+1, 1));

        maxi = max(maxi, a[day][2]+ helper(day+1, 2));

        maxi = max(maxi, a[day][3]+ helper(day+1, 3));

    }

    else if(chose == 1)

    {

        maxi = max(maxi, a[day][2] + helper(day+1, 2));  

        maxi = max(maxi, a[day][3] + helper(day+1, 3));

    }

    else if(chose == 1)

    {

        maxi = max(maxi, a[day][2]+ helper(day+1, 2));  

        maxi = max(maxi, a[day][3]+ helper(day+1, 3));

    }

    else if(chose == 2)

    {

        maxi = max(maxi, a[day][1]+ helper(day+1, 1));  

        maxi = max(maxi, a[day][3]+ helper(day+1, 3));

    }

    else 

    {

        maxi = max(maxi, a[day][2]+ helper(day+1, 2));  

        maxi = max(maxi, a[day][1]+ helper(day+1, 1));

    }

    return dp[day][chose] = maxi;

}

int main() {



    cin >> n;

    memset(dp, -1, sizeof dp);



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

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

            cin >> a[i][j];



    cout<<helper(0,0);

}

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

#define ll long long

using namespace std;

ll dp[100001][3];

ll n,a[100001][3];



ll helper(ll day, ll chose)

{

    if(day == n) return 0;



    if(dp[day][chose] != -1) return dp[day][chose];



    ll maxi = 0;

    if(chose == 0)

    {

        maxi = max(maxi, a[day][1]+ helper(day+1, 1));

        maxi = max(maxi, a[day][2]+ helper(day+1, 2));

        maxi = max(maxi, a[day][3]+ helper(day+1, 3));

    }

    else if(chose == 1)

    {

        maxi = max(maxi, a[day][2] + helper(day+1, 2));  

        maxi = max(maxi, a[day][3] + helper(day+1, 3));

    }

    else if(chose == 1)

    {

        maxi = max(maxi, a[day][2]+ helper(day+1, 2));  

        maxi = max(maxi, a[day][3]+ helper(day+1, 3));

    }

    else if(chose == 2)

    {

        maxi = max(maxi, a[day][1]+ helper(day+1, 1));  

        maxi = max(maxi, a[day][3]+ helper(day+1, 3));

    }

    else 

    {

        maxi = max(maxi, a[day][2]+ helper(day+1, 2));  

        maxi = max(maxi, a[day][1]+ helper(day+1, 1));

    }

    return dp[day][chose] = maxi;

}

int main() {



    cin >> n;

    memset(dp, -1, sizeof dp);



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

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

            cin >> a[i][j];



    cout<<helper(0,0);

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

using namespace std;



int main () {

  int n;

  cin>>n;

  

  int g[100001][3];

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

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

      cin>>g[i][j];

    }

  }

  

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

  dp[0][0]=0;

  dp[0][1]=0;

  dp[0][2]=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;

        }

        else {

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

        }

      }

    }

  }

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

  

}

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

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

using namespace std;

typedef long long ll;

int main() {

  int n;cin>>n;

  

  vector<int> order={0,1,2};

  

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

  

  rep(i,n) {

    rep(j,3) cin>>kou[i][j];

  }

  

  vector<vector<int>> dp(n+1,vector<int>(3,0));

  

  

  rep(i,n) {

    rep(j,3) {

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

    }

  }

  

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

  

  

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

using namespace std;



int main () {

  int n;

  cin>>n;

  

  int g[100001][3];

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

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

      cin>>g[i][j];

    }

  }

  

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

  dp[0][0]=0;

  dp[0][1]=0;

  dp[0][2]=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;

        }

        else {

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

        }

      }

    }

  }

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

  

}

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

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

#define chmax(x,y) x = max(x,y)

#define chmin(x,y) x = min(x,y)

using namespace std;

typedef long long ll;

typedef pair<int,int> P;

typedef tuple<ll,ll,ll> T;

const long long INF = 1LL<<60;

const int MOD = 1000000000+7;

#define rev(s) (string((s).rbegin(), (s).rend()))

template < typename T > inline string toString( const T &a ) { ostringstream oss; oss << a; return oss.str(); };

// cout << fixed << setprecision(10) << ans << endl;

// *min_element(c + l, c + r) *max_element(c + l, c + r)

// int dx[8]={1,1,0,-1,-1,-1,0,1};

// int dy[8]={0,1,1,1,0,-1,-1,-1};

// int dx[4]={1,0,-1,0};

// int dy[4]={0,1,0,-1};

// struct Edge {

//   int to, id;

//   Edge(int to, int id): to(to), id(id) {}

// };



int main() {

  int N;cin>>N;

  vector<vector<int>> happy(N,vector<int>(3,0));

  rep(i,N) cin >> happy[i][0] >> happy[i][1] >> happy[i][2];

  vector<vector<int>> dp(N + 1,vector<int>(3,0));

  rep(i,N) {

    rep(j,3) {

      chmax(dp[i + 1][(j + 1) % 3], dp[i][j] + happy[i][(j + 1) % 3]);

      chmax(dp[i + 1][(j + 2) % 3], dp[i][j] + happy[i][(j + 2) % 3]);

    }

  }

  int ans = 0;

  rep(i,3) chmax(ans, dp[N][i]);



  cout << ans << endl;

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

using namespace std;

#define INF 1000000000

int dp(int i,int last);

int n;

int a[9][100009],memo[100009][9];



int main(){

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

  cin>>n;

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

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

      cin>>a[j][i];

    }

  }

  cout<<dp(0,5)<<endl;

  return(0);

}



int dp(int i,int last){

  int rec=0;

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

  if(i==n)return 0;

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

    if(last!=j){

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

    }

  }

  return memo[i][last]=rec;

}


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

using namespace std;

int dp[100009][9];



int main(){

  int n;

  int a[100009][9];

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

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

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

	if(j!=k){

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

	}

      }

    }

  }



  int ans=0;

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

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

  }



  cout<<ans<<endl;

  return(0);

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

using namespace std;

#define INF 1000000000

int dp(int i,int last);

int n;

int a[9][100009],memo[100009][9];



int main(){

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

  cin>>n;

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

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

      cin>>a[j][i];

    }

  }

  cout<<dp(0,5)<<endl;

  return(0);

}



int dp(int i,int last){

  int rec=0;

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

  if(i==n)return 0;

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

    if(last!=j){

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

    }

  }

  return memo[i][last]=rec;

}


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

using namespace std;

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

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

#define fi first

#define se second

#define INF 1000000009

#define LLINF 1000000000000000009LL

#define chmax(a, b) a = max(a, b)

using ll = long long;



int dp[100009][3];



int main(){

  int n;

  cin>>n;

  vector<int>a(n);

  vector<int>b(n);

  vector<int>c(n);

  rep(i,n)cin>>a[i]>>b[i]>>c[i];

  rep(i,n){

    rep(j,3){

      if(j==0){

        chmax(dp[i+1][1],dp[i][0]+b[i]);

        chmax(dp[i+1][2],dp[i][0]+c[i]);

      }

      if(j==1){

        chmax(dp[i+1][2],dp[i][1]+c[i]);

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

      }

      if(j==2){

        chmax(dp[i+1][0],dp[i][2]+a[i]);

        chmax(dp[i+1][1],dp[i][2]+b[i]);

      }

    }

  }

  int ans=0;

  rep(i,3){

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

  }

  cout<<ans<<endl;

  return(0);

}
// Slower C++ solution:
// 27/07/19 // 16:32 PM //

// O(n)



#include <iostream>

#include <cstring>



using namespace std;



#define MAXN 100010



int n;

int value[MAXN][10];

int memo[MAXN][10];



int funcao(int i, int ant){  // ant = 0, 1, 2

    if(memo[i][ant]>=0) return memo[i][ant];



    if(i>n) return 0;



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

        if(k!=ant)

            memo[i][ant] = max(memo[i][ant], value[i][k] + funcao(i+1, k));



    return memo[i][ant];

}



int main(){



    ios_base::sync_with_stdio(false);

    cin.tie(NULL);



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



    cin >> n;



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

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



    cout << funcao(1, 4) << "\n";



    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 5.02338623778745x):
// 27/07/19 // 16:32 PM //

// O(n)



#include <iostream>



using namespace std;



#define MAXN 100010



int n;

int value[MAXN][10];

int memo[MAXN][10];



int funcao(){  // ant = 0, 1, 2





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

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

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

                if(j!=k)

                   memo[i][k] = max(memo[i][k], memo[i-1][j] + value[i][k]);





    return max(memo[n][0], max(memo[n][1], memo[n][2]));

}



int main(){



    ios_base::sync_with_stdio(false);

    cin.tie(NULL);



    cin >> n;



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

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





    cout << funcao() << "\n";



    return 0;

}