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

#define sf1(n) scanf("%d",&n)

#define sf2(n,q) scanf("%d %d",&n,&q)

#define f first

#define ss second

#define pb push_back

#define lli long long

#define mem(ar,false) memset(ar,false,sizeof(ar))

#define mpr make_pair

#define endl "\n"

#define mod 1000000007

#define inf 9000000000000000000LL

using namespace std;



int ar[200005][5];

int n,k;

int dp[100005][5];

int func(int i,int j)

{



    if(i==n+1)

        return 0;

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

        return dp[i][j];

    int ret = INT_MIN;



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

    {

        if(k==j)

            continue;

       ret = max(ret,ar[i][k]+func(i+1,k));

    }

    return dp[i][j] = ret;

}



int main()

{

    ios_base::sync_with_stdio(false);

    cin.tie(NULL);

    mem(dp,-1);

    cin >> n ;

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

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



     int ans = func(1,0);

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

    {

        int ret = INT_MAX;

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

        {

            if(i-j>=0)

                ret = min(ret,dp[i-j]+abs(ar[i]-ar[i-j]));

        }

        dp[i] = ret;

    }*/



    cout << ans << endl;

    return 0;



}


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

#define sf1(n) scanf("%d",&n)

#define sf2(n,q) scanf("%d %d",&n,&q)

#define f first

#define ss second

#define pb push_back

#define lli long long

#define mem(ar,false) memset(ar,false,sizeof(ar))

#define mpr make_pair

#define endl "\n"

#define mod 1000000007

#define inf 9000000000000000000LL

using namespace std;



int ar[200005][5];

int n,k;

int dp[100005][5];

int func(int i,int j)

{



    if(i==n+1)

        return 0;

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

        return dp[i][j];

    int ret = INT_MIN;



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

    {

        if(k==j)

            continue;

       ret = max(ret,ar[i][k]+func(i+1,k));

    }

    return dp[i][j] = ret;

}



int main()

{

    ios_base::sync_with_stdio(false);

    cin.tie(NULL);

   // mem(dp,-1);

    cin >> n ;

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

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





     //int ans = func(1,0);

     int j=0;

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

    {

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

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

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

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

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

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

    }



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

    return 0;



}


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

using namespace std;

//vector<vector<long long int> > dp(100001,vector<long long int>(4));

long long int dp[100001][4];

long long int n;

long long int gen(int i,int p,vector<vector<long long int> >&arr)

{

    long long int ans=INT_MIN;

    if(i>=n)

        return 0;

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

        return dp[i][p];

    if(p==0)

    {

        ans=max(ans,arr[i][1]+gen(i+1,1,arr));

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

    }

    else if(p==1)

    {

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

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

    }

    else if(p==2)

    {

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

       ans=max(ans,arr[i][1]+gen(i+1,1,arr));



    }

    else{

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

        ans=max(ans,arr[i][1]+gen(i+1,1,arr));

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

    }

    return dp[i][p]=ans;



}

int main()

{

    //int n;

    cin>>n;

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

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

    {

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

        {

            cin>>arr[i][j];

        }

    }

    //input part has been done

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



    long long ans=gen(0,3,arr);

    cout<<ans;



}


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

using namespace std;

long long int dp[100001][3];

int main()

{

    int n;

    cin>>n;

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

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

    {

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

    }

    //input part has been done

    //now initialization for day 1

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

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

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

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

    {

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



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



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

    }

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

    cout<<ans<<endl;

}


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

#include <vector>

#include <algorithm>

#include <array>

#include <cstring>



using namespace std;



const int INF = 1e9 + 7;



int n;

int happy[100000][3];

long long dp[100000][4];



long long dfs(int now, int before){

    if(now == n) return 0;

    if(~dp[now][before]) return dp[now][before];

    long long res = 0;

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

        if(before == i) continue;

        res = max(res, happy[now][i] + dfs(now + 1, i));

    }

    return dp[now][before] = res;

}



int main(){

    cin >> n;

    memset(dp, -1, sizeof dp);

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

        for(auto&& j : happy[i]){

            cin >> j;

        }

    }

    cout << dfs(0, 3) << "\n";

}

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

#include <vector>

#include <algorithm>

#include <array>

#include <cstring>



using namespace std;



const int INF = 1e9 + 7;



int n;

vector<array<int, 3>> happy;

int dp[100000][4];



int dfs(int now, int before){

    if(now == n) return 0;

    if(~dp[now][before]) return dp[now][before];

    int res = 0;

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

        if(before == i) continue;

        res = max(res, happy[now][i] + dfs(now + 1, i));

    }

    return dp[now][before] = res;

}



int main(){

    cin >> n;

    happy.resize(n);

    memset(dp, -1, sizeof dp);

    for(auto&& i : happy){

        for(auto&& j : i){

            cin >> j;

        }

    }

    cout << dfs(0, 3) << "\n";

}


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



using namespace std;

typedef long long int ll;



#define p(V) for(int iiv=0;iiv<V.size();iiv++) cout<<V[iiv]<<" ";

#define mp make_pair

#define pb push_back

#define graph vector<vector<int> >

#define pf first

#define ps second

#define pq priority_queue

const int N = 2e5;

const int l = 18;

const double pi = acos(-1);

struct d{

    int a , b , c;

    d(){

        a=b=c=0;

    }

};

int dp[N][4];

int n;

vector<d> day;

int calc(int idx,int back){

    if(idx==n) return 0;

    int &dp = ::dp[idx][back];

    if(dp!=-1) return dp;

    if(back!=0) dp=max(dp,day[idx].a+calc(idx+1,0));

    if(back!=1) dp=max(dp,day[idx].b+calc(idx+1,1));

    if(back!=2) dp=max(dp,day[idx].c+calc(idx+1,2));

    return dp;

}

int main(){   

    memset(dp,-1,sizeof dp);

    cin >> n;

    day.resize(n);

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

    cout<<calc(0,3);

    return 0;

}


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



using namespace std;

typedef long long int ll;



#define p(V) for(int iiv=0;iiv<V.size();iiv++) cout<<V[iiv]<<" ";

#define mp make_pair

#define pb push_back

#define graph vector<vector<int> >

#define pf first

#define ps second

#define pq priority_queue

const int N = 2e5;

const int l = 18;

const double pi = acos(-1);

struct d{

    int a , b , c;

    d(){

        a=b=c=0;

    }

};

int dp[N][3];

int n;

vector<d> day;

int main(){   

    cin >> n;

    day.resize(n);

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

    dp[n-1][0] = day[n-1].a;

    dp[n-1][1] = day[n-1].b;

    dp[n-1][2] = day[n-1].c;

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

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

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

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

    }

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

    return 0;

}


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

using namespace         std;

#define                 int long long

#define                 pb push_back

#define                 mp make_pair

#define st(arr,a)       memset(arr,a,sizeof arr)

#define                 nl cout<<endl

#define                 real signed

#define                 endl '\n'              

#define bitcount(x)     __builtin_popcountll(x)

const int MOD = 1000000007ll;

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

#define rfor(i,a,b)     for(int i=a;i>=b;i--)



int tot = 100500;

int n,dp[100500][3],arr[100500][3];





int ans(int ind,int cur)

{

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

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



   dp[ind][cur]=-MOD*MOD;



   forn(j,0,2)

   {

       if(j==cur)continue;

       dp[ind][cur]=max(dp[ind][cur],arr[ind][j]+ans(ind+1,j));

   }

   return dp[ind][cur];





}







signed main() 

{

	ios_base::sync_with_stdio(false);

    cin.tie(NULL);

    cin>>n;

    st(dp,-1);



    forn(i,1,n)

        forn(j,0,2) 

            cin>>arr[i][j];

    

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





    



     

     









     return 0;



}



//       g++ -g c2.cpp ; ./a.exe

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

using namespace         std;

#define                 int long long

#define                 pb push_back

#define                 mp make_pair

#define st(arr,a)       memset(arr,a,sizeof arr)

#define                 nl cout<<endl

#define                 real signed

#define                 endl '\n'              

#define bitcount(x)     __builtin_popcountll(x)

const int MOD = 1000000007ll;

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

#define rfor(i,a,b)     for(int i=a;i>=b;i--)

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

#define pi pair<int,int>

#define X first

#define Y second

#define N 1014159

#define  vi vector<int>

#define  v  vector



int n;

int A[4][N];

int dp[N][5];



void solve()

{

    cin>>n;

    forn(i,1,n)

    {

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

    }

    

    forn(i,1,n)

    {

        forn(j,1,3)

        {

           forn(k,1,3)

            {

                if(k == j)continue;

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

            }

        }    

    }



    int x = 0;

    forn(i,1,3)x = max(x,dp[n][i]);

    cout<<x;

    

}





signed main() 

{

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

    int t = 1;

    //cin>>t;

    while(t--)

    {

        solve();

    }







    return 0;



}



//       g++ -g c.cpp ; ./a.exe

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

using namespace std;

int n;

vector <int> sw(100005);

vector <int> bg(100005);

vector <int> hw(100005);

int dp[100005][3];

int solve(int i, int t){

    if(i>n){

        return 0;

    }

    if(dp[i][t]!=-1){

        return dp[i][t];

    }

    int q = 0;

    if(t==1){

        q = sw[i]+ max(solve(i+1,2), solve(i+1, 3));

    }

    else if(t==2){

         q = bg[i]+ max(solve(i+1,1), solve(i+1, 3));

    }

    else{

         q = hw[i]+ max(solve(i+1,2), solve(i+1, 1));

    }

    dp[i][t] = q;

    return q;

}

void initialize(){

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

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

            dp[i][j]=-1;

        }

    }

}

int main(){

    initialize();

    cin>>n;

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

        cin>>sw[i]>>bg[i]>>hw[i];

    }

    int ans = solve(1,1);

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

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

    cout<<ans<<endl;

}


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

using namespace std;

vector <int> dp;

int main(){

	int n;

  	cin>>n;

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

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

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

	}

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

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

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

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

				if(j==k) continue;

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

			}

		}

	}

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

}

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

using namespace std;

typedef long long int ll;

const int MAX=1e5+10;

int N,A[MAX],B[MAX],C[MAX],dp[MAX][3];

int solve(int idx,int pr)

{

    if(idx>N)return 0;

    if(dp[idx][pr]!=-1)return dp[idx][pr];

    int res=0;

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

        if(i==pr)continue;

        int val=0;

        if(i==1)val=A[idx];

        if(i==2)val=B[idx];

        if(i==3)val=C[idx];

        res = max(res,solve(idx+1,i)+val);

    }

    return dp[idx][pr]=res;

}

int  main ()

{

    memset(dp,-1,sizeof dp);

    cin >> N;

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

        cin >> A[i] >> B[i] >> C[i];

    }

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

    return 0;

}






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

#include<algorithm>

using namespace std;

int dp[2][3];

int memo[3];

int k[2] = { 0,1 };



int main(){

    int day;

    scanf("%d", &day); 

    int i = 1;

    while (day--) {

        scanf("%d%d%d", &memo[0], &memo[1], &memo[2]);

        dp[k[i % 2]][0] = memo[0] + max(dp[k[(i + 1) % 2]][1], dp[k[(i + 1) % 2]][2]);

        dp[k[i % 2]][1] = memo[1] + max(dp[k[(i + 1) % 2]][0], dp[k[(i + 1) % 2]][2]);

        dp[k[i % 2]][2] = memo[2] + max(dp[k[(i + 1) % 2]][1], dp[k[(i + 1) % 2]][0]);

        i++;

    }

    printf("%d\n", max(max(dp[k[(i + 1) % 2]][0], dp[k[(i + 1) % 2]][1]), dp[k[(i + 1) % 2]][2]));

    return 0;

}


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

using namespace std;

#define print printf("==================\n")

#define ll long long

#define pi acos(-1.0)

#define eps 1e-16

#define max3(a,b,c) max(a,max(b,c))

const ll INF = 1 << 30;

typedef pair<ll, ll  > payar;

typedef struct

{

    ll  x, y;

} point;

priority_queue<payar, vector<payar>,  greater<payar> > pq; ///accending

vector< pair<ll,payar  > > vpp;

vector < payar >  vp;



ll n ;

int ara[100005][4];

ll dp[100005][4];

ll cal(int day, int itm)

{

    if(day>n)

    {

        return 0;

    }



    if(dp[day][itm]!=-1)

        return dp[day][itm];

    ll maxi = -1;

    if(day==1)

    {

   return   maxi = dp[day][itm]=max3( cal(day+1,1)+ara[day][1], cal(day+1,2)+ara[day][2], cal(day+1,3)+ara[day][3] );



    }

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

    {

            if(i==itm)

                continue;

            maxi=max( cal(day+1,i)+ara[day][i],maxi);





    }

    return dp[day][itm]=maxi;

}



int main()

{



    cin>>n;

    memset(dp,-1,sizeof dp);

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

    {

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

        {

            cin>>ara[i][j];

        }

    }

    cout<<cal(1,1);







    return  0 ;

}


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

#include<algorithm>

using namespace std;

int dp[2][3];

int memo[3];

int k[2] = { 0,1 };



int main(){

    int day;

    scanf("%d", &day); 

    int i = 1;

    while (day--) {

        scanf("%d%d%d", &memo[0], &memo[1], &memo[2]);

        dp[k[i % 2]][0] = memo[0] + max(dp[k[(i + 1) % 2]][1], dp[k[(i + 1) % 2]][2]);

        dp[k[i % 2]][1] = memo[1] + max(dp[k[(i + 1) % 2]][0], dp[k[(i + 1) % 2]][2]);

        dp[k[i % 2]][2] = memo[2] + max(dp[k[(i + 1) % 2]][1], dp[k[(i + 1) % 2]][0]);

        i++;

    }

    printf("%d\n", max(max(dp[k[(i + 1) % 2]][0], dp[k[(i + 1) % 2]][1]), dp[k[(i + 1) % 2]][2]));

    return 0;

}


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

#include <cstdint>

#include <cinttypes>

#include <cstring>

#include <algorithm>



using i32 = std::int_fast32_t;



const int MaxN = 100001;



int A[MaxN][3], dp[MaxN][3];



signed main(signed argc, char **argv) {

	i32 n;

	std::scanf("%" SCNdFAST32, &n);

	

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

		std::scanf("%" SCNdFAST32 "%" SCNdFAST32 "%" SCNdFAST32, A[i], A[i] + 1, A[i] + 2);

	}

	

	memset(dp, 0, sizeof dp);

	

	dp[0][0] = A[0][0];

	dp[0][1] = A[0][1];

	dp[0][2] = A[0][2];

	

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

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

			for (i32 a = 0; a < 3; ++ a) {

				if (a == j) {

					continue;

				}

				dp[i][j] = std::max(dp[i][j], dp[i - 1][a] + A[i][j]);

			}

		}

	}

	

	std::printf("%" PRIdFAST32, std::max(dp[n - 1][0], std::max(dp[n - 1][1], dp[n - 1][2])));

	return 0;

}

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

#include<algorithm>

using namespace std;

int dp[2][3];

int memo[3];

int k[2] = { 0,1 };



int main(){

    int day;

    scanf("%d", &day); 

    int i = 1;

    while (day--) {

        scanf("%d%d%d", &memo[0], &memo[1], &memo[2]);

        dp[k[i % 2]][0] = memo[0] + max(dp[k[(i + 1) % 2]][1], dp[k[(i + 1) % 2]][2]);

        dp[k[i % 2]][1] = memo[1] + max(dp[k[(i + 1) % 2]][0], dp[k[(i + 1) % 2]][2]);

        dp[k[i % 2]][2] = memo[2] + max(dp[k[(i + 1) % 2]][1], dp[k[(i + 1) % 2]][0]);

        i++;

    }

    printf("%d\n", max(max(dp[k[(i + 1) % 2]][0], dp[k[(i + 1) % 2]][1]), dp[k[(i + 1) % 2]][2]));

    return 0;

}


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

using namespace std;

#define N (int)2e5

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



// a = 0, b = 1, c = 2

int dp(int curr , int last) {

    if (curr > n) return 0;

    int &sol = memo[curr][last];

    if (sol != -1) return sol;

    if (last == 0) {

        sol = max(

            dp(curr + 1 , 1) + b[curr] ,

            dp(curr + 1 , 2) + c[curr]

        );

    } else if (last == 1) {

        sol = max(

            dp(curr + 1 , 0) + a[curr] ,

            dp(curr + 1 , 2) + c[curr]

        );

    } else {

        sol = max(

            dp(curr + 1 , 0) + a[curr] ,

            dp(curr + 1 , 1) + b[curr]

        );

    }

    return sol;

}



int main() {

    scanf("%d" , &n);

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

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

    memset (memo , -1 , sizeof memo);

    printf("%d\n" , max(dp(2 , 0) + a[1] , max(dp(2 , 1) + b[1] , dp(2 , 2) + c[1])));

}


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

#include<algorithm>

using namespace std;

int dp[2][3];

int memo[3];

int k[2] = { 0,1 };



int main(){

    int day;

    scanf("%d", &day); 

    int i = 1;

    while (day--) {

        scanf("%d%d%d", &memo[0], &memo[1], &memo[2]);

        dp[k[i % 2]][0] = memo[0] + max(dp[k[(i + 1) % 2]][1], dp[k[(i + 1) % 2]][2]);

        dp[k[i % 2]][1] = memo[1] + max(dp[k[(i + 1) % 2]][0], dp[k[(i + 1) % 2]][2]);

        dp[k[i % 2]][2] = memo[2] + max(dp[k[(i + 1) % 2]][1], dp[k[(i + 1) % 2]][0]);

        i++;

    }

    printf("%d\n", max(max(dp[k[(i + 1) % 2]][0], dp[k[(i + 1) % 2]][1]), dp[k[(i + 1) % 2]][2]));

    return 0;

}


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

using namespace std;

typedef long long LL;



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

int dp[100007][5];



int Solve(int idx, int n, int prev){

	

	if(idx == n)return 0;

	

	if(dp[idx][prev] != -1)return dp[idx][prev];

	

	int ans = -1;

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

		if(i == prev)continue;

		if(i == 1)ans = max(ans, a[idx] + Solve(idx+1, n, i));

		else if(i == 2)ans = max(ans, b[idx] + Solve(idx+1, n, i));

		else ans = max(ans, c[idx] + Solve(idx+1, n, i));		

	}

	return dp[idx][prev] = ans;

}



int main(){

	int n; cin>>n;

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

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

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

	}

	int ret = -1;

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

		ret = max(ret, Solve(0,n,i));

	}

	cout<<ret<<endl;



//	cout<<Solve(0,n,0)<<endl;

	return 0;

}

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

#include<algorithm>

using namespace std;

int dp[2][3];

int memo[3];

int k[2] = { 0,1 };



int main(){

    int day;

    scanf("%d", &day); 

    int i = 1;

    while (day--) {

        scanf("%d%d%d", &memo[0], &memo[1], &memo[2]);

        dp[k[i % 2]][0] = memo[0] + max(dp[k[(i + 1) % 2]][1], dp[k[(i + 1) % 2]][2]);

        dp[k[i % 2]][1] = memo[1] + max(dp[k[(i + 1) % 2]][0], dp[k[(i + 1) % 2]][2]);

        dp[k[i % 2]][2] = memo[2] + max(dp[k[(i + 1) % 2]][1], dp[k[(i + 1) % 2]][0]);

        i++;

    }

    printf("%d\n", max(max(dp[k[(i + 1) % 2]][0], dp[k[(i + 1) % 2]][1]), dp[k[(i + 1) % 2]][2]));

    return 0;

}


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

#include <stdlib.h>

#include <string.h>

#include <math.h>

#include <queue>

#include <stack>

#include <vector>

#include <algorithm>

#include <iostream>



using namespace std;



const int MAXN = 1e5 + 5;

int n,m;

int a,b,c;

int d,e,f;

vector <int> A;

vector <int> B;

vector <int> C;

vector <int> dp[MAXN];



int main()

{

	scanf("%d",&n);

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

	{

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

		A.push_back(a);//第一天的a现在是a[0]

		B.push_back(b);

		C.push_back(c);

	}

	dp[0].push_back(A[0]);

	dp[0].push_back(B[0]);

	dp[0].push_back(C[0]);

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

	{

		d = max(dp[i - 1][1],dp[i - 1][2]);

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

		f = max(dp[i - 1][0],dp[i - 1][1]);

		dp[i].push_back(d + A[i]);

		dp[i].push_back(e + B[i]);

		dp[i].push_back(f + C[i]);

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

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

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

	}

	m = max(max(dp[n - 1][1],dp[n - 1][2]),dp[n - 1][0]);

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

	return 0;

}

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

#include<algorithm>

using namespace std;

int dp[2][3];

int memo[3];

int k[2] = { 0,1 };



int main(){

    int day;

    scanf("%d", &day); 

    int i = 1;

    while (day--) {

        scanf("%d%d%d", &memo[0], &memo[1], &memo[2]);

        dp[k[i % 2]][0] = memo[0] + max(dp[k[(i + 1) % 2]][1], dp[k[(i + 1) % 2]][2]);

        dp[k[i % 2]][1] = memo[1] + max(dp[k[(i + 1) % 2]][0], dp[k[(i + 1) % 2]][2]);

        dp[k[i % 2]][2] = memo[2] + max(dp[k[(i + 1) % 2]][1], dp[k[(i + 1) % 2]][0]);

        i++;

    }

    printf("%d\n", max(max(dp[k[(i + 1) % 2]][0], dp[k[(i + 1) % 2]][1]), dp[k[(i + 1) % 2]][2]));

    return 0;

}


// Fastest known solution for this problem: