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

using namespace std;

int v[100009][3];

int dp[100009][3];

int n;

int vacation(int i, int j)

{

    if(i==n-1)

        return dp[i][j]=v[i][j];

    if(i>=n || j>=3)

        return 0;

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

        return dp[i][j];

    int temp = INT_MIN;

        if(j==0)

        {

             temp = max(vacation(i+1,j+1)+v[i][j],temp);

        temp = max(vacation(i+1,j+2)+v[i][j],temp);

 return dp[i][j]= temp;

        }

      else  if(j==1)

        {

             temp = max(vacation(i+1,0)+v[i][j],temp);

        temp = max(vacation(i+1,2)+v[i][j],temp);

 return dp[i][j]= temp;

        }

        else

        {

             temp = max(vacation(i+1,1)+v[i][j],temp);

        temp = max(vacation(i+1,0)+v[i][j],temp);

         return dp[i][j]= temp;

        }

}



int main()

{

    cin>>n;

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

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

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

    cout<<max(vacation(0,0),max(vacation(0,1),vacation(0,2)))<<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<iostream>

#include<string>

#include<cmath>

#include<algorithm>

#include<vector>

#include<map>

#include<stack>

#include<queue>

using namespace std;

#define MAXN 100001

int dp[MAXN*3];

int main(){

	int N;

	cin>>N;

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

		cin>>dp[i];

	}

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

	for(int i=3;i<N*3;i++){

		if(i%3==0){

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

		}else if(i%3==1){

			dp[i]=dp[i]+max(dp[i-4],dp[i-2]);

		}else{

			dp[i]=dp[i]+max(dp[i-4],dp[i-5]);

		}

	}

	int x;

	if(dp[3*N-1]>=dp[3*N-2]){

		x=dp[3*N-1];

	}else{

		x=dp[3*N-2];

	}

   cout<<(x>dp[3*N-3]?x:dp[3*N-3]);

   return 0;

} 

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

#include <algorithm>



using namespace std;

const int maxn = 100000;

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



int main() {

    int n, a, b, c;

    scanf("%d", &n);

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

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

        arr[i][0] = a;

        arr[i][1] = b;

        arr[i][2] = c;

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

            if (i == 0) {

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

            } else {

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

            }

        }

    }

    int ans = -1;

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

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

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

        }

    }

    printf("%d", ans);

    return 0;

}






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

#define MAX 100005

#define pb push_back

#define mp make_pair

#define pii pair<long long,long long>

#define read(a) scanf("%d",&a)

#define p1(a) cout<<"Check "<<a<<endl;

#define p2(a,b) cout<<"Check "<<a<<' '<<b<<endl;

#define p3(a,b,c) cout<<"Check "<<a<<' '<<b<<' '<<c<<endl;

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

#define ll long long

#define clr(a) memset(a,0,sizeof(a))

#define MOD 10000000000

#define INF 10000000000000;



using namespace std;



int main()

{

    int n,m;

    cin>>n;

    int ar[n+5][4];

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

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

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

        }

    }

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

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

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

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

    }

    cout<<max(ar[n-1][0],max(ar[n-1][1],ar[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 <bits/stdc++.h>

#define ll long long

#define sz 100105

using namespace std;

ll dp[sz][10];

void solve(ll n , ll i){

   if(i<n){

        ll mx;

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

            mx = 0;

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

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

            dp[i][j] += mx;

        }

        solve(n,i+1);

   }

}



int main(){

    ios_base::sync_with_stdio(false);

    cin.tie(NULL);

    ll i , j , n , k , res = 0;

    cin>>n;

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

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

            cin>>dp[i][j];

    }

    solve(n,1);

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

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

    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>

#define ll long long

using namespace std;

struct actv{

ll f,s,t;

};

int main(){

    ll n;

    cin>>n;

    vector < actv > v,dp;

    v.resize(n);

    dp.resize(n);

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

        cin>>v[i].f>>v[i].s>>v[i].t;

        dp[i].f=dp[i].s=dp[i].t=0;

    }

    dp[0].f=v[0].f;

    dp[0].t=v[0].t;

    dp[0].s=v[0].s;

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

        dp[i].f=v[i].f+max(dp[i-1].s,dp[i-1].t);

        dp[i].s=v[i].s+max(dp[i-1].f,dp[i-1].t);

        dp[i].t=v[i].t+max(dp[i-1].s,dp[i-1].f);

    }

    cout<<max(max(dp[n-1].f,dp[n-1].s),dp[n-1].t);

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;



ll arr[100005][3];

ll dp[100005][3];





int main(){

    ll n;

    cin>>n;

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

        cin>>arr[i][0];

        cin>>arr[i][1];

        cin>>arr[i][2];

    }

    dp[0][0]=arr[0][0];

    dp[0][1]=arr[0][1];

    dp[0][2]=arr[0][2];

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

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

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

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

    }

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

}


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

#include <algorithm>



using namespace std;

const int maxn = 100000;

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



int main() {

    int n, a, b, c;

    scanf("%d", &n);

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

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

        arr[i][0] = a;

        arr[i][1] = b;

        arr[i][2] = c;

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

            if (i == 0) {

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

            } else {

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

            }

        }

    }

    int ans = -1;

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

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

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

        }

    }

    printf("%d", ans);

    return 0;

}






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



using namespace std;



#define ll long long

#define PI acos(-1.0)



int main()

{

	ll t, a[100005], b[100005], c[100005];

	

	scanf("%lld", &t);

	for(int i = 1; i <= t; i++) scanf("%lld %lld %lld", &a[i], &b[i], &c[i]);

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

	{

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

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

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

	}

	printf("%lld\n",max(a[t],max(b[t],c[t])));

	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 mat[100002][3], dp[100002][3];





int main() {

  int n;

  cin >> n;

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

    for (int j = 0; j < 3; ++j) cin >> mat[i][j];



  for (int i = 0; i < 3; ++i) dp[0][i] = mat[0][i];



  vector<vector<int>> now {{1, 2}, {0, 2}, {0, 1}};



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

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

      for (auto el: now[col]) {

        dp[pos][col] = max(dp[pos][col], mat[pos][col] + dp[pos - 1][el]);

      }

    }

  }



  int res = 0;

  for (int i = 0; i < 3; ++i) res = max(res, dp[n - 1][i]);

  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>

#define ll long long

using namespace std;

#define mx 100009

ll ar[mx][4];

ll br[mx][4];

ll n;



ll dp(){

    br[n][1]=ar[n][1];

    br[n][2]=ar[n][2];

    br[n][3]=ar[n][3];

    for(ll i=n-1;i>=1;i--){

        br[i][1]=ar[i][1]+max(br[i+1][2],br[i+1][3]);

        br[i][2]=ar[i][2]+max(br[i+1][1],br[i+1][3]);

        br[i][3]=ar[i][3]+max(br[i+1][1],br[i+1][2]);

    }

    return max(br[1][1],max(br[1][2],br[1][3]));

}



int main()

{

    ios_base::sync_with_stdio(false);

    cin.tie(NULL);

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

    //freopen("output.txt", "w", stdout);



    //ll n;

    cin>>n;

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

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

            cin>>ar[i][j];

        }

    }

    cout<<dp()<<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 ll;

typedef double dd;

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

#define endl "\n"

#define clr(n, r) memset(n,r,sizeof(n));



void fast() {

    cin.tie(0);

    cin.sync_with_stdio(0);

}

const int MAX=1e5+10;

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

int ans(int i,int prev){

    if(i==n)return 0;

    int path1=0,path2=0,path3=0;

    int &ret=dp[i][prev];

    if(prev!=-1&&ret!=-1)return ret;

    if(prev!=0){

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

    }

    if(prev!=1){

        path2=b[i]+ans(i+1,1);

    }

    if(prev!=2){

        path3=c[i]+ans(i+1,2);

    }

    return ret=max(path1,max(path2,path3));

}



int main() {

    fast();

    cin>>n;

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

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

    }

    clr(dp,-1);

    cout<<ans(0,-1);

}


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

#include <algorithm>

using std::max;



#define MAXN 100003

int d[MAXN][3],a[MAXN][3];

int main(void)

{

    int i,N;

    scanf("%d",&N);

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

    {

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

    }

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

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

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

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

    {

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

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

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

    }

    printf("%d\n",max(d[N-1][0],max(d[N-1][1],d[N-1][2])));



    return 0;

}

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

** author:mahmoud magdy

*/

#include <bits/stdc++.h>

using namespace std;



#define ll long long

#define endl "\n"



inline int D()

{

    int m ;

    cin>>m;

    return m ;

}

inline ll lD()

{

    ll m ;

    cin>>m;

    return m ;

}

inline double dD()

{

    double m;

    cin>>m;

    return m;

}

void fast()

{

    ios_base :: sync_with_stdio( 0 ) ;

    cin.tie( 0 ) ;

    cout.tie( 0 ) ;

}

int n;

ll a[100005],b[100005],c[100005];

ll dp[100005][5];

ll solve(int i,int prv){

 if(i==n)return 0;

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

 ll x=0;

 if(prv!=1){

 x=a[i]+solve(i+1,1);

 }

 ll y=0;

 if(prv!=2)

 y=b[i]+solve(i+1,2);

 ll z=0;

 if(prv!=3)

 z=c[i]+solve(i+1,3);

 return dp[i][prv]=max({x,y,z});

}

int main()

{

    memset(dp,-1,sizeof dp);

    fast();

    n=D();

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

        a[i]=D();b[i]=D();c[i]=D();

    }

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

}

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

#include <algorithm>

using std::max;



#define MAXN 100003

int d[MAXN][3],a[MAXN][3];

int main(void)

{

    int i,N;

    scanf("%d",&N);

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

    {

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

    }

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

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

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

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

    {

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

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

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

    }

    printf("%d\n",max(d[N-1][0],max(d[N-1][1],d[N-1][2])));



    return 0;

}

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

using namespace std;



#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)

template <typename Arg1>

void __f(const char* name, Arg1&& arg1) {

  cerr << name << " : " << arg1 << "\n";

}

template <typename Arg1, typename... Args>

void __f(const char* na, Arg1&& arg1, Args&&... args) {

  const char* c = strchr(na + 1, ',');

  cerr.write(na, c - na) << " : " << arg1 << " , ";

  __f(c + 1, args...);

}

#define endl "\n"

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

#define UNIQUE(v) sort(all(v)),v.erase(unique(all(v)),v.end())

typedef long long LL;

typedef pair <int,int> pii;

const double pi = acos(-1.0);

const double eps = 1e-8;

const int mod = 1e9 + 7;

const int inf = 1e9;

const LL INF = 1e18;

const int N = 1e5 + 5;



LL a[ N ][ 5 ];

LL dp[ N ][5 ];



int main() {

  ios_base::sync_with_stdio(false);

  cin.tie(0); cout.tie(0);

  #ifdef LOCAL

    freopen("input.txt", "r", stdin);

  #endif // LOCAL

  int n;

  cin >> n;

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

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

      cin >> a[i][j];

    }

  }

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

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

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

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

      }

    }

  }

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

  return 0;

}


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

#include <algorithm>

using std::max;



#define MAXN 100003

int d[MAXN][3],a[MAXN][3];

int main(void)

{

    int i,N;

    scanf("%d",&N);

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

    {

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

    }

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

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

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

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

    {

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

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

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

    }

    printf("%d\n",max(d[N-1][0],max(d[N-1][1],d[N-1][2])));



    return 0;

}

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

#include <queue>

#include <string>

#include <map>

#include <stdio.h>

#include <iomanip>

#include <math.h>

#include <vector>

#include <stack>

#include <algorithm>

#include <string.h>

#include <set>

#include <cstdlib>

#include <iostream>

#include <bits/stdc++.h>

#define pii pair<int,int> 

#define ll long long int 

#define sl(n) scanf("%lld",&n)

#define s(n) scanf("%d",&n)

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

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

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

#define pb push_back

#define pf printf

#define F first

#define S second

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

#define srtc(v,cmp) sort(v.begin(),v.end(),cmp);

#define srta(v,n) sort(v,v+n);

const ll LL_INF=0x3f3f3f3f3f3f;

const ll MOD=1073741824;





using namespace std;





int T;

int d,arr[100005][5];

int dp[100005][5];



int gen(int i,int prev){ // swim:0, bugs:1, homework:2

    if(i == d)return 0;

    int &ret = dp[i][prev];

    if(~ret)return ret;

    ret = gen(i+1,(prev+1)%3)+arr[i][(prev+1)%3];

    ret = max( ret , gen(i+1,(prev+2)%3)+arr[i][(prev+2)%3]);

    if(!i)ret = max( ret , gen(i+1,(prev)%3)+arr[i][(prev)%3]);

    return ret;

}



int main(){

    // pf("\t\t\t***************************************************************************\n\n");

    // pf("\t\t\t***************************************************************************\n\n");

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

    // freopen("output.txt","w",stdout);

    memset(dp,-1,sizeof dp);

    s(d);

    f(i,d){

        s(arr[i][0]);

        s(arr[i][1]);

        s(arr[i][2]);

    }

    pf("%d\n",gen(0,0));



    return 0;

} 

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

#include <algorithm>

using std::max;



#define MAXN 100003

int d[MAXN][3],a[MAXN][3];

int main(void)

{

    int i,N;

    scanf("%d",&N);

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

    {

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

    }

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

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

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

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

    {

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

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

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

    }

    printf("%d\n",max(d[N-1][0],max(d[N-1][1],d[N-1][2])));



    return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):


#include <bits/stdc++.h>

using namespace  std;



int N;

const int SIZE = 100005;

int mem [SIZE][4];

int arr [SIZE][3];





int dp (int i, int activ) {

  

  if (i == N) return 0;

  



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

  

  int ret = -1; 



  if (activ != 0) {

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

  }



  if (activ != 1) {

    ret = max (ret, arr[i][1] + dp(i+1, 1));

    

  }



  if (activ != 2) {

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

    

  }



  return  mem[i][activ] = ret;

}



int main (){ 



  cin  >> N ;

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

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

  }



  memset(mem, -1, sizeof mem);

  





  cout << dp(0,3);



}

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

#include <algorithm>

using std::max;



#define MAXN 100003

int d[MAXN][3],a[MAXN][3];

int main(void)

{

    int i,N;

    scanf("%d",&N);

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

    {

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

    }

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

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

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

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

    {

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

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

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

    }

    printf("%d\n",max(d[N-1][0],max(d[N-1][1],d[N-1][2])));



    return 0;

}

// Fastest known solution for this problem: