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

using namespace std;



int n;

int dp[100100][4];

vector<int> arr[100100];



int main() {

    cin >> n;

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

        int k;

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

            cin >> k;

            arr[i].push_back(k);

        }

    }



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



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

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



            if ( j == 0 ) 

                dp[pos][j] = arr[pos][j] + max(dp[pos - 1][1], dp[pos - 1][2]);

            

            else if ( j == 1 )

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



            else

                dp[pos][j] = arr[pos][j] + max(dp[pos - 1][0], dp[pos - 1][1]);

        }

    }



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

    return 0;

}


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

using namespace std;

const int maxn = 1e5+10;

int dp[maxn][4];

int main()

{

    int n;

    cin>>n;

    int ans =0;

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

    {

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

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

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

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

        ans = max(dp[i][1],max(dp[i][2],dp[i][3]));

    }

    cout<<ans<<endl;

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

using namespace std;



#define MAX_N 100005

int N, h[MAX_N][3], memo[MAX_N][3];



int main() {

  scanf("%i", &N);

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

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

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

	}

  }

  memset(memo, -1, sizeof memo);

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

  	memo[0][c] = h[0][c];

  }

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

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

  	  int &opt = memo[d][c];

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

  	    if (cc != c) {

  	      opt = max(opt, memo[d - 1][cc] + h[d][c]);

		  /*

  	      if (d == 1) {

  	        printf("%i %i %i %i\n", memo[d - 1][cc], h[d][c], cc, c);

		  }

		  */

		}

	  }

	}

  }

  /*

  puts("\ndebug");

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

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

  	  printf("%i ", memo[d][c]);

	}

	puts("");

  }

  puts("end-debug\n");

  */

  int ans = -1;

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

  	ans = max(ans, memo[N - 1][c]);

  }

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

  

}

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

using namespace std;

const int maxn = 1e5+10;

int dp[maxn][4];

int main()

{

    int n;

    cin>>n;

    int ans =0;

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

    {

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

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

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

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

        ans = max(dp[i][1],max(dp[i][2],dp[i][3]));

    }

    cout<<ans<<endl;

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

#include<cstring>

#include<algorithm>

#include<cmath>

using namespace std;

int main()

{

    int n,i,j,k;

    int a[100005][5];

    scanf("%d",&n);

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

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

    int dp[100005][5];

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

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

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

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

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

    {

        for(j = 1;j < 4;j++)

        {

            for(k = 1;k < 4;k++)

            {

                if(k != j)

                {

                    if(dp[i][j] < dp[i - 1][k] + a[i][j])

                    dp[i][j] = dp[i - 1][k] + a[i][j];

                }

            }

        }

    }

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

    return 0;

}

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

using namespace std;

const int maxn = 1e5+10;

int dp[maxn][4];

int main()

{

    int n;

    cin>>n;

    int ans =0;

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

    {

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

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

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

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

        ans = max(dp[i][1],max(dp[i][2],dp[i][3]));

    }

    cout<<ans<<endl;

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



using namespace std;



vector<vector<int> > v;

int ans[100005][3];

queue<pair<int,pair<int,int> > >s;

int maior,a=0;



void dp(int i, int bloq, int sum) {

  if(ans[i-1][bloq]>sum)

    return;

  

  if (i>=v.size())

    maior = max(sum, maior);

  else {

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

      if (bloq != j) {



        if(ans[i][j]==-1) {

          ans[i][j] = sum+v[i][j];

          s.push({sum+v[i][j],{i+1,j}});

        }

        else if (ans[i][j]<sum+v[i][j]) {

          ans[i][j] = sum+v[i][j];

          s.push({sum+v[i][j],{i+1,j}});

        }



        //dp(i+1, j, sum+v[i][j]);

      }

    }

  }

}



int main () {

  int n;

  cin>>n;

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

    int a,b,c;

    cin>>a>>b>>c;

    v.push_back({a,b,c});

  }



  maior=0;

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

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

    s.push({v[0][i],{1,i}});

    //dp(1, i, v[0][i]);

  }



  while(!s.empty()) {

    pair<int,pair<int,int> > aux = s.front();

    dp(aux.second.first, aux.second.second, aux.first);

    s.pop();

  }



  cout<<maior<<endl;



}




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

