stablegradients/pie-gem5-bysrc huggingface dataset:
// Original C++ solution (slower):
#include <iostream>

#include <cstdio>

#include <cstring>

#include <algorithm>

#include <cmath>

using namespace std;

int n,a[1000010],b[1000010], c[1000010],dp[1000010][5];

int main()

{

    cin>>n;

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

    {

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

    }

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

    dp[1][2]=b[1];

    dp[1][3]=c[1];

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

    {

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

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

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

    }

    cout<<max(dp[n][3],max(dp[n][1],dp[n][2]));

    return 0;

}

// Optimized solution to the same problem (faster):
#include <cstdio>

#include <algorithm>

using std::max;

const int maxn = 100003;

int d[maxn][3],a[maxn][3];

int main() {

	int i,n;

    scanf("%d", &n);

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

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

    }

    d[0][0] = a[0][0];

	d[0][1] = a[0][1];

	d[0][2] = a[0][2];

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

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

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

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

	}

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

	return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;

#define ll long long

#define ull unsigned long long

#define mst(a, b) memset((a), (b), sizeof(a))

#define mp(a, b) make_pair(a, b)

#define pi acos(-1)

#define pii pair<int, int>

#define pb push_back

const int INF = 0x3f3f3f3f;

const double eps = 1e-10;

const int maxn = 1e5 + 107;

const int maxm = 5e5 + 10;

int s[maxn][3];

int dp[maxn][3];

int main()

{

    ios::sync_with_stdio(0);

    cin.tie(0);

    cout.tie(0);

    int n;

    cin >> n;

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

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

    dp[0][0]=dp[0][1]=dp[0][2]=0;

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

    {

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

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

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

    }

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

    return 0;

}

// Optimized solution to the same problem (faster):
#include <cstdio>

#include <algorithm>

using std::max;

const int maxn = 100003;

int d[maxn][3],a[maxn][3];

int main() {

	int i,n;

    scanf("%d", &n);

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

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

    }

    d[0][0] = a[0][0];

	d[0][1] = a[0][1];

	d[0][2] = a[0][2];

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

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

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

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

	}

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

	return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;



const int nmax =100010;



int act[3];

int dp[nmax][3];



int main()

{

    ios_base::sync_with_stdio(0);

    cin.tie(0);



    int n;cin>>n;

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

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

    {

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

        dp[i][0]=max(b + act[0], c + act[0]);

        dp[i][1]=max(a + act[1], c + act[1]);

        dp[i][2]=max(a + act[2], b + act[2]);

        a = dp[i][0];

        b = dp[i][1];

        c = dp[i][2];

    }

    int sol = max({ dp[n][0],dp[n][1],dp[n][2] });

    cout<<sol;



    return 0;

}


// Optimized solution to the same problem (faster):
#include <cstdio>

#include <algorithm>

using std::max;

const int maxn = 100003;

int d[maxn][3],a[maxn][3];

int main() {

	int i,n;

    scanf("%d", &n);

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

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

    }

    d[0][0] = a[0][0];

	d[0][1] = a[0][1];

	d[0][2] = a[0][2];

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

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

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

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

	}

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

	return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
# include <bits/stdc++.h>



using namespace std;



int dp[100007][5], a[100007][5];



int main() {

	int n;

	cin >> n;

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

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

	}  

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

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

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

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

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

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

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

	}

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

}

// Optimized solution to the same problem (faster):
#include <cstdio>

#include <algorithm>

using std::max;

const int maxn = 100003;

int d[maxn][3],a[maxn][3];

int main() {

	int i,n;

    scanf("%d", &n);

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

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

    }

    d[0][0] = a[0][0];

	d[0][1] = a[0][1];

	d[0][2] = a[0][2];

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

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

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

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

	}

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

	return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>



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

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

#define fore(i, l, r) for (int i = int(l); i <= int(r); ++i)

#define ford(i, n) for (int i = int(n)-1; i >= 0; --i)

#define pb push_back

#define eb emplace_back

#define ms(a, x) memset(a, x, sizeof(a))

#define F first

#define S second

#define endl '\n'

#define de(x) cout<<#x<<" = "<<(x)<<endl

#define de2(x,y) cout<<#x<<" = "<<(x) <<' '<< #y<<" = "<<y<<endl;



using namespace std;



typedef long long ll;

typedef pair<int, int> pii;

const int INF = 0x3f3f3f3f;

mt19937 gen(chrono::high_resolution_clock::now().time_since_epoch().count());



const int N=1e5+5;

int a[N][3];

int dp[N][4];

int n;

int dfs(int x,int la){

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

	if(dp[x][la]) return dp[x][la];

	int res=0;

	forn(i,3){

		if(i==la) continue;

		res=max(res,a[x][i]+dfs(x+1,i));

	}

	return dp[x][la]=res;

}

int main() {

    ios::sync_with_stdio(false);

    cin.tie(nullptr);

	cin>>n;

	for1(i,n){

		forn(j,3) cin>>a[i][j];

	}

	cout<<dfs(1,4);

    return 0;

}




// Optimized solution to the same problem (faster):
#include <cstdio>

#include <algorithm>

using std::max;

const int maxn = 100003;

int d[maxn][3],a[maxn][3];

int main() {

	int i,n;

    scanf("%d", &n);

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

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

    }

    d[0][0] = a[0][0];

	d[0][1] = a[0][1];

	d[0][2] = a[0][2];

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

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

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

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

	}

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

	return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):


#include <iostream>

#include <bits/stdc++.h>



using namespace std;

int a[100005][5], p[100005][5];



int main()

{

    int n, i, j;

    cin>>n;

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

    {

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

        {

            cin>>a[i][j];

        }

    }

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

    {

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

        {

            if(i==0)

            {

                p[i][j]=a[i][j];

            }

            else if(j==0)

            {

                p[i][j]=a[i][j]+max(p[i-1][1],p[i-1][2]);

            }

            else if(j==1)

            {

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

            }

            else if(j==2)

            {

                p[i][j]=a[i][j]+max(p[i-1][1],p[i-1][0]);

            }

        }

    }

    n--;

    cout<<max(p[n][0],max(p[n][1],p[n][2]))<<"\n";



    return 0;

}


// Optimized solution to the same problem (faster):
#include <cstdio>

#include <algorithm>

using std::max;

const int maxn = 100003;

int d[maxn][3],a[maxn][3];

int main() {

	int i,n;

    scanf("%d", &n);

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

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

    }

    d[0][0] = a[0][0];

	d[0][1] = a[0][1];

	d[0][2] = a[0][2];

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

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

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

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

	}

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

	return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

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

#define mp make_pair

using namespace std;



using ll = long long;

using ii = pair<int, int>;

const int INF = (int)10e9;

int dp[3][100005];



int main() {

    std::ios::sync_with_stdio(false);

    cin.tie(NULL);

    cout.tie(NULL);



    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];

    }



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

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

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

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

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

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

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

    }

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



    return 0;

}

// Optimized solution to the same problem (faster):
#include <cstdio>

#include <algorithm>

using std::max;

const int maxn = 100003;

int d[maxn][3],a[maxn][3];

int main() {

	int i,n;

    scanf("%d", &n);

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

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

    }

    d[0][0] = a[0][0];

	d[0][1] = a[0][1];

	d[0][2] = a[0][2];

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

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

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

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

	}

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

	return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

    

using namespace std;

     

int main(){  

    int n;

    cin >> n;

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

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

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

            cin >> h[i][j];

        }

    }

 

    vector<array<long long,3>> dp(n);

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

        dp[0][j] = h[0][j];

    }

    

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

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

            dp[i][j] = 0;

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

                if (j == k) continue;

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

            }

        }

    }

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

}

// Optimized solution to the same problem (faster):
#include <cstdio>

#include <algorithm>

using std::max;

const int maxn = 100003;

int d[maxn][3],a[maxn][3];

int main() {

	int i,n;

    scanf("%d", &n);

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

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

    }

    d[0][0] = a[0][0];

	d[0][1] = a[0][1];

	d[0][2] = a[0][2];

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

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

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

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

	}

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

	return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
#include <cstdio>

#include <algorithm>

using std::max;

const int maxn = 100003;

int d[maxn][3],a[maxn][3];

int main() {

	int i,n;

    scanf("%d", &n);

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

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

    }

    d[0][0] = a[0][0];

	d[0][1] = a[0][1];

	d[0][2] = a[0][2];

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

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

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

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

	}

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

	return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}


// Optimized solution to the same problem (faster):
#include <cstdio>

#include <algorithm>

using std::max;

const int maxn = 100003;

int d[maxn][3],a[maxn][3];

int main() {

	int i,n;

    scanf("%d", &n);

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

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

    }

    d[0][0] = a[0][0];

	d[0][1] = a[0][1];

	d[0][2] = a[0][2];

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

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

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

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

	}

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

	return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<algorithm>

int main(){

  int j=1,x,da[3][100001],fun[3][2];

  scanf("%d",&x);

  fun[0][0] = fun[1][0] = fun[2][0] = 0;

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

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

    fun[0][j] = std::max(fun[1][j^1],fun[2][j^1]) + da[0][i];

    fun[1][j] = std::max(fun[0][j^1],fun[2][j^1]) + da[1][i];

    fun[2][j] = std::max(fun[1][j^1],fun[0][j^1]) + da[2][i];

    j^=1;

  }

  j^=1;

  printf("%d\n",std::max(std::max(fun[0][j],fun[1][j]),fun[2][j]));

  

  return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

	

}

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<algorithm>

int main(){

  int j=1,x,da[3][100001],fun[3][2];

  scanf("%d",&x);

  fun[0][0] = fun[1][0] = fun[2][0] = 0;

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

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

    fun[0][j] = std::max(fun[1][j^1],fun[2][j^1]) + da[0][i];

    fun[1][j] = std::max(fun[0][j^1],fun[2][j^1]) + da[1][i];

    fun[2][j] = std::max(fun[1][j^1],fun[0][j^1]) + da[2][i];

    j^=1;

  }

  j^=1;

  printf("%d\n",std::max(std::max(fun[0][j],fun[1][j]),fun[2][j]));

  

  return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}


// Optimized solution to the same problem (faster):
#include<cstdio>

#include<algorithm>

int main(){

  int j=1,x,da[3][100001],fun[3][2];

  scanf("%d",&x);

  fun[0][0] = fun[1][0] = fun[2][0] = 0;

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

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

    fun[0][j] = std::max(fun[1][j^1],fun[2][j^1]) + da[0][i];

    fun[1][j] = std::max(fun[0][j^1],fun[2][j^1]) + da[1][i];

    fun[2][j] = std::max(fun[1][j^1],fun[0][j^1]) + da[2][i];

    j^=1;

  }

  j^=1;

  printf("%d\n",std::max(std::max(fun[0][j],fun[1][j]),fun[2][j]));

  

  return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
/**

     **author : _KMS_

     **created : 01-04-2020

**/

#include<bits/stdc++.h>

using namespace std;

const long long M =3e5+10,MOD=1e9+7;

typedef long long ll;

vector<int>v[M];

bool vis[M];

int dp[M][7];

int arr[10];

int main()

{

    ios::sync_with_stdio(false);

    cin.tie(0);

    int t,n;

    cin>>n;

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

    {

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

      {

        cin>>arr[j];

       for(int k=1;k<=3;k++)if(j!=k)dp[i][j]=max(dp[i][j],dp[i-1][k]+arr[j]);

      }

    }



  cout<<max(dp[n][1],max(dp[n][2],dp[n][3]))<<"\n";

    return 0;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<algorithm>

int main(){

  int j=1,x,da[3][100001],fun[3][2];

  scanf("%d",&x);

  fun[0][0] = fun[1][0] = fun[2][0] = 0;

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

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

    fun[0][j] = std::max(fun[1][j^1],fun[2][j^1]) + da[0][i];

    fun[1][j] = std::max(fun[0][j^1],fun[2][j^1]) + da[1][i];

    fun[2][j] = std::max(fun[1][j^1],fun[0][j^1]) + da[2][i];

    j^=1;

  }

  j^=1;

  printf("%d\n",std::max(std::max(fun[0][j],fun[1][j]),fun[2][j]));

  

  return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<algorithm>

int main(){

  int j=1,x,da[3][100001],fun[3][2];

  scanf("%d",&x);

  fun[0][0] = fun[1][0] = fun[2][0] = 0;

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

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

    fun[0][j] = std::max(fun[1][j^1],fun[2][j^1]) + da[0][i];

    fun[1][j] = std::max(fun[0][j^1],fun[2][j^1]) + da[1][i];

    fun[2][j] = std::max(fun[1][j^1],fun[0][j^1]) + da[2][i];

    j^=1;

  }

  j^=1;

  printf("%d\n",std::max(std::max(fun[0][j],fun[1][j]),fun[2][j]));

  

  return 0;

}


// Fastest known solution for this problem: