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

using namespace std;

int n;

int arr[100005][3];



int dp[100005][4];



int solve(int i, int prev){

	if(i == n) return 0;

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



	dp[i][prev] = -1e7;

	int a = 0, b = 0, c = 0;

	if(prev != 0)

		a = solve(i+1, 0) + arr[i][0];

	if(prev != 1)

		b = solve(i+1, 1) + arr[i][1];

	if(prev != 2)

		c = solve(i+1, 2) + arr[i][2];



	dp[i][prev] = max(a, max(b, c));

	return dp[i][prev];

}



int main(){

	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, 3)<<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):
#define _CRT_SECURE_NO_WARNINGS

#include<iostream>

#include<functional>

#include <bitset>

#include<algorithm>

#include<fstream>

#include<string>

#include <cstring>

#include<stdio.h>

#include<iomanip>

#include<sstream>

#include<cmath>

#include<vector>

#include<assert.h>

#include<queue>

#include<deque>

#include<stack>

#include<set>

#include<map>

// never lose hope ..

typedef unsigned long long ull;

typedef long long ll;

typedef long double ld;

#define eps 1e-8

using namespace std;

double const  PI = 3.141592653;

const int OO = 1000000000 + 9;



void fast() {

	ios_base::sync_with_stdio(0);

	cin.tie(NULL), cout.tie(NULL);

}

struct s {

	int x, y, z;

};



int n;

int v[(int)1e5][4];

int dp[(int)1e5][4];



int solve(int i = 0, int pre = 4) {

	if (i >= n)return 0;

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

	int ch1, ch2, ch3;

	ch1 = ch2 = ch3 = 0;

	if (pre != 1) {

		ch1 += solve(i + 1, 1) + v[i][1];

	}

	if (pre != 2) {

		ch2 += solve(i + 1, 2) + v[i][2];

	}

	if (pre != 3) {

		ch3 += solve(i + 1, 3) + v[i][3];

	}

	return dp[i][pre] = max({ ch1,ch2,ch3 });

}





int main()

{

	fast();



	cin >> n;

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

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

	}

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

	cout << solve();



	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 main()

{

    int n,ans=0;

    cin>>n;

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

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

    {

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

    }

    int dp[n-1][3];

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

    dp[0][1]=b[0];

    dp[0][2]=c[0];

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

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

    {

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

        {

            if(j==0)

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

            if(j==1)

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

            if(j==2)

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

            if(i==n-1)

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

        }

    }

    cout<<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;

const int N = 1e5 + 5;

long long dp[3][N], ans;



int main()

{

	int n;

	cin >> n;

	int A[n], B[n], C[n];

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

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

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

	dp[1][0] = B[0];

	dp[2][0] = C[0];

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

	{

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

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

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

	}

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

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

	cout << ans << "\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 <bits/stdc++.h>

#define x first

#define y second

#define pii pair<int,int>

#define ll long long

#define pll pair<ll,ll>

#define mp make_pair

#define pb push_back

#define pf push_front

#define popb pop_back

#define popf pop_front

#define xmod 100000007

using namespace std;



int n,m,dp[100005][5],a[100005][5],ans;

int main (){

	ios::sync_with_stdio(false);

	cin >> n;

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

		cin >> 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 (j != k)

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

			}

			dp[i][j]+=a[i][j];

		}

	}

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

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

	}

	cout << ans << "\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<bits/stdc++.h>

using namespace std;

const int maxv=1000010;

typedef long long ll;

ll n;

ll a[maxv];

ll b[maxv];

ll c[maxv];

ll ans[maxv][3];



int main()

{



    cin>>n;





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

    {

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

    }



    ans[0][0]=a[0];

    ans[0][1]=b[0];

    ans[0][2]=c[0];

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

    {

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

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

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





    }

    ll result=max(ans[n-1][2],max(ans[n-1][0],ans[n-1][1]));

    cout<<result;



    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<bits/stdc++.h>

using namespace std;



typedef long long ll;



int n;

int a[100005][4];

ll dp[100005][4];



ll solve(int d,int l){

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

    if(dp[d][l]!=-1) return dp[d][l];

    ll ret=0;

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

        if(i!=l){

            ret=max(ret, solve(d+1,i)+a[d][i]);

        }

    }

    return dp[d][l]=ret;



}





int main()

{

    cin>>n;

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

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

            cin>>a[i][j];

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

    cout<<solve(1,-1);



    return 0;

}


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

using namespace std;

typedef long long ll;

typedef unsigned long long ull;

typedef pair<int, int> pii;

const int INF = 0x3f3f3f3f;

const int maxn = 1e5 + 9;

inline int read()

{

    int data = 0, f = 1;

    char ch = getchar();

    while (!isdigit(ch))

    {

        if (ch == '-')

            f = -1;

        ch = getchar();

    }

    while (isdigit(ch))

    {

        data = (data << 3) + (data << 1) + ch - '0';

        ch = getchar();

    }

    return f * data;

}

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

int main()

{

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

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

    //std::ios::sync_with_stdio(false);

    //std::cin.tie(0);

    int n = read();

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

    {

        a[i] = read(), b[i] = read(), c[i] = read();

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

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

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

    }

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

    //fclose(stdin);

    //fclose(stdout);

    return 0;

}


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

#define inf INFINITY

#define file freopen( "input.txt" , "r" , stdin) ; freopen( "output.txt" , "w" , stdout) ;

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

#define _3102_ main()

#define endl '\n'

typedef long long ll;

typedef unsigned long long ull;

typedef long double ld;

using namespace std;

const int N = 1e5 + 100;

const ll MX = 1e9;

const ll mod = 1e9 + 7;

const double eps = 1e-9 ;



ll n, a[N] , b[N] , c[N], dp[N][3] ;



ll solve( int i , int j ){

	if ( i < 0 ) return 0 ;

	ll &ret = dp[i][j] ;

	if ( ret != -1) return ret ;

	if ( j == 0 ){

		return ret = a[i] + max ( solve(i-1 , 1), solve(i-1,2) ) ;

	}

	else if ( j == 1 ){

		return ret = b[i] + max ( solve(i-1 , 0), solve(i-1,2) ) ;

	}

	return ret = c[i] + max ( solve(i-1 , 1), solve(i-1,0) ) ;

}



int _3102_{

	Gamed

	cin >> n ;

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

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

	}

	memset ( dp , -1 , sizeof dp);

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

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

	return 0;

}


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

using namespace std;

typedef long long ll;

typedef unsigned long long ull;

typedef pair<int, int> pii;

const int INF = 0x3f3f3f3f;

const int maxn = 1e5 + 9;

inline int read()

{

    int data = 0, f = 1;

    char ch = getchar();

    while (!isdigit(ch))

    {

        if (ch == '-')

            f = -1;

        ch = getchar();

    }

    while (isdigit(ch))

    {

        data = (data << 3) + (data << 1) + ch - '0';

        ch = getchar();

    }

    return f * data;

}

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

int main()

{

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

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

    //std::ios::sync_with_stdio(false);

    //std::cin.tie(0);

    int n = read();

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

    {

        a[i] = read(), b[i] = read(), c[i] = read();

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

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

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

    }

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

    //fclose(stdin);

    //fclose(stdout);

    return 0;

}


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

using namespace std;

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

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

typedef long long ll;

typedef unsigned long long ull;

#define FIN ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)

#define forr(i, a, b) for(ll i = (a); i < (ll) (b); i++)

#define forn(i, n) forr(i, 0, n)

#define pb push_back

#define mp make_pair

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

#define DBG(x) cerr << #x << " = " << (x) << endl

#define MOD 1000000007

#define MAXN 100005

ll dp[MAXN][3];

void solve(vector< vector<ll> > &a, int n)

{

    forn(i,MAXN)

    {

        forn(j,3)

        {

            dp[i][j]=(pow(10,9)+5)*(ll)(-1);

        }

    }

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

    forr(i,1,n)

    {

        forn(j,3)

        {

            forn(k,3)

            {

                if(j==k)

                {

                    continue;

                }

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

            }

        }

 

    }

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

}

 

int main() {

    FIN;

    int n;

    cin >> n;

    vector< vector<ll> > arreglo(n);

    forn(i,n) arreglo[i].resize(3);

    forn(i,n)

    {

        forn(j,3) cin >> arreglo[i][j];

    }

    solve(arreglo, n);

    return 0;

}

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

using namespace std;

typedef long long ll;

typedef unsigned long long ull;

typedef pair<int, int> pii;

const int INF = 0x3f3f3f3f;

const int maxn = 1e5 + 9;

inline int read()

{

    int data = 0, f = 1;

    char ch = getchar();

    while (!isdigit(ch))

    {

        if (ch == '-')

            f = -1;

        ch = getchar();

    }

    while (isdigit(ch))

    {

        data = (data << 3) + (data << 1) + ch - '0';

        ch = getchar();

    }

    return f * data;

}

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

int main()

{

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

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

    //std::ios::sync_with_stdio(false);

    //std::cin.tie(0);

    int n = read();

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

    {

        a[i] = read(), b[i] = read(), c[i] = read();

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

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

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

    }

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

    //fclose(stdin);

    //fclose(stdout);

    return 0;

}


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

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

#define ll long long

using namespace std;

const int N = 100010;

int dp[N][3];

int main(){

    fast;

	int n, i, a, b, c;

	cin >> n;

	memset(dp, 0, sizeof dp);

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

		cin >> a >> b >> c;

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

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

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

	}

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

	return 0;

}


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

using namespace std;

typedef long long ll;

typedef unsigned long long ull;

typedef pair<int, int> pii;

const int INF = 0x3f3f3f3f;

const int maxn = 1e5 + 9;

inline int read()

{

    int data = 0, f = 1;

    char ch = getchar();

    while (!isdigit(ch))

    {

        if (ch == '-')

            f = -1;

        ch = getchar();

    }

    while (isdigit(ch))

    {

        data = (data << 3) + (data << 1) + ch - '0';

        ch = getchar();

    }

    return f * data;

}

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

int main()

{

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

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

    //std::ios::sync_with_stdio(false);

    //std::cin.tie(0);

    int n = read();

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

    {

        a[i] = read(), b[i] = read(), c[i] = read();

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

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

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

    }

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

    //fclose(stdin);

    //fclose(stdout);

    return 0;

}


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

using namespace std;

#define tpl  tuple<int, int, int>

#define mt   make_tuple

#define LIM  100000





int n, dp[LIM+5][4], vis[LIM+5][4];

vector<tpl> v;



int f(int pos, int k){

    if(pos >= n) return 0;

    if(vis[pos][k]) return dp[pos][k];

    vis[pos][k] = 1;

    int ans;

    if(k == 0) ans = get<0>(v[pos]) + max(f(pos+1, 1), f(pos+1, 2));

    else if(k == 1) ans = get<1>(v[pos]) + max(f(pos+1, 0), f(pos+1, 2));

    else ans = get<2>(v[pos]) + max(f(pos+1, 0), f(pos+1, 1));



    return dp[pos][k] = ans;

}



int main(){

    scanf("%d", &n);

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

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

        v.push_back(mt(a, b, c));

    }

    int ans = f(0, 0);

    memset(dp, 0, sizeof dp);

    memset(vis, 0, sizeof vis);

    ans = max(ans, f(0, 1));

    memset(dp, 0, sizeof dp);

    memset(vis, 0, sizeof vis);

    ans = max(ans, f(0, 2));



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

}


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

using namespace std;



int main()

{

	int n;

	scanf("%d",&n);

	

	int a,b,c;

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

	

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

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

	

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

	{

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

		

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

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

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

	}

	

	printf("%d\n",*max_element(dp[n-1].begin(),dp[n-1].end()));

	

	return 0;

}


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

#define ll long long

#define endl "\n"

#define shalaby ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);cerr.tie(0);

#define mod 100000000

using namespace std;



inline int D()

{

    int t;

    scanf("%d",&t);

    return t;

}

inline ll llD()

{

    ll t;

    scanf("%lld",&t);

    return t;

}

const int Max=1e5+8;

const int OO=-1e6;

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

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

////////////////////////////////////////////////////////////////////////////////////



ll mem[100005][5],a[100005],b[100005],c[100005],n,m;

bool valid(int x,int y)

{

    return x>-1 && y>-1 && x<n && y<m;

}

void init()

{

    memset(mem,-1,sizeof mem);

}

/////////////////////////////////////////////////////////////////////

ll solve(int i=0, int k=3)

{

    if(i==n)

        return 0;

    ll &ret=mem[i][k];

    if(~ret)return ret;

    ret =0;

    if(k!=0)

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



    if(k!=1)

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

    if(k!=2)

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

    return ret;

}



int main()

{

    init();

    int T=1;

///   T=D();

    while(T-->0)

    {

        cin >> n ;

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

        {

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

        }

        cout << solve();

    }

}


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

using namespace std;



int main()

{

	int n;

	scanf("%d",&n);

	

	int a,b,c;

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

	

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

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

	

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

	{

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

		

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

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

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

	}

	

	printf("%d\n",*max_element(dp[n-1].begin(),dp[n-1].end()));

	

	return 0;

}


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



#define pb push_back

#define mp make_pair



using namespace std;



typedef vector<int> vi;

typedef pair<int, int> ii;

typedef vector<ii> vii;

typedef unsigned long long ull;

typedef long long ll;





int DP[100005][3];

int V[100005][3];

bool ok[100005][3];

int N;



int dp(int i, int m) {

    if (ok[i][m]) {

        return DP[i][m];

    }

    

    if (i == N+1) {

        return 0;

    } else {

        int res = 0;

        if (m == 0) {

            res = max(dp(i+1, 1) + V[i][1], dp(i+1, 2) + V[i][2]);

        } else if (m == 1) {

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

        } else {

            res = max(dp(i+1, 0) + V[i][0], dp(i+1, 1) + V[i][1]);

        }

        

        ok[i][m] = true;

        DP[i][m] = res;

        return DP[i][m];

    }

}



int main() {

    std::ios::sync_with_stdio(false);

    cin.tie(nullptr);

    cout.tie(nullptr);

    

    cin >> N;

    

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

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

    }

    

    int res = 0;

    res = dp(1, 1);

    memset(DP, 0, sizeof(DP));

    memset(ok, 0, sizeof(ok));

    res = max(res, dp(1, 2));

    memset(DP, 0, sizeof(DP));

    memset(ok, 0, sizeof(ok));

    res = max(res, dp(1, 3));

    

    cout << res << endl;

    

	return 0;

}

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

using namespace std;



int main()

{

	int n;

	scanf("%d",&n);

	

	int a,b,c;

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

	

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

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

	

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

	{

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

		

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

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

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

	}

	

	printf("%d\n",*max_element(dp[n-1].begin(),dp[n-1].end()));

	

	return 0;

}


// Fastest known solution for this problem: