stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;



typedef long long int ll;

typedef long double ld;



#define Pb push_back

#define Mp make_pair



#define ff first

#define ss second



#define Max(x,y,z) max(x,max(y,z))

#define Min(x,y,z) min(x,min(y,z))



#define INF LONG_LONG_MAX

#define MINF LONG_LONG_MIN



#define endl "\n"

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



#define db1(x) cout<<#x<<" : "<<x<<endl;

#define db2(x,y) cout<<#x<<" : "<<x<<"\t"<<#y<<" : "<<y<<endl;

#define db3(x,y,z) cout<<#x<<" : "<<x<<"\t"<<#y<<" : "<<y<<"\t"<<#z<<" : "<<z<<endl;



const int N = 1e5+5;

ll dp[N][3];

ll n;

ll v[N][3];



ll sol(int d,int a)

{

  if(d == n)

  {

    return 0;

  }

  ll &ans = dp[d][a];

  if(ans != -1)

  {

    return ans;

  }

  if(a == 0)

  {

    ans = v[d][a] + max(sol(d+1,1),sol(d+1,2));

  }

  else if(a == 1)

  {

    ans = v[d][a] + max(sol(d+1,0),sol(d+1,2));

  }

  else

  {

    ans = v[d][a] + max(sol(d+1,0),sol(d+1,1));

  }

  return ans;

}



void solve()

{

  cin >> n;

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

  {

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

  }

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

  ll ans = Max(sol(0,0),sol(0,1),sol(0,2));

  cout << ans;

}



signed main()

{

  IOS;

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

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

  int t=1;

  //cin >> t;

  while(t--)

  {

    solve();

  }

  return 0;

}

/*==============================================================================================================================================================*/


// Faster C++ solution for the same problem (measured speedup: 3.3755814629804943x):
#include <bits/stdc++.h>

using namespace std;



typedef long long int ll;

typedef long double ld;



#define Pb push_back

#define Mp make_pair



#define ff first

#define ss second



#define Max(x,y,z) max(x,max(y,z))

#define Min(x,y,z) min(x,min(y,z))



#define INF LONG_LONG_MAX

#define MINF LONG_LONG_MIN



#define endl "\n"

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



#define db1(x) cout<<#x<<" : "<<x<<endl;

#define db2(x,y) cout<<#x<<" : "<<x<<"\t"<<#y<<" : "<<y<<endl;

#define db3(x,y,z) cout<<#x<<" : "<<x<<"\t"<<#y<<" : "<<y<<"\t"<<#z<<" : "<<z<<endl;



const int N = 1e5+5;

ll dp[N][3];

ll n;

ll v[N][3];



void solve()

{

  cin >> n;

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

  {

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

  }

  dp[0][0]=v[0][0];

  dp[0][1]=v[0][1];

  dp[0][2]=v[0][2];

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

  {

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

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

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

  }

  ll ans = Max(dp[n-1][0],dp[n-1][1],dp[n-1][2]);

  cout << ans;

}



signed main()

{

  IOS;

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

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

  int t=1;

  //cin >> t;

  while(t--)

  {

    solve();

  }

  return 0;

}

/*==============================================================================================================================================================*/
// Slower C++ solution:
#include<iostream>

using namespace std;

#include<string.h>

#include<bits/stdc++.h>



int dp[100001][3] = {};



int rec(int mat[][3] , int type, int idx);



int main(){

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

	int n;

	cin >> n;

	int mat[n][3];

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

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

	}

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

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

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



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

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

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

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

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

		}

	}

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

}

// Faster C++ solution for the same problem (measured speedup: 2.1817136315939636x):
#include<bits/stdc++.h>

using namespace std;

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



int main(){

	IOS;

	int n;

	cin >> n;

	int a[n][3];



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

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

	}



	int dp[n][3];

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

	}

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

}
// Slower C++ solution:
//**KEEP IT SHORT AND SIMPLE**



#include <bits/stdc++.h>

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

#define FILES freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);

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

#define NL '\n'

#define F first

#define S second

using namespace std;

typedef long long ll;

typedef bool bl;

