stablegradients/pie-gem5-pairs huggingface dataset:
// 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.2141064888680293x):
#include <bits/stdc++.h>



using namespace std;



int vacation(int table_of_happiness[][3], int j, int r[][4], int nxt){

	if(j==-1){

		return 0;

	}

	if(r[j][nxt] >= 0){

		return r[j][nxt];

	}

	else{

		int t[3];

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

			if(nxt != i)

				t[i] = vacation(table_of_happiness, j-1, r, i) + table_of_happiness[j][i];

		int best=0;

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

			if(nxt != i && t[i] > best){

				best = t[i];

			}

		}

		r[j][nxt] = best;

		return r[j][nxt];

	}

}



int main(){

	int N;

	scanf("%d", &N);

	int table_of_happiness[N][3];

	int r[N][4];

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

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

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

		}

	}



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

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

			r[i][j] = -1;

		}

	}



	r[N-1][3] = vacation(table_of_happiness, N-1, r, 3);



	printf("%d\n", r[N-1][3]);

}
// 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.2954647869095757x):
#include <bits/stdc++.h>



using namespace std;



int vacation(int table_of_happiness[][3], int j, int r[][4], int nxt){

	if(j==-1){

		return 0;

	}

	if(r[j][nxt] >= 0){

		return r[j][nxt];

	}

	else{

		int t[3];

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

			if(nxt != i)

				t[i] = vacation(table_of_happiness, j-1, r, i) + table_of_happiness[j][i];

		int best=0;

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

			if(nxt != i && t[i] > best){

				best = t[i];

			}

		}

		r[j][nxt] = best;

		return r[j][nxt];

	}

}



int main(){

	int N;

	scanf("%d", &N);

	int table_of_happiness[N][3];

	int r[N][4];

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

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

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

		}

	}



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

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

			r[i][j] = -1;

		}

	}



	r[N-1][3] = vacation(table_of_happiness, N-1, r, 3);



	printf("%d\n", r[N-1][3]);

}
// 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.5317485636549417x):
#include <iostream>

#include <cmath>



using namespace std;



int escolhas(int dia, int k);



int n_dias;



int atividades[100001][3];



int pontos[100001][3];



int main(){

    cin >> n_dias;



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

        cin >> atividades[i][1];

        cin >> atividades[i][2];

        cin >> atividades[i][3];

    }



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

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

            pontos[i][j] = -1;

        }

    }



    int felicidade = escolhas(1, -1);



    cout << felicidade << endl;



    return 0;

}



int escolhas(int dia, int k){

    if(dia == n_dias + 1){

        return 0;

    }



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

        if(i != k){

            if(pontos[dia][i] == -1){

                pontos[dia][i] = atividades[dia][i] + escolhas(dia + 1, i);

            }

        }

    }



    int maximo = 0;



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

        if(i != k){

            if(pontos[dia][i] > maximo){

                maximo = pontos[dia][i];

            }

        }

    }



    return maximo;

}
// 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.1854481413124214x):
#include <iostream>

#include <cmath>



using namespace std;



int escolhas(int dia, int k);



int n_dias;



int atividades[100001][3];



int pontos[100001][3];



int main(){

    cin >> n_dias;



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

        cin >> atividades[i][1];

        cin >> atividades[i][2];

        cin >> atividades[i][3];

    }



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

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

            pontos[i][j] = -1;

        }

    }



    int felicidade = escolhas(1, -1);



    cout << felicidade << endl;



    return 0;

}



int escolhas(int dia, int k){

    if(dia == n_dias + 1){

        return 0;

    }



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

        if(i != k){

            if(pontos[dia][i] == -1){

                pontos[dia][i] = atividades[dia][i] + escolhas(dia + 1, i);

            }

        }

    }



    int maximo = 0;



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

        if(i != k){

            if(pontos[dia][i] > maximo){

                maximo = pontos[dia][i];

            }

        }

    }



    return maximo;

}
// 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.26575337601057x):
#include <iostream>

#include <cmath>



using namespace std;



int escolhas(int dia, int k);



int n_dias;



int atividades[100001][3];



int pontos[100001][3];



int main(){

    cin >> n_dias;



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

        cin >> atividades[i][1];

        cin >> atividades[i][2];

        cin >> atividades[i][3];

    }



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

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

            pontos[i][j] = -1;

        }

    }



    int felicidade = escolhas(1, -1);



    cout << felicidade << endl;



    return 0;

}



int escolhas(int dia, int k){

    if(dia == n_dias + 1){

        return 0;

    }



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

        if(i != k){

            if(pontos[dia][i] == -1){

                pontos[dia][i] = atividades[dia][i] + escolhas(dia + 1, i);

            }

        }

    }



    int maximo = 0;



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

        if(i != k){

            if(pontos[dia][i] > maximo){

                maximo = pontos[dia][i];

            }

        }

    }



    return maximo;

}
// 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: 1.5942082196845764x):
#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;



}
// 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.4944433641400146x):
#include <bits/stdc++.h>



using namespace std;



#define INF (1ll << 62)

#define pb push_back

#define ii pair<int,int>

#define OK cerr <<"OK"<< endl

#define debug(x) cerr << #x " = " << (x) << endl

#define ff first

#define ss second

#define int long long

#define tt tuple<int, int, int>

#define endl '\n'



int dp[200000][3];

int mat[200000][3];





signed main () {

  

  ios_base::sync_with_stdio(false);

  cin.tie(NULL);



  int n;



  cin >> n;



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

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

  		cin >> mat[i][j];

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

  			if(j == k)

  				continue;

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

  		}

  	}

  }



  cout << *max_element(dp[n], dp[n] + 3) << 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.1623636613524964x):
#include <bits/stdc++.h>



using namespace std;



#define INF (1ll << 62)

#define pb push_back

#define ii pair<int,int>

#define OK cerr <<"OK"<< endl

#define debug(x) cerr << #x " = " << (x) << endl

#define ff first

#define ss second

#define int long long

#define tt tuple<int, int, int>

#define endl '\n'



int dp[200000][3];

int mat[200000][3];





signed main () {

  

  ios_base::sync_with_stdio(false);

  cin.tie(NULL);



  int n;



  cin >> n;



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

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

  		cin >> mat[i][j];

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

  			if(j == k)

  				continue;

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

  		}

  	}

  }



  cout << *max_element(dp[n], dp[n] + 3) << 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.2418206468764716x):
#include <bits/stdc++.h>



using namespace std;



#define INF (1ll << 62)

#define pb push_back

#define ii pair<int,int>

#define OK cerr <<"OK"<< endl

#define debug(x) cerr << #x " = " << (x) << endl

#define ff first

#define ss second

#define int long long

#define tt tuple<int, int, int>

#define endl '\n'



int dp[200000][3];

int mat[200000][3];





signed main () {

  

  ios_base::sync_with_stdio(false);

  cin.tie(NULL);



  int n;



  cin >> n;



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

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

  		cin >> mat[i][j];

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

  			if(j == k)

  				continue;

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

  		}

  	}

  }



  cout << *max_element(dp[n], dp[n] + 3) << 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.1919617029897207x):
#include <bits/stdc++.h>



using namespace std;



#define INF (1ll << 62)

#define pb push_back

#define ii pair<int,int>

#define OK cerr <<"OK"<< endl

#define debug(x) cerr << #x " = " << (x) << endl

#define ff first

#define ss second

#define int long long

#define tt tuple<int, int, int>

#define endl '\n'



int dp[200000][3];

int mat[200000][3];





signed main () {

  

  ios_base::sync_with_stdio(false);

  cin.tie(NULL);



  int n;



  cin >> n;



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

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

  		cin >> mat[i][j];

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

  			if(j == k)

  				continue;

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

  		}

  	}

  }



  cout << *max_element(dp[n], dp[n] + 3) << endl;

}
// 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.581555655454633x):
#include<cstdio>

#include<iostream>

#include<algorithm>

#include<cmath>

using namespace std;



const int maxn = 1e5 + 5;

int dp[maxn][4];

int day[maxn][4];



int main(){

	int n; scanf("%d",&n);

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

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

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

		}

	}



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

		dp[1][i] = day[1][i];

	}



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

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

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

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

	}



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



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



	return 0;

}
// 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.216268799758493x):
#include<cstdio>

#include<iostream>

#include<algorithm>

#include<cmath>

using namespace std;



const int maxn = 1e5 + 5;

int dp[maxn][4];

int day[maxn][4];



int main(){

	int n; scanf("%d",&n);

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

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

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

		}

	}



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

		dp[1][i] = day[1][i];

	}



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

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

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

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

	}



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



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



	return 0;

}
// 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.2977065528464746x):
#include<cstdio>

#include<iostream>

#include<algorithm>

#include<cmath>

using namespace std;



const int maxn = 1e5 + 5;

int dp[maxn][4];

int day[maxn][4];



int main(){

	int n; scanf("%d",&n);

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

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

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

		}

	}



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

		dp[1][i] = day[1][i];

	}



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

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

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

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

	}



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



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



	return 0;

}