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

using namespace std;



const int N=1e5+30;

typedef long long ll;

int n;

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

ll dp[N][5];

ll solve(int i,int last){

    if(i==n)return 0;

    int path1=-1 ,path2=-1,path3=-1;

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

    if(~ret)return ret;

    if(last!=1){

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

    }

    if(last!=2){

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

    }

    if(last!=3){

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

    }

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



}

int main()

{

    memset(dp,-1,sizeof dp);

    cin>>n;

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

    cout<<solve(0,4);



}




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

#include <unordered_map>

#include <vector>

#include <stack>

#include <algorithm>

#include <list>

#include <set>

#include <climits>

#include <cmath>

#define lli long long int

using namespace std ;



void eval(vector<int> &a, vector<int> &b, vector<int> &c, int ind, vector<vector<int> > &dp)

{

	if(ind == 0)

	{

		dp[0][0] = a[0] ;

		dp[0][1] = b[0] ;

		dp[0][2] = c[0] ;



		return ;

	} 



	eval(a,b,c,ind-1,dp) ;



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

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

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



	return ; 

}



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

{

	int n ;

	cin>>n ;



	vector<int> a(n),b(n),c(n) ;

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

	{

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

	}



	vector<vector<int> > dp(n,vector<int> (3,-1)) ;

	eval(a,b,c,n-1,dp) ;

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

	cout<<ans<<endl ;

	return 0;

}
// Slower C++ solution:
/* MaTb3aa */

#include<bits/stdc++.h>

#define ll long long

using namespace std;

int n;

const int N = 100005;

vector<int>v;

ll dp[100005][4];

int A[N], B[N], C[N];

ll solve(int i,int lst) {

	if (i == n)return 0;

	ll& ret = dp[i][lst];

	if (~ret)return ret;

	if (lst != 1)ret = max(ret, solve(i + 1, 1) + A[i]);

	if (lst != 2)ret = max(ret, solve(i + 1, 2) + B[i]);

	if (lst != 3)ret = max(ret, solve(i + 1, 3) + C[i]);

	return ret;

}

int main() {

	cin >> n;

	for (int i = 0; i < n; i++)cin >> A[i] >> B[i] >> C[i];

	memset(dp, -1, sizeof dp);

	cout << solve(0, 0);

	

		 

}

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

#include <unordered_map>

#include <vector>

#include <stack>

#include <algorithm>

#include <list>

#include <set>

#include <climits>

#include <cmath>

#define lli long long int

using namespace std ;



void eval(vector<int> &a, vector<int> &b, vector<int> &c, int ind, vector<vector<int> > &dp)

{

	if(ind == 0)

	{

		dp[0][0] = a[0] ;

		dp[0][1] = b[0] ;

		dp[0][2] = c[0] ;



		return ;

	} 



	eval(a,b,c,ind-1,dp) ;



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

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

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



	return ; 

}



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

{

	int n ;

	cin>>n ;



	vector<int> a(n),b(n),c(n) ;

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

	{

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

	}



	vector<vector<int> > dp(n,vector<int> (3,-1)) ;

	eval(a,b,c,n-1,dp) ;

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

	cout<<ans<<endl ;

	return 0;

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

using namespace std;

int n;

int arr[100005][3];



int dp[100005][4];



int solve(int i, int prev){

	if(i == n) return 0;

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



	dp[i][prev] = -1e7;

	int a = 0, b = 0, c = 0;

	if(prev != 0)

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

	if(prev != 1)

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

	if(prev != 2)

		c = solve(i+1, 2) + arr[i][2];



	dp[i][prev] = max(a, max(b, c));

	return dp[i][prev];

}



int main(){

	cin>>n;

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

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



	memset(dp, -1, sizeof dp);

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

	return 0;

}

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

#include <unordered_map>

#include <vector>

#include <stack>

#include <algorithm>

#include <list>

#include <set>

#include <climits>

#include <cmath>

#define lli long long int

using namespace std ;



void eval(vector<int> &a, vector<int> &b, vector<int> &c, int ind, vector<vector<int> > &dp)

{

	if(ind == 0)

	{

		dp[0][0] = a[0] ;

		dp[0][1] = b[0] ;

		dp[0][2] = c[0] ;



		return ;

	} 



	eval(a,b,c,ind-1,dp) ;



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

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

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



	return ; 

}



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

{

	int n ;

	cin>>n ;



	vector<int> a(n),b(n),c(n) ;

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

	{

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

	}



	vector<vector<int> > dp(n,vector<int> (3,-1)) ;

	eval(a,b,c,n-1,dp) ;

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

	cout<<ans<<endl ;

	return 0;

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

#include<iostream>

#include<functional>

#include <bitset>

#include<algorithm>

#include<fstream>

#include<string>

#include <cstring>

#include<stdio.h>

#include<iomanip>

#include<sstream>

#include<cmath>

#include<vector>

#include<assert.h>

#include<queue>

#include<deque>

#include<stack>

#include<set>

#include<map>

// never lose hope ..

typedef unsigned long long ull;

typedef long long ll;

typedef long double ld;

#define eps 1e-8

using namespace std;

double const  PI = 3.141592653;

const int OO = 1000000000 + 9;



void fast() {

	ios_base::sync_with_stdio(0);

	cin.tie(NULL), cout.tie(NULL);

}

struct s {

	int x, y, z;

};



int n;

int v[(int)1e5][4];

int dp[(int)1e5][4];



int solve(int i = 0, int pre = 4) {

	if (i >= n)return 0;

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

	int ch1, ch2, ch3;

	ch1 = ch2 = ch3 = 0;

	if (pre != 1) {

		ch1 += solve(i + 1, 1) + v[i][1];

	}

	if (pre != 2) {

		ch2 += solve(i + 1, 2) + v[i][2];

	}

	if (pre != 3) {

		ch3 += solve(i + 1, 3) + v[i][3];

	}

	return dp[i][pre] = max({ ch1,ch2,ch3 });

}





int main()

{

	fast();



	cin >> n;

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

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

	}

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

	cout << solve();



	return 0;

}

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

#include <unordered_map>

#include <vector>

#include <stack>

#include <algorithm>

#include <list>

#include <set>

#include <climits>

#include <cmath>

#define lli long long int

using namespace std ;



void eval(vector<int> &a, vector<int> &b, vector<int> &c, int ind, vector<vector<int> > &dp)

{

	if(ind == 0)

	{

		dp[0][0] = a[0] ;

		dp[0][1] = b[0] ;

		dp[0][2] = c[0] ;



		return ;

	} 



	eval(a,b,c,ind-1,dp) ;



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

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

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



	return ; 

}



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

{

	int n ;

	cin>>n ;



	vector<int> a(n),b(n),c(n) ;

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

	{

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

	}



	vector<vector<int> > dp(n,vector<int> (3,-1)) ;

	eval(a,b,c,n-1,dp) ;

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

	cout<<ans<<endl ;

	return 0;

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

#include<bits/stdc++.h>

using namespace std;



typedef long long ll;



int n;

int a[100005][4];

ll dp[100005][4];



ll solve(int d,int l){

    if(d==n+1) return 0;

    if(dp[d][l]!=-1) return dp[d][l];

    ll ret=0;

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

        if(i!=l){

            ret=max(ret, solve(d+1,i)+a[d][i]);

        }

    }

    return dp[d][l]=ret;



}





int main()

{

    cin>>n;

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

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

            cin>>a[i][j];

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

    cout<<solve(1,-1);



    return 0;

}


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

#include <unordered_map>

#include <vector>

#include <stack>

#include <algorithm>

#include <list>

#include <set>

#include <climits>

#include <cmath>

#define lli long long int

using namespace std ;



void eval(vector<int> &a, vector<int> &b, vector<int> &c, int ind, vector<vector<int> > &dp)

{

	if(ind == 0)

	{

		dp[0][0] = a[0] ;

		dp[0][1] = b[0] ;

		dp[0][2] = c[0] ;



		return ;

	} 



	eval(a,b,c,ind-1,dp) ;



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

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

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



	return ; 

}



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

{

	int n ;

	cin>>n ;



	vector<int> a(n),b(n),c(n) ;

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

	{

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

	}



	vector<vector<int> > dp(n,vector<int> (3,-1)) ;

	eval(a,b,c,n-1,dp) ;

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

	cout<<ans<<endl ;

	return 0;

}
// Slower C++ solution:
/* MaTb3aa */

#include<bits/stdc++.h>

#define ll long long

using namespace std;

int n;

const int N = 100005;

vector<int>v;

ll dp[100005][4];

int A[N], B[N], C[N];

ll solve(int i,int lst) {

	if (i == n)return 0;

	ll& ret = dp[i][lst];

	if (~ret)return ret;

	if (lst != 1)ret = max(ret, solve(i + 1, 1) + A[i]);

	if (lst != 2)ret = max(ret, solve(i + 1, 2) + B[i]);

	if (lst != 3)ret = max(ret, solve(i + 1, 3) + C[i]);

	return ret;

}

int main() {

	cin >> n;

	for (int i = 0; i < n; i++)cin >> A[i] >> B[i] >> C[i];

	memset(dp, -1, sizeof dp);

	cout << solve(0, 0);

	

		 

}

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

using namespace std;



typedef long double LD;

typedef long long ll;

typedef map<int,int> mii;

typedef pair<int,int> pii;

typedef vector<pii> vpii;



#define pb              push_back

#define mp              make_pair

#define F               first

#define S               second

#define mod             1000000007

#define INF             2147483647

#define Endl            "\n"

#define FasterIO        ios_base::sync_with_stdio(0);cin.tie(0); cout.tie(0) ;

#define precision(a)    cout << fixed << setprecision(a)

#define bug(a,b)        cout<<a<<"\t"<<b<<"\n";

#define Case(a,b)       cout<<"Case "<<a<<": "<<b<<"\n";

#define reset(a,b)      memset(a,b,sizeof(a));

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



const int MAXN = 1000005;

const int SQRTN = 1003;

const int LOGN = 22;

const double PI=acos(-1);

const int sz=1e5+9;



int strtoint(string str){

    stringstream ss(str);

    int x = 0;

    ss >> x ;

    return x ;

}

string intostr(int x){

    stringstream ss;

    ss << x;

    string str = ss.str();

    return str;

}

void rem(string &s, const string &c) {

	auto pos = s.find(c);

	if (pos == string::npos) {

		cout << -1 << endl;

		exit(0);

	}

	s.erase(0, pos + 1);

}

void println (int n){

    cout<<n<<"\n";

}

void printvec(vector <int> &a){

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

        cout<<a[i]<<" ";

    }

    cout<<"\n";

}



