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

using namespace std;

#define ll long long

#define MX 100005



const ll INF = 1e12;



int A[MX][4];

ll dp[MX][4];



ll recur(int i, int j){

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

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



    ll mx = 0;

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

        if (x != j) mx = max(mx, recur(i-1, x));

    }

    return dp[i][j] = (mx + A[i][j]);

}



int main()

{

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

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

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

            scanf("%d", &A[i][j]);

            dp[i][j] = -1;

        }

    }



    ll ans = 0;

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

        ans = max(ans, recur(n, j));

    }



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



    return 0;

}


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

#include <algorithm>

using std::max;



int main(void)

{

    int dp[2][3],a[3],i,N;

    i=0; 

    dp[0][0]=0;

    dp[0][1]=0;

    dp[0][2]=0;

 

    scanf("%d",&N);

    while(N--)

    {

        scanf("%d%d%d",a,a+1,a+2);

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

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

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

        i=1-i;

    }

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

    return 0;

}

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

using namespace std;



#define pb push_back

#define mp make_pair

#define fi first

#define se second

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

#define rall(x) (x).rbegin(), (x).rend()

#define MAX 1000000

//3

typedef long long ll;

typedef pair<int,int> pii;

priority_queue< int > pq;

priority_queue< int , vector<int> , greater<int> > rpq;

set<int>s;

set <int > :: iterator itr; 

vector<int> vec[1001];

int r,n,m,inp,tc;

int arr[1000007]= {};

map<int , int> mymap;





int main()

{

	int n;

	int arr[3];

	int dp[3];

	cin>>n;

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

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

		if(i == 0 ) {

			dp[0] = arr[0];

			dp[1] = arr[1];

			dp[2] = arr[2];

		}

		else{

			int a = dp[0];

			int b = dp[1];

			int c = dp[2];

			dp[0] = max(arr[0] + b , arr[0] + c);

			dp[1] = max(arr[1] + c , arr[1] + a);

			dp[2] = max(arr[2] + a , arr[2] + b);

		}

		//cout<<dp[0]<<" "<<dp[1]<<" "<<dp[2]<<endl;

	}

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

    return 0;

}



//65 900 17 13

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

#include <algorithm>

using std::max;



int main(void)

{

    int dp[2][3],a[3],i,N;

    i=0; 

    dp[0][0]=0;

    dp[0][1]=0;

    dp[0][2]=0;

 

    scanf("%d",&N);

    while(N--)

    {

        scanf("%d%d%d",a,a+1,a+2);

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

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

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

        i=1-i;

    }

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

    return 0;

}

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

#define ll long long

using namespace std;



ll arr[100007][3];

ll dp[100007][3];

bool vis[100007][3];

int n;



ll rec( int i, int prev  )

{

    if(i>=n)

        return 0;

    if(vis[i][prev])

        return dp[i][prev];



    ll ans= 0;

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

    {

        if(prev==j)

            continue;

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

    }



    vis[i][prev]= 1;



    return dp[i][prev]= ans;

}



int main()

{

    cin>>n;



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

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

            cin>>arr[i][j];



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

}


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

#include <algorithm>

using std::max;



int main(void)

{

    int dp[2][3],a[3],i,N;

    i=0; 

    dp[0][0]=0;

    dp[0][1]=0;

    dp[0][2]=0;

 

    scanf("%d",&N);

    while(N--)

    {

        scanf("%d%d%d",a,a+1,a+2);

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

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

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

        i=1-i;

    }

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

    return 0;

}

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

#include <algorithm>



using namespace std;



int main() {

	int n;

	cin >> n;

	int activities[n][3];

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

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

			cin >> activities[i][j];

		}

	}



	int escolhaA = 0, escolhaB = 0, escolhaC = 0;

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

		int auxA = escolhaA, auxB = escolhaB, auxC = escolhaC;

		escolhaA = max(auxB,auxC) + activities[i][0];

		escolhaB = max(auxA,auxC) + activities[i][1];

		escolhaC = max(auxA,auxB) + activities[i][2];

	}



	cout << max(escolhaA,max(escolhaB,escolhaC)) << endl;

}

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

#include <algorithm>

using std::max;



int main(void)

{

    int dp[2][3],a[3],i,N;

    i=0; 

    dp[0][0]=0;

    dp[0][1]=0;

    dp[0][2]=0;

 

    scanf("%d",&N);

    while(N--)

    {

        scanf("%d%d%d",a,a+1,a+2);

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

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

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

        i=1-i;

    }

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

    return 0;

}

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

#include<algorithm>

#include<cstring>

using namespace std;

const int maxn = 1e5 + 5;



int a[maxn][4];

int dp[maxn][4];//d[i][j] i day choose j thing 's max happy



int main(){

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

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

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

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

		}

	}



	memset(dp, 0, sizeof dp);



	for(int i = 1 ; i <= 3 ; i++) dp[1][i] = a[1][i];



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

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

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

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

	}



	int ans = 0;

	for(int i = 1 ; i <= 3 ; i++)ans = max(dp[n][i],ans);

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

}

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

#include <algorithm>

using std::max;



int main(void)

{

    int dp[2][3],a[3],i,N;

    i=0; 

    dp[0][0]=0;

    dp[0][1]=0;

    dp[0][2]=0;

 

    scanf("%d",&N);

    while(N--)

    {

        scanf("%d%d%d",a,a+1,a+2);

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

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

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

        i=1-i;

    }

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

    return 0;

}

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



using namespace std;



    const int n = 1e5+5;

    int num;

    int act[n][3];

    int dyn[3][n];

    

    int init(int a, int i){

        

    	if(i == num) return 0;

    	

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

    	int& res = dyn[a][i];

    	

    	if(a==0){

    	    

    		res=max(res, init(1, i+1)+act[i][0]);

    		res=max(res, init(2, i+1)+act[i][0]);

    	}

    	

    	if(a==1){

    	    

    		res=max(res, init(0, i+1)+act[i][1]);

    		res=max(res, init(2, i+1)+act[i][1]);

    	}

    	

    	if(a==2){

    	    

    		res=max(res, init(0, i+1)+act[i][2]);

    		res=max(res, init(1, i+1)+act[i][2]);

    	}

    	

    	

    	return res;

    }

    

    int main(){

        

    	cin>>num;

    	for(int i=0; i<num; i++) cin>>act[i][0]>>act[i][1]>>act[i][2];

    	memset(dyn, -1, sizeof dyn);

    	cout<<max(init(0,0),max(init(1,0),init(2,0)))<<"\n";

    	return 0;

    	

    }

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

#include <algorithm>

using std::max;



int main(void)

{

    int dp[2][3],a[3],i,N;

    i=0; 

    dp[0][0]=0;

    dp[0][1]=0;

    dp[0][2]=0;

 

    scanf("%d",&N);

    while(N--)

    {

        scanf("%d%d%d",a,a+1,a+2);

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

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

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

        i=1-i;

    }

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

    return 0;

}

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

#include <algorithm>



using namespace std;



int main(void) {

    int n;



    cin >> n;



    int tasks[n][3];

    int points[n][3];



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

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

    }



    points[0][0] = tasks[0][0];

    points[0][1] = tasks[0][1];

    points[0][2] = tasks[0][2];



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

        points[i][0] = tasks[i][0] + max(points[i - 1][1], points[i - 1][2]);

        points[i][1] = tasks[i][1] + max(points[i - 1][0], points[i - 1][2]);

        points[i][2] = tasks[i][2] + max(points[i - 1][0], points[i - 1][1]);

    }



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



    return 0;

}


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

#include <algorithm>

using std::max;



int main(void)

{

    int dp[2][3],a[3],i,N;

    i=0; 

    dp[0][0]=0;

    dp[0][1]=0;

    dp[0][2]=0;

 

    scanf("%d",&N);

    while(N--)

    {

        scanf("%d%d%d",a,a+1,a+2);

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

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

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

        i=1-i;

    }

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

    return 0;

}

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



using namespace std;

const int N = (int)(1e5 + 123);

const int INF = (int)(1e9 + 7);



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

int dp[N][3];

int main() {

	int n;

	cin >> n;

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

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

	}

	

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

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

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

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

	}

	

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

	return 0;

}

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

#include <algorithm>

using std::max;



int main(void)

{

    int dp[2][3],a[3],i,N;

    i=0; 

    dp[0][0]=0;

    dp[0][1]=0;

    dp[0][2]=0;

 

    scanf("%d",&N);

    while(N--)

    {

        scanf("%d%d%d",a,a+1,a+2);

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

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

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

        i=1-i;

    }

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

    return 0;

}

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

 

#define mp make_pair

#define pb push_back

#define f first

#define s second

#define ll long long

#define int ll

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

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

#define VAR(v, i) __typeof( i) v=(i)

#define forit(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)

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

#define sz(x) ((int)(x).size())

#define file(s) freopen(s".in","r",stdin); freopen(s".out","w",stdout);

 

using namespace std;

 

const int maxn = (int)1e5 + 100;

const int mod = (int)1e9 + 7;

 

#define inf mod

 

typedef long double ld;

typedef pair<int, int> pii;

typedef pair<ll, ll> pll;

typedef vector<int> vi;   

typedef vector<ll> Vll;               

typedef vector<pair<int, int> > vpii;

typedef vector<pair<ll, ll> > vpll;

 

int n, dp[maxn][4], k;

int a[maxn][4];

main () {

	cin >> n;

	forn(i, 1, n)

		forn(j, 0, 2){

			cin >> a[i][j];

			if(i == 1) dp[i][j] = a[i][j];

		}

	forn(i, 2, n){

		forn(t1, 0, 2)

			forn(t2, 0, 2)

				if(t1 != t2)

					dp[i][t1] = max(dp[i][t1], dp[i - 1][t2] + a[i][t1]);

	}

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

}

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

#include <algorithm>

using std::max;



int main(void)

{

    int dp[2][3],a[3],i,N;

    i=0; 

    dp[0][0]=0;

    dp[0][1]=0;

    dp[0][2]=0;

 

    scanf("%d",&N);

    while(N--)

    {

        scanf("%d%d%d",a,a+1,a+2);

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

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

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

        i=1-i;

    }

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

    return 0;

}

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



using namespace std;

 

typedef long long ll;

typedef long double ld;

typedef unsigned long long ull;

 

#define nl '\n'

 

#define y1 aza_ibagaliev

 

#define f first

#define s second

	

#define ed end()

#define bg begin()

#define rbg rbegin()

 

#define rv reverse

#define mp make_pair

#define pb push_back

#define ppb pop_back

#define ppf pop_front

 

#define sz size()

#define all(x) x.bg, x.ed

 

#define pll pair < ll, ll >

#define pii pair < int, int >

 

#define sqr(x) ((x) * 1ll * (x))

#define sqrd(x) ((x) * 1.0 * (x))

 

#define speed ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)

 

#define rnl cout << "----------------------------------------" << nl

 

#define bit __builtin_popcount

 

inline ll bip(ll x, ll n, ll mod){ll res=1;while(n){if(n & 1){res=(res*x)%mod;}x=(x*x)%mod;n>>=1;}return res;}

        	

const int ppr = 257;

const ll INF = 2e18;

const int inf = 2e9;

const int mod = 1e9 + 7;

const int N = 1e5 + 123;

const ld pi = 3.1741592653589793238462643;

 

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

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



int n, x[5][N], dp[5][N];



main() {             	

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

//  freopen(".out", "w", stdout);



//	srand(time(NULL));



	scanf("%d", &n);



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

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

			scanf("%d", &x[j][i]);



		if(i == 1) {

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

				dp[j][i] = x[j][i];

		} else {

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

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

					if(a != b) dp[a][i] = max(dp[a][i], dp[b][i - 1] + x[a][i]);

		}

	}



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



    return 0;                                        

}

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

#include <algorithm>

using std::max;



int main(void)

{

    int dp[2][3],a[3],i,N;

    i=0; 

    dp[0][0]=0;

    dp[0][1]=0;

    dp[0][2]=0;

 

    scanf("%d",&N);

    while(N--)

    {

        scanf("%d%d%d",a,a+1,a+2);

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

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

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

        i=1-i;

    }

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

    return 0;

}

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

#include <iomanip>

#include <cmath>

#include <cstdio>

#include <cstring>

#include <ctime>

#include <string>

#include <stdio.h>

#include <set>

#include <map>

#include <algorithm>

#include <queue>

#include <vector>

#define ll long long

#define pb push_back

#define mp make_pair

#define f first

#define s second



using namespace std;



const int inf = 1e9 + 7;

const long long INF = 1e18 + 7;

const int maxn = 2e5 + 7;



int n, dp[maxn][4];

pair < pair < int, int >, int > a[maxn];



int main(){

	ios_base::sync_with_stdio(NULL);

	cin.tie(0);

	cout.tie(0);

	cin >> n;

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

		cin >> a[i].f.f >> a[i].f.s >> a[i].s;

	dp[1][1] = a[1].f.f;

	dp[1][2] = a[1].f.s;

	dp[1][3] = a[1].s;

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

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

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

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

	}

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

	return 0;

} 

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

#include <algorithm>

using std::max;



int main(void)

{

    int dp[2][3],a[3],i,N;

    i=0; 

    dp[0][0]=0;

    dp[0][1]=0;

    dp[0][2]=0;

 

    scanf("%d",&N);

    while(N--)

    {

        scanf("%d%d%d",a,a+1,a+2);

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

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

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

        i=1-i;

    }

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

    return 0;

}

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

#include<cstdio>

using namespace std;

const int N=100010;

int a[N][4],f[N][4];

int main()

{

	int n;

	scanf("%d",&n);

	for(int i=1;i<=n;i++) scanf("%d%d%d",&a[i][1],&a[i][2],&a[i][3]);

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

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

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

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

			}

		}

	}

	printf("%d\n",max(f[n][1],max(f[n][2],f[n][3])));

} 

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

#include <algorithm>

using std::max;



int main(void)

{

    int dp[2][3],a[3],i,N;

    i=0; 

    dp[0][0]=0;

    dp[0][1]=0;

    dp[0][2]=0;

 

    scanf("%d",&N);

    while(N--)

    {

        scanf("%d%d%d",a,a+1,a+2);

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

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

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

        i=1-i;

    }

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

    return 0;

}

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

#include<cstdio>

using namespace std;

int a[100001],b[100001],c[100001],n,dp[100001][3];

int main(){

	scanf("%d",&n);

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

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

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

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

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

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

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

	}

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

	return 0;

}

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

#include <algorithm>

using std::max;



int main(void)

{

    int dp[2][3],a[3],i,N;

    i=0; 

    dp[0][0]=0;

    dp[0][1]=0;

    dp[0][2]=0;

 

    scanf("%d",&N);

    while(N--)

    {

        scanf("%d%d%d",a,a+1,a+2);

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

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

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

        i=1-i;

    }

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

    return 0;

}

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

#include <algorithm>



const int maxn = 1e5+7;



int n;

int a[maxn][3];

int dp[maxn][3];



int main(int argc, char *argv[]) {  

	std::cin >> n;

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

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

			std::cin >> a[i][j];

		}

	}

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

		dp[0][j] = a[0][j];

	}

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

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

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

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

	}

	std::cout << std::max(std::max(dp[n-1][0], dp[n-1][1]), dp[n-1][2]) << '\n';

}

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

#include <algorithm>

using std::max;



int main(void)

{

    int dp[2][3],a[3],i,N;

    i=0; 

    dp[0][0]=0;

    dp[0][1]=0;

    dp[0][2]=0;

 

    scanf("%d",&N);

    while(N--)

    {

        scanf("%d%d%d",a,a+1,a+2);

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

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

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

        i=1-i;

    }

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

    return 0;

}

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

using namespace std;

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

#define ll long long

#define fastIO cout << fixed << setprecision(9), ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)

double const EPS = 1e-9, PI = acos(-1);

const int N = 1e5 + 9, M = 5e2 + 9, OO = 1e9 + 7, MOD = 1e9 + 9;

const ll inf = 1e18;



ll dp[N][3], happ[N][3];



int main() {

  fastIO;

#ifdef LOCAL

  freopen("input.in", "rt", stdin);

#endif



  int n;

  cin >> n;

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

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

      cin >> happ[i][j];



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

    dp[0][i] = happ[0][i];



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

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

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

        if(k == prv) continue;

        dp[i][k] = max(dp[i][k], dp[i-1][prv] + happ[i][k]);

      }

    }

  }



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

  return 0;

}


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

#include <algorithm>

using std::max;



int main(void)

{

    int dp[2][3],a[3],i,N;

    i=0; 

    dp[0][0]=0;

    dp[0][1]=0;

    dp[0][2]=0;

 

    scanf("%d",&N);

    while(N--)

    {

        scanf("%d%d%d",a,a+1,a+2);

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

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

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

        i=1-i;

    }

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

    return 0;

}

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

#include<algorithm>

using namespace std;

int a[100005][4];

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

int main()

{

	int n;

	cin>>n;

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

		cin>>a[i][0]>>a[i][1]>>a[i][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];

	}

	int ans=-1;

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

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

	cout<<ans<<endl;

	return 0;

}

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

#include <algorithm>

using std::max;



int main(void)

{

    int dp[2][3],a[3],i,N;

    i=0; 

    dp[0][0]=0;

    dp[0][1]=0;

    dp[0][2]=0;

 

    scanf("%d",&N);

    while(N--)

    {

        scanf("%d%d%d",a,a+1,a+2);

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

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

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

        i=1-i;

    }

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

    return 0;

}

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

using namespace std;

int n;

long long f[100005] , s[100005] , th[100005];

long long mem[100005][4];

long long sol(){

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

		mem[i][0] = mem[0][i] = 0;

	}

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

		mem[i][1] = max(mem[i-1][3] + f[i] , mem[i-1][2] +f[i]);

		mem[i][2] = max(mem[i-1][1] + s[i] , mem[i-1][3] + s[i]);

		mem[i][3] = max(mem[i-1][2] + th[i] , mem[i-1][1] +th[i]);

	}

	return max(mem[n][1] , max(mem[n][2],mem[n][3]));

}

int main()

{

	cin>>n;

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

		cin>>f[i]>>s[i]>>th[i];

	}

	cout<<sol();

	return 0;

}




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

#include <algorithm>

using std::max;



int main(void)

{

    int dp[2][3],a[3],i,N;

    i=0; 

    dp[0][0]=0;

    dp[0][1]=0;

    dp[0][2]=0;

 

    scanf("%d",&N);

    while(N--)

    {

        scanf("%d%d%d",a,a+1,a+2);

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

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

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

        i=1-i;

    }

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

    return 0;

}

// Fastest known solution for this problem: