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++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include<iostream>
using namespace std;
int max2 (int a, int b) {
  if (a < b) return b;
  else return a;
}
int max3 (int a, int b, int c) {
  int t = max2(a, b);
  int k = max2(t, c);
  return k;
}
int mind (int a, int b, int c) {
  int k = max2(a, b);
  if (k < c) return k;
  else {
    if (a + b - k > c) return a + b - k;
    else return c;
  }
}
int dore (int a, int b, int c) {
  if (max3(a, b, c) == c) return 3;
  if (max3(a, b, c) == b) return 2;
  if (max3(a, b, c) == a) return 1;
}
int dore2 (int a, int b, int c) {
  if (mind(a, b, c) == a) return 1;
  if (mind(a, b, c) == b) return 2;
  if (mind(a, b, c) == c) return 3;
}
int main () {
  int N;
  cin >> N;
  int a[100001];
  int b[100001];
  int c[100001];
  int d[100001];
  for (int i = 0; i < N; i ++) {
    cin >> a[i] >> b[i] >> c[i];
    d[i] = dore(a[i], b[i], c[i]);
  }
  d[N] = 5;
  int ans[100004];
  ans[0] = 0;
  ans[1] = max3(a[0], b[0], c[0]);
  if (N == 1) {
    cout << ans[1] << endl;
    return 0;
  }
  for (int i = 2; i <= N; i ++) {//DP部分
    int p = i - 1;
    if (d[p - 1] != d[p]) ans[i] = ans[p] + max3(a[p], b[p], c[p]);
    else {
      int k = ans[p] + mind(a[p], b[p], c[p]);
      int j = ans[p - 1] + mind(a[p - 1], b[p - 1], c[p - 1]) + max3(a[p], b[p], c[p]);
      if (k > j) {
        ans[i] = k;
        d[p] = dore2(a[p], b[p], c[p]);
      }
      else {
        ans[i] = j;
        ans[p] = ans[i] - max3(a[p], b[p], c[p]);
        if (k == j && d[p] == d[i]) {
          d[p] = dore2(a[p], b[p], c[p]);
          ans[p] = ans[i] - mind(a[p], b[p], c[p]);
        }
      }
    }
  }
    cout << ans[N] << endl;
}