stablegradients/pie-gem5-bysrc huggingface dataset:
// Original C++ solution (slower):


#include <bits/stdc++.h>



#define pb push_back

#define fst first

#define snd second

#define INF 0x7fffffff

#define INFLL 0x7FFFFFFFFFFFFFFF

#define lenA(ar) sizeof(ar)/sizeof(*ar)

#define lenrows(mat) sizeof(mat)/sizeof(*mat)

#define lencols(mat) sizeof(*mat)/sizeof(**mat)

#define fill(a,c) memset(&a, c, sizeof(a))

#define fore(i,x,y) for(ll i=x;i<y;i++)

#define foreI(i,x,y) for(ll i=x-1;i>=y;i--)

#define max2(a,b) max(a, b)

#define max3(a,b,c) max2(max2(a,b),c)

#define min2(a,b) min(a, b)

#define min3(a,b,c) min2(min2(a,b),c)

#define MOD 1000000009

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

#define rall(x) (x).rbegin(), (x).rend()

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



typedef long long ll;

typedef unsigned long long ull;



using namespace std;

ll arr[100010][3];

ll dp[100010][3];

int n;

ll solve(int i, int j){

    if(i==n-1) return arr[i][j];

    ll &r = dp[i][j];

    if(r>-1) return r;

    ll a = 0; ll b = 0;ll c = 0; ll act=arr[i][j];

    if(j!=0) a=solve(i+1, 0)+act;

    if(j!=1) b=solve(i+1, 1)+act;

    if(j!=2) c=solve(i+1, 2)+act;

   return r=max3(a,b,c);

}



int main() {

     

    cin>>n;

    arr[n][3];

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

    fore(i,0,n){

        fore(j,0,3){

            cin>>arr[i][j];

        }

    }



    cout<<max3(solve(0,0),solve(0,1),solve(0,2))<<"\n";



    return 0;

}






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



#define pb push_back

#define fst first

#define snd second

#define INF 0x7fffffff

#define INFLL 0x7FFFFFFFFFFFFFFF

#define lenA(ar) sizeof(ar)/sizeof(*ar)

#define fill(a,c) memset(&a, c, sizeof(a))

#define fore(i,x,y) for(ll i=x;i<y;i++)

#define foreI(i,x,y) for(ll i=x-1;i>=y;i--)

#define max3(a,b,c) max(max(a,b),c)

#define min3(a,b,c) min(min(a,b),c)

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

#define rall(x) (x).rbegin(), (x).rend()

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



typedef long long ll;

typedef unsigned long long ull;



using namespace std;



int main() {FIN;

   

    int n;cin>>n;

    int mat[n][3];

    ll dp[n][3]; fill(dp,0);

    fore(i,0,n){

        fore(j,0,3){

            cin>>mat[i][j];

        }

    }

    dp[0][0] = mat[0][0];

    dp[0][1] = mat[0][1];

    dp[0][2] = mat[0][2];

    fore(i,0,n-1){

        fore(j,0,3){

            fore(k,0,3){

                if(j!=k)

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

            }

        }

    }

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

#define INF 2147483647

#define inf 1000000007

#define SYOU(x) setprecision(x+1)

#define abs(x,y) (max(x,y)-min(x,y))

#define lol long long

#define mp make_pair

#define fi first

#define se second

using namespace std;

lol a,b;

lol w[111000],v[111000],q[111000];

lol mem[111000][5];

lol dp(int n,int m){

    if(n==a) return 0;

    if(mem[n][m]!=-1) return mem[n][m];

    if(m==0) return mem[n][m]=max(dp(n+1,1),dp(n+1,2))+w[n];

    if(m==1) return mem[n][m]=max(dp(n+1,0),dp(n+1,2))+v[n];

    return mem[n][m]=max(dp(n+1,0),dp(n+1,1))+q[n];

}

signed main(){

  cin.tie(0);

  ios::sync_with_stdio(false);

  lol c=1,d=1,e=0,f=0,g=0,h=0;

  string s;

  cin >>a;

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

      cin >>w[n]>>v[n]>>q[n];

  }

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

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

  return (0);

}


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

#define longinf 9223372036854775807LL

#define intinf 2147483647

#define inf 998244353

#define lol long long

#define in(n,s) insert(n,s)//ノート参照

#define SYOU(x) setprecision(x)

#define abs(x,y) (max(x,y)-min(x,y))

#define mp make_pair

#define fi first

#define se second

using namespace std;

lol a,b,c,d,e=0,g,h;

int i[111110][3];

lol dp[100011][3];

signed main(){

  cin.tie(0);

  ios::sync_with_stdio(false);

  cin >>a;

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

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

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

  }

  dp[0][0]=i[0][0];

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

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

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

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

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

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

  }

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

  return (0);

}


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

  #define endl "\n"

  #define ll long long int

  #define vi vector<int>

  #define vll vector<ll>

  #define vvi vector < vi >

  #define pii pair<int,int>

  #define pll pair<long long, long long>

  #define mod 1000000007

  #define inf 1000000000000000001;

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

  #define mp(x,y) make_pair(x,y)

  #define mem(a,val) memset(a,val,sizeof(a))

  #define pb push_back

  #define f first

  #define se second

  

  using namespace std;

  ll arr[100005][3];

  ll dp[100005][3];



  ll func(ll x,ll y,ll n){

    if(x>=n)

      return 0;

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

      return dp[x][y];



    ll sum1=0,sum2=0,sum3=0,sum=0;



    if(y!=0)

      sum1=arr[x][0]+func(x+1,0,n);

    

    if(y!=1)

      sum2=arr[x][1]+func(x+1,1,n);

    

    if(y!=2)

      sum3=arr[x][2]+func(x+1,2,n);

    

    sum=max({sum1,sum2,sum3});

    dp[x][y]=sum;



    return sum;

  }



  int main()

  {

    std::ios::sync_with_stdio(false);

    ll n=0,m=0,k=0,x=0,t1=0,t2=0,t3=0,f=0,sum=0,count=0;



    cin>>n;

    mem(arr,0);

    mem(dp,-1);



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

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

      {

        cin>>arr[i][j];

      }

    }



    cout<<func(0,-1,n);





    return 0;

  }

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

  #define endl "\n"

  #define ll long long int

  #define vi vector<int>

  #define vll vector<ll>

  #define vvi vector < vi >

  #define pii pair<int,int>

  #define pll pair<long long, long long>

  #define mod 1000000007

  #define inf 1000000000000000001;

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

  #define mp(x,y) make_pair(x,y)

  #define mem(a,val) memset(a,val,sizeof(a))

  #define pb push_back

  #define f first

  #define se second

  

  using namespace std;

  int main()

  {

    std::ios::sync_with_stdio(false);

    ll n=0,m=0,k=0,x=0,t1=0,t2=0,t3=0,f=0,sum=0,count=0;



    cin>>n;



    ll arr[n+1][3],dp[n+1][3];

    mem(arr,0); mem(dp,0);



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

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

      {

        cin>>arr[i][j];

      }

    }



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

    {

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

      {

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

      }

    }



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

using ll = long long;



template<class T>

void chmax(T &a, T b)

{

	if (a < b) a = b;

}



ll N;

constexpr ll DP_MAX = 100010;

ll dp[DP_MAX][3];

vector<ll> A, B, C;



int main(int argc, char **argv)

{

	cin >> N;

	A.resize(N), B.resize(N), C.resize(N);

	for (ll i = 0; i < N; ++i) cin >> A[i] >> B[i] >> C[i];

	for (ll i = 0; i < DP_MAX; ++i)

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

			dp[i][j] = -(1ll<<50);

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



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

	{

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

		// chmax(dp[i+1][1], max(dp[i][0]+B[i], dp[i][2]+B[i]));

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

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

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

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

		

	}

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

}


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

using namespace std;

using ll = long long;



ll dp[100010][3];



int main(int argc, char **argv)

{

	ll N; cin >> N;

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

	for (ll i = 0; i < N; ++i) cin >> A[i] >> B[i] >> C[i];



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

	{

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

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

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

	}

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

}


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

#include <algorithm>

#include <cstring>

const int maxn = 3e5 + 5;

using namespace std;



int N;

int a[maxn][4];

int dp[maxn][4];

int main()

{

	scanf("%d", &N);

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

	{

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

		{

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

		}

	}

	memset(dp, 0, sizeof dp);

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

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

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

	{

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

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

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

	}

	int ans = 0;

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

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

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

	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 db(x) cerr<<__LINE__<<": "<<#x<<" = "<<(x)<<'\n'

const int N = 1e5;

int A[N+7][5], mem[N+7][5];



int dp(int i, int j) {

	if(i==0) return 0;

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

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

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

	else return mem[i][j] = A[i][j] + max(dp(i-1, 1), dp(i-1, 2));

}

int main(){

	ios_base::sync_with_stdio(0);

	int a,b,n,T,cas=0;

	cin>>n;

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

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

	}

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

	int res = max(dp(n,1), max(dp(n,2), dp(n,3)));

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

using namespace std;



const int MaxN = 100005;



int n;

int f[MaxN][3], val[MaxN][3];



void upd(int &a, int b) {

	if (b > a) a = b;

}



int main() {

	scanf("%d", &n);

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

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

	}

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

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

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

				if (j == k) continue;

				upd(f[i][j], f[i - 1][k] + val[i][j]);

			}

		}

	}

	int Ans = 0;

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

			upd(Ans, f[n][j]);

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

	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 mp make_pair

#define pb push_back

#define st first

#define nd second

#define lb lower_bound

#define ub upper_bound

 

typedef long long ll;

typedef long double ld;

typedef pair<int,int> pi;

typedef pair<ll,ll> pll;

typedef vector<int> vi;

typedef vector<ll> vll;

typedef vector<pi> vpi;

typedef vector<pll> vpll;

 

const ll oo=1e18;

const int MOD=1000000007;

const int INF=1000000000;

const ld EPS=0.000000001;

 

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

 

signed main() {

    ios_base::sync_with_stdio(NULL);

    cin.tie(NULL);

    cout.tie(NULL);

    cin>>n;

    cin>>a>>b>>c;

    dp[1][0]=a;

    dp[1][1]=b;

    dp[1][2]=c;

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

        cin>>a>>b>>c;

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

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

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

    }

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

int a[100001][3], mx=0;

int calc(int pos, int u)

{

	if(pos==-1) return 0;

	if(a[pos][u]==-1)

	{

		//if(pos==0) a[pos][u]=mx(x[pos][(u+1)%3],x[pos][(u+2)%3]);

		a[pos][u]=max(calc(pos-1, (u+1)%3)+x[pos][(u+1)%3],calc(pos-1, (u+2)%3)+x[pos][(u+2)%3]);

	}

	return a[pos][u];

}



int main() {

	int n;

	cin>>n;

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

	{

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

		{

			cin>>x[i][u];

		}

	}

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

	{

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

		{

			a[i][u]=-1;

		}

	}

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

	{

		if(mx<calc(n, i) ) mx=calc(n-1, i);

	}

	cout<<mx;

	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

using namespace std;

const int N =1e6;



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



int main(){

    int n;

    cin >> n;

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

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

    }

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

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

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

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

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

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

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

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

    }

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

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



int main(){

    int n, i, j, k;

    scanf("%d",&n);

    int a[n+5], b[n+5], c[n+5];

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

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

    }

    int dp[n+5][3];

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

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

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

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

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

    }

    int ans  = 0;

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

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

    cout << ans << "\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 main(){



    int n, flag;

    long long ans = 0;



    cin >> n;

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

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

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

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

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

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

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

    }

    cout << max(dp[n][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:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;



typedef long long  int ll;



#define s(t) scanf("%I64d",&t)

#define s2(t,t1) scanf("%I64d%I64d",&t,&t1)





ll n,a[100005][3];

int main()

{

    cin>>n;

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

    {

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

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

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

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



    }

    cout<<max(a[n][2],max(a[n][0],a[n][1]))<<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: