stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#define __USE_MINGW_ANSI_STDIO

#define _CRT_SECURE_NO_WARNINGS

#include <bits/stdc++.h>

#include <unordered_set>

#include <unordered_map>



using namespace std;



typedef long long ll;

typedef long long ull;

typedef pair<ll, ll> ii;



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

#define sz(v)		((int)((v).size()))

#define endl		"\n"

#define fx(n)		fixed<<setprecision(n)

#define mk			make_pair	



void File()

{

#ifndef ONLINE_JUDGE

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

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

#endif

#ifdef ONLINE_JUDGE

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

	freopen("pyramid.in", "r", stdin);*/

#endif

}

void fast()

{

	ios::sync_with_stdio(NULL);

	cout.tie(NULL);

	cin.tie(NULL);

	//File();

}

const double pi = 2 * acos(0.0);

const ll oo = 0x3f3f3f3f;

const int MOD = 1e9 + 7;

const int nn = 1e5 + 15;

int dx[8] = { 1, -1, 0, 0, 1, -1, 1, -1 };

int dy[8] = { 0, 0, 1, -1, 1, -1, -1, 1 };



int n, k;

vector<pair<int, pair<int, int>>>v;

ll dp[nn][4];

ll solve(int idx, int last)

{

	if (idx >= n)

		return 0;

	ll &ret = dp[idx][last];

	if (~ret)

		return ret;

	ret = 0;

	if (last != 1)

		ret = max(ret, solve(idx + 1, 1) + v[idx].first);



	if (last != 2)

		ret = max(ret, solve(idx + 1, 2) + v[idx].second.first);



	if (last != 3)

		ret = max(ret, solve(idx + 1, 3) + v[idx].second.second);



	return ret;

}

int main()

{

	fast();

	cin >> n;

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

	{

		int x, y, z;

		cin >> x >> y >> z;

		v.push_back(mk(x, mk(y, z)));

	}

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

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

	return 0;

}

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

using namespace std;

#define MAXN 100000

#define INF 2000000000

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

int dpa[MAXN+1],dpb[MAXN+1],dpc[MAXN+1];

int n,k;

int main() {

	int i;

	int j;

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

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

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

	}

	dpa[n]=dpb[n]=dpc[n]=0;

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

		dpa[i]=a[i]+max(dpb[i+1],dpc[i+1]);

		dpb[i]=b[i]+max(dpa[i+1],dpc[i+1]);

		dpc[i]=c[i]+max(dpa[i+1],dpb[i+1]);

	}

	printf("%d\n",max(dpa[0], max(dpb[0], dpc[0])));

	return 0;

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

#define ull unsigned long long

#define ll long long

#define pb push_back

#define endl '\n'

#define line cout<<"----------------------"<<endl

#define mp make_pair

#define mod 1000000007

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

#define filein freopen("input.txt","r",stdin)

#define fileout freopen("output.txt","w",stdout)

using namespace std;

const ll  mx=300009;

struct edge

{

    ll  a,b,c;

};

vector<edge> v;

ll  dp[100009][4];

ll  call(ll  ind, ll  pre)

{

    if(ind==v.size()) return 0;

    ll  &ret=dp[ind][pre];

    if(ret!=-1) return ret;

    ret=0;

    ll  a=0,b=0,c=0;

    auto x=v[ind];

        if(pre==1)

        {

            a=x.b+call(ind+1,2);

            b=x.c+call(ind+1,3);

        }

        else if(pre==2)

        {

            a=x.a+call(ind+1,1);

            b=x.c+call(ind+1,3);

        }

        else if(pre==3)

        {

            a=x.a+call(ind+1,1);

            b=x.b+call(ind+1,2);

        }

        else

        {

            a=x.a+call(ind+1,1);

            b=x.b+call(ind+1,2);

            c=x.c+call(ind+1,3);

        }

    return ret=max(a,max(b,c));

}

int  main()

{

    ll  n ;

    cin>>n;

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

    {

        ll  a,b,c; cin>>a>>b>>c;

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

    }

    memset(dp,-1,sizeof dp);

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







}


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

using namespace std;

#define MAXN 100000

#define INF 2000000000

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

int dpa[MAXN+1],dpb[MAXN+1],dpc[MAXN+1];

int n,k;

int main() {

	int i;

	int j;

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

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

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

	}

	dpa[n]=dpb[n]=dpc[n]=0;

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

		dpa[i]=a[i]+max(dpb[i+1],dpc[i+1]);

		dpb[i]=b[i]+max(dpa[i+1],dpc[i+1]);

		dpc[i]=c[i]+max(dpa[i+1],dpb[i+1]);

	}

	printf("%d\n",max(dpa[0], max(dpb[0], dpc[0])));

	return 0;

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

#define ll long long

using namespace std;



ll n;

std::vector<ll> ara[100010];

ll dp[100010][4];



ll fnc(ll idx,ll last){

  if(idx == n) return 0;

  if(dp[idx][last] != -1) return dp[idx][last];



  ll res1 = - (1<<25);

  ll res2 = - (1<<25);

  ll res3 = - (1<<25);



  if(last == 0){

    res1 = max(res1,ara[idx][0] + fnc(idx+1,1));

    res2 = max(res2,ara[idx][1] + fnc(idx+1,2));

    res3 = max(res3,ara[idx][2] + fnc(idx+1,3));

  }else if(last == 1){

    res2 = max(res2,ara[idx][1] + fnc(idx+1,2));

    res3 = max(res3,ara[idx][2] + fnc(idx+1,3));

  }else if(last == 2){

    res1 = max(res1,ara[idx][0] + fnc(idx+1,1));

    res3 = max(res3,ara[idx][2] + fnc(idx+1,3));

  }else if(last == 3){

    res1 = max(res1,ara[idx][0] + fnc(idx+1,1));

    res2 = max(res2,ara[idx][1] + fnc(idx+1,2));

  }

  dp[idx][last] = max(res1, max(res2,res3));

  return dp[idx][last];

}



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

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

  cin >> n;

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

    int a,b,c;

    cin >> a >> b >> c;

    ara[i].push_back(a);

    ara[i].push_back(b);

    ara[i].push_back(c);

  }



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

  return 0;

}


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

using namespace std;

#define MAXN 100000

#define INF 2000000000

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

int dpa[MAXN+1],dpb[MAXN+1],dpc[MAXN+1];

int n,k;

int main() {

	int i;

	int j;

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

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

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

	}

	dpa[n]=dpb[n]=dpc[n]=0;

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

		dpa[i]=a[i]+max(dpb[i+1],dpc[i+1]);

		dpb[i]=b[i]+max(dpa[i+1],dpc[i+1]);

		dpc[i]=c[i]+max(dpa[i+1],dpb[i+1]);

	}

	printf("%d\n",max(dpa[0], max(dpb[0], dpc[0])));

	return 0;

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



using namespace std;

typedef long long int lli;



lli n, a[100005], b[100005], c[100005];



lli dp[100005][4];



lli solve(lli i, lli last){

    if(i == n) return dp[i][last] = 0;

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

    lli ans1, ans2, ans3;

    ans1 = ans2 = ans3 = LONG_LONG_MIN;

    if(last != 1) ans1 = a[i] + solve(i+1, 1);

    if(last != 2) ans2 = b[i] + solve(i+1, 2);

    if(last != 3) ans3 = c[i] + solve(i+1, 3);



    return dp[i][last] = max(ans1, max(ans2, ans3));

}





int main(){

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

    int T=1, cs=1;

    //scanf("%d", &T);

    while(T--){

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

        scanf("%lld", &n);

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

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

        }

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



    }



    return 0;

}

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

using namespace std;

#define MAXN 100000

#define INF 2000000000

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

int dpa[MAXN+1],dpb[MAXN+1],dpc[MAXN+1];

int n,k;

int main() {

	int i;

	int j;

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

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

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

	}

	dpa[n]=dpb[n]=dpc[n]=0;

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

		dpa[i]=a[i]+max(dpb[i+1],dpc[i+1]);

		dpb[i]=b[i]+max(dpa[i+1],dpc[i+1]);

		dpc[i]=c[i]+max(dpa[i+1],dpb[i+1]);

	}

	printf("%d\n",max(dpa[0], max(dpb[0], dpc[0])));

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



using namespace std;



int choose(const int &day, const int &last_activity, const int activities[][3], const int &n, int memo[][4]){

    int ans;

    if(day == n){

        ans = 0;

    }

    else{

        if(memo[day][last_activity] != -1){

            ans = memo[day][last_activity];

        }

        else if(last_activity == 0){

            ans = memo[day][0] = max(activities[day][0] + choose(day + 1, 1, activities, n, memo), max(activities[day][1] + choose(day + 1, 2, activities, n, memo), activities[day][2] + choose(day + 1, 3, activities, n, memo)));

        }

        else if(last_activity == 1){

            ans = memo[day][1] = max(activities[day][1] + choose(day + 1, 2, activities, n, memo), activities[day][2] + choose(day + 1, 3, activities, n, memo));

        }

        else if(last_activity == 2){

            ans = memo[day][2] = max(activities[day][0] + choose(day + 1, 1, activities, n, memo), activities[day][2] + choose(day + 1, 3, activities, n, memo));

        }

        else{

            ans = memo[day][3] = max(activities[day][0] + choose(day + 1, 1, activities, n, memo), activities[day][1] + choose(day + 1, 2, activities, n, memo));

        }

    }

    return ans;

}



int solve(const int activities[][3], const int &n, int memo[][4]){

    return choose(0, 0, activities, n, memo);

}



int main(){

    int n;

    while(scanf("%d", &n) != EOF){

        int activities[n][3], memo[n][4];

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

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

            memo[i][0] = -1; memo[i][1] = -1; memo[i][2] = -1; memo[i][3] = -1;

        }

        printf("%d\n", solve(activities, n, memo));

    }

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



using namespace std;



int choose(const int &day, const int &last_activity, const int activities[][3], const int &n, int memo[][4]){

    int ans;

    if(day == n){

        ans = 0;

    }

    else{

        if(memo[day][last_activity] != -1){

            ans = memo[day][last_activity];

        }

        else if(last_activity == 0){

            ans = memo[day][0] = max(activities[day][0] + choose(day + 1, 1, activities, n, memo), max(activities[day][1] + choose(day + 1, 2, activities, n, memo), activities[day][2] + choose(day + 1, 3, activities, n, memo)));

        }

        else if(last_activity == 1){

            ans = memo[day][1] = max(activities[day][1] + choose(day + 1, 2, activities, n, memo), activities[day][2] + choose(day + 1, 3, activities, n, memo));

        }

        else if(last_activity == 2){

            ans = memo[day][2] = max(activities[day][0] + choose(day + 1, 1, activities, n, memo), activities[day][2] + choose(day + 1, 3, activities, n, memo));

        }

        else{

            ans = memo[day][3] = max(activities[day][0] + choose(day + 1, 1, activities, n, memo), activities[day][1] + choose(day + 1, 2, activities, n, memo));

        }

    }

    return ans;

}



int solve(const int activities[][3], const int &n, int memo[][4]){

    return choose(0, 0, activities, n, memo);

}



int main(){

    int n;

    while(scanf("%d", &n) != EOF){

        int activities[n][3], memo[n][4];

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

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

            memo[i][0] = -1; memo[i][1] = -1; memo[i][2] = -1; memo[i][3] = -1;

        }

        printf("%d\n", solve(activities, n, memo));

    }

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



using namespace std;



int choose(const int &day, const int &last_activity, const int activities[][3], const int &n, int memo[][4]){

    int ans;

    if(day == n){

        ans = 0;

    }

    else{

        if(memo[day][last_activity] != -1){

            ans = memo[day][last_activity];

        }

        else if(last_activity == 0){

            ans = memo[day][0] = max(activities[day][0] + choose(day + 1, 1, activities, n, memo), max(activities[day][1] + choose(day + 1, 2, activities, n, memo), activities[day][2] + choose(day + 1, 3, activities, n, memo)));

        }

        else if(last_activity == 1){

            ans = memo[day][1] = max(activities[day][1] + choose(day + 1, 2, activities, n, memo), activities[day][2] + choose(day + 1, 3, activities, n, memo));

        }

        else if(last_activity == 2){

            ans = memo[day][2] = max(activities[day][0] + choose(day + 1, 1, activities, n, memo), activities[day][2] + choose(day + 1, 3, activities, n, memo));

        }

        else{

            ans = memo[day][3] = max(activities[day][0] + choose(day + 1, 1, activities, n, memo), activities[day][1] + choose(day + 1, 2, activities, n, memo));

        }

    }

    return ans;

}



int solve(const int activities[][3], const int &n, int memo[][4]){

    return choose(0, 0, activities, n, memo);

}



int main(){

    int n;

    while(scanf("%d", &n) != EOF){

        int activities[n][3], memo[n][4];

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

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

            memo[i][0] = -1; memo[i][1] = -1; memo[i][2] = -1; memo[i][3] = -1;

        }

        printf("%d\n", solve(activities, n, memo));

    }

    return 0;

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



using namespace std;



int choose(const int &day, const int &last_activity, const int activities[][3], const int &n, int memo[][4]){

    int ans;

    if(day == n){

        ans = 0;

    }

    else{

        if(memo[day][last_activity] != -1){

            ans = memo[day][last_activity];

        }

        else if(last_activity == 0){

            ans = memo[day][0] = max(activities[day][0] + choose(day + 1, 1, activities, n, memo), max(activities[day][1] + choose(day + 1, 2, activities, n, memo), activities[day][2] + choose(day + 1, 3, activities, n, memo)));

        }

        else if(last_activity == 1){

            ans = memo[day][1] = max(activities[day][1] + choose(day + 1, 2, activities, n, memo), activities[day][2] + choose(day + 1, 3, activities, n, memo));

        }

        else if(last_activity == 2){

            ans = memo[day][2] = max(activities[day][0] + choose(day + 1, 1, activities, n, memo), activities[day][2] + choose(day + 1, 3, activities, n, memo));

        }

        else{

            ans = memo[day][3] = max(activities[day][0] + choose(day + 1, 1, activities, n, memo), activities[day][1] + choose(day + 1, 2, activities, n, memo));

        }

    }

    return ans;

}



int solve(const int activities[][3], const int &n, int memo[][4]){

    return choose(0, 0, activities, n, memo);

}



int main(){

    int n;

    while(scanf("%d", &n) != EOF){

        int activities[n][3], memo[n][4];

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

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

            memo[i][0] = -1; memo[i][1] = -1; memo[i][2] = -1; memo[i][3] = -1;

        }

        printf("%d\n", solve(activities, n, memo));

    }

    return 0;

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



using namespace std;



int choose(const int &day, const int &last_activity, const int activities[][3], const int &n, int memo[][4]){

    int ans;

    if(day == n){

        ans = 0;

    }

    else{

        if(memo[day][last_activity] != -1){

            ans = memo[day][last_activity];

        }

        else if(last_activity == 0){

            ans = memo[day][0] = max(activities[day][0] + choose(day + 1, 1, activities, n, memo), max(activities[day][1] + choose(day + 1, 2, activities, n, memo), activities[day][2] + choose(day + 1, 3, activities, n, memo)));

        }

        else if(last_activity == 1){

            ans = memo[day][1] = max(activities[day][1] + choose(day + 1, 2, activities, n, memo), activities[day][2] + choose(day + 1, 3, activities, n, memo));

        }

        else if(last_activity == 2){

            ans = memo[day][2] = max(activities[day][0] + choose(day + 1, 1, activities, n, memo), activities[day][2] + choose(day + 1, 3, activities, n, memo));

        }

        else{

            ans = memo[day][3] = max(activities[day][0] + choose(day + 1, 1, activities, n, memo), activities[day][1] + choose(day + 1, 2, activities, n, memo));

        }

    }

    return ans;

}



int solve(const int activities[][3], const int &n, int memo[][4]){

    return choose(0, 0, activities, n, memo);

}



int main(){

    int n;

    while(scanf("%d", &n) != EOF){

        int activities[n][3], memo[n][4];

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

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

            memo[i][0] = -1; memo[i][1] = -1; memo[i][2] = -1; memo[i][3] = -1;

        }

        printf("%d\n", solve(activities, n, memo));

    }

    return 0;

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



using namespace std;



int choose(const int &day, const int &last_activity, const int activities[][3], const int &n, int memo[][4]){

    int ans;

    if(day == n){

        ans = 0;

    }

    else{

        if(memo[day][last_activity] != -1){

            ans = memo[day][last_activity];

        }

        else if(last_activity == 0){

            ans = memo[day][0] = max(activities[day][0] + choose(day + 1, 1, activities, n, memo), max(activities[day][1] + choose(day + 1, 2, activities, n, memo), activities[day][2] + choose(day + 1, 3, activities, n, memo)));

        }

        else if(last_activity == 1){

            ans = memo[day][1] = max(activities[day][1] + choose(day + 1, 2, activities, n, memo), activities[day][2] + choose(day + 1, 3, activities, n, memo));

        }

        else if(last_activity == 2){

            ans = memo[day][2] = max(activities[day][0] + choose(day + 1, 1, activities, n, memo), activities[day][2] + choose(day + 1, 3, activities, n, memo));

        }

        else{

            ans = memo[day][3] = max(activities[day][0] + choose(day + 1, 1, activities, n, memo), activities[day][1] + choose(day + 1, 2, activities, n, memo));

        }

    }

    return ans;

}



int solve(const int activities[][3], const int &n, int memo[][4]){

    return choose(0, 0, activities, n, memo);

}



int main(){

    int n;

    while(scanf("%d", &n) != EOF){

        int activities[n][3], memo[n][4];

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

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

            memo[i][0] = -1; memo[i][1] = -1; memo[i][2] = -1; memo[i][3] = -1;

        }

        printf("%d\n", solve(activities, n, memo));

    }

    return 0;

}