stablegradients/pie-gem5-bysrc huggingface dataset:
// Original C++ solution (slower):
#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";

}

// Optimized solution to the same problem (faster):
#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]));

}

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

#include <ctime>

#include <unordered_map>

#include <stdlib.h>

using namespace std;

#define ll  long long int

#define mod 1000000007

#define modd 998244353

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

// #include<boost/multiprecision/cpp_int.hpp>

// using boost ::multiprecision::cpp_int;

/*__builtin_clz();

__builtin_ctz();

__builtin_popcount();*/

ll n;

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

ll dp[100002][4];

ll fun(ll cnt ,char x)

{

    if(cnt == n+1)

    {

        return 0;

    }

    ll k;

    if(x=='A') k=1;

    else if(x=='B') k=2;

    else k=3;

    if(dp[cnt][k] != -1)

    {

        return dp[cnt][k];

    }

    ll z,y;

    if(x=='A')

    {

       z= a[cnt]+ fun(cnt+1,'B');

       y= a[cnt] + fun(cnt+1,'C');

    }

    if(x=='B')

    {

        z= b[cnt] + fun(cnt+1,'C');

        y= b[cnt] + fun(cnt+1,'A');

    }

    if(x=='C')

    {

        z = c[cnt] + fun(cnt+1,'A');

        y= c[cnt] + fun(cnt+1,'B');

    }

    return dp[cnt][k] =  max(z,y);

}

int main()

{

    boost;

    memset(dp,-1,sizeof dp); 

    cin>>n;

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

    {

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

    }

   ll x =  fun(1,'A');

   ll y = fun(1,'B');

   ll z =  fun(1,'C');

   cout<<max(x,max(y,z))<<"\n";

    cerr << "\nTime elapsed: " << 1000 * clock()/CLOCKS_PER_SEC << "ms\n";

}

/* AnsHuman_7 || Anshuman Gaur */


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

#include <ctime>

#include <unordered_map>

#include <stdlib.h>

using namespace std;

#define ll  long long int

#define mod 1000000007

#define modd 998244353

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

// #include<boost/multiprecision/cpp_int.hpp>

// using boost ::multiprecision::cpp_int;

/*__builtin_clz();

__builtin_ctz();

__builtin_popcount();*/

int main()

{

    boost;

    ll n;

    cin>>n;

    ll a[n+1],b[n+1],c[n+1];

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

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

    ll dp[n+1][4];

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

    {

        if(i==1)

        {

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

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

            dp[1][3] = c[1];

        }

        else 

        {

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

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

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

        }

    }

    cout<<max(dp[n][1],max(dp[n][2],dp[n][3]))<<"\n";



    cerr << "\nTime elapsed: " << 1000 * clock()/CLOCKS_PER_SEC << "ms\n";

}

/* AnsHuman_7 || Anshuman Gaur */


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



using namespace std;

int a[100009][4],dp[100009][5],n,k;

int sol(int x,int y)

{

    if (x==n) return 0;

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

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

    {

        if (i==y) continue;

        dp[x][y]=max(dp[x][y],sol(x+1,i)+a[x][i]);

    }

    return dp[x][y];

}

int main()

{

    cin>>n;

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

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

    {

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

    }

    cout<<sol(0,3);

    return 0;

}


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

using namespace std;

long long dp[100099][3];

int n,k,a[100099][3];

int main()

{

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

   {

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

       {

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

           {

               if (k==j) continue;

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

           }

       }

   }

   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 M[100010][4];

int dp[100010][4];

int g(int i , int sw){

    if(i == 0 ) return 0;

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

    if(sw == 0){

        dp[i][sw] = max(g(i-1,1)+M[i][1],g(i-1,2)+M[i][2]);

    }else{

        if(sw==1){

            dp[i][sw] = max(g(i-1,0)+M[i][0],g(i-1,2)+M[i][2]);

        }else{

            dp[i][sw] = max(g(i-1,0)+M[i][0],g(i-1,1)+M[i][1]);

        }

    }

    return dp[i][sw];

}

int main(){

    memset(dp,-1,sizeof dp);

    memset(M,0,sizeof M);

    int n;cin>>n;

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

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

    }

    int res = max(g(n,1),g(n,0));

    res = max(res,g(n,2));

    cout<<res<<"\n";

    return 0;

}




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

using namespace std;

#define PB push_back

#define MP make_pair

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

typedef long long ll;

typedef int i;

typedef vector<i>vi;

typedef pair<i,i>pi;

int M[100010][4];

int dp[100010][4];



int g(int i,int sw){

    if(i < 0) return 0;

    int &r = dp[i][sw];

    if(r!=-1) return r;



    if(sw == 0){

        r = max( g(i-1 , 1) , g(i-1,2) ) + M[i][0];

    }

    else{

        if(sw == 1){

            r = max( g(i-1 , 0) , g(i-1,2) ) + M[i][1];

        }else{

            r = max( g(i-1 , 0) , g(i-1,1) ) + M[i][2];

        }

    }

    return r;



}

int main(){

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

    ios::sync_with_stdio(0);

    cin.tie(0);

    int n;

    while(cin>>n){

        memset(dp,-1,sizeof dp);

        fora(i,0,n){

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

        }



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

        a = max(a , g(n-1,1));

        a = max(a , g(n-1,2));

        cout<<a<<"\n";

    }



    return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):


#include <bits/stdc++.h>



using namespace std;



const int N = 1e5 + 5 , M = 5;



int n;

int happiness[N][M];



int mem[N][M]; // definition: maximum happiness by doing activity M on day N



int dp(int day , int lastActivity){



    // last day

    if(day == n) return 0;



    if(mem[day][lastActivity] != -1) return mem[day][lastActivity];





    int ans = 0;



    // try to do an activity on this day

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



            // this activity must not be equal to the activity that have been done on the day before

            if(newActivity != lastActivity){



                    ans = max(ans , happiness[day][newActivity] + dp(day + 1 , newActivity));



            }

    }



    mem[day][lastActivity] = ans; // saving the answer of this state



    return mem[day][lastActivity]; // returning the answer of this state after saving

}





main(){



    scanf("%d" , &n);



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



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



                    scanf("%d" , &happiness[day][activity]);



            }

    }



    int ans = 0;



    memset(mem , -1 , sizeof mem);



    // try to do first 3 activities

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



            ans = max(ans , happiness[0][activity] + dp(1 , activity));



    }



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



    return 0;

}


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


#include <bits/stdc++.h>



using namespace std;





const int N = 1e5 + 5 , M = 5;



int happiness[N][M];

int maxHappiness[N][M]; // definition: maximum happiness on day N by doing activity M on that day



int n;



main(){



    scanf("%d" , &n);



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



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



                    scanf("%d" , &happiness[day][activity]);



            }

    }



    // initialize dp table with base values

    maxHappiness[0][0] = happiness[0][0];

    maxHappiness[0][1] = happiness[0][1];

    maxHappiness[0][2] = happiness[0][2];



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



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



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





                            if(newActivity == lastActivity) continue; // same activity



                            maxHappiness[currentDay][newActivity] =



                            max(maxHappiness[currentDay  ][newActivity ] ,

                                maxHappiness[currentDay-1][lastActivity] + happiness[currentDay][newActivity]);



                    }



            }

    }



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

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



    return 0;

}


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



using namespace std;

int n,k;

int ar[100005][4];

long long mem[100005][4];



long long solve(int ind,int prev){

    if(ind==n){

        return 0;

    }

    if(mem[ind][prev]!=-1){

        return mem[ind][prev];

    }

    int ch1=solve(ind+1,1)+ar[ind][0],ch2=solve(ind+1,2)+ar[ind][1],ch3=solve(ind+1,3)+ar[ind][2];

    if(prev==1){

        return mem[ind][prev]=max(ch2,ch3);

    }

    else if(prev==2){

        return mem[ind][prev]=max(ch1,ch3);

    }

    else if(prev==3){

        return mem[ind][prev]=max(ch1,ch2);

    }

    else{

        return mem[ind][prev]=max(ch1,max(ch2,ch3));

    }

}



int main() {

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

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

    ios::sync_with_stdio(0);

    cin.tie(0);

    cout.tie(0);

    memset(mem,-1,sizeof mem);

    cin>>n;

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

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

    }

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

    return 0;

}

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

using namespace std;

#define ll long long



//const INF = 1e9+5;



int main() {

    ios::sync_with_stdio(0);

    cin.tie(0);

    cout.tie(0);



    int n;

    cin>>n;

    vector<int> dp(3);

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

        vector<int> ndp(3,0);

        int ar[3];

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

            cin>>ar[j];

        }

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

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

                if(j!=k){

                    ndp[k]=max(ndp[k],dp[j]+ar[k]);

                }

            }

        }

        dp.swap(ndp);

    }

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

    return 0;

}

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



using namespace std;

int n;

int ar[100005][4];

int mem[100005][4];



int solve(int ind,int prv){

    if(ind==n){

        return 0;

    }

    if(mem[ind][prv]!=-1){

        return mem[ind][prv];

    }

    int ret=0;

    if(prv==0){

        ret=max(ret,solve(ind+1,1)+ar[ind][1]);

        ret=max(ret,solve(ind+1,2)+ar[ind][2]);

    }else if(prv==1){

        ret=max(ret,solve(ind+1,2)+ar[ind][2]);

        ret=max(ret,solve(ind+1,0)+ar[ind][0]);

    }else if(prv==2){

        ret=max(ret,solve(ind+1,0)+ar[ind][0]);

        ret=max(ret,solve(ind+1,1)+ar[ind][1]);

    }else {

        ret=max(ret,solve(ind+1,0)+ar[ind][0]);

        ret=max(ret,solve(ind+1,1)+ar[ind][1]);

        ret=max(ret,solve(ind+1,2)+ar[ind][2]);

    }

    return mem[ind][prv]=ret;

}



int main() {

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

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

    ios::sync_with_stdio(0);

    cin.tie(0);

    cout.tie(0);

    memset(mem,-1,sizeof mem);

    cin>>n;

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

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

        {

            cin>>ar[i][j];

        }

    }

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

    return 0;

}

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

using namespace std;

#define ll long long



//const INF = 1e9+5;



int main() {

    ios::sync_with_stdio(0);

    cin.tie(0);

    cout.tie(0);



    int n;

    cin>>n;

    vector<int> dp(3);

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

        vector<int> ndp(3,0);

        int ar[3];

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

            cin>>ar[j];

        }

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

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

                if(j!=k){

                    ndp[k]=max(ndp[k],dp[j]+ar[k]);

                }

            }

        }

        dp.swap(ndp);

    }

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

    return 0;

}

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



using namespace std;

int n;

int ar[100005][4];

int dp[100005][4];



int main() {

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

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

    ios::sync_with_stdio(0);

    cin.tie(0);

    cout.tie(0);

    memset(dp,-1,sizeof dp);

    cin>>n;

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

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

        {

            cin>>ar[i][j];

        }

    }

    dp[0][0]=ar[0][0];

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

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

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

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

            int tmp=0;

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

                if(j==k){

                    continue;

                }

                tmp=max(tmp,dp[ind-1][k]);

            }

            dp[ind][j]=ar[ind][j]+tmp;

        }

    }

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

    return 0;

}

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

using namespace std;

#define ll long long



//const INF = 1e9+5;



int main() {

    ios::sync_with_stdio(0);

    cin.tie(0);

    cout.tie(0);



    int n;

    cin>>n;

    vector<int> dp(3);

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

        vector<int> ndp(3,0);

        int ar[3];

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

            cin>>ar[j];

        }

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

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

                if(j!=k){

                    ndp[k]=max(ndp[k],dp[j]+ar[k]);

                }

            }

        }

        dp.swap(ndp);

    }

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

    return 0;

}

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



using namespace std;

#define ll long long

const int N=100005;

int n;

int ar[N],br[N],cr[N];

int mem[N][5];



int solve(int ind,int lst){

    if(ind>=n){

        return 0;

    }

    if(mem[ind][lst]!=-1){

        return mem[ind][lst];

    }

    int ret=0;

    if(lst==1){

        ret=max(ret,solve(ind+1,2)+br[ind]);

        ret=max(ret,solve(ind+1,3)+cr[ind]);

    }

    else if(lst==2){

        ret=max(ret,solve(ind+1,1)+ar[ind]);

        ret=max(ret,solve(ind+1,3)+cr[ind]);



    }

    else if(lst==3){

        ret=max(ret,solve(ind+1,1)+ar[ind]);

        ret=max(ret,solve(ind+1,2)+br[ind]);



    }

    else{

        ret=max(ret,solve(ind+1,1)+ar[ind]);

        ret=max(ret,solve(ind+1,2)+br[ind]);

        ret=max(ret,solve(ind+1,3)+cr[ind]);



    }

    return mem[ind][lst]=ret;

}



int main() {

    ios::sync_with_stdio(0);

    cin.tie(0);

    cout.tie(0);

    memset(mem,-1,sizeof mem);

    cin>>n;

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

        cin>>ar[i]>>br[i]>>cr[i];

    }

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

    return 0;

}

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

using namespace std;

#define ll long long



//const INF = 1e9+5;



int main() {

    ios::sync_with_stdio(0);

    cin.tie(0);

    cout.tie(0);



    int n;

    cin>>n;

    vector<int> dp(3);

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

        vector<int> ndp(3,0);

        int ar[3];

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

            cin>>ar[j];

        }

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

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

                if(j!=k){

                    ndp[k]=max(ndp[k],dp[j]+ar[k]);

                }

            }

        }

        dp.swap(ndp);

    }

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

    return 0;

}

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

#define int long long int

#define pb push_back

#define inf 1e9

#define pp pop_back

#define pii pair<int,int>

#define vec vector<int>

#define mp make_pair

using namespace std;

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

int dp[100001][4];

int ans(int n,int x)

{

  if(n==0)

     return 0;

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

    return dp[n][x];

  if(x==0)

    return dp[n][x]=max(a[n]+ans(n-1,1),max(b[n]+ans(n-1,2),c[n]+ans(n-1,3)));

  else if(x==1)

    return dp[n][x]=max(b[n]+ans(n-1,2),c[n]+ans(n-1,3));

  else if(x==2)

    return dp[n][x]=max(a[n]+ans(n-1,1),c[n]+ans(n-1,3));

  else

    return dp[n][x]=max(a[n]+ans(n-1,1),b[n]+ans(n-1,2));

}

int32_t main()

{

  ios_base::sync_with_stdio(false);

  cin.tie(NULL);

  cout.tie(NULL);

  int t=1;

  //cin>>t;

  while(t--)

  {

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

    int n;

    cin>>n;

    int i;

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

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

    cout<<ans(n,0);

  }

}


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

#define int long long int

#define pb push_back

#define inf 1e9

#define pp pop_back

#define pii pair<int,int>

#define vec vector<int>

#define mp make_pair

using namespace std;

int32_t main()

{

  ios_base::sync_with_stdio(false);

  cin.tie(NULL);

  cout.tie(NULL);

  int t=1;

  //cin>>t;

  while(t--)

  {

    int n;

    cin>>n;

    int i;

    int a[n+1],b[n+1],c[n+1];

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

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

    int dp[n+1][4];

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

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

    {

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

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

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

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

    }

    cout<<dp[n][0];

  }

}


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

using namespace std;

int vacation(int a[][3],int n,int last,int i,int dp[100001][3]){

    if(i==n){return 0;    }

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

    int r;

    if(i==0){

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

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

        

    }

    else {

        if(last==0){

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

        }

        else if(last==1){

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

        }

        else if(last==2){

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

        }

    }

    return dp[i][last];

}

int main() {

	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[100001][3];

	memset(dp,-1,sizeof dp);

	cout<<vacation(a,n,0,0,dp);

	return 0;

}

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

using namespace std;



int main() {

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

    }

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

	return 0;

}

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

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

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

typedef long long ll;

using namespace std;

const int MOD = 1000000007;

const int INF = 1010000000;

const double EPS = 1e-10;

const array<pair<int,int>,4> fd{make_pair(1,0),make_pair(-1,0),make_pair(0,1),make_pair(0,-1)}; 



int main(){

  int n, a[100000][3];cin>>n;

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

  int dp[100000][3];rep(i,100000)rep(j,3)dp[i][j] = 0;

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

  repd(i,1,n){

    rep(j,3){

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

    }

  }

  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>

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

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

#define int long long

typedef long long ll;

const int MOD = 1000000007;

const int INF = 1010000000;

const double EPS = 1e-10;

using namespace std;

const pair<int,int> fd[] = {make_pair(1,0),make_pair(-1,0),make_pair(0,1),make_pair(0,-1)}; 



signed main() {

  int n;cin>>n;

  int dp[110000][3];

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

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

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

  rep(i,n-1){

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

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

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

  }

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

}

// Fastest known solution for this problem: