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

using namespace std;

//#define sort(vec) sort(vec.begin(),vec.end())

#define reverse(vec) reverse(vec.begin(),vec.end())

#define unsort(vec); sort(vec);reverse(vec);

#define int long long

#define inf 1000000007



int gcd(int x,int y){

	if(y==0) return x;

	else return gcd(y,x%y);

}



int lcm(int x,int y){

	return x/gcd(x,y)*y;

}



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

int dp[114514][5];



int rec(int i,int j){

	if(dp[i][j]>=0)return dp[i][j];

	int res;

	if(i==n){

		res=0;

	}

	else if(j==0){

		res=max(rec(i+1,1)+b[i],rec(i+1,2)+c[i]);

	}

	else if(j==2){

		res=max(rec(i+1,0)+a[i],rec(i+1,1)+b[i]);

	}

	else{

		res=max(rec(i+1,0)+a[i],rec(i+1,2)+c[i]);

	}

	return dp[i][j]=res;

}



signed main(){

	cin>>n;

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

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

	cout<<max({rec(0,0),rec(0,1),rec(0,2)})<<endl;

}

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

using namespace std;

//#define sort(vec) sort(vec.begin(),vec.end())

#define reverse(vec) reverse(vec.begin(),vec.end())

#define unsort(vec); sort(vec);reverse(vec);

#define int long long

#define inf 1000000007



int gcd(int x,int y){

	if(y==0) return x;

	else return gcd(y,x%y);

}



int lcm(int x,int y){

	return x/gcd(x,y)*y;

}



signed main(){

	int n;

	cin>>n;

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

	memset(dp,0,sizeof(dp));

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

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

	}

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

	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-1][0],dp[n-1][1],dp[n-1][2]})<<endl;

}

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

using namespace std;

const int ms = 1e5+9;

int dp[ms][3], n;

int activity[ms][3];



int solve(int pos, int action){



  if(pos == n)

    return 0;



  int &ans  = dp[pos][action];

  if(~ans) return ans;

  

  ans = 0;



  int next_action = (action + 1) %3;

  int prev_action = (action + 2) %3;



  ans = max(activity[pos][next_action] + solve(pos+1, next_action), activity[pos][prev_action] + solve(pos + 1, prev_action));



  return ans;

}







int main(){



  cin >> n;

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

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

  }



  memset(dp, -1, sizeof dp);



  int ans = 0;



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

    ans = max(ans, solve(0, i));

  }



  cout<<ans;





}

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

using namespace std;

const int ms = 1e5+9;

int dp[ms][3], n;

int activity[ms][3];





int main(){



  cin >> n;

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

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

  }



  int ans = 0;





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

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

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

      int next_pos = (j+1)%3;

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

      next_pos = (next_pos + 1)%3;

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

    }

  }



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

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





  cout<<ans;





}

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

using namespace std;

 

typedef pair<int, int> ii;

typedef vector<ii> vii;

typedef vector<int> vi;

typedef pair<int,ii> iii;

typedef pair<int, iii> iiii;

typedef pair<double, ii> dii;

typedef long long ll;

typedef pair<ll, ii> lii;

const int INF = 1000000000;

const double EPS = 1e-9;

#define LSOne(S) (S & (-S))

int mods(int a, int b) { return (b + (a%b)) % b; }



int n, mx, a[100005][3], dp[100005][3];



int solve(int days, int prev) {

    if (days == n) return 0;

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



    int res = 0;

    if (prev != 0) res = max(res, a[days][0] + solve(days+1, 0));

    if (prev != 1) res = max(res, a[days][1] + solve(days+1, 1));

    if (prev != 2) res = max(res, a[days][2] + solve(days+1, 2));



    return dp[days][prev] = res;

}

 

int main() {

    ios_base::sync_with_stdio(false);

    cin.tie(0);

    cout.tie(0);

 

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

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



    cin >> n;



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

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



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

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

 

    return 0;

}


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

using namespace std;

 

typedef pair<int, int> ii;

typedef vector<ii> vii;

typedef vector<int> vi;

typedef pair<int,ii> iii;

typedef pair<int, iii> iiii;

typedef pair<double, ii> dii;

typedef long long ll;

typedef pair<ll, ii> lii;

const int INF = 1000000000;

const double EPS = 1e-9;

#define LSOne(S) (S & (-S))

int mods(int a, int b) { return (b + (a%b)) % b; }



int n, mx, a[3], dp[3], new_dp[3];

 

int main() {

    ios_base::sync_with_stdio(false);

    cin.tie(0);

    cout.tie(0);

 

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

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



    cin >> n;



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

        memset(new_dp, 0, sizeof(new_dp));

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

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

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

                if (j != k) new_dp[k] = max(new_dp[k], dp[j] + a[k]);

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

            dp[j] = new_dp[j];

    }



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

        mx = max(mx, dp[i]);



    cout << mx << '\n';

 

    return 0;

}


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



using namespace std;

typedef long long ll;

const int N = 1e5 + 5;



int n, s[N][3];

ll dp[N][4];



ll solve(int i, int t) {

    if(i == n) return 0;

    ll& res = dp[i][t];

    if(~res) return res;

    res = 0;

    if(t != 0) res = max(res, solve(i+1, 0) + s[i][0]);

    if(t != 1) res = max(res, solve(i+1, 1) + s[i][1]);

    if(t != 2) res = max(res, solve(i+1, 2) + s[i][2]);

    return res;

}



int main() {

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

    scanf("%d", &n);

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

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

    printf("%I64d\n", solve(0, 3));

    return 0;

}


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



using namespace std;



typedef long long ll;



void fastInOut();

const int N = 1e5 + 2;



int a[3][N], n;



int dp[N][4];

int solve(int i = 0, int prv = 3)

{

    if(i == n) return 0;

    int &res = dp[i][prv];

    if(~res) return res;

    res = 0;

    if(prv != 0) res = max(res, solve(i+1, 0) + a[0][i]);

    if(prv != 1) res = max(res, solve(i+1, 1) + a[1][i]);

    if(prv != 2) res = max(res, solve(i+1, 2) + a[2][i]);

    return res;

}

int main() {

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

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

    fastInOut();

    cin >> n;

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

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

    memset(dp, -1, sizeof dp);

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

    return 0;

}



void fastInOut() {

    ios_base::sync_with_stdio(0);

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

}


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

#include <vector>

#include <algorithm>

#include <queue>

#include <cstring>

#include <cstdio>

#include <cmath>

#include <set>

#include <map>

#include <bitset>

using namespace std;

using ll = long long;

using itn = int;

const ll MOD = 1000000007;

const ll INF = MOD * MOD;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }

template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }



ll dp[100010][3];



int main() {



    int n;

    cin >> n;



    int a[100010][3];

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

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

            cin >> a[i][j];

        }

    }



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

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

            dp[i][j] = 0;

        }

    }



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

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

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

            

                if(j != k) {

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

                }

            }

        }

    }



    ll ans = 0;

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

        chmax(ans, dp[n][i]);

    }



    cout << ans << endl;

    

}

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

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

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

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

#define RALL(a) a.rbegin(), a.rend()

#define maxs(x,y) x = max(x, y)

#define mins(x,y) x = min(x, y)

using namespace std;

using ll = long long;

typedef pair<int, int> pint;

typedef pair<ll, ll> pll;

const ll MOD = 1000000007;

const ll INF = MOD * MOD;

const int inf = (1<<29);



int main() {



    int n;

    cin >> n;

    

    vector<int> a(n), b(n), c(n);

    REP(i, n) cin >> a[i] >> b[i] >> c[i];



    int dp[3][n];

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



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

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

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

    }



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



    return 0;

}

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

using namespace std;

typedef long long ll; const ll MM = 1000000000;ll mod = MM + 7;

#define rep(i, n) for(ll i=0; i<n; i++)

#define Rep(i, j, n) for(ll i=j; i<n; i++)

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

template<class T> inline bool chmin(T& a, T b) {if(a > b) {a = b; return true;} return false;}

template<class T> inline bool chmax(T& a, T b) {if(a < b) {a = b; return true;} return false;}

const ll INF = 1LL << 60;



ll dp[110000][3];



int main() {

    int n; cin >> n;

    vector<int> a(n), b(n), c(n);

    rep(i, n) cin >> a[i] >> b[i] >> c[i];

    rep(i, 110000) rep(j, 3) dp[i][j] = 0;

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

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

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

    Rep(i, 1, n) {

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

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

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

    }

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

}

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

using namespace std;

typedef long long ll;

const int MM = 1000000000;

const int MOD = MM + 7;

const int MAX = 510000;

#define rep(i, n) for(ll i=0; i<n; i++)

#define Rep(i, j, n) for(ll i=j; i<n; i++)

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

template<class T> inline bool chmin(T& a, T b) {if(a > b) {a = b; return true;} return false;}

template<class T> inline bool chmax(T& a, T b) {if(a < b) {a = b; return true;} return false;}

const ll INF = 1LL << 60;



ll dp[100100][3];



int main() {

    int n; cin >> n;

    vector<int> a(n), b(n), c(n);

    rep(i, n) cin >> a[i] >> b[i] >> c[i];

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

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

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

    rep(i, n) {

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

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

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

    }

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

}

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

using namespace std;

int dp[100004][4];

int n=0;

int vacation[100004][4];

int main() {

    cin>>n;

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

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

            dp[i][j]=INT_MIN;

    }

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

    memset(vacation,0,sizeof(vacation));

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

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

            cin>>vacation[i][j];

        }

    }

    // cout<<endl;

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

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

    //         cout<<vacation[i][j]<<" ";

    //     }

    //     cout<<endl;

    // }

    // cout<<endl;

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

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

            vector<int> v;

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

                if(j!=k){

                    v.push_back(dp[i-1][k]);

                }

            }

            dp[i][j]=vacation[i][j]+max(v[0],v[1]);

        }

    }

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

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

    //         cout<<dp[i][j]<<" ";

    //     }

    //     cout<<endl;

    // }

    // cout<<endl;

    int ans=0;

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

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

    }

    cout<<ans<<endl;

    return 0;

}


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

using namespace std;

#define ll long long int

#define inf 1e9

int main(){

ll N=0;cin>>N;

ll a[N][3];

for(ll i=0;i<N;++i){

	a[i][0]=a[i][1]=a[i][2]=0;

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

}

ll dp[N][3];

for(ll i=0;i<N;++i){

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

}

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

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

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

		ll option1= (j==0)?0:dp[i-1][0];

		ll option2= (j==1)?0:dp[i-1][1];

		ll option3= (j==2)?0:dp[i-1][2];

		dp[i][j]=a[i][j]+max(option1,max(option2,option3));

	}		

}

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

return 0;

}

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

using namespace std;



using ll = long long;



const int N = 2 * 1e5 + 10;

const ll inf = 5e18;



ll res, n;

ll a[N][4], dp[N][4];



ll go(ll idx, ll prev){

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

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

	ll mx = -inf;

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

		if(prev == i) continue;

		mx = max(mx, a[idx][i] + go(idx + 1, i));

	}

	return dp[idx][prev] =  mx;

}





void solve(){

	cin >> n;

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

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

	}

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

	cout << go(1, 0);

}



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

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

  ll t = 1;

  //cin >> t;

  while(t--){

    solve();

  }

    return 0;

}

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

using namespace std;



typedef long long ll;



const int N = 1 * 1e5 + 10;

const ll inf = 1 * 1e9 + 10;



ll res;

ll a[N];



void solve() {

	ll  n;

	cin >> n;

	ll dp[n][3];

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

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

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

			cin >> a[j];

		}

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

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

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

	}

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

}



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

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

  ll t = 1;

  //cin >> t;

  while(t--){

    solve();

  }

    return 0;

}

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

using namespace std;



#define forl(V, N) for(int V = 0; V < N; V++)

#define forll(V, S, E) for(int V = S; V < E; V++)

#define forlc(V, S, E, I) for(int V = S; V < E; V += I)

#define fore(E, X) for(auto &E:X)

#define print(X) cout << X

#define printsp(X) cout << X << " "

#define println(X) cout << X << endl

#define rem(X) X.top(); X.pop()

#define mid(A, B) ((A+B) / 2)

#define setmin(A, B) A = min(A, B)

#define setmax(A, B) A = max(A, B)

#define graph unordered_map<int, unordered_set<int>>

#define vi vector<int>

#define pb push_back

#define ll long long



int N;

int AC[100001][3];

int DP[100001][3];



int recur(int I, int BN) {

    if(I==N) return 0;

    int MX = 0;

    forl(i, 3) {

        if(i == BN) continue;

        if(DP[I+1][i] == -1) DP[I+1][i] = recur(I+1, i);

        setmax(MX, AC[I][i] + DP[I+1][i]);

    }

    return MX;

}



int main() {

    cin >> N;

    forl(i, 100001) fill_n(DP[i], 3, -1);

    forl(i, N) {

        forl(j, 3) cin >> AC[i][j]; 

    }

    println(recur(0, -1));

}

// Optimized solution to the same problem (faster):
// Justin Choi

#include <bits/stdc++.h>

using namespace std;



string FILENAME = "template";

#define io ifstream fin(FILENAME + ".in"); ofstream fout(FILENAME + ".out")

#define fwrite(X) fout << X;

#define fwriteln(X) fout << X << endl;



#define forl(V, N) for(int V = 0; V < N; V++)

#define fore(E, X) for(auto &E:X)

#define print(X) cout << X

#define println(X) cout << X << endl

#define printe(X) for(auto &E:X) cout << E << " "; cout << endl

#define rem(X) X.top(); X.pop()

#define mid(A, B) ((A+B) / 2)

#define setmin(A, B) A = min(A, B)

#define setmax(A, B) A = max(A, B)

#define graph unordered_map<int, unordered_set<int>>

#define vi vector<int>

#define pb push_back

#define ll long long



int main() {

    int N;

    cin >> N;

    vector<vi> active;

    forl(i, N) {

        int A, B, C;

        cin >> A >> B >> C;

        active.pb({A, B, C});

    }

    ll dp[200000][3];

    dp[0][0] = active[0][0];

    dp[0][1] = active[0][1];

    dp[0][2] = active[0][2];

    forl(i, N-1) {

        forl(j, 3){

            forl(k, 3) {

                if(k == j) continue;

                setmax(dp[i+1][k], active[i+1][k] + dp[i][j]);

            }

        }

    }

    println(max(dp[N-1][0], max(dp[N-1][1], dp[N-1][2])));

}

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

using namespace std;

#define ll long long

int N;

int A[100010][4];

int dp[100010][4];

int call(int x, int y)

{

    if(x>N) return 0;

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

    

    if(y==1)

    {

        if(x<N)

        return dp[x][y]=A[x][y]+max(call(x+1,2), (call(x+1,3)));

        else

        return dp[x][y]=A[x][y];

    }

    else if(y==2)

    {

        if(x<N)

        return dp[x][y]=A[x][y]+max(call(x+1,1), (call(x+1,3)));

        else

        return dp[x][y]=A[x][y];

    }

    else

    {

        if(x<N)

        return dp[x][y]=A[x][y]+max(call(x+1,2), (call(x+1,1)));

        else

        return dp[x][y]=A[x][y];

    }

}

