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

using namespace std;

typedef long long ll;

typedef unsigned long long ull;

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

#define     bug(a,b)        cout<<a<<"\t"<<b<<"\n";

#define     Case(a,b)       cout<<"Case "<<a<<": "<<b<<"\n";

#define     watch(a)        cout<<fixed<<setprecision(a)

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

#define     Endl            "\n";

#define     tab             "\t"

#define     reset(a,b)      memset(a,b,sizeof(a));

#define     sf(a)           scanf(" %d", &a);

#define     srt(a)          sort(a.begin(),a.end());

#define     pi              2*acos(0.0)

#define     inf             INT_MAX

const int sz=1e5+9;

int n,x,arr[sz][3],dp[sz][3];



int clac(int i,int ignore ,int value){

    if(i>n)return value;

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

    int ret=0;

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

        if(j==ignore)

            continue;

        ret=max(ret,value+clac(i+1,j,arr[i][j]));

    }

    return dp[i][ignore]=ret;

}



void solve(){

    cin>>n;

    reset(dp,-1);

    //cout<<dp[2][0]<<tab<<dp[2][1]<<tab<<dp[2][2]<<endl;

    lp(i,1,n+1)cin>>arr[i][0]>>arr[i][1]>>arr[i][2];

    int ans1=clac(2,0,arr[1][0]);

    //cout<<dp[2][0]<<tab<<dp[2][1]<<tab<<dp[2][2]<<endl;

    int ans2=clac(2,1,arr[1][1]);

    int ans3=clac(2,2,arr[1][2]);

    //cout<<ans1<<tab<<ans2<<tab<<ans3<<endl;

    //int ans=max(ans1,(max(ans2,ans3)));

   // bug(ans1,1);

    cout<<max(ans1,(max(ans2,ans3)))<<"\n";

}



int main(){

    //FasterIO;

    #ifndef ONLINE_JUDGE

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

    #endif

    // int t;

    //cin>>t;

    //while(t--)

        solve();

}


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

using namespace std;

int n;

int a[4][100005];

int f[100005][4];

int main()

{

	scanf("%d",&n);

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

	   scanf("%d%d%d",&a[1][i],&a[2][i],&a[3][i]);

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

	{

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

		{

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

			{

				if(j==k) continue;

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

			}

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

		}

		//cout<<endl;

	}

	int ans=0;

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

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

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

	return 0;

 } 

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

using namespace std;



#define ll long long

#define size 100001



int a[size];

int b[size];

int c[size];

int dp[size][4];



int max_sum(int n, int p)

{

    if(n <= 0)

        return 0;

    if(n > 0 && dp[n][p] != -1)

        return dp[n][p];

    int ans = INT_MIN;

    if(p != 0)

    {

        ans = max(ans,a[n]+max_sum(n-1,0));

    }

    if(p != 1)

    {

        ans = max(ans,b[n]+max_sum(n-1,1));

    }

    if(p != 2)

    {

        ans = max(ans,c[n]+max_sum(n-1,2));

    }



    return dp[n][p] = ans;

}







int main()

{

  //OJ;

  int t = 1;

  //cin>>t;

  while(t--){

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

  int n;

  cin>>n;



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

  {

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

  }

  cout<<max_sum(n,3)<<endl;

  }

   return 0;

}


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

using namespace std;

int n;

int a[4][100005];

int f[100005][4];

int main()

{

	scanf("%d",&n);

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

	   scanf("%d%d%d",&a[1][i],&a[2][i],&a[3][i]);

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

	{

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

		{

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

			{

				if(j==k) continue;

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

			}

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

		}

		//cout<<endl;

	}

	int ans=0;

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

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

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

	return 0;

 } 

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





using namespace std;



vector<vector<int>> vec(100007);

int n;



long long meme[100007][5];





long long solve(int index, int flag){

    if(index==n){

        return 0;

    }

    

    long long &res = meme[index][flag]; 

    

    if(res != -1){

        return res;

    }

    res = 0;

    

    if(flag!=0){

        res = max(res,vec[index][0]+solve(index+1,0));

    }

    if(flag!=1){

        res =  max(res,vec[index][1]+solve(index+1,1));

    }

    if(flag!=2){

        res =  max(res,vec[index][2]+solve(index+1,2));

    }

    return res;

}



int main(){

    cin>>n;

    int num1,num2,num3;

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

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

        cin>>num1>>num2>>num3;

        vec[i].push_back(num1);

        vec[i].push_back(num2);

        vec[i].push_back(num3);

    }

    long long sol = solve(0,3);

    cout<<sol<<endl;

    return 0;

}

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

using namespace std;

int n;

int a[4][100005];

int f[100005][4];

int main()

{

	scanf("%d",&n);

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

	   scanf("%d%d%d",&a[1][i],&a[2][i],&a[3][i]);

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

	{

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

		{

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

			{

				if(j==k) continue;

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

			}

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

		}

		//cout<<endl;

	}

	int ans=0;

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

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

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

	return 0;

 } 

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

using namespace std;

int a[100000+5];

int b[100000+5];

int c[100000+5];



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

{

	int N;

	while(cin >> N) {

		memset(a, 0, sizeof(a));

		memset(b, 0, sizeof(b));

		memset(c, 0, sizeof(c));

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

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

		}

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

			int tempa = a[i - 1];

			int tempb = b[i - 1];

			int tempc = c[i - 1];

			a[i] = a[i] + max(tempb, tempc);

			b[i] = b[i] + max(tempa, tempc);

			c[i] = c[i] + max(tempa, tempb);

		}

	    cout << max(a[N], max(b[N], c[N])) << endl;

    }

	return 0;

}

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



#define  ll         long long

#define  llu        unsigned long long

#define  mod        1000000007

#define  pi         ((2.0)*acos(0.0))

#define  pb         push_back

#define  pf         push_front

#define  eb         emplace_back

#define  mp         make_pair

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

#define  X          first

#define  Y          second



/*loop*/

#define  fr(i,m,n)   for(ll i=m;i<n;++i)



/*output shortcuts*/

#define  nl         "\n"

#define  Yes        cout<<"Yes\n"

#define  yes        cout<<"yes\n"

#define  YES        cout<<"YES\n"

#define  No         cout<<"No\n"

#define  no         cout<<"no\n"

#define  NO         cout<<"NO\n"



using namespace std;

int main()

{

    ios::sync_with_stdio(0);

    cin.tie(0);

    cout.tie(0);



    ll n;

    cin>>n;

    ll a[n][3];



    fr(i,0,n)

        fr(j,0,3)

            cin>>a[i][j];



    //ll r=1;

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



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

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

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



    fr(r,1,n)

    {

    for(ll i=0;i<3 && r<n; i++)  // cur_row

    {

        for(ll j=0;j<3;j++) // pre_row

        {

            if(j==i)

                continue;

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

        }

    }

    }



    /*cout<<nl<<nl;

    fr(i,0,n)

        {

            fr(j,0,3)

            {cout<<dp[i][j]<<" ";}

            cout<<nl;

        }

    */

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



return 0;

}




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

using namespace std;

#define ll long long

vector<vector<int>> vc;

int n;

int dp[100005][3];



int go(int i , int prev) {

	if (i >= n)

		return 0;

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

		return dp[i][prev];

	int res = -1;

	if (prev == 0)

		res = max(vc[i][1] + go(i + 1, 1), vc[i][2] + go(i + 1, 2));

	else if (prev == 1)

		res = max(vc[i][0] + go(i + 1, 0), vc[i][2] + go(i + 1, 2));

	else

		res = max(vc[i][1] + go(i + 1, 1), vc[i][0] + go(i + 1, 0));

	return dp[i][prev] = res;

}





int main()

{

	ios::sync_with_stdio(0);

	cin.tie(0);

	cout.tie(0);

	cin >> n;

	int t = n;

	int x, y, z;

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

	while (t--) {

		cin >> x >> y >> z;

		// int one = max({x, y, z});

		// int three = min({x, y, z});

		// int two = x ^ y ^ z ^ one ^ three;



		vc.push_back({x, y, z});

	}

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

	return 0;

}

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


#include <bits/stdc++.h>

using namespace std;

#define ll long long

struct act

{

	int a, b, c;

};



int main()

{

	ios::sync_with_stdio(0);

	cin.tie(0);

	cout.tie(0);



	int n;

	cin >> n;

	int dp[n][3];

	vector<act> vc(n + 1);

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

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

	dp[0][0] = vc[0].a;

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

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

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

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

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

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

	}

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

	return 0;

}

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

#include <ext/pb_ds/assoc_container.hpp>



#define INF 1000000021

#define MOD 1000000007

#define pb push_back

#define sqr(a) (a)*(a)

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

#define F first

#define S second

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

#define deb(x) cerr << #x << " = " << x << '\n'

#define N 222222



using namespace std;

using namespace __gnu_pbds;



typedef long double ld;

typedef long long ll;

typedef pair<int,int> pii;

typedef pair<ll, ll> pll;

typedef vector<int> vi;

typedef vector<ll> vll;

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;



const ld pi = 2 * acos(0.0);

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

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

template<class T, class TT> bool pal(T a, TT n){int k=0;for(int i=0;i<=n/2;i++){if(a[i]!=a[n-i-1]){k=1;break;}}return k?0:1;}



//int month[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};



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

int n;

ll f(int id, int pre)

{

	if(id > n)

		return 0;

	ll ans = dp[id][pre];

	if(ans != -1)

		return ans;

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

		if(i != pre)

			umax(ans, a[id][i] + f(id + 1, i));

	return dp[id][pre] = ans;

}



int main()

{

	memset(dp, -1, sizeof dp);

	cin >> n;

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

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

			cin >> a[i][j];

	cout << f(1, 0);

}

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

#include <ext/pb_ds/assoc_container.hpp>



#define INF 1000000021

#define MOD 1000000007

#define pb push_back

#define sqr(a) (a)*(a)

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

#define F first

#define S second

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

#define deb(x) cerr << #x << " = " << x << '\n'

#define N 222222



using namespace std;

using namespace __gnu_pbds;



typedef long double ld;

typedef long long ll;

typedef pair<int,int> pii;

typedef pair<ll, ll> pll;

typedef vector<int> vi;

typedef vector<ll> vll;

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;



const ld pi = 2 * acos(0.0);

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

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

template<class T, class TT> bool pal(T a, TT n){int k=0;for(int i=0;i<=n/2;i++){if(a[i]!=a[n-i-1]){k=1;break;}}return k?0:1;}



//int month[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};



ll dp[N][5];

int main()

{

	int n;

	cin >> n;

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

	{

		ll a, b, c;

		cin >> a >> b >> c;

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









	











	getchar();

	getchar();

	return 0;

	//ios::sync_with_stdio(false);

	//cin.tie(0);

}

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

#include <algorithm>

#include <climits>

#define SIZE 100001

#define INF INT_MAX >> 1



using namespace std;



int dp[SIZE][3];



int main(void){

	int n; cin >> n;

	static int abc[SIZE][3];

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

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

	}



	fill(dp[0], dp[SIZE], 0);

	//配るDP

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

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

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

				if(from != to){

					dp[i+1][to] = max(dp[i+1][to], dp[i][from]+abc[i][to]);

				}

			}

		}

	}



	int ans = -1;

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

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

	}

	cout << ans << endl;

}

// Optimized solution to the same problem (faster):
#define _USE_MATH_DEFINES

#include <iostream>

#include <algorithm>

#include <queue>

#include <stack>

#include <deque>

#include <climits>

#include <string>

#include <vector>

#include <set>

#include <map>

#include <cmath>

#include <cstdio>

#include <cstdlib>

#include <cstring>

//#include <tuple>



#define INF INT_MAX>>1 

#define SIZE 100010

#define MOD 1000000007

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

#define rep2(i, from, to) for(int i=(from); i<(int)(to); i++)

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

#define LLPair pair<ll, ll>

#define vint vector<int>

#define vll vector<ll>

typedef long long ll;



using namespace std;



struct Point { int x, y; };

struct Edge { int from, to, cost; };

struct gNode { int from, to, cost; };

struct tNode { int value; int parent; vector<int> child; };



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

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



int gcd(int a, int b){ return a % b == 0 ? b : gcd(b, a%b); }

int lcm(int a, int b){ return a * b/ gcd(a, b); }

int extgcd(int a, int b, int& x, int& y){

	int d = a;

	if(b != 0){

		d = extgcd(b, a%b, y, x);

		y -= (a/b) * x;

	} else{

		x = 1; y = 0;

	}

	return d;

}

ll mod_pow(ll x, ll n){

	if(n == 0) return 1;

	ll res = mod_pow(x*x % MOD, n/2);

	if(n & 1) res = res * x % MOD;

	return res;

}



//int dp[SIZE][3];

#define A 0

#define B 1

#define C 2



int main(void){

	static int dp[SIZE][3];

	int n; cin >> n;

	vint a(n), b(n), c(n);

	rep(i, n){

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

	}



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

	dp[0][B] = b[0];

	dp[0][C] = c[0];



	rep2(i, 0, n-1){

		dp[i+1][A] = max(dp[i+1][A], dp[i][B] + a[i+1]);

		dp[i+1][A] = max(dp[i+1][A], dp[i][C] + a[i+1]);



		dp[i+1][B] = max(dp[i+1][B], dp[i][A] + b[i+1]);

		dp[i+1][B] = max(dp[i+1][B], dp[i][C] + b[i+1]);



		dp[i+1][C] = max(dp[i+1][C], dp[i][A] + c[i+1]);

		dp[i+1][C] = max(dp[i+1][C], dp[i][B] + c[i+1]);

	}



	int ans = 0;

	rep(i, 3){

		//cout << dp[n-1][i] << endl;

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

	}

	cout << ans << endl;

	return 0;

}

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

using namespace std;



const int N = 1e5 + 10;

int n;

int dp[N][3];

int a[N][3];

int solve(int r,int c){



    if(r > n-1)

        return 0;

        

    if(dp[r][c] != -1)  return dp[r][c];

    

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

        if(i != c)

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

    }

    

    return dp[r][c];    

} 

int main()

{

    cin>>n;

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

   

    

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

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

            cin>>a[i][j];

    int ans = -1;

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

    {

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

    }

    

    cout<<ans<<endl;

    

    return 0;

    

}

    

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

#define int64_t int 



using namespace std;



int main(){

	

	int n;

	cin>>n;

	

	int a[n][3];

	int dp[n][3];

	

	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++){

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

		}

	}

	int ans = INT_MIN;

	

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

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

	}

	

	cout<<ans;

}

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

#include <string>

#include<iostream>

#include<vector>

#include <bits/stdc++.h>

#include <stdio.h>

#include <numeric>

#include<math.h>

#include <ctype.h>

#include <algorithm> // sort

#include <map> // pair

using namespace std;

#define nin(n) int n; cin >> n;

#define kin(k) int k; cin >> k;

const int INF = 1001001001;

int main(){

  int n;cin >> n;

  long long h[100010][3];

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

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

      int tmp;

      cin >> tmp;

      h[i][j]=tmp;

    }

  }

 long long dp[10010][3];

 for(int i=0;i<10010;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) continue;

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

      }

    }

  }

  long long res = 0;

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

      res=max(res, dp[n][j]);

    }

  

    cout << res << endl;

}

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

using namespace std;

#define nin(n) int n; cin >> n;

#define kin(k) int k; cin >> k;

 

int main() {

  int n; cin >> n;

  vector<int> a(n);

  vector<int> b(n);

  vector<int> c(n);

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

    cin >> a.at(i) >> b.at(i) >> c.at(i);

  }

  int dp[n+10][3]={};

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

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

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

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

      }

      if(j==1){

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

      }

      if(j==2){

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

      }

    }

  }

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



const int MAX=100005;

vector<int> Sa(MAX,1000000005),Sb(MAX,1000000005),Sc(MAX,1000000005),A(MAX),B(MAX),C(MAX);

int N;



void DP(int u){

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

        Sa[i]=max(Sb[i-1],Sc[i-1])+A[i];

        Sb[i]=max(Sa[i-1],Sc[i-1])+B[i];

        Sc[i]=max(Sa[i-1],Sb[i-1])+C[i];

    }

    cout<<max(Sa[u],max(Sb[u],Sc[u]))<<endl;

}



int main(){

    

    cin>>N;

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

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

    }

    Sa[0]=0;Sb[0]=0;Sc[0]=0;

    DP(N);

}


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

using namespace std;



typedef long long ll;

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

const int mod=1000000007,INF=1<<30;



int main(){

    

    int N;cin>>N;

    vector<int> A(N),B(N),C(N);

    int dp[3][N+1];

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

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

    }

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

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

            dp[j][i]=-INF;

        }

    }

    dp[0][0]=A[0];dp[1][0]=B[0];dp[2][0]=C[0];

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

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

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

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

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

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

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

    }

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

    

}


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

using namespace std;

#define ll long long

int n , arr[100001][3]={},dp[100001][3];



int f(int i = 0 , int t = -1)

{

    if( i == n )

        return arr[n][t];

    if(i==0)

        return max({f(i+1,0),f(i+1,1),f(i+1,2)});

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

        return dp[i][t];

    if(t!=0)

        dp[i][t] = max(dp[i][t],f(i+1,0));

    if(t!=1)

        dp[i][t] = max(dp[i][t],f(i+1,1));

    if(t!=2)

        dp[i][t] = max(dp[i][t],f(i+1,2));

    return dp[i][t]+= arr[i][t];

}

int main()

{

    memset(dp,-1,sizeof dp);

    cin >> n;

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

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

    cout << f();

    return 0;

}

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

using namespace std;

#define ll long long

int main()

{

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

    scanf("%d", &n);

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

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

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

    dp[n-1][1] = b[n-1];

    dp[n-1][2] = c[n-1];

    for(int i=n-2; i>=0; --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[0][0], dp[0][1], dp[0][2]});

    return 0;

}

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

using namespace std;

#define ll long long

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

int f(int i, int p)

{

    if( i == n )

        return 0;

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

        return dp[i][p];

    int mx = -1;

    if( p != 0 )

    {

        mx = max(mx , f(i+1, 0)+a[i]);

    }

    if( p != 1 )

    {

        mx = max(mx , f(i+1, 1)+b[i]);

    }

    if( p != 2 )

    {

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

    }

    return dp[i][p] = mx;



}

int main()

{

    scanf("%d", &n);

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

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

    memset(dp, -1, sizeof dp);

    cout << f(0, 3);

    return 0;

}

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

using namespace std;

#define ll long long

int main()

{

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

    scanf("%d", &n);

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

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

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

    dp[n-1][1] = b[n-1];

    dp[n-1][2] = c[n-1];

    for(int i=n-2; i>=0; --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[0][0], dp[0][1], dp[0][2]});

    return 0;

}

// Fastest known solution for this problem: