stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#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);

}


// Faster C++ solution for the same problem (measured speedup: 2.502645020824685x):
#include <bits/stdc++.h>

using namespace std;



#define ll long long

#define mp make_pair

#define pb push_back

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

#define rloop(i, n)     for(int i = int(n)-1 ; i >= 0 ; --i)

#define loopOne(i, n)   for(int i = 1 ; i <= n; ++i)

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

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



void GO(){

    ios_base::sync_with_stdio(false);

    cin.tie(0);

    cout.tie(0);

}

const ll N = 1e5+100, OO = 1e9;

int mx = -OO, mn = OO;

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

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



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



int Solve(int idx, int prev){

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

    if(idx == n)    return 0;

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



    int res = -OO;

    if(prev == 0){  // The previous one was 'A'

        memo[idx][prev] = max(b[idx] + Solve(idx+1, 1), c[idx] + Solve(idx+1, 2));

    }else if(prev == 1){  // The previous one was 'b'

        memo[idx][prev] = max(a[idx] + Solve(idx+1, 0), c[idx] + Solve(idx+1, 2));

    }else if(prev == 2){  // The previous one was 'c'

        memo[idx][prev] = max(b[idx] + Solve(idx+1, 1), a[idx] + Solve(idx+1, 0));

    }else{

        memo[idx][prev] = max(max(a[idx] + Solve(idx+1, 0), b[idx] + Solve(idx+1, 1)), c[idx] + Solve(idx+1, 2));

    }

    return memo[idx][prev];

}

int main() {

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

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

    GO();

    scanf("%d", &n);

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

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

    }

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

    printf("%d", Solve(0, 3));

    return 0;

}
// Slower C++ solution:
#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();

    }

}


// Faster C++ solution for the same problem (measured speedup: 1.94650045101691x):
#include <bits/stdc++.h>

using namespace std;



#define ll long long

#define mp make_pair

#define pb push_back

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

#define rloop(i, n)     for(int i = int(n)-1 ; i >= 0 ; --i)

#define loopOne(i, n)   for(int i = 1 ; i <= n; ++i)

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

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



void GO(){

    ios_base::sync_with_stdio(false);

    cin.tie(0);

    cout.tie(0);

}

const ll N = 1e5+100, OO = 1e9;

int mx = -OO, mn = OO;

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

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



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



int Solve(int idx, int prev){

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

    if(idx == n)    return 0;

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



    int res = -OO;

    if(prev == 0){  // The previous one was 'A'

        memo[idx][prev] = max(b[idx] + Solve(idx+1, 1), c[idx] + Solve(idx+1, 2));

    }else if(prev == 1){  // The previous one was 'b'

        memo[idx][prev] = max(a[idx] + Solve(idx+1, 0), c[idx] + Solve(idx+1, 2));

    }else if(prev == 2){  // The previous one was 'c'

        memo[idx][prev] = max(b[idx] + Solve(idx+1, 1), a[idx] + Solve(idx+1, 0));

    }else{

        memo[idx][prev] = max(max(a[idx] + Solve(idx+1, 0), b[idx] + Solve(idx+1, 1)), c[idx] + Solve(idx+1, 2));

    }

    return memo[idx][prev];

}

int main() {

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

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

    GO();

    scanf("%d", &n);

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

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

    }

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

    printf("%d", Solve(0, 3));

    return 0;

}
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 1.1481598243967328x):
#include <bits/stdc++.h>

using namespace std;



#define ll long long

#define mp make_pair

#define pb push_back

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

#define rloop(i, n)     for(int i = int(n)-1 ; i >= 0 ; --i)

#define loopOne(i, n)   for(int i = 1 ; i <= n; ++i)

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

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



void GO(){

    ios_base::sync_with_stdio(false);

    cin.tie(0);

    cout.tie(0);

}

const ll N = 1e5+100, OO = 1e9;

int mx = -OO, mn = OO;

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

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



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



int Solve(int idx, int prev){

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

    if(idx == n)    return 0;

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



    int res = -OO;

    if(prev == 0){  // The previous one was 'A'

        memo[idx][prev] = max(b[idx] + Solve(idx+1, 1), c[idx] + Solve(idx+1, 2));

    }else if(prev == 1){  // The previous one was 'b'

        memo[idx][prev] = max(a[idx] + Solve(idx+1, 0), c[idx] + Solve(idx+1, 2));

    }else if(prev == 2){  // The previous one was 'c'

        memo[idx][prev] = max(b[idx] + Solve(idx+1, 1), a[idx] + Solve(idx+1, 0));

    }else{

        memo[idx][prev] = max(max(a[idx] + Solve(idx+1, 0), b[idx] + Solve(idx+1, 1)), c[idx] + Solve(idx+1, 2));

    }

    return memo[idx][prev];

}

int main() {

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

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

    GO();

    scanf("%d", &n);

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

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

    }

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

    printf("%d", Solve(0, 3));

    return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h> 

using namespace std;

#define INF INT_MAX

#define N 100005

#define K 3

int n, k;

int arr[N][K];

int dp[N][K][K];

int solve(int idx, int last_k, int cur_k){

    int res, take = -1, leave = -1;

    if(idx == n)

        return 0;

    if(cur_k == K)

        return -1;

    if(~last_k && ~dp[idx][last_k][cur_k])

        return dp[idx][last_k][cur_k];

    if(cur_k == last_k)

        return dp[idx][last_k][cur_k] = solve(idx, last_k, cur_k+1);

    take = solve(idx+1, cur_k, 0);

    leave = solve(idx, last_k, cur_k+1);

    if(take == -1)

        return dp[idx][last_k][cur_k] = leave;

    take += arr[idx][cur_k];

    if(leave == -1)

        return dp[idx][last_k][cur_k] = take;

    if(idx == 1 && cur_k == 1 && last_k == 2)

        cout << take << ' ' << leave << '\n';

    return dp[idx][last_k][cur_k] = max(take, leave);

}

int main()

{

    cin >> n;

    memset(dp, -1, sizeof(dp[0][0][0]) * N * K * K);

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

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

            cin >> arr[i][j];

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

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 4.601822293451138x):
#include<bits/stdc++.h>

using namespace std;

const int N = 1e5 + 5;

int dp[N][3];

int main(){

	int n;

	cin >> n;

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

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

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

			cin >> arr[i][j];

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

		dp[n-1][j] = arr[n-1][j];

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

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

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

	}

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

		dp[0][0] = max(dp[0][0], dp[0][j]);

	cout << dp[0][0];

	return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;

int n,arr[100009][3];

int dp[100009][3];

int bt(int x,int tur)

{

    if(x==n)

        return 0;

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

        return dp[x][tur];

    int &ret=dp[x][tur];

    ret=arr[x][tur];

    int mx=0;

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

    {

        if(i==tur)

            continue;

        mx=max(mx,bt(x+1,i));

    }

    ret+=mx;

    return ret;

}

int main()

{

    cin>>n;

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

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

    {

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

    }

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

}


// Faster C++ solution for the same problem (measured speedup: 2.2059676690315118x):
#include<bits/stdc++.h>

using namespace std;

int n,k,dp[100009][3],a[100009][3],mx=1e9+8;

int main()

{

    cin>>n;

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

    {

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

    }

    for(int i=0;i<3;i++)dp[0][i]=a[0][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)continue;

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

            }

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

        }

    }

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

}
// Slower C++ solution:
#include <bits/stdc++.h>



using namespace std;

long long dp[100009][3],n,arr[100009][3];

int bt(int x,int b)

{

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

        return dp[x][b];

    if(x==n)

        return 0;

    long long &ret=dp[x][b]=-1;

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

    {

        if(i==b)

            continue;

        ret=max(bt(x+1,i)+arr[x+1][i],ret);

    }

    return ret;

}

int main()

{

    cin>>n;

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

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

    {

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

        {

            cin>>arr[i][j];

        }

    }

    cout<<max(bt(0,0)+arr[0][0],max(bt(0,1)+arr[0][1],bt(0,2)+arr[0][2]));

}


// Faster C++ solution for the same problem (measured speedup: 3.4011906352695767x):
#include<bits/stdc++.h>

using namespace std;

int n,k,dp[100009][3],a[100009][3],mx=1e9+8;

int main()

{

    cin>>n;

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

    {

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

    }

    for(int i=0;i<3;i++)dp[0][i]=a[0][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)continue;

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

            }

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

        }

    }

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

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

int n,k;

int dp[200009][3],arr[200009][3];

int bt(int x,int pre)

{

    if(x==n)return 0;

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

    int &ret=dp[x][pre];

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

    {

        if(pre==i)continue;

        ret=max(ret,bt(x+1,i)+arr[x][i]);

    }

    return ret;

}

int main()

{

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

    cin>>n;

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

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

}


// Faster C++ solution for the same problem (measured speedup: 3.4007059534454576x):
#include<bits/stdc++.h>

using namespace std;

int n,k,dp[100009][3],a[100009][3],mx=1e9+8;

int main()

{

    cin>>n;

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

    {

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

    }

    for(int i=0;i<3;i++)dp[0][i]=a[0][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)continue;

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

            }

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

        }

    }

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

}
// Slower C++ solution:
#include <bits/stdc++.h>

 

using namespace std;

 

#define lli long long int

#define REP(i,s,l) for(lli i=s;i<l;i++)

#define DEBUG 0

#define INF (1LL<<50)

#define MOD 1000000007



lli dp[3][100010];



signed main(){



	lli n;

	cin>>n;

	map<lli,vector<lli>> d;

	REP(i,0,n){

		lli a,b,c;

		cin>>a>>b>>c;

		d[0].push_back(a);

		d[1].push_back(b);

		d[2].push_back(c);

	}





	REP(i,0,3)REP(j,0,100010)dp[i][j]=0;



	REP(i,0,3){

		dp[i][0]=d[i][0];

		if(DEBUG)cout<<dp[i][0]<<" ";

	}



	if(DEBUG)cout<<endl;



	for(lli i=1;i<n;i++){/*深さ*/

		for(lli j=0;j<3;j++){//今の行動

			for(lli k=0;k<3;k++){//前の行動

				if(j==k)continue;

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

			}

			if(DEBUG)cout<<dp[j][i]<<" ";

		}

		if(DEBUG)cout<<endl;

	}



	lli ans=0;

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

	cout<<ans<<endl;



	return 0;

}


// Faster C++ solution for the same problem (measured speedup: 3.4046272731556266x):
#include <bits/stdc++.h>

using namespace std;



#define lli long long int

#define REP(i,s,n) for(int i=s;i<n;i++)

#define MOD 1000000007

#define NUM 2520

#define INF (1LL<<50)

#define DEBUG 0

#define mp(a,b) make_pair(a,b)

#define SORT(V) sort(V.begin(),V.end())

#define PI (3.141592653589794)



//dp[i][j],i前の日にしたもの,j:index

lli dp[3][101000];

vector<lli> a[3];



int main(){



	lli n;

	cin>>n;



	REP(i,0,3)a[i].resize(n);

	REP(i,0,n)REP(j,0,3)cin>>a[j][i];



	REP(i,0,3)dp[i][0]=a[i][0];



	//j before,k,after

	REP(i,0,n-1)REP(j,0,3)REP(k,0,3){

		if(j==k)continue;

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

	}



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





    return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>



using namespace std;



int n;

int cost[100001][3];

long long dp[100001][3];

long long rec(int pos , int type){

    if(pos >= n)return 0;

    if(~dp[pos][type])return dp[pos][type];

    long long ans = 0;

    if(type==0){

        ans += max(rec(pos + 1, 1) , rec(pos + 1, 2)) + cost[pos][type];

    }

    if(type == 1){

        ans += max(rec(pos + 1 , 2) , rec(pos + 1 , 0)) + cost[pos][type];

    }

    if(type == 2){

        ans += max(rec(pos + 1 , 1), rec(pos + 1, 0)) + cost[pos][type];

    }

    return dp[pos][type] = ans;



}



int main() {

    cin>>n;

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

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

            cin>>cost[i][j];

        }

    }

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

    long long ret0 = rec(0 , 0);

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

    long long ret1 = rec(0 , 1);

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

    long long ret2 = rec(0 , 2);

    cout<<max({ret0 , ret1 , ret2})<<"\n";

}

// Faster C++ solution for the same problem (measured speedup: 4.991501464439651x):
#include <bits/stdc++.h>



using namespace std;

int cost[100001][3];

long long dp[100001][3];



int main()

{

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

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

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

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

       }

   }

   dp[0][0]=cost[0][0];

   dp[0][1]=cost[0][1];

   dp[0][2]=cost[0][2];

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

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

           if(j==0){

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

           }

           if(j==1){

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

           }

           if(j==2){

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

           }

       }

   }

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

   

   

   return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;

int n;

int dp[100001][4];

int point[100001][4];



int maxP(int i,  int last){

    if(i==n)

    return 0;

    

    int ret=INT_MIN;

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

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

        

        if(j!=last)

        {

            ret= max(ret, point[i][j]+maxP(i+1, j));

        }

        

        

     }

     return dp[i][last] = ret;

}







int main() {

     cin>>n;

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

     {

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

     }

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

     cout<<maxP(0,0);

     return 0;

    

}


// Faster C++ solution for the same problem (measured speedup: 2.5983907906145762x):
#include <bits/stdc++.h>

using namespace std;

// int n;

// int dp[100001][4];

// int point[100001][4];



// int maxP(int i,  int last){

//     if(i==n)

//     return 0;

    

//     int ret=INT_MIN;

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

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

        

//         if(j!=last)

//         {

//             ret= max(ret, point[i][j]+maxP(i+1, j));

//         }

        

        

//      }

//      return dp[i][last] = ret;

// }







int main() {

    int n;

     cin>>n;

     vector<int>val(3,0);

      vector<int>dp1(3,0);

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

         vector<int>dp2(3,0);

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

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

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

                 if(i!=j){

                     dp2[j] = max( dp2[j],dp1[i] + val[j]);

                 }

             }

             

         }

         dp1=dp2;

     }

     

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

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

    //  {

    //     cin>>point[i][1]>>point[i][2]>>point[i][3];

    //  }

    

     //cout<<maxP(0,0);

     return 0;

    

}
// Slower C++ solution:
//https://atcoder.jp/contests/dp/tasks/dp_c

#include <bits/stdc++.h>

//#define long int64_t

#define cerr if(0)cerr

#define watch(x) cerr << (#x) << " is " << (x) << endl

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

#define endl "\n"

#define max3(a,b,c) (a>=b and a>=c ? a : b>=c and b>=a ? b : c)

#define max(a,b) (a>b?a:b)

#define min(a,b) (a<b?a:b)

#define MOD 1e8 + 7

#define INF 2e9

#define DESPACITO 1e18

#define PI acos(-1);

#define E 998244353

#define pb push_back

#define mp make_pair

#define ll long long



using namespace std;

const int N = 1e5 + 5;

ll a[N], b[N], c[N], cache[N][3];



ll dp (int idx, int opt) {

    if (idx == 0)

        return 0;

    ll &ans = cache[idx][opt];

    if (ans != -1) 

        return ans;

    else {

        switch(opt) {

            case 0:

                ans = max(dp(idx-1, 1), dp(idx-1, 2)) + a[idx];

                break;

            case 1:

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

                break;

            case 2:

                ans = max(dp(idx-1, 0), dp(idx-1, 1)) + c[idx];

                break;

        }

        //cerr << "cache[" << idx << "][" << opt << "] is " << cache[idx][opt] << endl;

        return ans;

    }

}



int main() {

    int i, n;

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

    cin >> n;

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

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

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

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.530578405750117x):
//https://atcoder.jp/contests/dp/tasks/dp_c

#include <bits/stdc++.h>

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

using namespace std;

#define max3(x,y,z) max(x,max(y,z))

#define min(a,b) (a<b?a:b)

const int N = 1e5 + 5;

int n, a[N][3], cache[N][3];



int dp (int idx, int opt) {

    if (idx < 0)

        return 0;

    int &ans = cache[idx][opt];

    if (ans != -1)

        return ans;

    ans = a[idx][opt] + max(dp(idx-1, (opt+1)%3), dp(idx-1, (opt+2)%3));

    return ans;

}



int32_t main() {

    IOS;

    int i;

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

    cin >> n;

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

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

    cout << max3(dp(n, 0), dp(n, 1), dp(n, 2));

    return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;

using ll = long long;



const int N = 1e5 + 5;



int a[N][3];

ll dp[4][N];

int n;



ll solve(int p, int i) {

  if (i == n)

    return 0;

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

      return dp[p][i];

  for (int c = 0; c < 3; c++) if (c != p) {

    dp[p][i] = max(a[i][c] + solve(c, i+1), dp[p][i]);

  }

  return dp[p][i];

}



int main () {

  ios_base::sync_with_stdio(0);

  cin.tie(0);

  

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

  cin >> n;

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

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

  }

  cout << solve(3, 0) << '\n';

  return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.2367717258763196x):
#include <bits/stdc++.h>

using namespace std;

using ll = long long;



const int N = 1e5 + 5;



int a[N][3];

ll dp[N][3];

int n;



int main () {

  ios_base::sync_with_stdio(0);

  cin.tie(0);

  

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

  cin >> n;

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

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

  }

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

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

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

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

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

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

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

  }

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

  return 0;

}