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 Python (3.4.3) (judge verdict: Time Limit Exceeded)

import sys
read = sys.stdin.readline


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_matrix(H):
    '''
    H is number of rows
    '''
    return [list(map(int, read().split())) for _ in range(H)]


def read_map(H):
    '''
    H is number of rows
    文字列で与えられた盤面を読み取る用
    '''
    return [read() for _ in range(H)]


def read_col(H, n_cols):
    '''
    H is number of rows
    n_cols is number of cols

    A列、B列が与えられるようなとき
    '''
    ret = [[] for _ in range(n_cols)]
    for _ in range(H):
        tmp = list(map(int, read().split()))
        for col in range(n_cols):
            ret[col].append(tmp[col])

    return ret

# 全探索だと
# 3^N通りになってTLEだこれ

# 制限がなければ、iごとにmaxを取っておわりO(N)

# iの時の最善の状態が[i,j](jは行動)のとき、i+1最善の状態は[i+1,k](k!=j)となる
# これはDPで実装できそう！


import numpy as np

N = read_a_int()
ABC = np.array(read_matrix(N))
# A, B, C = read_col(N, 3)

dp = np.full((N, 3), 0, dtype='int32')
# 0->a,1->b,2->c

dp[0, :] = ABC[0]

for i in range(N - 1):
    for j, abc in enumerate(ABC[i+1]):
        dp[i + 1, j] = dp[i, [k for k in range(3) if k != j]].max() + abc

    # dp[i + 1, 0] = dp[i, 1:].max()+ABC[i+1, 0]
    # dp[i + 1, 1] = dp[i, (0, 2)].max() + ABC[i+1, 1]
    # dp[i + 1, 2] = dp[i, :2].max()+ABC[i+1, 2]

# print(dp)
print(dp[-1].max())

# TLEしたのでpypy3仕様にかきなおし

# ABC = read_matrix(N)

# # dp = np.full((N, 3), -float('inf'))
# dp = [[-float('inf') for _ in range(3)] for _ in range(N)]
# # 0->a,1->b,2->c

# for i in range(3):
#     dp[0][i] = ABC[0][i]

# for i in range(N - 1):

#     for j in range(3):
#         iterlist = [0, 1, 2]
#         iterlist.remove(j)
#         dp[i+1][j] = max([dp[i][k] + ABC[i+1][j] for k in iterlist])

# # print(dp)
# print(int(max(dp[-1])))