void printgraph(vector <int> g[],int e){

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

        cout<<i<<" --> ";

        for (int j=0;j<g[i].size();j++){

            cout<<g[i][j]<<" ";

        }

        cout<<"\n";

    }

}



void printmap (map <int,int> &mymap){

    for (auto itr = mymap.begin(); itr != mymap.end(); ++itr) {

        cout << '\t' << itr->first

             << '\t' << itr->second << '\n';

    }

    cout << "\n";

}



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

int N;



int rec(int pos, int type){



    if(pos>N) return 0;



    if(dp[pos][type]!=-1) return dp[pos][type];



    int ret = -1;

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

        if(i!=type)

            ret = max (ret, rec(pos+1,i) + a[pos+1][i]);



    return dp[pos][type] = ret;

}





void solve(){

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

    int i,j;

    cin>>N;

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

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

            cin>>a[i][j];





   cout<< rec(0,0) << "\n";



    //printvec(v);

}





int main(){

    //FasterIO;



    solve();

    return 0;

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

#include<bits/stdc++.h>

using namespace std;



typedef long long ll;



int n;

int a[100005][4];

ll dp[100005][4];



ll solve(int d,int l){

    if(d==n+1) return 0;

    if(dp[d][l]!=-1) return dp[d][l];

    ll ret=0;

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

        if(i!=l){

            ret=max(ret, solve(d+1,i)+a[d][i]);

        }

    }

    return dp[d][l]=ret;



}





int main()

{

    cin>>n;

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

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

            cin>>a[i][j];

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

    cout<<solve(1,-1);



    return 0;

}


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

using namespace std;



typedef long double LD;

typedef long long ll;

typedef map<int,int> mii;

typedef pair<int,int> pii;

typedef vector<pii> vpii;



#define pb              push_back

#define mp              make_pair

#define F               first

#define S               second

#define mod             1000000007

#define INF             2147483647

#define Endl            "\n"

#define FasterIO        ios_base::sync_with_stdio(0);cin.tie(0); cout.tie(0) ;

#define precision(a)    cout << fixed << setprecision(a)

#define bug(a,b)        cout<<a<<"\t"<<b<<"\n";

#define Case(a,b)       cout<<"Case "<<a<<": "<<b<<"\n";

#define reset(a,b)      memset(a,b,sizeof(a));

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



const int MAXN = 1000005;

const int SQRTN = 1003;

const int LOGN = 22;

const double PI=acos(-1);

const int sz=1e5+9;



int strtoint(string str){

    stringstream ss(str);

    int x = 0;

    ss >> x ;

    return x ;

}

string intostr(int x){

    stringstream ss;

    ss << x;

    string str = ss.str();

    return str;

}

void rem(string &s, const string &c) {

	auto pos = s.find(c);

	if (pos == string::npos) {

		cout << -1 << endl;

		exit(0);

	}

	s.erase(0, pos + 1);

}

void println (int n){

    cout<<n<<"\n";

}

void printvec(vector <int> &a){

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

        cout<<a[i]<<" ";

    }

    cout<<"\n";

}



void printgraph(vector <int> g[],int e){

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

        cout<<i<<" --> ";

        for (int j=0;j<g[i].size();j++){

            cout<<g[i][j]<<" ";

        }

        cout<<"\n";

    }

}



void printmap (map <int,int> &mymap){

    for (auto itr = mymap.begin(); itr != mymap.end(); ++itr) {

        cout << '\t' << itr->first

             << '\t' << itr->second << '\n';

    }

    cout << "\n";

}



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

int N;



int rec(int pos, int type){



    if(pos>N) return 0;



    if(dp[pos][type]!=-1) return dp[pos][type];



    int ret = -1;

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

        if(i!=type)

            ret = max (ret, rec(pos+1,i) + a[pos+1][i]);



    return dp[pos][type] = ret;

}





void solve(){

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

    int i,j;

    cin>>N;

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

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

            cin>>a[i][j];





   cout<< rec(0,0) << "\n";



    //printvec(v);

}





int main(){

    //FasterIO;



    solve();

    return 0;

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

using namespace std;



long long w1,w2,hap=0,maxi,aa,bb,cc,N,i,j,k,a[100005][5];

long long M[100005][5];





int main()

{

    ios_base::sync_with_stdio(false);

    cin.tie(NULL);

    cin>>N;

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

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

            M[i][j]=0;



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

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

    M[N-1][0]=a[N-1][0];

    M[N-1][1]=a[N-1][1];

    M[N-1][2]=a[N-1][2];



    for(i=N-2; i>=0; i--)

    {

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

        {

            if(j==0)

            {

                w1=1;

                w2=2;

            }

            else if(j==1)

            {

                w1=0;

                w2=2;

            }

            else

            {

                w1=0;

                w2=1;

            }

        M[i][j]=a[i][j]+max(M[i+1][w1],M[i+1][w2]);

        }

    }

    aa=max(M[0][0],M[0][1]);

    aa=max(aa,M[0][2]);

    cout<<aa<<endl;

    return 0;

}






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

using namespace std;

const int inf=0x3f3f3f3f;

void A() {

    int n;

    cin>>n;

    vector<int> a(n,0);

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

        cin>>a[i];

    vector<int> dp(n+1,inf);

    dp[0]=0;

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

        dp[i]=min(dp[i],dp[i-1]+abs(a[i]-a[i-1]));

        if(i>=2)

            dp[i]=min(dp[i],dp[i-2]+abs(a[i]-a[i-2]));

    }

    cout<<dp[n-1]<<endl;



}

void B() {

    int n,k;

    cin>>n>>k;

    vector<int> dp(n+1,inf);

    vector<int> h(n,0);

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

        cin>>h[i];

    dp[0]=0;

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

        for(int len=1; len<=min(k+1,i+1); len++) {

            int j=i-len+1;

            dp[i]=min(dp[i],dp[j]+abs(h[j]-h[i]));

        }

    }

    cout<<dp[n-1]<<endl;

}

void C() {

    int n;

    cin>>n;

    vector<vector<int>> a(n,vector<int>(3,0));

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

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

    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++) {

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

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

                if(j==k)

                    continue;

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

                //cout<<dp[i][j]<<endl;

            }

        }

    }

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

}

int main() {

    ios::sync_with_stdio(false);

    cin.tie(0);

    C();

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

using namespace std;

#define INF 100005

#define maxx 1000000000000



int n;

long long a[INF][3];

long long mem[INF][3];



long long rec(int level,int pre)

{

    if(level==n) return 0;

    if(mem[level][pre]!=-1) return mem[level][pre];

    long long now=0;

    if(pre==0)

    {

        now=max(now,max(a[level][0]+rec(level+1,1),a[level][0]+rec(level+1,2)));

    }else

    if(pre==1)

    {

        now=max(now,max(a[level][1]+rec(level+1,0),a[level][1]+rec(level+1,2)));

    }else

    if(pre==2)

    {

        now=max(now,max(a[level][2]+rec(level+1,0),a[level][2]+rec(level+1,1)));

    }

    return mem[level][pre]=now;

}



int main()

{

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

    scanf("%d",&n);

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

    {

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

    }

    long long ans=max(rec(0,0),max(rec(0,1),rec(0,2)));

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

}


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

using namespace std;

const int inf=0x3f3f3f3f;

void A() {

    int n;

    cin>>n;

    vector<int> a(n,0);

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

        cin>>a[i];

    vector<int> dp(n+1,inf);

    dp[0]=0;

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

        dp[i]=min(dp[i],dp[i-1]+abs(a[i]-a[i-1]));

        if(i>=2)

            dp[i]=min(dp[i],dp[i-2]+abs(a[i]-a[i-2]));

    }

    cout<<dp[n-1]<<endl;



}

void B() {

    int n,k;

    cin>>n>>k;

    vector<int> dp(n+1,inf);

    vector<int> h(n,0);

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

        cin>>h[i];

    dp[0]=0;

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

        for(int len=1; len<=min(k+1,i+1); len++) {

            int j=i-len+1;

            dp[i]=min(dp[i],dp[j]+abs(h[j]-h[i]));

        }

    }

    cout<<dp[n-1]<<endl;

}

void C() {

    int n;

    cin>>n;

    vector<vector<int>> a(n,vector<int>(3,0));

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

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

    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++) {

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

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

                if(j==k)

                    continue;

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

                //cout<<dp[i][j]<<endl;

            }

        }

    }

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

}

int main() {

    ios::sync_with_stdio(false);

    cin.tie(0);

    C();

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

#include<algorithm>

#include<cstring>

using namespace std;

const int maxn = 1e5 + 5;



int a[maxn][4];

int dp[maxn][4];//d[i][j] i day choose j thing 's max happy



int main(){

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

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

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

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

		}

	}



	memset(dp, 0, sizeof dp);



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



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

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

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

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

	}



	int ans = 0;

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

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

}

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

using namespace std;

const int inf=0x3f3f3f3f;

void A() {

    int n;

    cin>>n;

    vector<int> a(n,0);

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

        cin>>a[i];

    vector<int> dp(n+1,inf);

    dp[0]=0;

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

        dp[i]=min(dp[i],dp[i-1]+abs(a[i]-a[i-1]));

        if(i>=2)

            dp[i]=min(dp[i],dp[i-2]+abs(a[i]-a[i-2]));

    }

    cout<<dp[n-1]<<endl;



}

void B() {

    int n,k;

    cin>>n>>k;

    vector<int> dp(n+1,inf);

    vector<int> h(n,0);

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

        cin>>h[i];

    dp[0]=0;

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

        for(int len=1; len<=min(k+1,i+1); len++) {

            int j=i-len+1;

            dp[i]=min(dp[i],dp[j]+abs(h[j]-h[i]));

        }

    }

    cout<<dp[n-1]<<endl;

}

void C() {

    int n;

    cin>>n;

    vector<vector<int>> a(n,vector<int>(3,0));

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

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

    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++) {

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

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

                if(j==k)

                    continue;

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

                //cout<<dp[i][j]<<endl;

            }

        }

    }

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

}

int main() {

    ios::sync_with_stdio(false);

    cin.tie(0);

    C();

}