typedef vector < vector<long long> > vvl;

typedef vector <long long> vll;

typedef pair <long long, long long> pll;

typedef set <long long> sll;

typedef map <long long, long long> mll;

typedef map <long long, bool> mlb;

typedef priority_queue<long long, vector<long long>, greater<long long> > minPq;

double pi = 2 * acos(0.0);

int OO = 0x3f3f3f3f;

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

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

const int NMAX = 100010;

ll n, A[NMAX], B[NMAX], C[NMAX], dp[NMAX][5];

ll solve(ll i, int last)

{

    if(i == n)          return 0;

    if(~dp[i][last])    return dp[i][last];

    ll ans = 0;

    if(last == 0)       ans = max(solve(i + 1, 1) + B[i], solve(i + 1, 2) + C[i]);

    else if(last == 1)  ans = max(solve(i + 1, 0) + A[i], solve(i + 1, 2) + C[i]);

    else if(last == 2)  ans = max(solve(i + 1, 0) + A[i], solve(i + 1, 1) + B[i]);

    else                ans = max(solve(i + 1, 0) + A[i], max(solve(i + 1, 1) + B[i], solve(i + 1, 2) + C[i]));

    return dp[i][last] = ans;

}

int main()

{

    IO

    cin >> n;

    REP(i, 0, n)

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

    memset(dp, -1, sizeof dp);

    cout << solve(0, 3);

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 5.032656927721041x):
//**KEEP IT SHORT AND SIMPLE**



#include <bits/stdc++.h>

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

#define FILES freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);

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

#define NL '\n'

#define F first

#define S second

#define dot(a, b) (conj(a) * (b)).real()

#define cross(a, b) (conj(a)* (b)).imag()

using namespace std;

typedef long long ll;

typedef long double ld;

typedef bool bl;

typedef vector < vector<long long> > vvl;

typedef vector <long long> vll;

typedef pair <long long, long long> pll;

typedef map <long long, long long> mll;

typedef map <long long, bool> mlb;

typedef priority_queue<long long, vector<long long>, greater<long long> > minPq;

typedef tuple<ll, ll, ll> line;

typedef complex<double> point;

const double pi = 2 * acos(0.0);

const int OO = 0x3f3f3f3f;

ll ans[100005][5];

int main()

{

    IO

    ll n, Max = 0;

    cin >> n;

    ll arr[n][3];

    REP(i, 0, n)

        REP(j, 0, 3)

            cin >> arr[i][j];

    REP(i, 0, 3)

        ans[0][i] = arr[0][i];

    REP(i, 1, n)

    {

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

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

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

    }

    REP(i, 0, 3)    Max = max(Max, ans[n - 1][i]);

    cout << Max;

    return 0;

}
// Slower C++ solution:
#include<iostream>

#include<algorithm>



using namespace std;



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

int f[110000][3];

int main()

{

    int n;



    cin>>n;



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

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

    f[1][0] = max(b[1],c[1]);

    f[1][1] = max(a[1],c[1]);

    f[1][2] = max(a[1],b[1]);

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

    {

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

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

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

    }

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

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

    cout<<ans;

}   

// Faster C++ solution for the same problem (measured speedup: 5.212982992870885x):
#include <cstdio>

#include <algorithm>

using std::max;

int main(void)

{

    int dp[2][3],a[3],i,N;

    i=0; 

    dp[0][0]=0;

    dp[0][1]=0;

    dp[0][2]=0;

 

    scanf("%d",&N);

    while(N--)

    {

        scanf("%d%d%d",a,a+1,a+2);

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

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

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

        //printf("%d\t%d\t%d\n",dp[1-i][0],dp[1-i][1],dp[1-i][2]);

        i=1-i;

    }

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

    return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>



using namespace std;





int main()

{

	int n;

	cin >> n;



	vector<vector<int>> h(n, vector<int>(3)), dp(n, vector<int>(3));



	for(auto &cur : h)

		cin >> cur[0] >> cur[1] >> cur[2];



	dp[0] = h[0];



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

	{

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

		{

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

			{

				if(k == j)

					continue;



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

			}

		}

	}



	cout << *max_element(dp[n - 1].begin(), dp[n - 1].end()) << endl;

}

// Faster C++ solution for the same problem (measured speedup: 5.282814090612983x):
#include <cstdio>

#include <algorithm>

using std::max;

int main(void)

{

    int dp[2][3],a[3],i,N;

    i=0; 

    dp[0][0]=0;

    dp[0][1]=0;

    dp[0][2]=0;

 

    scanf("%d",&N);

    while(N--)

    {

        scanf("%d%d%d",a,a+1,a+2);

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

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

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

        //printf("%d\t%d\t%d\n",dp[1-i][0],dp[1-i][1],dp[1-i][2]);

        i=1-i;

    }

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

    return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;



int ar[100010][5],n;

int dp[100010][5];



int call(int i,int f)

{



    if(i==n)

        return 0;



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

        return dp[i][f];



    int ans=0;



    if(f==0)

        ans+=max(ar[i][1]+call(i+1,1),ar[i][2]+call(i+1,2));



    else if(f==1)

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

    else

        ans+=max(ar[i][1]+call(i+1,1),ar[i][0]+call(i+1,0));



        dp[i][f]=ans;

        return ans;



}



int main()

{



    cin>>n;



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

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



    memset(dp,-1,sizeof dp);



    int ans=max(max(call(0,0),call(0,1)),call(0,2));



    cout<<ans<<endl;



}


// Faster C++ solution for the same problem (measured speedup: 3.0004360952098126x):
#include<bits/stdc++.h>

using namespace std;



int h[3][100005], a[100005], b[100005], c[100005];



int main(){



    int n; cin >> n;

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

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

    }



    h[0][1] = a[1];

    h[1][1] = b[1];

    h[2][1] = c[1];



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

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

        h[1][i] = max({h[1][i - 2] + b[i], h[0][i-1] + b[i], h[2][i-1] + b[i]});

        h[2][i] = max({h[2][i - 2] + c[i], h[0][i-1] + c[i], h[1][i-1] + c[i]});

    }



    cout << max({h[0][n], h[1][n], h[2][n]}) << endl;

    return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;



int ar[100010][5],n;

int dp[100010][5];



int call(int i,int f)

{



    if(i==n)

        return 0;



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

        return dp[i][f];



    int ans=0;



    if(f==0)

        ans+=max(ar[i][1]+call(i+1,1),ar[i][2]+call(i+1,2));



    else if(f==1)

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

    else

        ans+=max(ar[i][1]+call(i+1,1),ar[i][0]+call(i+1,0));



        dp[i][f]=ans;

        return ans;



}



int main()

{



    cin>>n;



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

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



    memset(dp,-1,sizeof dp);



    int ans=max(max(call(0,0),call(0,1)),call(0,2));



    cout<<ans<<endl;



}


// Faster C++ solution for the same problem (measured speedup: 2.969228529106177x):
#include <bits/stdc++.h>

using namespace std;



#define fr(i,n) _back

#define pb	push_back

#define eb	emplace_back

#define mk	make_pair

#define fi	first

#define se	second

#define endl	'\n'



typedef long long ll;

typedef pair<int,int> ii;

typedef vector<ii> vii;

const int INF = 0x3f3f3f3f;

const double PI = acos(-1.0);

int dp[2][3];



int main() {

    ios_base::sync_with_stdio(false);

    int n; cin >> n;

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

        for(int j = 0; j  < 3; j++) cin >> dp[1][j];

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

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

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

        for(int j = 0; j < 3; j++) dp[0][j] = dp[1][j];

    }

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

    return 0;

}
// Slower C++ solution:


#include<bits/stdc++.h>

using namespace std;



#define MAX 100000



int dp[MAX+5][4];

int val[MAX+5][4];

int n;



void initialize()

{

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

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

            dp[i][j] = -1;

}



int vacation(int i, int j)

{

    if(i<=0) return 0;



  /*  if(i==1)

    {

        return val[i][j];

    }

*/

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



    int mx = INT_MIN;



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

    {

        if(j==k) continue;



        mx = max(mx, vacation(i-1, k)+ val[i][j]);

    }





    dp[i][j] = mx;



    return mx;

}



int main()

{

    initialize();



    cin>>n;

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

    {

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

            cin>>val[i][j];

    }



    int retVal1 = vacation(n, 1);



    initialize();



    int retVal2 = vacation(n, 2);



    initialize();



    int retVal3 = vacation(n, 3);



    cout<<max({retVal1, retVal2, retVal3})<<endl;



    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 3.589653012481416x):
#include <bits/stdc++.h>

using namespace std;



#define fr(i,n) _back

#define pb	push_back

#define eb	emplace_back

#define mk	make_pair

#define fi	first

#define se	second

#define endl	'\n'



typedef long long ll;

typedef pair<int,int> ii;

typedef vector<ii> vii;

const int INF = 0x3f3f3f3f;

const double PI = acos(-1.0);

int dp[2][3];



int main() {

    ios_base::sync_with_stdio(false);

    int n; cin >> n;

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

        for(int j = 0; j  < 3; j++) cin >> dp[1][j];

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

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

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

        for(int j = 0; j < 3; j++) dp[0][j] = dp[1][j];

    }

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

    return 0;

}
// Slower C++ solution:


#include<bits/stdc++.h>

using namespace std;



#define MAX 100000



int dp[MAX+5][4];

int val[MAX+5][4];

int n;



void initialize()

{

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

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

            dp[i][j] = -1;

}



int vacation(int i, int j)

{

    if(i<=0) return 0;



  /*  if(i==1)

    {

        return val[i][j];

    }

*/

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



    int mx = INT_MIN;



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

    {

        if(j==k) continue;



        mx = max(mx, vacation(i-1, k)+ val[i][j]);

    }





    dp[i][j] = mx;



    return mx;

}



int main()

{

    initialize();



    cin>>n;

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

    {

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

            cin>>val[i][j];

    }



    int retVal1 = vacation(n, 1);



    initialize();



    int retVal2 = vacation(n, 2);



    initialize();



    int retVal3 = vacation(n, 3);



    cout<<max({retVal1, retVal2, retVal3})<<endl;



    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 1.207815005657491x):
#include<bits/stdc++.h>

using namespace std;

int arr[100010][5];

int dp[100010][5];



int vac(int a, int b){

	if(a == 0) return 0;

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

	

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

		if(i != b){

			dp[a][b] = max(dp[a][b], vac(a - 1,i) + arr[a][i]);

		}

	}

	

	return dp[a][b];

}



int main (){

	memset(dp,-1,sizeof dp);

	int n;

	cin >> n;

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

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

	}

	

	

	int ans = 0;

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

		ans = max(ans , vac(n - 1, i) + arr[n][i]);

	}

	

	cout << ans << endl;

	

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;



int ar[100010][5],n;

int dp[100010][5];



int call(int i,int f)

{



    if(i==n)

        return 0;



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

        return dp[i][f];



    int ans=0;



    if(f==0)

        ans+=max(ar[i][1]+call(i+1,1),ar[i][2]+call(i+1,2));



    else if(f==1)

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

    else

        ans+=max(ar[i][1]+call(i+1,1),ar[i][0]+call(i+1,0));



        dp[i][f]=ans;

        return ans;



}



int main()

{



    cin>>n;



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

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



    memset(dp,-1,sizeof dp);



    int ans=max(max(call(0,0),call(0,1)),call(0,2));



    cout<<ans<<endl;



}


// Faster C++ solution for the same problem (measured speedup: 3.043109159344514x):
#include<bits/stdc++.h>

using namespace std;

int n,a[100010][5],dp[100010][5];



int main()

{

	scanf("%d",&n);

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

	{

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

	}

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

	{

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

	}

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

	{

	//	printf("%d. ",i);

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

		{

			dp[i][j] = -1;

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

			{

			//	printf("%d %d : %d %d\n",i,j,dp[i-1][j]+a[i][k],a[i][k]);

				if(j!=k)

				{

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

				}

			}

			//printf("%d ",dp[i][j]);

		}

		//printf("\n");

	}

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

	return 0;

}
// Slower C++ solution:


#include<bits/stdc++.h>

using namespace std;



#define MAX 100000



int dp[MAX+5][4];

int val[MAX+5][4];

int n;



void initialize()

{

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

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

            dp[i][j] = -1;

}



int vacation(int i, int j)

{

    if(i<=0) return 0;



  /*  if(i==1)

    {

        return val[i][j];

    }

*/

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



    int mx = INT_MIN;



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

    {

        if(j==k) continue;



        mx = max(mx, vacation(i-1, k)+ val[i][j]);

    }





    dp[i][j] = mx;



    return mx;

}



int main()

{

    initialize();



    cin>>n;

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

    {

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

            cin>>val[i][j];

    }



    int retVal1 = vacation(n, 1);



    initialize();



    int retVal2 = vacation(n, 2);



    initialize();



    int retVal3 = vacation(n, 3);



    cout<<max({retVal1, retVal2, retVal3})<<endl;



    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 3.6789711044704174x):
#include<bits/stdc++.h>

using namespace std;

int n,a[100010][5],dp[100010][5];



int main()

{

	scanf("%d",&n);

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

	{

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

	}

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

	{

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

	}

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

	{

	//	printf("%d. ",i);

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

		{

			dp[i][j] = -1;

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

			{

			//	printf("%d %d : %d %d\n",i,j,dp[i-1][j]+a[i][k],a[i][k]);

				if(j!=k)

				{

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

				}

			}

			//printf("%d ",dp[i][j]);

		}

		//printf("\n");

	}

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

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

int arr[100010][5];

int dp[100010][5];



int vac(int a, int b){

	if(a == 0) return 0;

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

	

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

		if(i != b){

			dp[a][b] = max(dp[a][b], vac(a - 1,i) + arr[a][i]);

		}

	}

	

	return dp[a][b];

}



int main (){

	memset(dp,-1,sizeof dp);

	int n;

	cin >> n;

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

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

	}

	

	

	int ans = 0;

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

		ans = max(ans , vac(n - 1, i) + arr[n][i]);

	}

	

	cout << ans << endl;

	

}

// Faster C++ solution for the same problem (measured speedup: 3.0459723444715094x):
#include<bits/stdc++.h>

using namespace std;

int n,a[100010][5],dp[100010][5];



int main()

{

	scanf("%d",&n);

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

	{

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

	}

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

	{

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

	}

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

	{

	//	printf("%d. ",i);

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

		{

			dp[i][j] = -1;

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

			{

			//	printf("%d %d : %d %d\n",i,j,dp[i-1][j]+a[i][k],a[i][k]);

				if(j!=k)

				{

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

				}

			}

			//printf("%d ",dp[i][j]);

		}

		//printf("\n");

	}

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

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;



int ar[100010][5],n;

int dp[100010][5];



int call(int i,int f)

{



    if(i==n)

        return 0;



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

        return dp[i][f];



    int ans=0;



    if(f==0)

        ans+=max(ar[i][1]+call(i+1,1),ar[i][2]+call(i+1,2));



    else if(f==1)

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

    else

        ans+=max(ar[i][1]+call(i+1,1),ar[i][0]+call(i+1,0));



        dp[i][f]=ans;

        return ans;



}



int main()

{



    cin>>n;



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

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



    memset(dp,-1,sizeof dp);



    int ans=max(max(call(0,0),call(0,1)),call(0,2));



    cout<<ans<<endl;



}


// Faster C++ solution for the same problem (measured speedup: 3.04500966892476x):
#include<cstdio>

#include<iostream>

#include<algorithm>

#include<queue>

#define inf 0x3f3f3f3f

using namespace std;

typedef long long LL;

const int maxn = 1e5+5;



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



int main(){

	int n;

	scanf("%d",&n);

	int m;

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

	dp[1][0] = a[1]; dp[1][1] = b[1]; dp[1][2] = c[1];

	for(int i = 2; 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]);

		}

	}

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

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

	printf("%d",m);

}