Problem:
Score : 100 points

Problem Statement

Taro's summer vacation starts tomorrow, and he has decided to make plans for it now.

The vacation consists of N days.
For each i (1 \leq i \leq N), Taro will choose one of the following activities and do it on the i-th day:

A: Swim in the sea. Gain a_i points of happiness.

B: Catch bugs in the mountains. Gain b_i points of happiness.

C: Do homework at home. Gain c_i points of happiness.

As Taro gets bored easily, he cannot do the same activities for two or more consecutive days.

Find the maximum possible total points of happiness that Taro gains.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq a_i, b_i, c_i \leq 10^4

Input

Input is given from Standard Input in the following format:

N
a_1 b_1 c_1
a_2 b_2 c_2
:
a_N b_N c_N

Output

Print the maximum possible total points of happiness that Taro gains.

Sample Input 1

3
10 40 70
20 50 80
30 60 90

Sample Output 1

210

If Taro does activities in the order C, B, C, he will gain 70 + 50 + 90 = 210 points of happiness.

Sample Input 2

1
100 10 1

Sample Output 2

100

Sample Input 3

7
6 7 8
8 8 3
2 5 2
7 8 6
4 6 8
2 3 4
7 5 1

Sample Output 3

46

Taro should do activities in the order C, A, B, A, C, B, A.

Solution in C++ (GCC 9.2.1) (judge verdict: Time Limit Exceeded)

#include <bits/stdc++.h>
#define ll long long int
#define ull unsigned long long int
#define loop(i ,a,b) for(int i = a;i<b;i++)
#define rloop(i,a,b) for(int i = a ; i>b;i--)
#define loopll(i,a,b) for( long long int i = a; i<b;i++)
#define rloopll(i,a,b) for(long long int i = a; i>b;i--)
#define eb emplace_back
#define pii pair<int ,int>
#define pll pair<ll ,ll>
#define mk make_pair
#define intmin INT_MIN
#define intmax INT_MAX
#define cout(x) cout<<x<<" ";
using namespace std;
void vec(vector<int> & v){

    for(int i = 0;i<v.size() ; i++){
        cout<<v[i]<< " ";
    }
    cout<<endl;
}
void vec_ll(vector<long long int> & v){

    for(int i = 0;i<v.size() ; i++){
        cout<<v[i]<< " ";
    }
    cout<<endl;
}
int parsestring(string temp){
    int num= 0;
    int digs = temp.size() - 1;
    for(int i = 0 ;i<=digs;i++){
        num += pow(10 , digs -i)*(temp[i] - '0');
    }
    return num;
}

int mindig(string & s){
    int temp = INT_MAX;
//    int index = -1;
    for(int i = 0;i<s.size() ; i++){
        if(temp > s[i] - '0'){
            temp = s[i] - '0';
//            index = i;
        }
    }
    return temp;
}
//}
ll power(ll base ,ll exp){
    ll ans = 1;
    for(ll i = 1;i<=exp ; i++){
        ans *= base;
    }
    return ans;
}
//}
//
void factor(ll n ,set<ll> &s ){
        ll x = 2;
        while(n!= 1){
            if(n%x == 0){
                s.insert(x);
                n/= x;
            }
            else{
                x++;
            }
        }
    }

bool prime(int n){
    bool ok = true;
    for(int i = 2 ; i*i <= n ; i++){
        if(n%i == 0){ok =false ; break;}
    }
    return ok;
}
pair<ll ,ll > ass(pair<ll ,ll > &a , pair<ll ,ll> &b){
    pair<ll ,ll> x;
    x.first = -1e10;
    x.second = -1e10;
    if(a.second < b.first){return x;}
    if(b.second < a.first){return x;}
    if(a.first <= b.first){
        x.first = b.first;
        x.second = min(a.second , b.second);
    }
    else {
        x.first = a.first;
        x.second = min(a.second , b.second);
    }
    return x;
}
int MX = 100005;
vector<vector<int>> adj(MX , vector<int>());
vector<int> subt(MX , 1);
vector<bool> vis(MX, false);
vector<bool> vis2(MX, false);
vector<int> neigh(MX , 0);

int ans = 0;
int dfs(int x){
    if(vis[x] ){return 0;}
    vis[x]  = true;
    for(int i = 0;i<adj[x].size() ;i++){
        subt[x] += dfs(adj[x][i]);
    }
    return subt[x];
}

void dfs2(int x){
    if(vis2[x]){return;}
    vis2[x] = true;
    for(int i = 0;i<adj[x].size() ;i++){
        if(neigh[x] > 1 && subt[adj[x][i]]%2 == 0 && !vis2[adj[x][i]]){
            cout<<x<< " " <<adj[x][i]<< " " <<neigh[x]<< " " <<subt[adj[x][i]]<<endl;
            ans++;
            neigh[x]--;
            neigh[adj[x][i]]--;
//            cout<<"a "<< x<< " " <<adj[x][i]<<endl;
        }
        dfs(adj[x][i]);
    }
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll n;
    cin>>n;
    vector<pair<int , ll>> v;
    int ptr = 0;
    ll maxi = -1e18;
    ll k = 0;
    for(int i = 0 ;i<n;i++){
        vector<ll> val(3);
        for(int j = 0;j<3;j++){
            cin>>val[j];
        }
        if(v.size() == 0){
            v.eb(mk(0 , val[0]));
            v.eb(mk(1 ,val[1]));
            v.eb(mk(2 , val[2]));
            maxi = max(maxi , max(val[0] , max(val[1] , val[2])));
            k = 3;
        }
        else{
            ll temp = 0;
            for(int j = 0 ;j<k; j++){
                vector<bool> vis(3,false);
                vis[v[ptr + j].first] = true;
                for(int q = 0;q<3;q++){
                    if(!vis[q]){
                        v.eb(mk(q , v[ptr + j].second + val[q]));
                        maxi = max(maxi , v.back().second);
                        temp++;
                    }
                }
            }
            ptr += k;
            k = temp;
        }
        if(maxi > 1e9){break;}
    }
    cout<<maxi<<endl;
}