using namespace std;

const int maxn = 1e5+10;

int dp[maxn][4];

int main()

{

    int n;

    cin>>n;

    int ans =0;

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

    {

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

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

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

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

        ans = max(dp[i][1],max(dp[i][2],dp[i][3]));

    }

    cout<<ans<<endl;

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

using namespace std;

typedef long long ll;

typedef long long int lli;

typedef long double ld;

const int oo = 1e9;



int n;

const int nx = 1e5+5;

int ac[nx][3];

int dp[3][nx];



int go(int a, int i){

	if(i == n) return 0;

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

	int& ans = dp[a][i];

	

	if(a==0){

		ans=max(ans, go(1, i+1)+ac[i][0]);

		ans=max(ans, go(2, i+1)+ac[i][0]);

	}

	if(a==1){

		ans=max(ans, go(0, i+1)+ac[i][1]);

		ans=max(ans, go(2, i+1)+ac[i][1]);

	}

	if(a==2){

		ans=max(ans, go(0, i+1)+ac[i][2]);

		ans=max(ans, go(1, i+1)+ac[i][2]);

	}

	

	return ans;

}



int main(){

	cin>>n;

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

	memset(dp, -1, sizeof dp);

	cout<<max(go(0,0),max(go(1,0),go(2,0)))<<"\n";

	return 0;

}


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

using namespace std;

const int maxn = 1e5+10;

int dp[maxn][4];

int main()

{

    int n;

    cin>>n;

    int ans =0;

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

    {

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

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

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

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

        ans = max(dp[i][1],max(dp[i][2],dp[i][3]));

    }

    cout<<ans<<endl;

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

using namespace std;

const int oo = 1e9;

int number;

const int nx = 1e5+5;

int ac[nx][3];

int dyn[3][nx];

int init(int a, int i){

	if(i == number) return 0;

	if(dyn[a][i] != -1) return dyn[a][i];

	int& res = dyn[a][i];

	

	if(a==0){

		res=max(res, init(1, i+1)+ac[i][0]);

		res=max(res, init(2, i+1)+ac[i][0]);

	}

	if(a==1){

		res=max(res, init(0, i+1)+ac[i][1]);

		res=max(res, init(2, i+1)+ac[i][1]);

	}

	if(a==2){

		res=max(res, init(0, i+1)+ac[i][2]);

		res=max(res, init(1, i+1)+ac[i][2]);

	}

	

	return res;

}

int main(){

	cin>>number;

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

	memset(dyn, -1, sizeof dyn);

	cout<<max(init(0,0),max(init(1,0),init(2,0)))<<"\n";

	return 0;

}




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

using namespace std;

const int maxn = 1e5+10;

int dp[maxn][4];

int main()

{

    int n;

    cin>>n;

    int ans =0;

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

    {

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

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

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

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

        ans = max(dp[i][1],max(dp[i][2],dp[i][3]));

    }

    cout<<ans<<endl;

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

using namespace std;

typedef long long ll;

typedef long long int lli;

typedef long double ld;

const int oo = 1e9;



int n;

const int nx = 1e5+5;

int ac[nx][3];

int dp[3][nx];

int go(int x, int i){

	if(i==n) return 0;

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

	int& ans = dp[x][i];

	if(x==0)

	{

		ans=max(ans, go(1, i+1)+ac[i][0]);

		ans=max(ans, go(2, i+1)+ac[i][0]);

	}

	if(x==1)

	{

		ans=max(ans, go(0, i+1)+ac[i][1]);

		ans=max(ans, go(2, i+1)+ac[i][1]);

	}

	if(x==2)

	{

		ans=max(ans, go(0, i+1)+ac[i][2]);

		ans=max(ans, go(1, i+1)+ac[i][2]);

	}

	

	return ans;

	

}

int main(){

    cin>>n;

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

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

    }

    memset(dp, -1, sizeof dp);

    cout<<max(go(0,0), max(go(1,0),go(2,0)))<<"\n";

    return 0;

}

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

using namespace std;

const int maxn = 1e5+10;

int dp[maxn][4];

int main()

{

    int n;

    cin>>n;

    int ans =0;

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

    {

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

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

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

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

        ans = max(dp[i][1],max(dp[i][2],dp[i][3]));

    }

    cout<<ans<<endl;

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

using namespace std;

typedef long long ll;



ll arr[100009][3];

int dp[100009][2][2][2];



long long VAC1(int n,int a, int b, int c)

{



    if(!n && a) return arr[n][0];

    else if(!n && b) return arr[n][1];

    else if(!n && c) return arr[n][2];

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



    else

    {



        ll x,y,z;

        if(a)

        {

            x = VAC1(n-1,0,1,0)+arr[n][0];

            y = VAC1(n-1,0,0,1)+arr[n][0];



        }



        else if(b)

        {

            x = VAC1(n-1,1,0,0)+arr[n][1];

            y = VAC1(n-1,0,0,1)+arr[n][1];

        }



        else if(c)

        {

            x = VAC1(n-1,1,0,0)+arr[n][2];

            y = VAC1(n-1,0,1,0)+arr[n][2];

        }



        dp[n][a][b][c] = max(x,y);;

        return dp[n][a][b][c];





    }

}



int main()

{

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

    int n;

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

    cin >> n;

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

    {

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

        {

            cin >> arr[i][j];

        }

    }

    ll res = VAC1(n-1,1,0,0);

    res= max(res, VAC1(n-1,0,1,0));

    res = max(res,VAC1(n-1,0,0,1));



    cout << res << endl;

}


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

using namespace std;

const int maxn = 1e5+10;

int dp[maxn][4];

int main()

{

    int n;

    cin>>n;

    int ans =0;

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

    {

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

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

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

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

        ans = max(dp[i][1],max(dp[i][2],dp[i][3]));

    }

    cout<<ans<<endl;

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

using namespace std;

#define ll long long

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

int dp[100009][3];

int main() {

    ios::sync_with_stdio(false);

    int n;cin>>n;

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

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

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

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

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

    return 0;

}


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

using namespace std;

const int maxn = 1e5+10;

int dp[maxn][4];

int main()

{

    int n;

    cin>>n;

    int ans =0;

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

    {

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

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

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

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

        ans = max(dp[i][1],max(dp[i][2],dp[i][3]));

    }

    cout<<ans<<endl;

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

using namespace std;

const int mx=100010;

int arr[mx][3],n;

int dp[mx][3];

int f(int pos,int ty){

	if(pos==n)return 0;

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

	if(ans^-1)return ans;

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

		if(ty^i){

			ans=max(ans,f(pos+1,i)+arr[pos][i]);

		}

	}

	return ans;

}



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

{

	memset(dp,-1,sizeof dp);

	cin>>n;

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

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

			cin>>arr[i][j];

		}

	}

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



	return 0;

}

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

using namespace std;

const int maxn = 1e5+10;

int dp[maxn][4];

int main()

{

    int n;

    cin>>n;

    int ans =0;

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

    {

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

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

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

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

        ans = max(dp[i][1],max(dp[i][2],dp[i][3]));

    }

    cout<<ans<<endl;

}
// Slower C++ solution:
#define _CRT_SECURE_NO_WARNINGS

#include<bits/stdc++.h>

#include<unordered_map>

#include<unordered_set>	

using namespace std;

typedef long long ll;

#define bay_bay return 0;

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

#define for1(n) for(int i=1;i<=n;i+=3)

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

#define sz(v) v.size()

#define endl "\n"

constexpr auto EPS = 1e-9;

#define ff fflush(stdout);

const int mod = 1000000007LL;

clock_t T;

#define ctime cerr << "Time : " << double(clock() - T) / CLOCKS_PER_SEC << endl

void GOAT()

{

	std::ios_base::sync_with_stdio(0);

	cin.tie(NULL);

	cout.tie(NULL);

#ifndef ONLINE_JUDGE

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

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

#else

#endif

}

struct day

{

	ll a, b, c;



};

day days[100009];

ll mem[100009][4];

int n;

ll solve(int idx, int ch)

{

	if (idx == n)

	{

		return 0;

	}

	ll &ret = mem[idx][ch];

	if (~ret)

		return ret;

	ret = LLONG_MIN;

	if (ch != 1)

		ret=max(ret, days[idx].a + solve(idx + 1, 1));

	if (ch != 2)

		ret=max(ret, days[idx].b + solve(idx + 1, 2));

	if (ch != 3)

		ret=max(ret, days[idx].c + solve(idx + 1, 3));

	return ret;



}

int main()

{

	//GOAT();

	cin >> n;

	memset(mem, -1,sizeof mem);

	fori(n)

	{

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

	}

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

	

}

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

using namespace std;

const int maxn = 1e5+10;

int dp[maxn][4];

int main()

{

    int n;

    cin>>n;

    int ans =0;

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

    {

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

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

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

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

        ans = max(dp[i][1],max(dp[i][2],dp[i][3]));

    }

    cout<<ans<<endl;

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

#define p5 100005

using namespace std;



int dp[p5][4],a[p5][4];

int n;



int solve(int day,int state)

{

    if(day==n) return a[day][state];

    if(dp[day][state]!=-1) return dp[day][state];

    int cost=a[day][state];

    if(state==1) cost+=max(solve(day+1,2),solve(day+1,3));

    else if(state==2) cost+=max(solve(day+1,1),solve(day+1,3));

    else if(state==3) cost+=max(solve(day+1,1),solve(day+1,2));

    else cost=max( solve(day+1,1), max(solve(day+1,2),solve(day+1,3)) );



    return dp[day][state]=cost;

}



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(dp,-1,sizeof(dp));

    printf("%d\n",solve(0,0));



    return 0;

}


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

using namespace std;

const int maxn = 1e5+10;

int dp[maxn][4];

int main()

{

    int n;

    cin>>n;

    int ans =0;

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

    {

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

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

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

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

        ans = max(dp[i][1],max(dp[i][2],dp[i][3]));

    }

    cout<<ans<<endl;

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

using namespace std;

const int mx=100010;

int arr[mx][3],n;

int dp[mx][3];

int f(int pos,int ty){

	if(pos==n)return 0;

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

	if(ans^-1)return ans;

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

		if(ty^i){

			ans=max(ans,f(pos+1,i)+arr[pos][i]);

		}

	}

	return ans;

}



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

{

	memset(dp,-1,sizeof dp);

	cin>>n;

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

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

			cin>>arr[i][j];

		}

	}

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



	return 0;

}

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

#include <bits/stdc++.h>

#define mod 998244353

typedef long long ll;



using namespace std;



void Fastio(){

    ios::sync_with_stdio(0);

    cin.tie(0);

    cout.tie(0);

}



ll fact[1000006], inv[1000006];



ll fp(ll base, ll exp)

{

    if (exp == 0)

        return 1;

    ll ans = fp(base, exp / 2);

    ans = (ans * ans) % mod;

    if (exp % 2 != 0)

        ans = (ans * (base % mod)) % mod;

    return ans;

}



void calcFacAndInv(ll n)

{

    fact[0] = inv[0] = 1;

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

    {

        fact[i] = (i * fact[i - 1]) ;

        inv[i] = fp(fact[i], mod - 2);

    }

}



ll ncr(ll n, ll r)

{

    return ((fact[n] * inv[r]) % mod * inv[n - r]) % mod;

}



ll npr(ll n, ll r)

{

    return (fact[n] * inv[n - r]) % mod;

}



long double  NCR(int n, int m){

    long double res = 1;

    m = min(m, n - m);

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

        res = res * ( n - i) / (i + 1);

 }

    return res;

}

ll tol(string a){

    ll res=0;

    while(a[0]=='0'){

        a.erase(a.begin());

    }

    if(a=="")a="0";

    for(int i=0; i<a.size(); i++){

        res*=10; res+=a[i]-'0';

    }

    return res;

}

int check(string a, string b){

    for(int i=0; i<a.size(); i++){

        if(a[i]>b[i])return 1;

        if(b[i]>a[i])return -1;

    }

    return 0;

}

class data{

public:

    ll v,d,p;

    data(){}

    data(ll vv, ll dd, ll pp){

        v=vv;  d=dd; p=pp;

    }

};



int primeFactors(int n)

{

    ll c=0;

    while (n % 2 == 0)

    {

        c++;

        n = n/2;

    }

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

    {

        while (n % i == 0)

        {

            c++;

            n = n/i;

        }

    }

    if (n > 2)

        c++;

    return c;

}



int main()

{

    Fastio();

    int n; cin>>n;

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

    ll dp[n+1][5];

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

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

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

    }

    for(int i=n-1; i>=0; i--){

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

            ll res1,res2,res0;

            res0=res1=res2=-1e9;

            res0=dp[i+1][0]+a[i];

            res1=dp[i+1][1]+b[i];

            res2=dp[i+1][2]+c[i];

            if(y==0){

                dp[i][y]=max(res1,res2);

            }

            if(y==1){

                dp[i][y]=max(res0,res2);

            }

            if(y==2){

                dp[i][y]=max(res0,res1);

            }

        }

    }

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





    return 0;

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

using namespace std;



int n;

int dp[100100][4];

vector<int> arr[100100];



int main() {

    cin >> n;

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

        int k;

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

            cin >> k;

            arr[i].push_back(k);

        }

    }



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



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

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



            if ( j == 0 ) 

                dp[pos][j] = arr[pos][j] + max(dp[pos - 1][1], dp[pos - 1][2]);

            

            else if ( j == 1 )

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



            else

                dp[pos][j] = arr[pos][j] + max(dp[pos - 1][0], dp[pos - 1][1]);

        }

    }



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

    return 0;

}


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

using namespace std;

const int N = 2e5 ;

int dpA[N], dpB[N] , dpC[N] ,a[N] , b[N] , c[N],n;

int main()

{

    cin >> n ;

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

    dpA[0] = a[0] ;

    dpB[0] = b[0] ;

    dpC[0] = c[0] ;

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

        dpA[i] = max(dpB[i-1],dpC[i-1])+a[i] ;

        dpB[i] = max(dpA[i-1],dpC[i-1])+b[i] ;

        dpC[i] = max(dpB[i-1],dpA[i-1])+c[i] ;

    }

    cout << max(dpA[n-1],max(dpB[n-1],dpC[n-1]));

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

using namespace std;



#define MAX_N 100005

int N, h[MAX_N][3], memo[MAX_N][3];



int main() {

  scanf("%i", &N);

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

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

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

	}

  }

  memset(memo, -1, sizeof memo);

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

  	memo[0][c] = h[0][c];

  }

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

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

  	  int &opt = memo[d][c];

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

  	    if (cc != c) {

  	      opt = max(opt, memo[d - 1][cc] + h[d][c]);

		  /*

  	      if (d == 1) {

  	        printf("%i %i %i %i\n", memo[d - 1][cc], h[d][c], cc, c);

		  }

		  */

		}

	  }

	}

  }

  /*

  puts("\ndebug");

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

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

  	  printf("%i ", memo[d][c]);

	}

	puts("");

  }

  puts("end-debug\n");

  */

  int ans = -1;

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

  	ans = max(ans, memo[N - 1][c]);

  }

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

  

}

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

using namespace std;

const int N = 2e5 ;

int dpA[N], dpB[N] , dpC[N] ,a[N] , b[N] , c[N],n;

int main()

{

    cin >> n ;

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

    dpA[0] = a[0] ;

    dpB[0] = b[0] ;

    dpC[0] = c[0] ;

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

        dpA[i] = max(dpB[i-1],dpC[i-1])+a[i] ;

        dpB[i] = max(dpA[i-1],dpC[i-1])+b[i] ;

        dpC[i] = max(dpB[i-1],dpA[i-1])+c[i] ;

    }

    cout << max(dpA[n-1],max(dpB[n-1],dpC[n-1]));

}