int main()

{

    cin>>N;

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

    {

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

    }

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

    cout<<max(call(1,1),max(call(1,2),call(1,3)));

    return 0;

}

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

 

using namespace std;

 

int main() {

    int N;

    cin>>N;

    int A,A0,B,B0,C,C0;

    cin>>A>>B>>C;

    A0=A;

    B0=B;

    C0=C;

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

    {

        cin>>A>>B>>C;

        int a0,b0,c0;

        a0=A+max(B0,C0);

        b0=B+max(A0,C0);

        c0=C+max(B0,A0);

        A0=a0;

        B0=b0;

        C0=c0;

    }

    cout<<max(A0,max(B0,C0));

    

    return 0;

}

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

using namespace std;

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

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

#define REVERSE(c) reverse((c).begin(),(c).end())

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

const long long MOD = 1000000007;

const long long INF = 1LL << 60;



int main(){

	int n;

	cin >> n;

	long long a[100010][3];

	long long dp[100010][3];

	//i日目にjを選んだ時の最大値

	rep(i, n){

		rep(j, 3){

			cin >> a[i][j];

			dp[i][j] = 0;

		}

	}

	

	//dp[i + 1][a]...dp[i][b]とdp[i][c]のどちらから遷移する方が最大になるか

	

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

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

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

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

	}

	

	

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

}

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

using namespace std;



int main(){

    int N;

    scanf("%d", &N);

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

    for(int i = 0; i < N; i++)scanf("%d%d%d", &a[i], &b[i], &c[i]);

    int dp[N][3];

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

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

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

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

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

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

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

    }

    int ans = max({dp[N - 1][0], dp[N - 1][1], dp[N - 1][2]});

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

}

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



using namespace std;

const int N = 1e5 + 3 , OO = 0x3f3f3f3f ;

int arr1[N]  , arr2[N] , arr3[N], mem[N][5];

int n  ;

int solve(int idx , int pre){

    if(idx == n)

        return 0;

    int &ret = mem[idx][pre];

    if(~ret)

        return ret;

        if(pre == 0){

            ret = max(ret , arr1[idx] + solve(idx + 1 , 1));

            ret = max(ret , arr1[idx] + solve(idx + 1 , 2));

        }

         if(pre == 1){

            ret = max(ret , arr2[idx] + solve(idx + 1 , 0));

            ret = max(ret , arr2[idx] + solve(idx + 1 , 2));

        }

         if(pre == 2){

            ret = max(ret , arr3[idx] + solve(idx + 1 , 1));

            ret = max(ret , arr3[idx] + solve(idx + 1 , 0));

        }

    return ret;

}

int main()

{

    cin >> n ;

    for(int i = 0 ; i < n ; ++i) cin >> arr1[i] >> arr2[i] >> arr3[i];

    memset(mem , -1 , sizeof mem);

    cout << max({solve(0 , 0) , solve(0 , 1) , solve(0 , 2)});

    return 0;

}


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

using namespace std;

typedef long long ll;

const int N = 1e5 + 9 ;

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

int n;

int dp[N][5];

int solve(int day , int curr){          // top down

    if(day == n)

        return 0;

    int &ret = mem[day][curr];

    if(~ret)

        return ret ;

    if(curr == 0){

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

        ret = max(ret , a[day] + solve(day + 1 , 2)) ;

    }

    if(curr == 1){

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

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

    }

    if(curr == 2){

        ret = max(ret , c[day] + solve(day + 1 , 0)) ;

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

    }

    return ret ;



}

int main()

{



   // memset(mem ,-1 , sizeof mem);

    cin >> n ;

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

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

    }

    int ans = 0 ;

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

//        ans = max(ans , solve(0 , i));

//    cout << ans << endl ;

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

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

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

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

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

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

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

     }

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

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

     }

     cout << ans ;



    return 0;

}


// Fastest known solution for this problem: