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

    #define ll long long int

    using namespace std;

    int main()

    {

    int a[100000][3]={0};

    int dp[100000][3]={0};

    ll n;

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

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

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

    }



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

    }


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

#define N 500003

#define MOD 1000000007

using namespace std;





int main()

{

    int n;

    cin>>n;

    int a[n][3];

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

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

            cin>>a[i][j];

    }

    int dp[n][3]={0};

    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(j!=k)

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

            }

        }

    }

    cout<<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>

#define endl "\n"

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

using ll = long long;

const int INF = 0x3f3f3f3f;

using namespace std;



int n, arr[100001][3];

ll dp[100001][3];



ll solve(int i = 0, int lst = 0) {

    if (i==n) {

        return 0;

    }

    if (i==0) {

        return max(arr[i][0] + solve(i+1, 0), max(arr[i][1] + solve(i+1, 1), arr[i][2] + solve(i+1, 2)));

    }

    ll& ret = dp[i][lst];

    if (~ret) {

        return ret;

    }

    if (lst == 0) {

        return ret = max(arr[i][1] + solve(i+1, 1), arr[i][2] + solve(i+1, 2));

    }

    if (lst == 1) {

        return ret = max(arr[i][0] + solve(i+1, 0), arr[i][2] + solve(i+1, 2));

    }

    return ret = max(arr[i][0] + solve(i+1, 0), arr[i][1] + solve(i+1, 1));

}

int main()

{

    IOS

    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 << solve() << endl;







    return 0;

}


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

#define endl "\n"

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

using ll = long long;

const int INF = 0x3f3f3f3f;

using namespace std;



int n, arr[100001][3];

ll dp[100001][3];



ll solve(int i = 0, int lst = 0) {

    if (i==n) {

        return 0;

    }

    if (i==0) {

        return max(arr[i][0] + solve(i+1, 0), max(arr[i][1] + solve(i+1, 1), arr[i][2] + solve(i+1, 2)));

    }

    ll& ret = dp[i][lst];

    if (~ret) {

        return ret;

    }

    if (lst == 0) {

        return ret = max(arr[i][1] + solve(i+1, 1), arr[i][2] + solve(i+1, 2));

    }

    if (lst == 1) {

        return ret = max(arr[i][0] + solve(i+1, 0), arr[i][2] + solve(i+1, 2));

    }

    return ret = max(arr[i][0] + solve(i+1, 0), arr[i][1] + solve(i+1, 1));

}

int main()

{

    IOS

    cin >> n;

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

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

            cin >> arr[i][j];

        }

    }

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

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

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

                if (j != k) {

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

                }

            }

        }

    }

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







    return 0;

}


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

using namespace std;

#define N 100009

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



int solve(int i, int p)

{

    if(i > n) return 0;

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

    int ans = 0;

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

    {

        if(j != p)

        {

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

        }

    }

    return dp[p][i] = ans;

}



int main()

{

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

    cin >> n;

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

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

}


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

using namespace std;

#define N 100009

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



int main()

{

    int n;

    cin >> n;

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

    {

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

    }

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

}






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

using namespace std;

const int N=1e5+1;



int n,A[N],B[N],C[N],dp[N][4];



int calc(int idx,int cur){

    if(idx==n)

    return 0;

    

    int &ret = dp[idx][cur];

    

    if(ret != -1)

    return ret;

    

    if(cur==1)

    return ret = max(A[idx]+calc(idx+1,2) , A[idx]+calc(idx+1,3));

    else if(cur==2) 

    return ret = max(B[idx]+calc(idx+1,1) , B[idx]+calc(idx+1,3));

    else 

    return ret = max(C[idx]+calc(idx+1,1) , C[idx]+calc(idx+1,2));

}



int main(){

cin>>n;

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

scanf("%d%d%d",&A[i],&B[i],&C[i]);



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

cout<<max(calc(0,1),max(calc(0,2),calc(0,3)));



return 0;    

}

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

using namespace std;

const int N=1e5+1;



int main(){

int n,A[N],B[N],C[N],dp[N][4];



cin>>n;

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

scanf("%d%d%d",&A[i],&B[i],&C[i]);



dp[0][1]=A[0];

dp[0][2]=B[0];

dp[0][3]=C[0];



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

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

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

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

}

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

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 = 100005;

int ar[N][4];

ll dp[N][4];

int n;



long long f(int pos, int work){

	if (pos > n)

		return 0;

	if (dp[pos][work] != -1)

		return dp[pos][work];

	ll x = -1000;

	ll y = x;

	ll z = x;

	if (work == 0){

		x = f(pos + 1, 1) + ar[pos][1];

		y = f(pos + 1, 2) + ar[pos][2];

		z = f(pos + 1, 3) + ar[pos][3];

	}

	if (work == 1){

		x = f(pos + 1, 2) + ar[pos][2];

		y = f(pos + 1, 3) + ar[pos][3];

	}

	if (work == 2){

		x = f(pos + 1, 1) +  ar[pos][1];

		y = f(pos + 1, 3) + ar[pos][3];

	}

	if(work == 3){

		x = f(pos + 1, 1) + ar[pos][1];

		y = f(pos + 1, 2) + ar[pos][2];

	}

	return dp[pos][work] = max(x , max(y, z));

}



int main(){

	cin >> n;

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

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

			cin >> ar[i][j];

		}

	}

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

	cout << f(1, 0) << '\n';

	return 0;

}

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

using namespace std;



typedef long long ll;

typedef unsigned long long ull;

typedef long double ld;

#define endl '\n';

#define FASTER ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

const ll POSITIVE_INFINITY = 9223372036854775807;

const ll NEGATIVE_INFINITY = -9223372036854775807;

const ll MOD = 1000000007;

const ld PI = acos(-1.0);



template <typename T> inline T bigMod(T A,T B,T M = MOD){A %= M;T ret = 1;while(B){if(B & 1) ret = (ret * A) % M;A = (A * A) % M;B = B >> 1;}return ret;}

template <typename T> inline T invMod (T A,T M = MOD){return bigMod(A,M-2,M);}



const int N = 200005;

const ll INF = (ll) (1e18) + 5;

int main(){

  FASTER;

  int n;

  cin >> n;

  vector<int>dp(3, 0);

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

    vector<int>cost(3);

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

      cin >> cost[j];

    }

    vector<int>temp(3, 0);

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

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

        if (i != j){

          temp[j] = max(temp[j], dp[i] + cost[j]);

        }

      }

    }

    dp = temp;

  }

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

  return 0;

}

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

#define ll long long

#define f(i,l,r) for(int i=l;i<r;i++)

using namespace std;

ll n,dp[100001][4],a[3][100001];

ll solve(int i,int flag)

{

 //  cout<<"HI3"; 

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

    {

 //       cout<<"HII";

        return dp[i][flag];

    }

  //  if(i==0)

    //    return dp[i][flag]= max(a[0][i]+solve(i+1,0),max(a[1][i]+solve(i+1,1),a[2][i]+solve(i+1,2)));

    if(i==n)

    {

   //     cout<<"HII0"<<i<<n;

        return 0;

    }



    dp[i][flag]=0;

  //  cout<<"HII";

    if(flag!=0)

    {

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

 //       cout<<"A";

    }

    if(flag!=1)

    {

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

 //       cout<<"B";

    }

    if(flag!=2)

    {

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

 //       cout<<"C";

    }

    return dp[i][flag];

}

int main()

{

   ios_base::sync_with_stdio(false);

   cin.tie(NULL);

   // int n;

    cin>>n;

    f(i,0,100001)

        f(j,0,4)

            dp[i][j]=-1;

  //  ll ans=solve(0,3);

    f(i,0,n)

    {

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

     //   cout<<a[0][i]<<" "<<a[1][i]<<" "<<a[2][i]<<"\n";

    }

    ll ans=solve(0,3);

    // f(i,0,n)

    // {

    //     f(j,0,4)

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

    //     cout<<"\n";

    // }

    cout<<ans;

    return 0;

}

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

#define ll long long

#define f(i,l,r) for(int i=l;i<=r;i++)

#define debug(x) cout<<#x<<'='<<(x)<<endl

using namespace std;

template<class T> bool maxs(T& a,T& b)

{

	if(a<b)

	{

		T t;

		t=a;

		a=b;

		b=t;

		return true;

	}

	return false;

}



template<class T> T maxi(T& a,T b)

{

	if(a<b)

	{

		a=b;	

		return b;

	}

	return a;

}

template<class T> T mini(T& a,T b)

{

	if(a>b)

	{

		a=b;

		return b;

	}

	return a;

}



int main()

{

	ios_base::sync_with_stdio(false);

	cin.tie(NULL);

	ll n;

	cin>>n;

	ll a[n+1][3];

	f(i,1,n)

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

	ll dp[n+1][3];

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

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

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

	f(i,2,n)

	{

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

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

	}

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

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
/* BISMILLAHIR RAHMANIR RAHIM */

#include <bits/stdc++.h>

using namespace std;



const int mxn = 100005;

int n, a[mxn][4];

int ff[mxn][4];



int dfs( int r, int c ) 

{

  if( r == 0 ) return 0;

  int& ret = ff[r][c];

  if( ~ret ) return ret;



  ret = 0;

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

  {

    if( i == c ) continue;

    ret = max(ret, a[r][c] + dfs( r - 1, i ));

  } 

  return ret;

}



int main() 

{

  scanf("%d", &n);

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

  {

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

  }

  memset(ff, -1, sizeof ff);

  printf("%d\n", max(dfs( n, 3 ), max(dfs( n, 2 ), dfs( n, 1 ))));

  return 0;

}

// Optimized solution to the same problem (faster):
/* BISMILLAHIR RAHMANIR RAHIM */

#include <bits/stdc++.h>

using namespace std;



const int mxn = 100005;

int n, a[mxn][4];

int ff[mxn][4];



void solution()

{

  ff[1][1] = a[1][1], ff[1][2] = a[1][2], ff[1][3] = a[1][3];

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

  {

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

    {

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

      {

        if( i == c ) continue;

        ff[r][c] = max(ff[r][c], a[r][c] + ff[r - 1][i]);

      }

    }

  }

}



int main() 

{

  scanf("%d", &n);

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

  {

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

  }

  solution();

  printf("%d\n", max(ff[n][3], max(ff[n][2], ff[n][1])));

  return 0;

}

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

using namespace std;

typedef long long LL;

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



LL dp[100001][3];

//LL c=0;

LL val[100001][3];



LL cal(LL i,LL j)

{

    //cout<<"FUNCTION ENTERED \n";

    //c++;



    if(i==0)

    {

        return 0;

    }

    else

    {

        if(dp[i][j]=-1)

        {



                if(dp[i-1][(j+1)%3]==-1)

                {

                    dp[i-1][(j+1)%3]=cal(i-1,(j+1)%3);

                }

                if(dp[i-1][(j+2)%3]==-1)

                {

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

                }

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

            

        }

        return dp[i][j];

    }

    

    

}



int main()

{

    LL n,i,j,k;

    cin>>n;

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

    {

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

        {

            dp[i][j]=-1;

            val[i][j]=-1;

        }

    }



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

    {

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

        {

            cin>>val[i][j];

        }

    }

    

    //cout<<val[n][0]<<" "<<val[n][1]<<" "<<val[n][2]<<endl;



    dp[n][0]=val[n][0]+MAX(cal(n-1,2),cal(n-1,1));

    //cout<<(MAX(cal(n-1,2),cal(n-1,1)))<<endl;

    dp[n][1]=val[n][1]+MAX(cal(n-1,0),cal(n-1,2));

    dp[n][2]=val[n][2]+MAX(cal(n-1,0),cal(n-1,1));



    //cout<<val[n][0]<<" "<<val[n][1]<<" "<<val[n][2]<<endl;



    //cout<<dp[n][0]<<" "<<dp[n][1]<<" "<<dp[n][2]<<endl;

    LL ans=MAX(MAX(dp[n][0],dp[n][1]),dp[n][2]);

    //cout<<"VALUE OF c is "<<c<<endl;

    cout<<ans<<endl;

    return 0;

}

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

using namespace std;

typedef long long LL;



int main()

{

    LL n,i,j,k;

    cin>>n;

    LL a[n+1],b[n+1],c[n+1];

    LL dp[n+1][3];

    LL cost[n+1][3];

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

    {

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

    }



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

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

    dp[1][2]=cost[1][2];



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

    {

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

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

    }



    LL maxima=-1;

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

    {

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

    }

    cout<<maxima<<endl;



}

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

using namespace std;



using ll = long long;

#define pb push_back

#define fastread ios_base::sync_with_stdio(false);cin.tie(NULL);

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

#define file freopen("in.txt", "r", stdin);

#define pii pair<int, int>

#define pll pair<ll, ll>



struct DATA{

	int a, b, c;

};

const int maxn = 1e5 + 5;

DATA arr[maxn];

int n;

ll dp[maxn][4], inf = 1e15;



ll call(int i, int last){

	if(i >= n+1)return 0;

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

	ll ret = 0;

	if(last == 1){

		ret = max(ret, arr[i].b + call(i+1, 2));

		ret = max(ret, arr[i].c + call(i+1, 3));

	}	

	if(last == 2){

		ret = max(ret, arr[i].a + call(i+1, 1));

		ret = max(ret, arr[i].c + call(i+1, 3));

	}

	if(last == 3){

		ret = max(ret, arr[i].a + call(i+1, 1));

		ret = max(ret, arr[i].b + call(i+1, 2));

	}

	return dp[i][last] = ret;

}



int main(){

	//file

	scanf("%d", &n);

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

		int x, y, z;

		scanf("%d %d %d", &x, &y, &z);

		arr[i] = {x, y, z};

	}

	ll ans = 0;

	memset(dp, -1, sizeof dp);

	ans = max(ans, call(1, 1)+arr[0].a);

	memset(dp, -1, sizeof dp);

	ans = max(ans, call(1, 2)+arr[0].b);

	memset(dp, -1, sizeof dp);

	ans = max(ans, call(1, 3)+arr[0].c);

	cout<<ans<<endl;

}

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

using namespace std;



const int maxn = 1e5 + 5;

long long dp[maxn][4];

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



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

{

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

	int n;

	scanf("%d", &n);

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

		scanf("%d %d %d", &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], max(dp[i-1][2]+a[i], dp[i-1][3]+a[i]));

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

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

	}

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



	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#pragma GCC optimize("O3")

#include<bits/stdc++.h>

#define INF 2147483647

#define LLINF 9223372036854775807

using namespace std;

int a[100010][3];

int dp[100010][3];

int main(){

	int n;

	cin>>n;

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

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

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

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

    		if(j==0){

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

			}

			else if(j==1){

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

			}

			else{

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

			}

		}

	}

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

	return 0;

}

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

#include <algorithm>

#include <bitset>

#include <cassert>

#include <cmath>

#include <complex>

#include <cstdio>

#include <cstdlib>

#include <cstring>

#include <ctime>

#include <deque>

#include <fstream>

#include <functional>

#include <iomanip>

#include <iostream>

#include <map>

#include <queue>

#include <set>

#include <sstream>

#include <stack>

#include <string>

#include <utility>

#include <vector>

#include <istream>

#define INF 2147483647

#define LLINF 9223372036854775807

typedef long long ll;

using namespace std;

int a[100010];

int b[100010];

int c[100010];

int dp[100010][5];

int main(){

//  cout<<fixed<<setprecision(1)<<1<<endl;

	int n;

	cin>>n;

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

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

	}

	for(int i=0;i<n;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++){

			dp[i][j]=0;

		}

	}

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

			}

			if(j==1){

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

			}

			if(j==2){

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

			}

		}

	}

	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 ;

const int N=1e5+6 , M=6;



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

int mem[N][M];

int happiness[N][M];



int dp(int i , int activity)

{                                   // 0 is for a and 1 is for b and 2 is for c ;

    if(i==n)return 0;



    if(mem[i][activity]!=-1)return mem[i][activity];



    int ans=-2e9;



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

        if(activity==j)continue;

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

    }



    mem[i][activity]=ans;

    return mem[i][activity];

}



int main()

{

    scanf("%d" , &n);



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

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

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

        }

    }



    memset(mem , -1 , sizeof mem);



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







}


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



using namespace std;

const int N=1e5+6, M=7 ;



int maxHappiness[N][M];

int happiness[N][M];

int n ;



int main()

{

    scanf("%d", &n);



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

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

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

        }

    }



    maxHappiness[0][0]=happiness[0][0];

    maxHappiness[0][1]=happiness[0][1];

    maxHappiness[0][2]=happiness[0][2];



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



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



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



                if(lastactivity==newactivity)continue;

                        maxHappiness[currentday][newactivity]=max(maxHappiness[currentday][newactivity] , maxHappiness[currentday-1][lastactivity]+happiness[currentday][newactivity]);



            }



        }



    }

    int ans=max({maxHappiness[n-1][0] , maxHappiness[n-1][1] , maxHappiness[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 = 100001;

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

int n;



int solve(int pos, int pre) {

    if (pos == 0) {

        return 0;

    }

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

    if (ans != -1) {

        return ans;

    }

    ans = 0;

    if (pre != 1) {

        ans = max(ans, solve(pos - 1, 1) + a[pos]);

    }

    if (pre != 2) {

        ans = max(ans, solve(pos - 1, 2) + b[pos]);

    }

    if (pre != 3) {

        ans = max(ans, solve(pos - 1, 3) + c[pos]);

    }

    return ans;

}



int main() {

    cin >> n;

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

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

    }

    memset(dp, -1, sizeof dp);

    cout << solve(n, 0);

    return 0;

}


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



using namespace std;



const int N = 1e5 + 1;

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

int dp[N][3];



int main() {

    int n;

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

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

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

    }

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

    return 0;

}


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

using namespace std;

#define f first

#define s second

typedef long long int ll;

const int N=1e5+5;

int n,a[3][N],memo[4][N];

int solve(int ind, int prev){

    if(ind==n)

        return 0;

    if(~memo[prev][ind])

        return memo[prev][ind];

    int ret=0;

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

        if(i==prev&&ind)

            continue;

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

    }

    return memo[prev][ind]=ret;

}

int main(){

    cin.tie(NULL);

    ios_base::sync_with_stdio(false);

    memset(memo,-1,sizeof memo);

    cin>>n;

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

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

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

    return 0;

}


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

using namespace std;

#define f first

#define s second

typedef long long int ll;

const int N=1e5+5,MAX=1e9+2;

int n,a[3][N],dp[4][N];

int main(){

    cin.tie(NULL);

    cout.tie(NULL);

    ios_base::sync_with_stdio(false);

    cin>>n;

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

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

    }

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

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

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

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

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

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

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

    }

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

    return 0;

}


// Fastest known solution for this problem: