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

#include <iostream>

#include <string>

#include <vector>

#include <algorithm>

#include <sstream>

#include <queue>

#include <deque>

#include <bitset>

#include <iterator>

#include <list>

#include <stack>

#include <map>

#include <set>

#include <functional>

#include <numeric>

#include <utility>



#include <cmath>

#include <iomanip>



using namespace std;



int main()

{

  ios::sync_with_stdio(false);

  cin.tie(0);

  int n;

  cin >> n;

  int dp[n][3]; // dp[i][j] represents the maximum happiness at the end of day i+1 if Taro did activity number j that day.

  int cur;

  cin >> cur;

  dp[0][0] = cur;

  cin >> cur;

  dp[0][1] = cur;

  cin >> cur;

  dp[0][2] = cur;

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

    cin >> cur;

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

    cin >> cur;

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

    cin >> cur;

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

  }

  cout << *max_element(dp[n-1], dp[n-1]+3) << "\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>

using namespace std;

#define ff first

#define ss second

#define ll long long int

#define pii pair<int,int>

#define pll pair<ll,ll>

#define mem(x) memset(x, 0, sizeof x)

#define mem1(x) memset(x, -1, sizeof x)

#define MX 100005

ll arr[MX][3],arr2[MX],arr3[MX], dp[MX][3],n;

//ll rec(ll i, ll score)

//{

//    if(i==n-1) return score;

//    else if(i>=n) return 1e9;

//    else if(dp[i]<score && dp[i]!=-1) return dp[i];

//    else if(!i || dp[i]==-1 || score<dp[i])

//    {

//        ll ret1=1e9, ret2=1e9;

//        if(i+1<=n-1) ret1= rec(i+1, score+llabs(arr[i+1]-arr[i]));

//        if(i+2<=n-1) ret2= rec(i+2, score+llabs(arr[i+2]-arr[i]));

//        return dp[i]=min(ret1,ret2);

//    }

//}

int main()

{

    ll i,j,k,l, m;

    scanf("%lld",&n);

    for(i=0; i<n; i++) scanf("%lld%lld%lld",&arr[i][0],&arr[i][1],&arr[i][2]);

//    for(i=1; i<n; i++) dp[i]=1e9;

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

    {

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

        {

            ll mx=0;

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

                if(k!=j) mx=max(arr[i-1][k],mx);

            arr[i][j]+=mx;

        }

    }

    printf("%lld\n",max(arr[n-1][0], max(arr[n-1][1],arr[n-1][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<iostream>

using namespace std;

int n;

int a[110000][4];

int m[110000][4];

int dp(int numTaken, int prevTaken){

	if(numTaken == n+1){

		return 0;

	}

	

	if(m[numTaken][prevTaken]!=-1) return m[numTaken][prevTaken];

	

	int ans = 0;

	// 3 pilihan

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

		if(i != prevTaken){

			ans = max(ans, dp(numTaken+1, i) + a[numTaken][i-1]);

		}

	}

	return m[numTaken][prevTaken] = ans;

}

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<=n;i++){

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

			m[i][j] = -1;

		}

	}

	cout<<dp(0,0)<<"\n";

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

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

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

//		}cout<<"\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>

using namespace std;



int dp[3][112345];

int mat[3][112345];



signed main()

{

    int n;

    cin >> n;



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

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



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

        dp[i][n] = 0;



    for(int i = n-1; i >= 0; --i)

    {

        dp[0][i] = max(mat[0][i] + dp[1][i+1],

                       mat[0][i] + dp[2][i+1]);



        dp[1][i] = max(mat[1][i] + dp[0][i+1],

                       mat[1][i] + dp[2][i+1]);



        dp[2][i] = max(mat[2][i] + dp[0][i+1],

                       mat[2][i] + dp[1][i+1]);

    }



    cout << max({dp[0][0], dp[1][0], dp[2][0]}) << 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>      

using namespace std;

const int N = 1e5 + 100;

#define pb push_back

#define mp make_pair

#define f first

#define s second

#define ull unsigned long long

#define ll long long

#define pii pair <int,int>

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

#define FOR(l,i,n) for(int i = l;i < n;++i)

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

#define pll pair <ll,ll>

const ll inf = 1e9 + 7;

const ll ll_inf = 1e17 + 100;   

const double pi = 3.14159265358979323;

inline void fr(){

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

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

}

void boost()

{

	ios_base::sync_with_stdio(0);

	cin.tie(0);

	cout.tie(0);

}

 

ll binpow (ll a, ll n)

{

    ll res = 1;

    while (n) {

        if (n & 1)

        {

            res *= a;

            res %= inf;

        }

        a *= a;

        n >>= 1;

        a %= inf;

    }

    return res;

}

        

int n;

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

int dp[N][5];

 

main()	

{                                                                        

	cin >> n;

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

	{

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

	}



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

	dp[1][2] = b[1];

	dp[1][3] = c[1];

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

	{

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

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

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

	}

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

}	 


// 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):
//by Top1

#include <bits/stdc++.h>

     

#define f first

#define s second

#define int long long

#define ld long double

#define ull unsigned long long

#define pii pair <int, int>

#define pll pair <long, long>

#define mp make_pair

#define pb push_back

#define pf push_front

#define pp pop_back

#define rev reverse

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

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

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

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

#define nxtp next_permutation

#define pvtp prev_permutation

#define KZ return 

#define x1 x21212121

#define y1 y21212121

#define sqr(x) x * x 

 

const int inf = INT_MAX;

const int mod = 1e9 + 7;

const int maxn = 6e5 + 111;

     

using namespace std;



int n, k, a[maxn], b[maxn], c[maxn], dp[5][maxn], q;



void solve(){    

	cin >> n;



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

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



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

	dp[2][1] = b[1];

	dp[3][1] = c[1];



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

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

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

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

	}



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

}



main(){

    boost;

	    

	q = 1;



	while (q--){

		solve();

	}

}

// 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 f first

#define se second

#define pb push_back

#define mp make_pair

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

#define lb lower_bound

#define ub upper_bound

#define pii pair<int, int>



using namespace std;



const int N = 1e5 + 100, mod = 1e9, inf = 1e9 + 7;

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



ll bin_pow(int x, int y){

    if(y == 0)

        return 1;

    if(y == 1)

        return x;

    if(y % 2 == 0){

        ll z = bin_pow(x, y / 2);

        return (z * z) % mod;

    }

    return (bin_pow(x, y - 1) * x) % mod;

}





int main(){

    boost;

    cin >> n;

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

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

    }

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

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

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

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

    }

    cout << max({dp[n][1], dp[n][2], dp[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):
/*|In The Name Of Allah|*/



#include <bits/stdc++.h>



using namespace std;



const int N = 1e5+5;



int n;

int a[N][3];

int mem[N][3];



int go(int idx , int lst){

    if(idx == n + 1)

        return 0;

    if(~mem[idx][lst])

        return mem[idx][lst];

    int best = 0;

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

        if(idx == 1 || lst != i){

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

        }

    }

    return mem[idx][lst] = best;

}



int main(){

    scanf("%d" , &n);

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

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

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

        }

    }

    memset(mem , -1 , sizeof mem);

    printf("%d\n" , go(1 , 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;

typedef long long ll;

typedef long long int lli;

typedef long double ld;

const int oo = 1e9;

//const int mod = 1e9+9;

/*ll gcd(ll x, ll y){return (y ? gcd(y, x%y) : x); }

*ll lcm(ll x, ll y){return x*(y/gcd(x,y));}

*void exEuclid(int a, int b){

	if(b==0){x = 1; y=0; d=a; return ;}

	exEuclid(b, a%b);

	int x1 = y;

	int y1 = x-(a/b)*y;

	x = x1; 100000

	y = y1;

} 

* */

//===========================================================//

int n;

const int nx = 1e5+5;

int ac[nx][3];

int dp[3][nx];

int go(int a, int i){

	if(i == n) return 0;

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

	int& ans = dp[a][i];

	

	if(a==0){

		ans=max(ans, go(1, i+1)+ac[i][0]);

		ans=max(ans, go(2, i+1)+ac[i][0]);

	}

	if(a==1){

		ans=max(ans, go(0, i+1)+ac[i][1]);

		ans=max(ans, go(2, i+1)+ac[i][1]);

	}

	if(a==2){

		ans=max(ans, go(0, i+1)+ac[i][2]);

		ans=max(ans, go(1, i+1)+ac[i][2]);

	}

	

	return ans;

}

int main(){

	cin>>n;

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

	memset(dp, -1, sizeof dp);

	cout<<max(go(0,0),max(go(1,0),go(2,0)))<<"\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>

using namespace std;



int m;

const int mx = 1e5+5;

int tasks[mx][3];

int diario[3][mx];

int cosa(int a, int i){

	if(i == m){

     return 0;

     }



	if(diario[a][i] != -1) {

        return diario[a][i];

    }

	int& ans = diario[a][i];



        if(a==0){

		ans=max(ans, cosa(1, i+1)+tasks[i][0]);

		ans=max(ans, cosa(2, i+1)+tasks[i][0]);

	}

        if(a==1){

		ans=max(ans, cosa(0, i+1)+tasks[i][1]);

		ans=max(ans, cosa(2, i+1)+tasks[i][1]);

        }

	if(a==2){

		ans=max(ans, cosa(0, i+1)+tasks[i][2]);

		ans=max(ans, cosa(1, i+1)+tasks[i][2]);

	}



	return ans;

}

int main(){

	cin>>m;

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

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

	}

	memset(diario, -1, sizeof diario);

	cout<<max(cosa(0,0),max(cosa(1,0),cosa(2,0)))<<"\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>

using namespace std;

typedef long long ll;

typedef long long int lli;

typedef long double ld;

const int oo = 1e9;



int nn;

const int nxx = 1e5+5;

int acc[nxx][3];

int dpp[3][nxx];

int go(int aa, int ii)

{

	if(ii==nn) return 0;

	if(dpp[aa][ii] != -1) return dpp[aa][ii];

	int& ans = dpp[aa][ii];

	if(aa==0)

	{

		ans=max(ans, go(1, ii+1)+acc[ii][0]);

		ans=max(ans, go(2, ii+1)+acc[ii][0]);

	}

	if(aa==1)

	{

		ans=max(ans, go(0, ii+1)+acc[ii][1]);

		ans=max(ans, go(2, ii+1)+acc[ii][1]);

	}

	if(aa==2)

	{

		ans=max(ans, go(0, ii+1)+acc[ii][2]);

		ans=max(ans, go(1, ii+1)+acc[ii][2]);

	}



	return ans;



}

int main()

{

    cin>>nn;

    for(int ii=0; ii<nn; ii++) cin>>acc[ii][0]>>acc[ii][1]>>acc[ii][2];

    memset(dpp, -1, sizeof dpp);

    cout<<max(go(0,0), max(go(1,0),go(2,0)))<<"\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>

using namespace std;

typedef long long ll;

typedef long long int lli;

typedef long double ld;

const int oo = 1e9;



int n;

const int nx = 1e5+5;

int ac[nx][3];

int dp[3][nx];

int go(int a, int i)

{

	if(i==n) return 0;

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

	int& ans = dp[a][i];

	if(a==0)

	{

		ans=max(ans, go(1, i+1)+ac[i][0]);

		ans=max(ans, go(2, i+1)+ac[i][0]);

	}

	if(a==1)

	{

		ans=max(ans, go(0, i+1)+ac[i][1]);

		ans=max(ans, go(2, i+1)+ac[i][1]);

	}

	if(a==2)

	{

		ans=max(ans, go(0, i+1)+ac[i][2]);

		ans=max(ans, go(1, i+1)+ac[i][2]);

	}

	

	return ans;

	

}

int main()

{

    cin>>n;

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

    memset(dp, -1, sizeof dp);

    cout<<max(go(0,0), max(go(1,0),go(2,0)))<<"\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: