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.



Sample input:
3
10 40 70
20 50 80
30 60 90


Sample output:
210

Submission status: Accepted

Solution code:
n=int(input())
dp=[[0for _ in range(3)]for _ in range(n)]
for i in range(n):
  a,b,c=map(int,input().split())
  if i==0:dp[i]=[a,b,c]
  else:
    dp[i][0]=max(dp[i-1][1],dp[i-1][2])+a
    dp[i][1]=max(dp[i-1][0],dp[i-1][2])+b
    dp[i][2]=max(dp[i-1][0],dp[i-1][1])+c
print(max(dp[n-1]))

Submission status: Accepted

Solution code:
from sys import stdin
import math
import fractions
from collections import deque

INF = 10 ** 10

N = int(stdin.readline().rstrip())
abc = [list(map(int, stdin.readline().rstrip().split())) for _ in range(N)]

# i日目に(0, 1, 2):=(a, b, c)の行動を行った時の幸福度の最大値
dp = [[-INF] * 3 for _ in range(N+10)]

a, b, c = abc[0]
dp[0][0] = a
dp[0][1] = b
dp[0][2] = c

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

print(max(dp[N-1]))

Submission status: Accepted

Solution code:
from sys import stdin
import math
import fractions
from collections import deque

INF = 10 ** 10

N = int(stdin.readline().rstrip())
abc = [list(map(int, stdin.readline().rstrip().split())) for _ in range(N)]
# add dummy
abc = abc + [[-INF, -INF, -INF]]

# i日目に(0, 1, 2):=(a, b, c)の行動を行った時の幸福度の最大値
dp = [[-INF] * 3 for _ in range(N+10)]

a, b, c = abc[0]
dp[0][0] = a
dp[0][1] = b
dp[0][2] = c

for i in range(N):
    for j in range(3):
        for k in range(3):
            if j == k:
                continue
            dp[i+1][j] =  max(dp[i+1][j], dp[i][k]+abc[i+1][j])

print(max(dp[N-1]))

Submission status: Accepted

Solution code:
N = int(input())
hs =[list(map(int, input().split())) for i in range(N)]
a = []
b = []
c = []
for i, j, k in hs:
    a.append(i)
    b.append(j)
    c.append(k)
adp = [a[0]] + [0]*(N-1)
bdp =[b[0]] + [0]*(N-1)
cdp = [c[0]] + [0]*(N-1)
for i in range (1, N):
    adp[i] = max(bdp[i-1] + a[i], cdp[i-1]+a[i])
    bdp[i] = max(adp[i-1] + b[i], cdp[i-1]+b[i])
    cdp[i] = max(bdp[i-1] + c[i], adp[i-1]+c[i])
print(max(adp[N-1], bdp[N-1], cdp[N-1]))

Submission status: Accepted

Solution code:

def chmax(a,b):
    if(a>b):
        return a
    return b


N = int(input())

A = []
B = []
C = []
dp = [[0,0,0]]
INF = 0

for i in range(0,N):
    a,b,c = map(int,input().split(" "))
    A.append(a)
    B.append(b)
    C.append(c)
    dp.append([INF,INF,INF])

ABC = [A,B,C]


for i in range(0,N):
    for r in range(0,3):
        ls = [0,1,2]
        ls.remove(r)
        f1 = ls[0]
        f2 = ls[1]

        dp[i][r] = max(dp[i-1][f1],dp[i-1][f2]) + ABC[r][i]
    

print(max(dp[N-1]))

Submission status: Accepted

Solution code:
# import doctest


def solve(N, abc):
    """
    >>> solve(3, [(10, 40, 70), (20, 50, 80), (30, 60, 90)])
    210
    """
    # dp[i][j] = i日目にjの活動を選んだ時の幸福度総和の最大値
    # 本日の活動を幸福度が高い順にx, y, zとして、
    # dp[i][j] = max(dp[i - 1][y] + x, dp[i - 1][z] + x, dp[i - 1][x] + y, dp[i - 1][z] + y)
    dp = [[None, None, None] for _ in range(N + 1)]
    a1, b1, c1 = abc[0]
    # a, b, c を 0, 1, 2と読み替える
    dp[1][0] = a1
    dp[1][1] = b1
    dp[1][2] = c1
    for i in range(2, N + 1):
        a, b, c = abc[i - 1]
        dp[i][0] = max(dp[i - 1][1] + a, dp[i - 1][2] + a)  # aを選ぶとき
        dp[i][1] = max(dp[i - 1][0] + b, dp[i - 1][2] + b)  # bを選ぶとき
        dp[i][2] = max(dp[i - 1][0] + c, dp[i - 1][1] + c)  # cを選ぶとき
    return max(dp[N][0], dp[N][1], dp[N][2])


# doctest.testmod()
N = int(input())
abc = [tuple(map(int, input().split())) for _ in range(N)]
print(solve(N, abc))

Submission status: Accepted

Solution code:
N = int(input())
data = [[int(i) for i in input().split()] for _ in range(N)]

dp = [[0] * 3 for _ in range(N)]
dp[0] = data[0]

for i in range(1, N):
    for j in range(3):
        dp[i][j] = data[i][j] + max(dp[i-1][(j+1)%3], dp[i-1][(j+2)%3])
print(max(dp[N-1]))

Submission status: Accepted

Solution code:
#問題 C
N=int(input())
ha,hb,hc = map(int,input().split())
# print(ha,hb,hc)
for i in range(1,N):
    a,b,c=map(int,input().split())
    ha,hb,hc = max(a+hb,a+hc),max(b+ha,b+hc),max(c+hb,c+ha)
#     hb = max(b+ha,b+hc)
#     hc = max(c+hb,c+ha)
#     print("tmpmax",ha,hb,hc)
hap=max(max(ha,hb),hc)
print(hap)
#     x,y,z = map(int,input().split())
#     print(x,y,z)

Submission status: Accepted

Solution code:
N=int(input())
C=[ list(map(int,input().split(' '))) for i in range(N)]
dp=[]
for i in range(N):
	if i==0:
		dp.append(C[0]) #[a0,b0,c0]
	else:
		dp.append([ C[i][j]+max(dp[i-1][(j+1)%3], dp[i-1][(j+2)%3]) for j in range(3) ])
print(max(dp[N-1]))

Submission status: Accepted

Solution code:
# 時間tにおいて行動a,b,cを取った時の幸福度の最大値を求める

N = int(input())
abc = [list(map(int, input().split())) for _ in range(N)]

happy = [[0 for _ in range(3)] for _ in range(N)]
happy[0] = abc[0] # 最初の日はその行動の幸福度によってのみ決まる

for i in range(1, N):
    for j in range(3):
        if j == 0:
            s, t = 1, 2
        elif j == 1:
            s, t = 0, 2
        else:
            s, t = 0, 1  
        happy[i][j] = max(abc[i][j] + happy[i-1][s],
                          abc[i][j] + happy[i-1][t])

print(max(happy[N-1]))

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())

# init
# dp[day][action]
# day: 1-N, action: 0-2(a-c)
dp = np.zeros((N + 1, 3)).astype(int)

# loop
for i in range(1, N + 1):
    abc = list(map(int, input().split()))
    dp[i][0] = max(dp[i-1][1], dp[i-1][2]) + abc[0]
    dp[i][1] = max(dp[i-1][0], dp[i-1][2]) + abc[1]
    dp[i][2] = max(dp[i-1][0], dp[i-1][1]) + abc[2]

# output
print(max(dp[N]))

Submission status: Accepted

Solution code:
N=int(input())
DATA = [tuple(map(int,input().split())) for i in range(N)]


for idx,datum in enumerate(DATA):
  if idx == 0:
    prev = datum
  else:
    a,b,c = datum
    prev = a+max(prev[1],prev[2]),b+max(prev[0],prev[2]),c+max(prev[0],prev[1])

print(max(prev))

Submission status: Accepted

Solution code:
"""
AtCoder Educational DP Contest
https://atcoder.jp/contests/dp/tasks/dp_c

C - Vaction
"""
INF = 100000007
import collections

def solve(troikas):
    dp = collections.defaultdict(lambda: -INF)
    for act in range(3):
        dp[(0, act)] = troikas[0][act]

    for i in range(1, N):
        for act in range(3):
            dp[(i, act)] = max(
                dp[(i - 1, prev_act)] + troikas[i][act]
                for prev_act in range(3)
                if prev_act != act
            )
    return max(dp[(N - 1, act)] for act in range(3))

N = int(input())
troikas = [[int(s) for s in input().split()] for _ in range(N)]
res = solve(troikas)
print(res)

Submission status: Accepted

Solution code:
abc = []
n = int(input())
for i in range(n):
  abc.append([int(s) for s in input().split()])

dp = [[0, 0, 0]for i in range(n)]
dp[0][0] = abc[0][0]
dp[0][1] = abc[0][1]
dp[0][2] = abc[0][2]
for i in range(1, n):
  dp[i][0] = max(dp[i-1][1]+abc[i][0], dp[i-1][2]+abc[i][0])
  dp[i][1] = max(dp[i-1][0]+abc[i][1], dp[i-1][2]+abc[i][1])
  dp[i][2] = max(dp[i-1][0]+abc[i][2], dp[i-1][1]+abc[i][2])
print(max(dp[n-1]))

Submission status: Accepted

Solution code:
num = int(input())
actions = []
for i in range(num):
    actions.append(list(map(int, input().split())))

dp = [[0 for i in range(3)] for j in range(num)]
for i in range(num):
    if i == 0:
        dp[i][0] = actions[i][0]
        dp[i][1] = actions[i][1]
        dp[i][2] = actions[i][2]
    else:
        dp[i][0] = max(dp[i-1][1] + actions[i][0], dp[i-1][2] + actions[i][0])
        dp[i][1] = max(dp[i-1][0] + actions[i][1], dp[i-1][2] + actions[i][1])
        dp[i][2] = max(dp[i-1][0] + actions[i][2], dp[i-1][1] + actions[i][2])

print(max( dp[num-1][0], dp[num-1][1] ,dp[num-1][2] ))

Submission status: Accepted

Solution code:
if __name__=="__main__":
    n = int(input())
    A = [0 for i in range(n)]
    B = [0 for i in range(n)]
    C = [0 for i in range(n)]
    for i in range(n):
        A[i], B[i], C[i] = [int(i) for i in input().split(' ')]
    f = [0, 0, 0]
    for i in range(n):
        g = [0, 0, 0]
        g[0] = max(f[0], max(f[1], f[2]) + A[i])
        g[1] = max(f[1], max(f[0], f[2]) + B[i])
        g[2] = max(f[2], max(f[0], f[1]) + C[i])
        f = g
    print(max(f[0], max(f[1], f[2])))

Submission status: Accepted

Solution code:
N = int(input())
l = [[0, 0, 0]] + [list(map(int, input().split())) for _ in range(N)]

dp = [[0, 0, 0] for _ in range(N + 1)]
for i in range(1, N + 1):
    for j in range(3):
        for k in range(3):
            if k == j:
                continue
            dp[i][j] = max(dp[i][j], dp[i - 1][k] + l[i][k])

print(max(dp[N]))

Submission status: Accepted

Solution code:
N = int(input())
a = [0]
b = [0]
c = [0]
for i in range(N):
    a_i, b_i, c_i = map(int, input().split())
    a.append(a_i)
    b.append(b_i)
    c.append(c_i)


dp = [[0, 0, 0] for _ in range(N + 1)]
for i in range(1, N + 1):
    for j in range(3):
        l = [a[i], b[i], c[i]]
        for k in range(3):
            if k == j:
                continue
            dp[i][j] = max(dp[i][j], dp[i - 1][k] + l[k])

print(max(dp[N]))

Submission status: Accepted

Solution code:
N = int(input())
abcs = list( list(map(int,input().split())) for _ in range(N))
dp = [[0]*3 for _ in range(N)]

dp[0] = abcs[0]
# print(dp)

for n in range(1,N):
    for i in range(3):
        for j in range(3):
            if i == j:
                continue
            else:
                if dp[n][i] < dp[n-1][j] + abcs[n][i]:
                    dp[n][i] = dp[n-1][j] + abcs[n][i]
                    
print(max(dp[N-1]))

Submission status: Accepted

Solution code:
import sys

N = int(input())
dp = [[0 for i in range(3)] for j in range(N)]

a, b, c = map(int,input().split())
dp[0][0] = a
dp[0][1] = b
dp[0][2] = c

if N ==1:
    print(max(a, b, c))
    sys.exit()

a, b, c = map(int,input().split())
case1 = a + dp[0][1] 
case2 = a + dp[0][2] 
case3 = b + dp[0][0] 
case4 = b + dp[0][2] 
case5 = c + dp[0][0] 
case6 = c + dp[0][1]

dp[1][0] = max(case1,case2)
dp[1][1] = max(case3,case4)
dp[1][2] = max(case5,case6)

if N ==2:
    print(max(dp[1][0], dp[1][1], dp[1][2]))
    sys.exit()

for i in range (2, N):
    a, b, c = map(int,input().split())
    case1 = a + dp[i-1][1]
    case2 = a + dp[i-1][2]
    case3 = b + dp[i-1][0]
    case4 = b + dp[i-1][2]
    case5 = c + dp[i-1][0]
    case6 = c + dp[i-1][1]
    
    dp[i][0] = max(case1,case2)
    dp[i][1] = max(case3,case4)
    dp[i][2] = max(case5,case6)
    
print(max(dp[N-1][0],dp[N-1][1],dp[N-1][2]))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
abc = [[0,0,0]]+[list(map(int,input().split())) for _ in range(N)]
dp = [[0,0,0] for _ in range(N+1)]

for i in range(1,N+1):
    for j in range(3):
        for k in range(3):
            if j == k:continue
            #print('i={},j={},k={}'.format(i,j,k))
            #print(dp[i][j],abc[i][j],abc[i-1][k])
            dp[i][j] = max(dp[i][j],abc[i][j]+dp[i-1][k])
print(max(dp[-1]))

Submission status: Accepted

Solution code:
N = int(input())
dp = [[0 for i in range(3)] for j in range(N)]

#Lはa,b,c * Nを格納
L=[]
for i in range(N):
    a,b,c = map(int, input().split())
    L.append([a,b,c])

#N=0時は標準入力そのまま
dp[0][0] = L[0][0]
dp[0][1] = L[0][1]
dp[0][2] = L[0][2]

for i in range(1,N):
    dp[i][0] = max(dp[i-1][1],dp[i-1][2]) + L[i][0]
    dp[i][1] = max(dp[i-1][0],dp[i-1][2]) + L[i][1]
    dp[i][2] = max(dp[i-1][0],dp[i-1][1]) + L[i][2]
print(max(dp[N-1][0],dp[N-1][1],dp[N-1][2]))

Submission status: Accepted

Solution code:
N=int(input())
dp=[[0 for i in range(3)]for j in range(N)]

L=[]
for i in range(N):
    a,b,c=map(int,input().split())
    L.append([a,b,c])

dp[0][0]=L[0][0]
dp[0][1]=L[0][1]
dp[0][2]=L[0][2]

for i in range(1,N):
    dp[i][0]=max(dp[i-1][1],dp[i-1][2])+L[i][0]
    dp[i][1]=max(dp[i-1][0],dp[i-1][2])+L[i][1]
    dp[i][2]=max(dp[i-1][0],dp[i-1][1])+L[i][2]
print(max(dp[N-1][0],dp[N-1][1],dp[N-1][2]))

Submission status: Accepted

Solution code:
n=int(input())
A,B,C=[],[],[]
for i in range(n):
  a,b,c=map(int,input().split())
  A.append(a)
  B.append(b)
  C.append(c)
DP=[[0 for i in range(3)] for j in range(n)]
DP[0]=[A[0],B[0],C[0]]
for i in range(1,n):
  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]
print(max(DP[n-1][i] for i in range(3)))

Submission status: Accepted

Solution code:
N = int(input())
abcList = [[int(i) for i in input().split()] for _ in range(N)]

dp = [[0 for _ in range(3)] for _ in range(N)]
dp[0] = abcList[0]
for i in range(1, N):
    for j in range(3):
        for k, element in enumerate(abcList[i]):
            if j == k:
                continue
            dp[i][k] = max(dp[i - 1][j] + element, dp[i][k])

print(max(dp[N - 1]))

Submission status: Accepted

Solution code:
N = int(input())
Aij = []
for i in range(N):
    Aij.append([int(i) for i in input().split()])

dp = [[0]*3]*N

dp[0] = Aij[0]

for i in range(1,N):
    a,b,c = dp[i-1]
    dp[i][0] = Aij[i][0] + max(b, c)
    dp[i][1] = Aij[i][1] + max(c, a)
    dp[i][2] = Aij[i][2] + max(a, b)

    ### このやり方はなにかまずい(中身が書き換わっている？)
    # dp[i][0] = Aij[i][0] + max(dp[i-1][1], dp[i-1][2])
    # dp[i][1] = Aij[i][1] + max(dp[i-1][2], dp[i-1][0])
    # dp[i][2] = Aij[i][2] + max(dp[i-1][0], dp[i-1][1])


print(max(dp[-1]))

Submission status: Accepted

Solution code:
N=int(input())
dp=[[0 for i in range(N+1)] for i in range(3)]

for j in range(N):
    abc=list(map(int,input().split()))
    for i in range(3):
        if j==0:
            dp[i][j]=abc[i]
        else:
            dp[i][j]=max([dp[k][j-1]+abc[i] for k in range(3) if k!=i])
print(max([dp[i][N-1] for i in range(3)]))

Submission status: Accepted

Solution code:
N = int(input())
h = []
for i in range(N):
    h.append(list(map(int, input().split(" "))))

cost = h[-1]

for day in range(N-2, -1, -1):
    today_cost = []
    today_cost.append(h[day][0] + max(cost[1], cost[2]))
    today_cost.append(h[day][1] + max(cost[0], cost[2]))
    today_cost.append(h[day][2] + max(cost[0], cost[1]))
    cost = today_cost

print(max(cost))

Submission status: Accepted

Solution code:
N = int(input())
h = []
for i in range(N):
    h.append(list(map(int, input().split(" "))))

cost = h[-1]

for day in range(N-2, -1, -1):
    today_cost = [0 for i in range(3)]
    today_cost[0] = h[day][0] + max(cost[1], cost[2])
    today_cost[1] = h[day][1] + max(cost[0], cost[2])
    today_cost[2] = h[day][2] + max(cost[0], cost[1])
    cost = today_cost

print(max(cost))

Submission status: Accepted

Solution code:

def dp_solve(dp0, dp1, dp2, n):
    for i in range(1, n):
        dp0[i] = max(dp1[i-1], dp2[i-1])+ax[i]
        dp1[i] = max(dp0[i-1], dp2[i-1])+bx[i]
        dp2[i] = max(dp0[i-1], dp1[i-1])+cx[i]
    return max(dp0[-1], dp1[-1], dp2[-1])


if __name__ == "__main__":
    n = int(input())
    ax = []
    bx = []
    cx = []
    for i in range(n):
        a, b, c = map(int, input().split())
        ax.append(a)
        bx.append(b)
        cx.append(c)

    dp0 = [ax[0]]+[0]*(n-1)
    dp1 = [bx[0]]+[0]*(n-1)
    dp2 = [cx[0]]+[0]*(n-1)
    print(dp_solve(dp0, dp1, dp2, n))

Submission status: Accepted

Solution code:
n = int(input())
h = [list(map(int, input().split())) for _ in range(n)]
#i(0~n-1)日目にa,b,cで得られる幸福度はh[n-1][0,1,2]でアクセス

def solve():
#0日目～n-1日目と考えるので注意
    dp = [[0]*3 for _ in range(n)] #初期化
    for i in range(n):
        if i == 0:
            dp[i] = [h[0][j] for j in range(3)]
            continue
        for j in range(3):
            dp[i][j] = max([dp[i-1][jk] for jk in ({0,1,2} - {j}) ]) + h[i][j]
            #0,1,2からjを除いたものをjkとし、それの合計の高い方にh[i][j]を足す
    print(max(dp[n-1]))

solve()

Submission status: Accepted

Solution code:
N=int(input())
ABC=[[]]*N
for i in range(N):
  ABC[i] = list(map(int, input().split()))

dp = [[0 for i in range(3)] for j in range(N+1)]
for i in range(1,N+1):
  for j in range(3):
    for k in range(3):
      if j!=k:
        dp[i][j] = max(dp[i][j], dp[i-1][k]+ABC[i-1][k])
print(max(dp[N][0],dp[N][1],dp[N][2]))

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**7)
mo=998244353
n=int(input())
dp=list(map(int,input().split()))#[0,0,0]
for i in range(1,n):
    a,b,c=list(map(int,input().split()))
    dp=[a+max(dp[1],dp[2]),b+max(dp[0],dp[2]),c+max(dp[0],dp[1])]
print(max(dp))

Submission status: Accepted

Solution code:
N = int(input())
a = [0 for _ in range(N)]
b = [0 for _ in range(N)]
c = [0 for _ in range(N)]

for i in range(N):
    a[i], b[i], c[i] = map(int, input().split(" "))
happy = [[0 for _ in range(3)] for _ in range(N)]
for i in range(N):
    if i == 0:
        happy[i][0] = a[i]
        happy[i][1] = b[i]
        happy[i][2] = c[i]
    else:
        happy[i][0] = max(happy[i-1][1]+a[i], happy[i-1][2]+a[i])
        happy[i][1] = max(happy[i-1][0]+b[i], happy[i-1][2]+b[i])
        happy[i][2] = max(happy[i-1][0]+c[i], happy[i-1][1]+c[i])
print(max(happy[-1]))

Submission status: Accepted

Solution code:
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='int16')
# # 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

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

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

N = read_a_int()
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):
        dp[i+1][j] = max([dp[i][k] + ABC[i+1][j] for k in range(3) if k != j])

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

Submission status: Accepted

Solution code:
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), -float('inf'))
# # 0->a,1->b,2->c

# dp[0, :] = ABC[0]

# for i in range(N - 1):
#     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(int(dp[N-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])))

Submission status: Accepted

Solution code:
N = int(input())
ABC = (list(map(int, input().split())) for _ in range(N))

DP = [[0, 0, 0] for _ in range(N)]

for i, abc in enumerate(ABC):
  if i == 0:
    for j in range(3):
      DP[i][j] = abc[j]
  else:
    for j in range(3):
      DP[i][j] = max(DP[i-1][k] + abc[j] for k in range(3) if j != k)

print(max(DP[N-1]))

Submission status: Accepted

Solution code:
N = int(input())
points =[list(map(int, input().split())) for _ in range(N)]

dp = [ [0 , 0 , 0] for _ in range(N + 1)]

for step in range(len(points)):
    # a
    for act in range(3):
        for next_act in range(3):
            if act == next_act:
                continue
            dp[step + 1][next_act] = max(points[step][next_act]+dp[step][act], dp[step + 1][next_act])


print(max(dp[N]))

Submission status: Accepted

Solution code:
N = int(input())
a = []
for i in range(N):
    a.append(list(map(int, input().split())))

dp = [[0, 0, 0] for i in range(N+1)]

for i in range(N):
    for j in range(3):
        for k in range(3):
            if j != k:
                dp[i+1][k] = max(dp[i+1][k], dp[i][j]+a[i][k])

print(max(dp[-1]))

Submission status: Accepted

Solution code:
n = int(input())
ans = [0] * 3

for i in range(n):
    t1 = list(map(int, input().split()))
    t2 = [0] * 3
    for i in range(3):
        for j in range(3):
            if i != j:
                t2[j] = max(t2[j], ans[i] + t1[j])
    ans = t2
print(max(ans))

Submission status: Accepted

Solution code:
n=int(input())
dp=[[0 for i in range(n+1)] for j in range(3)]
for i in range(1,n+1):
    a,b,c=map(int,input().split())
    dp[0][i]=max(dp[1][i-1]+a,dp[2][i-1]+a)
    dp[1][i]=max(dp[0][i-1]+b,dp[2][i-1]+b)
    dp[2][i]=max(dp[0][i-1]+c,dp[1][i-1]+c)
print(max(dp[0][n],dp[1][n],dp[2][n]))

Submission status: Accepted

Solution code:
N = int(input())
Action = [list(map(int, input().split())) for i in range(N)]


# dp[i][j] := i日目に行動jを行った場合の最大値
dp = [[0] * 3 for i in range(N + 1)]

for i in range(N):
    for j in range(3):
        dp[i + 1][(j + 1) % 3] = max(dp[i + 1][(j + 1) % 3], dp[i][j] + Action[i][j])
        dp[i + 1][(j + 2) % 3] = max(dp[i + 1][(j + 2) % 3], dp[i][j] + Action[i][j])

print(max(dp[-1]))

Submission status: Accepted

Solution code:
n=int(input())
dp=[[0 for i in range(n+1)] for j in range(3)]
for i in range(1,n+1):
  a,b,c=map(int,input().split())
  dp[0][i]=max(dp[1][i-1]+a,dp[2][i-1]+a)
  dp[1][i]=max(dp[0][i-1]+b,dp[2][i-1]+b)
  dp[2][i]=max(dp[0][i-1]+c,dp[1][i-1]+c)
print(max(dp[0][n],dp[1][n],dp[2][n]))

Submission status: Accepted

Solution code:
N = int(input())
abc = [list(map(int,input().split())) for _ in range(N)]

dp = [row for row in abc]
for i in range(1,N):
    for a in range(3):
        dp[i][a] = abc[i][a] + max(dp[i-1][:a]+dp[i-1][a+1:])
print(max(dp[-1]))

Submission status: Accepted

Solution code:
from collections import Counter
from functools import reduce
import fractions
import math
import statistics
import sys
import time
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def LI(): return [int(x) for x in sys.stdin.readline().split()]  # LIST INT
def LF(): return [float(x) for x in sys.stdin.readline().split()]  # LIST FLOAT
def LS(): return sys.stdin.readline().split()  # LIST STRING
def MI(): return map(int, sys.stdin.readline().split())  # MAP INT
def II(): return int(sys.stdin.readline())  # INPUT INT
def IS(): return input()  # INPUT STRING
def P(x): return print(x)
def C(x): return Counter(x)
def GCD_LIST(numbers):  # greatest common divisor
    return reduce(fractions.gcd, numbers)
def LCM_LIST(numbers):  # least common multiple
    return reduce(LCM, numbers)
def LCM(m, n):
    return (m * n // fractions.gcd(m, n))


n = II()
happiness = [LI() for _ in range(n)]
dp = [[0 for i in range(3)] for i in range(n+1)]
for i in range(n):
    dp[i+1][0] = max(dp[i][1]+happiness[i][0], dp[i][2]+happiness[i][0])
    dp[i+1][1] = max(dp[i][0]+happiness[i][1], dp[i][2]+happiness[i][1])
    dp[i+1][2] = max(dp[i][0]+happiness[i][2], dp[i][1]+happiness[i][2])
P(max(dp[-1]))

Submission status: Accepted

Solution code:
n = int(input())

scores = []
for i in range(0, n):
  a, b, c = [int(elem) for elem in input().split()]
  if not scores == []:
    prev_a, prev_b, prev_c = scores[-1] # 最後が a, b, c のときの最大値
    if max(prev_b, prev_c) == prev_b:
      if max(prev_a, prev_c) == prev_a:
        if max(prev_a, prev_b) == prev_b:
          scores.append((a + prev_b, b + prev_a, c + prev_b))
        else:
          scores.append((a + prev_b, b + prev_a, c + prev_a))
      else:
        if max(prev_a, prev_b) == prev_b:
          scores.append((a + prev_b, b + prev_c, c + prev_b))
        else:
          scores.append((a + prev_b, b + prev_c, c + prev_a))
    else:
      if max(prev_a, prev_c) == prev_a:
        if max(prev_a, prev_b) == prev_b:
          scores.append((a + prev_c, b + prev_a, c + prev_b))
        else:
          scores.append((a + prev_c, b + prev_a, c + prev_a))
      else:
        if max(prev_a, prev_b) == prev_b:
          scores.append((a + prev_c, b + prev_c, c + prev_b))
        else:
          scores.append((a + prev_c, b + prev_c, c + prev_a))      
  else:
    scores.append((a, b, c))

print(max(scores[-1]))

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    days = int(input())
    acts = []
    happiness = [[0, 0, 0] for _ in range(days)]
    for day in range(days):
        acts.append(list(map(int, input().split())))

    for day in range(days):
        if day == 0:
            happiness[day] = [acts[0][0], acts[0][1], acts[0][2]]
        else:
            for act in range(3):
                happiness[day][act] = max(happiness[day - 1][(act + 1) % 3] + acts[day][act],
                                          happiness[day - 1][(act + 2) % 3] + acts[day][act])
    print(max(happiness[days - 1]))

Submission status: Accepted

Solution code:

n = int(input())
lista = []
for i in range(n):
    arr = list(map(int,input().split()))
    lista.append(arr)

dp = [ [0 for i in range(3)] for j in range(n)]

dp[0][1] = lista[0][1]
dp[0][0] = lista[0][0]
dp[0][2] = lista[0][2]

for i in range(1,n):
    for j in range(3):
        for k in range(3):
            if(j != k):
                dp[i][j] = max(dp[i][j] ,dp[i-1][k] + lista[i][j])

ans = 0
for i in range(3):
    ans = max(ans ,dp[n-1][i])
print(ans)

Submission status: Accepted

Solution code:
import sys

ns = lambda: sys.stdin.readline().rstrip()
ni = lambda: int(ns())
nm = lambda: map(int, sys.stdin.readline().split())
nms = lambda: map(str, sys.stdin.readline().split())
nl = lambda: list(nm())

n = ni()
l = [nl() for i in range(n)]
dp = [[-1, -1, -1] for i in range(n + 1)]
dp[0] = [0, 0, 0]
dp[1] = [l[0][0], l[0][1], l[0][2]]

for i in range(1, n + 1):
    dp[i][0] = max(dp[i-1][1] + l[i-1][0], dp[i-1][2] + l[i-1][0])
    dp[i][1] = max(dp[i-1][2] + l[i-1][1], dp[i-1][0] + l[i-1][1])
    dp[i][2] = max(dp[i-1][0] + l[i-1][2], dp[i-1][1] + l[i-1][2])
print(max(dp[n]))

Submission status: Accepted

Solution code:
N = int(input())

dp = [[0 for _ in range(3)] for _ in range(N)]

a,b,c = [int(i) for i in input().split()]
dp[0][0] = a
dp[0][1] = b
dp[0][2] = c

for i in range(1,N):
    li = [int(i) for i in input().split()]
    for j in range(3):
        tmp = []
        for k in range(3):
            if j != k:
                tmp.append(dp[i-1][k] + li[j])
        dp[i][j] = max(tmp)

print(max(dp[-1]))

Submission status: Accepted

Solution code:
n = int(input())
dpa = [0 for i in range(n)]
dpb = [0 for i in range(n)]
dpc = [0 for i in range(n)]
a,b,c = map(int,input().split())
dpa[0] = a
dpb[0] = b
dpc[0] = c
for i in range(1,n):
    a,b,c = map(int,input().split())
    dpa[i] = max(dpb[i-1],dpc[i-1])+a
    dpb[i] = max(dpc[i-1],dpa[i-1])+b
    dpc[i] = max(dpa[i-1],dpb[i-1])+c
print(max(dpa[n-1],dpb[n-1],dpc[n-1]))

Submission status: Accepted

Solution code:
N = int(input())
ABC = [[int(i) for i in input().split()]for _ in range(N)]

dp = [[0]*4 for _ in range(N+1)]
for i in range(N):
  for j in range(3):
    for k in range(3):
      if j == k:
        continue
      dp[i+1][j] = max(dp[i+1][j], dp[i][k]+ABC[i][j])
print(max(dp[N]))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
n = int(input())
a,b,c = map(int,input().split())

dpa = [a]+[0]*(n-1)
dpb = [b]+[0]*(n-1)
dpc = [c]+[0]*(n-1)

for i in range(1,n):
    a,b,c = map(int,input().split())
    dpa[i] = max(dpb[i-1],dpc[i-1])+a
    dpb[i] = max(dpc[i-1],dpa[i-1])+b
    dpc[i] = max(dpa[i-1],dpb[i-1])+c
    
print(max(dpa[-1],dpb[-1],dpc[-1]))

Submission status: Accepted

Solution code:
n = int(input())

dp = [[0]*3 for i in range(n+1)]

for i in range(n):
    a,b,c = map(int, input().split())
    dp[i+1][0] = max(dp[i][1], dp[i][2]) + a
    dp[i+1][1] = max(dp[i][2], dp[i][0]) + b
    dp[i+1][2] = max(dp[i][0], dp[i][1]) + c
print(max(dp[-1]))

Submission status: Accepted

Solution code:
import sys

n = int(sys.stdin.readline())

arr = [[]] * 3
dp = [0] * 3

for i in range(n):
    arr = list(map(int, sys.stdin.readline().split()))
    new_dp = [0] * 3
    for j in range(3):

        for k in range(3):

            if j != k:

                new_dp[k] = max(new_dp[k], dp[j] + arr[k])
    dp = new_dp

print(max(dp))

Submission status: Accepted

Solution code:
N = int(input())
dp = [[0] * 3 for j in range(N)]
d = []
for i in range(N):
    a, b, c = map(int, input().split())
    if i == 0:
        dp[0][0] = a
        dp[0][1] = b
        dp[0][2] = c
    else:
        d.append((a, b, c))


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

num = 0
for i in range(3):
    num = max(num, dp[N - 1][i])
print(num)

Submission status: Accepted

Solution code:
#!/usr/bin/python3

def main():
    N = int(input())

    dp = [[0 for _ in range(N+ 1)] for _ in range(3)]

    for i in range(0, N):
        todo = tuple(map(int, input().split()))

        for k in range(3):
            dp[k][i+1] = max(dp[j][i] for j in range(3) if j != k) + todo[k]

    print(max(dp[k][N] for k in range(3)))


if __name__ == '__main__':
    main()