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



using namespace std;



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

#define DB( x ) cout << #x << " = " << x << endl;

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

#define endl '\n'



typedef long long ll;

typedef unsigned long long ull;

typedef pair<ll, ll> pii;

typedef pair<int, pii> tri;

typedef vector <vector <ll>> matrix;

typedef pair<string, string> pss;



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

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



const ll oo = 0x3f3f3f3f3f3f3f3f;



ll a[MAX][5];



ll dp[MAX][5];



int main() {

    ios_base::sync_with_stdio(0);

    cin.tie(0);



    int n; 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 k = 1;k <= 3;++k){

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

                if(k == j) continue;

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

            }

        }

    }



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

}


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

#include <algorithm>



using namespace std;

const int maxn = 100000;

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



int main() {

    int n, a, b, c;

    scanf("%d", &n);

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

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

        arr[i][0] = a;

        arr[i][1] = b;

        arr[i][2] = c;

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

            if (i == 0) {

                dp[i][j] = arr[i][j];

            } else {

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

            }

        }

    }

    int ans = -1;

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

        if (dp[n - 1][j] > ans) {

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

        }

    }

    printf("%d", ans);

    return 0;

}






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

 

#define ull unsigned long long

#define ll long long

#define pb push_back

#define mp make_pair

#define y1 my_y1

#define fi first

#define se second

#define lcm mylcm

 

using namespace std;

 

const ll mod = 998244353;

const ll inf = 2e9;

const int N = 1e5 + 5;



ll n, a, c, b, d[N][5];



int main () {

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

    cin.tie(NULL);

	cout.tie(NULL);

	ios_base::sync_with_stdio(false);

	cin >> n;

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

		cin >> a >> b >> c;

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

		d[i][2] = max(d[i - 1][1], d[i - 1][3]) + b;

		d[i][3] = max(d[i - 1][1], d[i - 1][2]) + c;

	}

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

	return 0;

}

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

#include <algorithm>



using namespace std;

const int maxn = 100000;

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



int main() {

    int n, a, b, c;

    scanf("%d", &n);

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

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

        arr[i][0] = a;

        arr[i][1] = b;

        arr[i][2] = c;

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

            if (i == 0) {

                dp[i][j] = arr[i][j];

            } else {

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

            }

        }

    }

    int ans = -1;

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

        if (dp[n - 1][j] > ans) {

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

        }

    }

    printf("%d", ans);

    return 0;

}






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

using namespace std;

 

#define ll long long int



int main() {

    ll n;

    cin >> n;

    ll dp[n][3] = {};

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

        ll a, b, c;

        cin >> a >> b >> c;

        if(i == 0) {

            dp[i][0] = a;

            dp[i][1] = b;

            dp[i][2] = c;

        }

        else {

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

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

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

        }

    }

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

}

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

#include <algorithm>



using namespace std;

const int maxn = 100000;

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



int main() {

    int n, a, b, c;

    scanf("%d", &n);

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

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

        arr[i][0] = a;

        arr[i][1] = b;

        arr[i][2] = c;

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

            if (i == 0) {

                dp[i][j] = arr[i][j];

            } else {

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

            }

        }

    }

    int ans = -1;

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

        if (dp[n - 1][j] > ans) {

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

        }

    }

    printf("%d", ans);

    return 0;

}






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

/**

*   author: j o s e m a g o

*   date:

**/

using namespace std;

#pragma GCC optimize ("Ofast")

#define f first

#define s second

#define mp make_pair

#define lb lower_bound

#define ub upper_bound

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

#define mset(a,v) memset(a,v,sizeof(a))

#define pb push_back

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

#define rsz resize

#define ln "\n"



typedef double db;

typedef unsigned long long ull;

typedef long long ll;

typedef long double ld;

const ld PI = 2*acos(0.0);

const int MOD = 1000000007;





int matriz[100009][3];

int dp[100009][3];







int main(){



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

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



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





    int n;

    cin >> n;

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

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

            cin >> matriz[i][j];

        }

    }

    int m = 0;

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

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



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



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

                if(k != j){

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

                }

            }

            dp[i][j]+=m;

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

            m = 0;

        }

    }



    int sol = 0;

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

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

    }

    cout<<sol;







    return 0;

}

/*

7

6 7 8

8 8 3

2 5 2

7 8 6

4 6 8

2 3 4

7 5 1







*/

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

#include <algorithm>



using namespace std;

const int maxn = 100000;

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



int main() {

    int n, a, b, c;

    scanf("%d", &n);

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

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

        arr[i][0] = a;

        arr[i][1] = b;

        arr[i][2] = c;

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

            if (i == 0) {

                dp[i][j] = arr[i][j];

            } else {

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

            }

        }

    }

    int ans = -1;

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

        if (dp[n - 1][j] > ans) {

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

        }

    }

    printf("%d", ans);

    return 0;

}






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



/* run this program using the console pauser or add your own getch, system("pause") or input loop */

using namespace std;





int maxRow(int dp[][3],bool calculated[][3],int M[][3],int selection,int row){

	if(calculated[row][selection])return dp[row][selection];

	if(row==0){

		calculated[row][selection]=true;

		return dp[row][selection]=M[row][selection];

		

		}

		int max=-1;

		int parent=-1;

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

			if(i!=selection){

				if(max<maxRow(dp,calculated,M,i,row-1)+M[row][selection]){

				max=maxRow(dp,calculated,M,i,row-1)+M[row][selection];	

				parent=i;

				}

				

				

			}

		}

		calculated[row][selection]=true;

	

		

		return	dp[row][selection]=max; 

				

	}



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

	int N;

	cin>>N;

   int M[N][3];	

   bool calculated[N][3];

   int dp[N][3];

   

   





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

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

			cin>>M[i][j];

		calculated[i][j]=false;



		}

	}

	

	//cout<<maxRow(dp,calculated,M,0,N-1)<<endl;	

		int max=-1;

		int head=-1;

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

		if(max<maxRow(dp,calculated,M,i,N-1))

		max=maxRow(dp,calculated,M,i,N-1);

		

		}

	

	cout<<max<<endl;

	

	return 0;

}

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

#include <algorithm>



using namespace std;

const int maxn = 100000;

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



int main() {

    int n, a, b, c;

    scanf("%d", &n);

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

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

        arr[i][0] = a;

        arr[i][1] = b;

        arr[i][2] = c;

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

            if (i == 0) {

                dp[i][j] = arr[i][j];

            } else {

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

            }

        }

    }

    int ans = -1;

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

        if (dp[n - 1][j] > ans) {

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

        }

    }

    printf("%d", ans);

    return 0;

}






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

#include <algorithm>

#include <iostream>

#include <cstring>

#include <cstdlib>

#include <iomanip>

#include <vector>

#include <cstdio>

#include <stack>

#include <queue>

#include <deque>

#include <cmath>

#include <list>

#include <set>

#include <map>



using namespace std;



long long a[100010],b[100010],c[100010],f[100010][10];



int main(void)

{

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

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

	int n;

	cin >> n;

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

	{

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

	}

	f[1][1] = a[1];

	f[1][2] = b[1];

	f[1][3] = c[1];

	

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

	{

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

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

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

	}

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

	return 0;

}
























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

#include <algorithm>



using namespace std;

const int maxn = 100000;

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



int main() {

    int n, a, b, c;

    scanf("%d", &n);

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

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

        arr[i][0] = a;

        arr[i][1] = b;

        arr[i][2] = c;

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

            if (i == 0) {

                dp[i][j] = arr[i][j];

            } else {

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

            }

        }

    }

    int ans = -1;

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

        if (dp[n - 1][j] > ans) {

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

        }

    }

    printf("%d", ans);

    return 0;

}






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

#define eps 1e-3

#define pi acos(-1.0)

#define inf 0x3f3f3f3f

#define debug1 cout<<"&&";

#define debug2 cout<<"**";

#define pb push_back

#define ms(a, x) memset(a, x, sizeof(a))

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

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

typedef double db;

typedef long long ll;

typedef unsigned long long ull;

using namespace std;

const int mod = 1e9;

const int N = 1e5+10;

/*=======================================================================*/



int n;

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

// ll ab, ac, ba, bc, ca, cb;

ll aa, bb, cc;

ll ans = 0;



int main() {

   scanf("%d", &n);

   for1(i, n) {

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

   }

   for1(i, n) {

      if(i == 1) {

         aa += a[i];

         bb += b[i];

         cc += c[i];

      } else {

         int ta = aa, tb = bb, tc = cc;

         aa = max(tb + a[i], tc + a[i]);

         bb = max(ta + b[i], tc + b[i]);

         cc = max(ta + c[i], tb + c[i]);

      }

   }

   ans = max(ans, aa);

   ans = max(ans, cc);

   ans = max(ans, bb);

   cout << ans << endl;

   return 0;

}

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

#include <algorithm>



using namespace std;

const int maxn = 100000;

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



int main() {

    int n, a, b, c;

    scanf("%d", &n);

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

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

        arr[i][0] = a;

        arr[i][1] = b;

        arr[i][2] = c;

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

            if (i == 0) {

                dp[i][j] = arr[i][j];

            } else {

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

            }

        }

    }

    int ans = -1;

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

        if (dp[n - 1][j] > ans) {

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

        }

    }

    printf("%d", ans);

    return 0;

}






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

#define int long long

#define ll long long

#define ld long double

#define mod ((ll)1e9+7)

#define maxx ((ll)1e6+5)

#define pb push_back

#define ins insert

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

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

#define test ll t; cin>>t; while(t--)

ll exp(ll x,ll y,ll p){ ll res=1; while(y) {if(y%2) res=(res*x%p)%p; x=(x*x)%p; y/=2; } return res;}

ll expo(ll x,ll y){ ll res=1; while(y) {if(y%2) res=(res*x%mod)%mod; x=(x*x)%mod; y/=2; } return res;}

/*int sv[1000000]={0};

ll fact[1000007]={0};

void facto() {fact[0]=1;fact[1]=1;for(int i=2;i<100007;i++)fact[i]=(fact[i-1]*i)%mod;}

void sve() {sv[0]=1;sv[1]=1;for(int i=2;i*i<=maxx;i++){if(!sv[i]){for(int j=2*i;j<=maxx;j+=i)sv[j]=1;}}}//0 hai to prime

ll ncr(ll n,ll r) {ll res=1; res=fact[n]; res=(res*(expo(fact[r],mod-2)))%mod; res=(res*(expo(fact[n-r],mod-2)))%mod; return res;}

ll npr(ll n,ll r) {ll res=1; res=fact[n]; res=(res*(expo(fact[n-r],mod-2)))%mod; return res;}*/

int d[100005][3];

using namespace std;

signed main()

{

    int n,k,i,j;

    cin >> n;

    int a[n][3];

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

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

    		cin >> a[i][j];

    d[0][0] = a[0][0];

    d[0][1] = a[0][1];

    d[0][2] = a[0][2];

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

    {

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

    	{

    		int p=0;

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

    		{

    			if(k!=j)

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

    		}

    		d[i][j] = p;

        }

   	}

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

    return 0;

}

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

#include <algorithm>



using namespace std;

const int maxn = 100000;

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



int main() {

    int n, a, b, c;

    scanf("%d", &n);

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

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

        arr[i][0] = a;

        arr[i][1] = b;

        arr[i][2] = c;

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

            if (i == 0) {

                dp[i][j] = arr[i][j];

            } else {

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

            }

        }

    }

    int ans = -1;

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

        if (dp[n - 1][j] > ans) {

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

        }

    }

    printf("%d", ans);

    return 0;

}






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

     

#define ll long long

#define pb push_back

#define ff first

#define ss second

#define sz size()

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

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

#define mod 1000000007

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

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

     

using namespace std;

     

int main(){

    IO;       

    int n;

    cin >> n;

    vector<array<int,3>> h(n);

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

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

            cin >> h[i][j];

        }

    }

 

    vector<array<ll,3>> dp(n);

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

        dp[0][j] = h[0][j];

    }

    

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

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

            dp[i][j] = 0;

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

                if (j == k) continue;

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

            }

        }

    }

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

}

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

#include <algorithm>



using namespace std;

const int maxn = 100000;

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



int main() {

    int n, a, b, c;

    scanf("%d", &n);

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

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

        arr[i][0] = a;

        arr[i][1] = b;

        arr[i][2] = c;

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

            if (i == 0) {

                dp[i][j] = arr[i][j];

            } else {

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

            }

        }

    }

    int ans = -1;

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

        if (dp[n - 1][j] > ans) {

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

        }

    }

    printf("%d", ans);

    return 0;

}






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

#include <cstdio>

#include <string>



using namespace std;





int n;

int dp[100007][4];

int a[100007][4];

int main() {

    

    scanf("%d",&n);

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

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

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

        }

    }

    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] = 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 = max(dp[n-1][0],dp[n-1][1]);

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

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

}

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

#include <algorithm>



using namespace std;

const int maxn = 100000;

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



int main() {

    int n, a, b, c;

    scanf("%d", &n);

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

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

        arr[i][0] = a;

        arr[i][1] = b;

        arr[i][2] = c;

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

            if (i == 0) {

                dp[i][j] = arr[i][j];

            } else {

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

            }

        }

    }

    int ans = -1;

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

        if (dp[n - 1][j] > ans) {

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

        }

    }

    printf("%d", ans);

    return 0;

}






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

#include<algorithm>

using namespace std;

int main()

{

	int dp[2][3],a[3],n,i=0;

	cin >> n;

	dp[0][0] = 0;

	dp[0][1] = 0;

	dp[0][2] = 0;

	while (n--) {                                              

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

			i = 1 - i;

	}

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

	return 0;

}

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

#include <algorithm>



using namespace std;

const int maxn = 100000;

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



int main() {

    int n, a, b, c;

    scanf("%d", &n);

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

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

        arr[i][0] = a;

        arr[i][1] = b;

        arr[i][2] = c;

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

            if (i == 0) {

                dp[i][j] = arr[i][j];

            } else {

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

            }

        }

    }

    int ans = -1;

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

        if (dp[n - 1][j] > ans) {

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

        }

    }

    printf("%d", ans);

    return 0;

}






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

    #include <fstream>

    #include <iomanip>

    #include <iostream>

    #include <map>

    #include <math.h>

    #include <set>

    #include <stdio.h>

    #include <string>

    #include <utility>

    #include <vector>

     

    using namespace std;

     

    int main() {

      int n;

      cin >> n;

      vector<long long> act[3];

      act[0].resize(n);

      act[1].resize(n);

      act[2].resize(n);

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

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

     

      long long dp[n + 1][3];

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

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

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

          dp[i][j] = 0;

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

            if (j == k)

              continue;

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

          }

        }

      }

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

      return 0;

    }

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

#include <algorithm>



using namespace std;

const int maxn = 100000;

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



int main() {

    int n, a, b, c;

    scanf("%d", &n);

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

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

        arr[i][0] = a;

        arr[i][1] = b;

        arr[i][2] = c;

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

            if (i == 0) {

                dp[i][j] = arr[i][j];

            } else {

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

            }

        }

    }

    int ans = -1;

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

        if (dp[n - 1][j] > ans) {

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

        }

    }

    printf("%d", ans);

    return 0;

}






// Fastest known solution for this problem: