stablegradients/pie-gem5-bysrc huggingface dataset:
// Original C++ solution (slower):
/*BISMILLAH

THE WHITE WOLF

NO DREAM IS TOO BIG AND NO DREAMER IS TOO SMALL*/



#include<bits/stdc++.h>

using namespace std;



#define io ios_base::sync_with_stdio(false)

#define ll long long

#define ull unsigned long long

#define vi vector<int>

#define vll vector<long long>

#define pb push_back

#define mod  10000007

#define pii pair<int, int>

#define PI 2*acos(0.0)

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





int main()

{

    io;

    int n;

    cin>>n;

    ll dp[n+1][3];

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

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

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

        dp[0][i] = 0;

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

    {

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

        {

            ll ma = 0;

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

            {

                if(a == j)

                    continue;

                ma = max(ma, dp[i-1][a]);

            }

            dp[i][j] += ma;

        }

    }

    ll ans = 0;

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

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

    cout<<ans;

    return 0;

}


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

#include<algorithm>

int main(){

  int j=1,x,da[3][100001],fun[3][2];

  scanf("%d",&x);

  fun[0][0] = fun[1][0] = fun[2][0] = 0;

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

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

    fun[0][j] = std::max(fun[1][j^1],fun[2][j^1]) + da[0][i];

    fun[1][j] = std::max(fun[0][j^1],fun[2][j^1]) + da[1][i];

    fun[2][j] = std::max(fun[1][j^1],fun[0][j^1]) + da[2][i];

    j^=1;

  }

  j^=1;

  printf("%d\n",std::max(std::max(fun[0][j],fun[1][j]),fun[2][j]));

  

  return 0;

}


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

#include<iostream>

using namespace std;

#define pii pair<int,int>

#define ll long long

#define pb push_back

#define fi first

#define se second

#define mp make_pair

#define gap ' '

#define abs(x) ((x)>0?(x):-(x))

#define INF 1e18

#define mod  10000007

int main()

{

 ios::sync_with_stdio(0);

    cin.tie(0);

    int n;

    cin>>n;

    vector<int> v[n];

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

    {

        int x,y,z;

        cin>>x>>y>>z;

        v[i].pb(x);

        v[i].pb(y);

        v[i].pb(z);

    }

    int dp[n+1][3];

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

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

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

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

    {

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

        {

            dp[i][j]=0;

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

            {

                if(j==k) continue;

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

            }

        }

    }

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

}


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

#include<algorithm>

int main(){

  int j=1,x,da[3][100001],fun[3][2];

  scanf("%d",&x);

  fun[0][0] = fun[1][0] = fun[2][0] = 0;

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

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

    fun[0][j] = std::max(fun[1][j^1],fun[2][j^1]) + da[0][i];

    fun[1][j] = std::max(fun[0][j^1],fun[2][j^1]) + da[1][i];

    fun[2][j] = std::max(fun[1][j^1],fun[0][j^1]) + da[2][i];

    j^=1;

  }

  j^=1;

  printf("%d\n",std::max(std::max(fun[0][j],fun[1][j]),fun[2][j]));

  

  return 0;

}


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

using namespace std;

#define vb __int128

#define ll long long

#define ld long double

#define full(a) a.begin(),a.end()

#define stoink stack<ll>

#define vec vector <ll>

#define vg vector<vector<ll> >

#define vgw vector<vector<pair<ll,ll> >

#define pa pair<ll,ll>

#define mp make_pair

#define pb push_back

#define ff first

#define ss second

#define pre 0.00000000000000000001

#define inf 1000000000000

#define mod 998244353

vg arr;

ll n;

vg dp;

ll func(ll ind,ll act) {

	if(dp[ind][act]!=-1)

		return dp[ind][act];

	if(ind == n - 1)

		return arr[ind][act];

	ll i;

	ll res = -1;

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

		if(i == act)

			continue;

		res = max(res,arr[ind][act] + func(ind + 1,i));

	}

	return dp[ind][act] = res;

}

int main()

{	ios_base::sync_with_stdio(false);

	cin.tie(NULL);

	cout.tie(NULL);

	cin >> n;

	ll i,j;

	arr.assign(n,vector<ll>(3,0));

	dp.assign(n,vector<ll>(3,-1));

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

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

			cin >> arr[i][j];

	}

	ll res = -1;

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

		res = max(res,func(0,i));

	}

	cout << res;

	return 0;

}

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

#include<algorithm>

int main(){

  int j=1,x,da[3][100001],fun[3][2];

  scanf("%d",&x);

  fun[0][0] = fun[1][0] = fun[2][0] = 0;

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

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

    fun[0][j] = std::max(fun[1][j^1],fun[2][j^1]) + da[0][i];

    fun[1][j] = std::max(fun[0][j^1],fun[2][j^1]) + da[1][i];

    fun[2][j] = std::max(fun[1][j^1],fun[0][j^1]) + da[2][i];

    j^=1;

  }

  j^=1;

  printf("%d\n",std::max(std::max(fun[0][j],fun[1][j]),fun[2][j]));

  

  return 0;

}


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

using namespace std;



typedef long long int ll;

typedef pair<int,int> pi;

priority_queue<pi, vector<pi>, greater<pi> > pq; //min heap



ll mini(ll a,ll b){	return a<b?a:b;}

ll maxi(ll a,ll b){	return a>b?a:b;}

ll gcd(ll a,ll b){	if(b == 0)	return a;return gcd(b,a%b);}

ll binpow(ll n,ll x){	ll ans = 1;while(x){if(x & 1)	ans *= n;n *= n;x >>= 1;}return ans;}



int main()

{

    ios::sync_with_stdio(false);

    cin.tie(0);

    cout.tie(0);



    int n;

    cin>>n;



    vector<int> tot(3,0);

    vector<int> vals(3,0);



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

        vector<int> cur(3,0);

        for(int j=0;j<3;++j)    cin>>vals[j];

        

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

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

                if(i!=j){

                    cur[j] = max(cur[j], tot[i] + vals[j]);

                }

            }

        }



        tot = cur;

    }



    cout<<max(tot[0], max(tot[1], tot[2]))<<"\n";



    return 0;

}

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

#include<algorithm>

int main(){

  int j=1,x,da[3][100001],fun[3][2];

  scanf("%d",&x);

  fun[0][0] = fun[1][0] = fun[2][0] = 0;

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

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

    fun[0][j] = std::max(fun[1][j^1],fun[2][j^1]) + da[0][i];

    fun[1][j] = std::max(fun[0][j^1],fun[2][j^1]) + da[1][i];

    fun[2][j] = std::max(fun[1][j^1],fun[0][j^1]) + da[2][i];

    j^=1;

  }

  j^=1;

  printf("%d\n",std::max(std::max(fun[0][j],fun[1][j]),fun[2][j]));

  

  return 0;

}


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

#define MAX 100005

using namespace std;



int n;

int d[3][MAX];

long long dp[3][MAX];

long long m;



int main(){

    cin>>n;

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

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

            cin>>d[j][i];

    }

    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[j][i] = max(dp[k][i-1]+d[j][i], dp[j][i]);

                m = max(dp[j][i], m);

            }

    }

    cout<<m<<"\n";

    return 0;

}

/*

3

10 40 70

20 50 80

30 60 90

*/


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

#include<algorithm>

int main(){

  int j=1,x,da[3][100001],fun[3][2];

  scanf("%d",&x);

  fun[0][0] = fun[1][0] = fun[2][0] = 0;

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

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

    fun[0][j] = std::max(fun[1][j^1],fun[2][j^1]) + da[0][i];

    fun[1][j] = std::max(fun[0][j^1],fun[2][j^1]) + da[1][i];

    fun[2][j] = std::max(fun[1][j^1],fun[0][j^1]) + da[2][i];

    j^=1;

  }

  j^=1;

  printf("%d\n",std::max(std::max(fun[0][j],fun[1][j]),fun[2][j]));

  

  return 0;

}


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

using namespace std;



#define ll long long int

#define oo 1L<<60;



int N;

int happiness [100005][3];

ll memo [100005][3];



ll costo(){

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

	memo[0][i] = happiness[0][i];

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

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

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

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

}



int main() {

	cin>>N;

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

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

	cout<<costo()<<endl;

	return 0;

}

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

#include<algorithm>

int main(){

  int j=1,x,da[3][100001],fun[3][2];

  scanf("%d",&x);

  fun[0][0] = fun[1][0] = fun[2][0] = 0;

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

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

    fun[0][j] = std::max(fun[1][j^1],fun[2][j^1]) + da[0][i];

    fun[1][j] = std::max(fun[0][j^1],fun[2][j^1]) + da[1][i];

    fun[2][j] = std::max(fun[1][j^1],fun[0][j^1]) + da[2][i];

    j^=1;

  }

  j^=1;

  printf("%d\n",std::max(std::max(fun[0][j],fun[1][j]),fun[2][j]));

  

  return 0;

}


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

using namespace std;

#define MAXN 100000

#define INF 2000000000

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

int dpa[MAXN+1],dpb[MAXN+1],dpc[MAXN+1];

int n,k;

int main() {

	int i;

	int j;

	scanf("%d",&n,&k);

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

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

	}

	dpa[n]=dpb[n]=dpc[n]=0;

	for (i=n-1; i>=0; 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]);

	}

	printf("%d\n",max(dpa[0], max(dpb[0], dpc[0])));

	return 0;

}

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

#include<algorithm>

int main(){

  int j=1,x,da[3][100001],fun[3][2];

  scanf("%d",&x);

  fun[0][0] = fun[1][0] = fun[2][0] = 0;

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

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

    fun[0][j] = std::max(fun[1][j^1],fun[2][j^1]) + da[0][i];

    fun[1][j] = std::max(fun[0][j^1],fun[2][j^1]) + da[1][i];

    fun[2][j] = std::max(fun[1][j^1],fun[0][j^1]) + da[2][i];

    j^=1;

  }

  j^=1;

  printf("%d\n",std::max(std::max(fun[0][j],fun[1][j]),fun[2][j]));

  

  return 0;

}


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



using namespace std;



int choose(const int &day, const int &last_activity, const int activities[][3], const int &n, int memo[][4]){

    int ans;

    if(day == n){

        ans = 0;

    }

    else{

        if(memo[day][last_activity] != -1){

            ans = memo[day][last_activity];

        }

        else if(last_activity == 0){

            ans = memo[day][0] = max(activities[day][0] + choose(day + 1, 1, activities, n, memo), max(activities[day][1] + choose(day + 1, 2, activities, n, memo), activities[day][2] + choose(day + 1, 3, activities, n, memo)));

        }

        else if(last_activity == 1){

            ans = memo[day][1] = max(activities[day][1] + choose(day + 1, 2, activities, n, memo), activities[day][2] + choose(day + 1, 3, activities, n, memo));

        }

        else if(last_activity == 2){

            ans = memo[day][2] = max(activities[day][0] + choose(day + 1, 1, activities, n, memo), activities[day][2] + choose(day + 1, 3, activities, n, memo));

        }

        else{

            ans = memo[day][3] = max(activities[day][0] + choose(day + 1, 1, activities, n, memo), activities[day][1] + choose(day + 1, 2, activities, n, memo));

        }

    }

    return ans;

}



int solve(const int activities[][3], const int &n, int memo[][4]){

    return choose(0, 0, activities, n, memo);

}



int main(){

    int n;

    while(scanf("%d", &n) != EOF){

        int activities[n][3], memo[n][4];

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

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

            memo[i][0] = -1; memo[i][1] = -1; memo[i][2] = -1; memo[i][3] = -1;

        }

        printf("%d\n", solve(activities, n, memo));

    }

    return 0;

}

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

#include<algorithm>

int main(){

  int j=1,x,da[3][100001],fun[3][2];

  scanf("%d",&x);

  fun[0][0] = fun[1][0] = fun[2][0] = 0;

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

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

    fun[0][j] = std::max(fun[1][j^1],fun[2][j^1]) + da[0][i];

    fun[1][j] = std::max(fun[0][j^1],fun[2][j^1]) + da[1][i];

    fun[2][j] = std::max(fun[1][j^1],fun[0][j^1]) + da[2][i];

    j^=1;

  }

  j^=1;

  printf("%d\n",std::max(std::max(fun[0][j],fun[1][j]),fun[2][j]));

  

  return 0;

}


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



using namespace std;



typedef long long lint;

const lint N = 1e5+100;

lint n, happiness[N][3], abc[N][3];



int main(){

#ifdef LOCAL

	freopen("input", "r", stdin);

#endif



	scanf("%lld", &n);



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

		scanf("%lld %lld %lld", &abc[i][0], &abc[i][1], &abc[i][2]);

	}



	happiness[0][0] = abc[0][0]; happiness[0][1] = abc[0][1]; happiness[0][2] = abc[0][2];



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

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

			if (j==2) happiness[i][j] = max(happiness[i-1][0] + abc[i][j], happiness[i-1][1] + abc[i][j]);

			else if (j==1) happiness[i][j] = max(happiness[i-1][0] + abc[i][j], happiness[i-1][2] + abc[i][j]);

			else happiness[i][j] = max(happiness[i-1][1] + abc[i][j], happiness[i-1][2] + abc[i][j]);

		}

	}

	

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



	return 0; 

}

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

#include<algorithm>

int main(){

  int j=1,x,da[3][100001],fun[3][2];

  scanf("%d",&x);

  fun[0][0] = fun[1][0] = fun[2][0] = 0;

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

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

    fun[0][j] = std::max(fun[1][j^1],fun[2][j^1]) + da[0][i];

    fun[1][j] = std::max(fun[0][j^1],fun[2][j^1]) + da[1][i];

    fun[2][j] = std::max(fun[1][j^1],fun[0][j^1]) + da[2][i];

    j^=1;

  }

  j^=1;

  printf("%d\n",std::max(std::max(fun[0][j],fun[1][j]),fun[2][j]));

  

  return 0;

}


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

#include<vector>

#include<map>

using namespace std;

vector<vector<int>>niv;

vector<vector<int>>dat;

string k;

int m(int n,int r){

    if(n>=niv.size())return 0;

    if(dat[n][r]!=0)return dat[n][r];

    dat[n][r]=0;

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

        if(i!=r){

            int l=m(n+1,i);

           dat[n][r]=max(dat[n][r],l+niv[n][r]);

        }

    }

    return dat[n][r];

}



int main(){

    int a,aux;

    cin>>a;

    dat=vector<vector<int>>(a+1,vector<int>(4,0));

    vector<int> aux2(4,0);

    niv.push_back(aux2);

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

        aux2.clear();

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

            cin>>aux;

            aux2.push_back(aux);

        }

        niv.push_back(aux2);

    }

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

    return 0;

}


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

#include<algorithm>

int main(){

  int j=1,x,da[3][100001],fun[3][2];

  scanf("%d",&x);

  fun[0][0] = fun[1][0] = fun[2][0] = 0;

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

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

    fun[0][j] = std::max(fun[1][j^1],fun[2][j^1]) + da[0][i];

    fun[1][j] = std::max(fun[0][j^1],fun[2][j^1]) + da[1][i];

    fun[2][j] = std::max(fun[1][j^1],fun[0][j^1]) + da[2][i];

    j^=1;

  }

  j^=1;

  printf("%d\n",std::max(std::max(fun[0][j],fun[1][j]),fun[2][j]));

  

  return 0;

}


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

#define IO ios::sync_with_stdio(0), cin.tie(0)

using namespace std;

typedef long long ll;

const int maxn = 1e5 + 5;

const int inf = ~0u >> 1;

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

int h[maxn][5];

int d[maxn][5];

int main() {

    IO;

    int n;

    cin >> n;

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

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

    }

    d[1][1] = h[1][1];

    d[1][2] = h[1][2];

    d[1][3] = h[1][3];

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

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

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

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

        }

    }

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

    return 0;

}

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

#include<algorithm>

int main(){

  int j=1,x,da[3][100001],fun[3][2];

  scanf("%d",&x);

  fun[0][0] = fun[1][0] = fun[2][0] = 0;

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

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

    fun[0][j] = std::max(fun[1][j^1],fun[2][j^1]) + da[0][i];

    fun[1][j] = std::max(fun[0][j^1],fun[2][j^1]) + da[1][i];

    fun[2][j] = std::max(fun[1][j^1],fun[0][j^1]) + da[2][i];

    j^=1;

  }

  j^=1;

  printf("%d\n",std::max(std::max(fun[0][j],fun[1][j]),fun[2][j]));

  

  return 0;

}


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

using namespace std;



#define ll long long int



int N;

int dias[100005][3];

ll memo[100005][3];



ll solve(int dia, int act){

	if(!dia) return dias[dia][act];

	if(memo[dia][act]) return memo[dia][act];

	return memo[dia][act] = 

		dias[dia][act]+max(solve(dia-1, (act+1)%3), solve(dia-1, (act+2)%3));

}



ll solve(){

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

		memo[0][i] = dias[0][i];

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

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

			memo[i][j] = dias[i][j]+

							max(memo[i-1][(j+1)%3], 

								memo[i-1][(j+2)%3]);

	ll maxi = 0;

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

		maxi = max(maxi, memo[N-1][i]);

	return maxi;

}



int main() {

	cin>>N;

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

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

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

	return 0;

}

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

#include<algorithm>

int main(){

  int j=1,x,da[3][100001],fun[3][2];

  scanf("%d",&x);

  fun[0][0] = fun[1][0] = fun[2][0] = 0;

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

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

    fun[0][j] = std::max(fun[1][j^1],fun[2][j^1]) + da[0][i];

    fun[1][j] = std::max(fun[0][j^1],fun[2][j^1]) + da[1][i];

    fun[2][j] = std::max(fun[1][j^1],fun[0][j^1]) + da[2][i];

    j^=1;

  }

  j^=1;

  printf("%d\n",std::max(std::max(fun[0][j],fun[1][j]),fun[2][j]));

  

  return 0;

}


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

using namespace std;



#define ll long long int



int N;

int dias[3];

ll memo[2][3];



ll solve(){

	cin>>memo[0][0]>>memo[0][1]>>memo[0][2];

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

		cin>>dias[0]>>dias[1]>>dias[2];

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

			memo[i&1][j]=dias[j]+max(memo[(i+1)&1][(j+1)%3],memo[(i+1)&1][(j+2)%3]);

	}

	ll maxi = 0;

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

		maxi = max(memo[(N-1)&1][j], maxi);

	return maxi;

}



int main() {

	cin>>N;

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

	return 0;

}

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

#include<algorithm>

int main(){

  int j=1,x,da[3][100001],fun[3][2];

  scanf("%d",&x);

  fun[0][0] = fun[1][0] = fun[2][0] = 0;

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

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

    fun[0][j] = std::max(fun[1][j^1],fun[2][j^1]) + da[0][i];

    fun[1][j] = std::max(fun[0][j^1],fun[2][j^1]) + da[1][i];

    fun[2][j] = std::max(fun[1][j^1],fun[0][j^1]) + da[2][i];

    j^=1;

  }

  j^=1;

  printf("%d\n",std::max(std::max(fun[0][j],fun[1][j]),fun[2][j]));

  

  return 0;

}


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

using namespace std;



#define ll long long int



int felicidad[100005][3];

ll memo[100005][3];

int N;



ll maxFeliz(int dia, int act){

	if(!dia) return felicidad[0][act];

	if(memo[dia][act]) return memo[dia][act];

	return memo[dia][act] = felicidad[dia][act]+

					max(maxFeliz(dia-1, (act+1)%3),

						maxFeliz(dia-1, (act+2)%3));

}



int main() {

	cin>>N;

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

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

	cout<<max(maxFeliz(N-1,0), max(maxFeliz(N-1,1), maxFeliz(N-1,2)))<<"\n";

	return 0;

}

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

#include<algorithm>

int main(){

  int j=1,x,da[3][100001],fun[3][2];

  scanf("%d",&x);

  fun[0][0] = fun[1][0] = fun[2][0] = 0;

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

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

    fun[0][j] = std::max(fun[1][j^1],fun[2][j^1]) + da[0][i];

    fun[1][j] = std::max(fun[0][j^1],fun[2][j^1]) + da[1][i];

    fun[2][j] = std::max(fun[1][j^1],fun[0][j^1]) + da[2][i];

    j^=1;

  }

  j^=1;

  printf("%d\n",std::max(std::max(fun[0][j],fun[1][j]),fun[2][j]));

  

  return 0;

}


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

#include <cstring>

#include <stdio.h>

using namespace std;

const int N=1e+5;

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

int memo[N][4];

int dp(int i,int t)

{

    if(i==n)

        return 0;

    int &ret=memo[i][t];

    if(~ret)

        return ret;

    ret=0;

    if(t!=0)

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

    if(t!=1)

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

    if(t!=2)

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

    return ret;





}

int main()

{

    memset(memo,-1,sizeof memo);

    scanf("%d",&n);

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

    {

         scanf("%d %d %d",a+i,b+i,c+i);

    }

    printf("%d\n",dp(0,3));

    return 0;

}


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

#include<algorithm>

int main(){

  int j=1,x,da[3][100001],fun[3][2];

  scanf("%d",&x);

  fun[0][0] = fun[1][0] = fun[2][0] = 0;

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

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

    fun[0][j] = std::max(fun[1][j^1],fun[2][j^1]) + da[0][i];

    fun[1][j] = std::max(fun[0][j^1],fun[2][j^1]) + da[1][i];

    fun[2][j] = std::max(fun[1][j^1],fun[0][j^1]) + da[2][i];

    j^=1;

  }

  j^=1;

  printf("%d\n",std::max(std::max(fun[0][j],fun[1][j]),fun[2][j]));

  

  return 0;

}


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

using namespace std;



#define clr(arr,val) memset(arr,val ,sizeof arr)

//#define first f

//#define second s



typedef  long long ull;

typedef long long ll;

ll const oo=1e18 ;



int const SZ= (int)1e5+5;

//const ll mod=(ll)1e9+7;

    

//int dp[SZ][SZ];

int arr[SZ][4];

int dp[SZ][4];

int n;

int solve(int r ,int prev ){



    if(r>=n){

        return 0;

    }

    int &ret=dp[r][prev];

    if(prev!=-1 && ret !=-1){

        return ret;

    }

    int mx=0;

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

        if(i==prev) continue;

        mx=max(mx,arr[r][i]+solve(r+1,i));

    }

    return ret=mx;

}



int main() {

//#ifdef CLION

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

//    freopen("out.txt", "w", stdin);

//#endif

    

    cin>>n;

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

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

    }

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

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

    return 0;

}

 

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

#include <cstring>

#include <cstdio>

#include <vector>

#include <map>

#include <queue>

#include <algorithm>

#include <cmath>

#include <cstdio>

#include <utility>

using namespace std;

const int N=1e5+10;

int dp[N][3],a[N],b[N],c[N];



int main()

{

	int n;

	scanf("%d",&n);

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

	{

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

	}

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

	{

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

		{

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

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

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

		}

	}

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

   return 0;

}


// Fastest known solution for this problem: