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())
vv = []
for i in range(n):
    v = [int(x) for x in input().split()]
    vv.append(v)

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

for i in range(n):
    for h in range(3):
        dp[i+1][h] = max(dp[i][(h+1)%3],dp[i][(h+2)%3]) + vv[i][h]

print(max(dp[n]))

Submission status: Accepted

Solution code:
n = int(input())
a = [[int(x) for x in input().split()] for i in range(n)]
dp = [[0] * 3 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][j] = max(dp[i+1][j], dp[i][k] + a[i][j])
print(max(dp[n][0], dp[n][1], dp[n][2]))

Submission status: Accepted

Solution code:
n = int(input())
a_ls = [0] * n
b_ls = [0] * n
c_ls = [0] * n
for i in range(n):
    a,b,c = map(int,input().split())
    a_ls[i] = a
    b_ls[i] = b
    c_ls[i] = c

dp = [[0]*3 for _ in range(n+1)]
dp[1] = [a_ls[0], b_ls[0], c_ls[0]]
for i in range(1,n):
    dp[i+1][0] = max(dp[i+1][0], dp[i][1] + a_ls[i], dp[i][2] + a_ls[i])
    dp[i+1][1] = max(dp[i+1][1], dp[i][0] + b_ls[i], dp[i][2] + b_ls[i])
    dp[i+1][2] = max(dp[i+1][2], dp[i][0] + c_ls[i], dp[i][1] + c_ls[i])

print(max(dp[-1]))

Submission status: Accepted

Solution code:
n = int(input())

dp = [[0] * 3 for i in range(n)]
dp[0] = list(map(int, input().split()))

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

print(max(dp[-1]))

Submission status: Accepted

Solution code:
n = int(input())
a_ls = [0] * n
b_ls = [0] * n
c_ls = [0] * n
for i in range(n):
    a,b,c = map(int,input().split())
    a_ls[i] = a
    b_ls[i] = b
    c_ls[i] = c

dp = [[0]*3 for _ in range(n)]
dp[0] = [a_ls[0], b_ls[0], c_ls[0]]
for i in range(1,n):
    dp[i][0] = max(dp[i][0], dp[i-1][1] + a_ls[i], dp[i-1][2] + a_ls[i])
    dp[i][1] = max(dp[i][1], dp[i-1][0] + b_ls[i], dp[i-1][2] + b_ls[i])
    dp[i][2] = max(dp[i][2], dp[i-1][0] + c_ls[i], dp[i-1][1] + c_ls[i])
print(max(dp[-1]))

Submission status: Accepted

Solution code:
def solve(a):
    dp = [[0]*3 for _ in range(len(a)+1)]
    for i in range(len(a)):
        for j in range(3):
            for k in range(3):
                if j != k:
                    dp[i+1][j] = max(dp[i+1][j], a[i][j] + dp[i][k])
    return max(dp[-1])
  
n = int(input())
a = []
for _ in range(n):
    a.append(list(map(int, input().split())))
print(solve(a))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n=int(input())
mat=[]
dp=[]

for i in range(n):
    l=list(map(int,input().split()))
    mat.append(l)
    dp.append([0]*3)

dp[0][0]=mat[0][0]
dp[0][1]=mat[0][1]
dp[0][2]=mat[0][2]

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

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

Submission status: Accepted

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

table = [[0]*3 for i in range(N)]


for i in range(0, N):# i日目
    if i == 0:
        table[i] = ABC[i]
    else:
        for j in range(3): #a,b,c
            table[i][j] = max(
                [table[i-1][j1] + ABC[i][j] for j1 in range(3) if not j1 == j ]
                 )
# print(table)
        
print(max(table[-1]))

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)

dpa=[0]*(N+1)
dpb=[0]*(N+1)
dpc=[0]*(N+1)

for i in range(N):
  dpa[i]=max(dpb[i-1],dpc[i-1])+A[i]
  dpb[i]=max(dpa[i-1],dpc[i-1])+B[i]
  dpc[i]=max(dpa[i-1],dpb[i-1])+C[i]

print(str(max(dpa[N-1],dpb[N-1],dpc[N-1])))

Submission status: Accepted

Solution code:
import numpy as np

n = int(input())
dp = [0] * 3

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

print(max(dp))

Submission status: Accepted

Solution code:
n = int(input())
dp = [[0] * 3 for i in range(n+1)]
for i in range(1,n+1):
  a,b,c = map(int,input().split())
  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[-1]))

Submission status: Accepted

Solution code:
def resolve():
    N = int(input())
    abc = []
    for i in range(N):
        abc.append([int(i) for i in input().split()])
        if i == 0:
            dp = [abc[0]]
        else:
            dp.append(
                [
                    abc[i][j] + max(dp[-1][k] for k in range(3) if j != k)
                    for j in range(3)
                ]
            )
    print(max(dp[-1]))


if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
n = int(input())
abc = tuple(tuple(map(int, input().split())) for _ in range(n))

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

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

Submission status: Accepted

Solution code:
#今回は行列を使用します。
#行列演算に完全勝利したコード
n = int(input())
dp = [[0, 0, 0] for i in range(n)]
h = [list(map(int, input().split())) for i in range(n)]
#print(h)
#print(dp)
for j in range(3):
    dp[0][j] = h[0][j]
for i in range(1,n):
    dp[i][0] = h[i][0]+max(dp[i-1][1], dp[i-1][2])
    dp[i][1] = h[i][1]+max(dp[i-1][2], dp[i-1][0])
    dp[i][2] = h[i][2]+max(dp[i-1][0], dp[i-1][1])
print(max(dp[-1]))

Submission status: Accepted

Solution code:
#今回は自力で組んでみます。図は見ましたが。
n = int(input())
a = [0]*n
b = [0]*n
c = [0]*n
dpa = [0]*n
dpb = [0]*n
dpc = [0]*n
for i in range(n):
    a[i], b[i], c[i] = map(int, input().split())
dpa[0] = a[0]
dpb[0] = b[0]
dpc[0] = c[0]
for i in range(1,n):
    dpa[i] = a[i]+max(dpb[i-1], dpc[i-1])
    dpb[i] = b[i]+max(dpc[i-1], dpa[i-1])
    dpc[i] = c[i]+max(dpa[i-1], dpb[i-1])
print(max(dpa[-1], dpb[-1], dpc[-1]))

Submission status: Accepted

Solution code:
N=int(input())

DP=[[0 for i in range(3)] for j in range(N)]
DP[0]=list(map(int,input().split()))
for i in range(1,N):
    lis=list(map(int,input().split()))
    for j in range(3):
        DP[i][j]=lis[j]+max(DP[i-1][(j+1)%3],DP[i-1][(j+2)%3])
print(max(DP[N-1]))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
n = int(input())
dp = [[0]*3 for _ 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][0], dp[i][2])+b
    dp[i+1][2] = max(dp[i][0], dp[i][1])+c
print(max(dp[-1]))

Submission status: Accepted

Solution code:
n=int(input())
dp=[[0]*3 for i in range(n)]
happy=[]
for i in range(n):
    tmp=list(map(int,input().split()))
    happy.append(tmp)
dp[0][0]=happy[0][0]
dp[0][1]=happy[0][1]
dp[0][2]=happy[0][2]
for i in range(1,n):
    dp[i][0] = max(dp[i-1][1],dp[i-1][2])+happy[i][0]
    dp[i][1] = max(dp[i-1][0],dp[i-1][2])+happy[i][1]
    dp[i][2] = max(dp[i-1][0],dp[i-1][1])+happy[i][2]
print(max(dp[n-1][0],dp[n-1][1],dp[n-1][2]))

Submission status: Accepted

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

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


for i, abc in enumerate(abc_list):
    for j in range(3):
        temp = dp[i][:]
        temp.pop(j)
        dp[i+1][j] = max(temp) + abc[j]

print(max(dp[-1]))

Submission status: Accepted

Solution code:
n = int(input())
abc = [list(map(int, input().split())) for i in range(n)]
dp = [[0]*3 for i 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][k] = max(dp[i+1][k], dp[i][j] + abc[i][k])
result = max(dp[n][0], dp[n][1], dp[n][2])
print(result)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys


def solve(N: int, a: "List[int]", b: "List[int]", c: "List[int]"):

    dp = []
    for _ in range(N):
        l = [0]*3
        dp.append(l)

    dp[0][0], dp[0][1], dp[0][2] = 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][0], dp[N - 1][1], dp[N - 1][2]))

    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    a = [int()] * (N)  # type: "List[int]"
    b = [int()] * (N)  # type: "List[int]"
    c = [int()] * (N)  # type: "List[int]"
    for i in range(N):
        a[i] = int(next(tokens))
        b[i] = int(next(tokens))
        c[i] = int(next(tokens))
    solve(N, a, b, c)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
a = [[int(i) for i in input().split()] for _ in range(N)]
dp = [[0]*3 for __  in range(N)]
dp[0] = a[0]
for i in range(1,N):
    for j in range(3):
        dp[i][j] = max(dp[i-1][(j+1)%3],dp[i-1][(j+2)%3])+a[i][j]
print(max(dp[N-1]))

Submission status: Accepted

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

dp[0][0] = potential_plans[0][0]
dp[0][1] = potential_plans[0][1]
dp[0][2] = potential_plans[0][2]

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

ans = max(dp[N-1][0],dp[N-1][1],dp[N-1][2])
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
mat=[]
dp=[]

for i in range(n):
    l=list(map(int,input().split()))
    mat.append(l)
    dp.append([0]*3)

dp[0][0]=mat[0][0]
dp[0][1]=mat[0][1]
dp[0][2]=mat[0][2]

for i in range(1,n):
    dp[i][0]=max(dp[i-1][1], dp[i-1][2])+mat[i][0]
    dp[i][1]=max(dp[i-1][0], dp[i-1][2])+mat[i][1]
    dp[i][2]=max(dp[i-1][0], dp[i-1][1])+mat[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] * 3 for _ in range(N)]

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

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-


def main():
    n = int(input())
    work_list = [tuple(map(int, input().split())) for i in range(n)]

    # dp = [[0] * 3 for i in range(n + 1)]
    # for i, work in enumerate(work_list):
    #     for i_now in range(3):
    #         for i_pre in range(3):
    #             if i_now == i_pre:
    #                 continue
    #
    #             dp[i + 1][i_now] = max(dp[i + 1][i_now], dp[i][i_pre] + work[i_now])
    dp_pre = [0] * 3
    dp_now = [0] * 3
    for i, work in enumerate(work_list):
        for i_now in range(3):
            for i_pre in range(3):
                if i_now == i_pre:
                    continue

                dp_now[i_now] = max(dp_now[i_now], dp_pre[i_pre] + work[i_now])

        dp_pre[0] = dp_now[0]
        dp_pre[1] = dp_now[1]
        dp_pre[2] = dp_now[2]

    print(max(dp_now))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-


def main():
    n = int(input())
    work_list = [tuple(map(int, input().split())) for i in range(n)]

    dp = [[0] * 3 for i in range(n + 1)]
    for i, work in enumerate(work_list):
        for i_now in range(3):
            for i_pre in range(3):
                if i_now == i_pre:
                    continue

                dp[i + 1][i_now] = max(dp[i + 1][i_now], dp[i][i_pre] + work[i_now])

    print(max(dp[-1]))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n=int(input())
a=[list(map(int,input().split())) for i in range(n)]
dp=[[0 for j 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:
                continue
            dp[i][j]=max(dp[i][j],dp[i-1][k]+a[i-1][k]) 

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

print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-


def main():
    n = int(input())
    work_list = [tuple(map(int, input().split())) for i in range(n)]

    dp = [[0] * 3 for i in range(n)]
    for i, work in enumerate(work_list):
        for i_now in range(3):
            for i_pre in range(3):
                if i_now == i_pre:
                    continue

                if i - 1 >= 0:
                    dp[i][i_now] = max(dp[i][i_now], dp[i - 1][i_pre] + work[i_now])
                else:
                    dp[i][i_now] = work[i_now]

    print(max(dp[-1]))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n=int(input())
happy=[list(map(int, input().split())) for i in range(n)]
dp=[[0 for j in range(3)] for i in range(n+1)]

for i in range(1,n+1):
  for place in range(3):
    for placeY in range(3):
      if place==placeY:
        continue
      dp[i][place]=max(dp[i][place],
                       dp[i-1][placeY]+happy[i-1][place])
      
ans=0
for place in range(3):
  ans=max(ans,dp[n][place])
print(ans)

Submission status: Accepted

Solution code:


n = int(input())

A = [[]]
for _ in range(n):
    A += [[int(x) for x in input().split()]]

B = [[0, 0, 0] for _ in range(n + 1)]

for i in range(1, n + 1):
    for j in range(3):
        B[i][j] = A[i][j] + max([B[i - 1][(j + m) % 3] for m in [1, 2]])

print(max(B[n]))

Submission status: Accepted

Solution code:
n = int(input())
a = [list(map(int,input().split())) for _ in range(n)]
dp = [[0, 0, 0] for _ 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][j] = max(dp[i+1][j], dp[i][k]+a[i][j])
print(max(dp[n]))

Submission status: Accepted

Solution code:
n = int(input())
a = [list(map(int,input().split())) for _ in range(n)]
dp = [[0, 0, 0] for _ 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[n]))

Submission status: Accepted

Solution code:
n=int(input())
s=[list(map(int,list(input().split()))) for i in range(n)]
dp=[[0]*3 for i in range(n+1)]
for i in range(n):
    for j in range(3):#today
        for k in range(3):#yesterday
            if j==k:
                continue
            dp[i+1][j]=max(dp[i+1][j],dp[i][k]+s[i][j])
print(max(dp[n][:]))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n = int(input())
dp = [[0 for _ in range(3)] for _ in range(n)]

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

for i in range(1,n):
    abc = map(int,input().split())
    for j,v in enumerate(abc):
        for k in range(3):
            if j != k:
                dp[i][j] = max(dp[i][j],dp[i-1][k] + v)

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

Submission status: Accepted

Solution code:
import operator as op

def main():
	N   = int(input())
	abc = [tuple(map(int, input().split())) for _ in range(N)]

	a = list( map(op.itemgetter(0), abc) )
	b = list( map(op.itemgetter(1), abc) )
	c = list( map(op.itemgetter(2), abc) )

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

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

main()

Submission status: Accepted

Solution code:
n=int(input())
x=[[0]*3 for i in range(n)]

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

Submission status: Accepted

Solution code:
n = int(input())
b = [0, 0, 0]

for _ in range(n):
    a = [int(i) for i in input().split()]
    c = [0] * 3
    for i in range(3):
        for j in range(3):
            if i == j:
                continue
            c[i] = max(c[i], b[j] + a[i])
    b = c
print(max(b))

Submission status: Accepted

Solution code:
n = int(input())
a = [list(map(int, input().split())) for i in range(n)]
dp = [[0 for i in range(3)] for j 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][j] = max(dp[i+1][j], dp[i][k]+a[i][j])
print(max(dp[n]))

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.buffer.readline
def even(n): return 1 if n%2==0 else 0

n = int(readline())
dp = [[0]*3 for _ in range(n)]
lst1 = list(map(int,readline().split()))
for i in range(3):
	dp[0][i] = lst1[i]
for i in range(n-1):
	lst1 = list(map(int,readline().split()))
	dp[i+1][0] = max(dp[i][1]+lst1[0],dp[i][2]+lst1[0])
	dp[i+1][1] = max(dp[i][0]+lst1[1],dp[i][2]+lst1[1])
	dp[i+1][2] = max(dp[i][0]+lst1[2],dp[i][1]+lst1[2])

print(max(dp[-1]))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

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

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

for day, abc in enumerate(ABC):
  for t_act in range(3):
    if (day == 0):
      dp[day][t_act] = abc[t_act]
      continue
    t_max = dp[day][t_act] # 0
    for y_act in range(3):
      if(t_act == y_act):
        continue
      happy = dp[day - 1][y_act] + abc[t_act]
      if (t_max < happy):
        t_max = happy
    
    dp[day][t_act] = t_max

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

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
n=int(input())
a=[[0]*3 for _ in range(n)]
for i in range(n):
    a[i]=list(map(int,input().split()))
dp=[[0]*3 for _ 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][j]=max(dp[i][k]+a[i][k],dp[i+1][j])
res=0
for i in range(3):
    if res<dp[n][i]:
        res=dp[n][i]
print(res)

Submission status: Accepted

Solution code:
n = int(input())
a, b, c = 0, 0, 0
for i in range(n):
    ai, bi, ci = map(int, input().split())
    a, b, c = ai + max(b, c), bi + max(c, a), ci + max(a, b)
print(max(a, b, c))

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
n=int(input())
l=[[float("INF")]*3 for i in range(n)]
l[0]=list(map(int,input().split()))
for day in range(1,n):
    f=list(map(int,input().split()))
    for choice in range(3):
        l[day][choice]=max(l[day-1][j]+f[choice] for j in range(3) if j!=choice)
print(max(l[-1]))

Submission status: Accepted

Solution code:
n = int(input())

hapiness = [list(map(int,input().split())) for i in range(n)]

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

dp[0][0], dp[0][1], dp[0][2] = hapiness[0][0], hapiness[0][1], hapiness[0][2]

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

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

Submission status: Accepted

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

dp = [[0] * 3 for _ 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[N]))

Submission status: Accepted

Solution code:
N=int(input())

DP=[[0 for i in range(3)] for j in range(N)]
DP[0]=list(map(int,input().split()))
for i in range(1,N):
    lis=list(map(int,input().split()))
    for j in range(3):
        DP[i][j]=lis[j]+max(DP[i-1][(j+1)%3],DP[i-1][(j+2)%3])
print(max(DP[N-1]))

Submission status: Accepted

Solution code:
N=int(input())
L = [list(map(int, input().split())) for i in range(N)]
dp=[[0 for j in range(3)] for i in range(N)]

for i in range(N):
  for P in range(3):
    for PY in range(3):
      if P==PY:
        continue
      elif i==0:
        dp[i][P]=L[i][P]
      else:
        dp[i][P]=max(dp[i][P],dp[i-1][PY]+L[i][P])
ans=0
for i in range(3):
  ans=max(ans,dp[N-1][i])
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
L = [list(map(int, input().split())) for i in range(N)]
dp=[[0 for j in range(3)] for i in range(N)]
ans=0
for i in range(N):
  for P in range(3):
    for PY in range(3):
      if P==PY:
        continue
      elif i==0:
        dp[i][P]=L[i][P]
        ans=max(ans,dp[i][P])
      else:
        dp[i][P]=max(dp[i][P],dp[i-1][PY]+L[i][P])
        ans=max(ans,dp[i][P])
'''
for i in range(3):
  ans=max(ans,dp[N-1][i])
'''
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
L = [list(map(int, input().split())) for i in range(N)]
dp=[[0 for j in range(3)] for i in range(N)]
ans=[0]*(N+1)
for i in range(N):
  if i==0:
    dp[i][0]=L[i][0]
    dp[i][1]=L[i][1]
    dp[i][2]=L[i][2]
  else:
    dp[i][0]=max(dp[i-1][1],dp[i-1][2])+L[i][0]
    dp[i][1]=max(dp[i-1][2],dp[i-1][0])+L[i][1]
    dp[i][2]=max(dp[i-1][0],dp[i-1][1])+L[i][2]
  ans=max(dp[i][0],dp[i][1],dp[i][2])
  #print(ans)
print(ans)

Submission status: Accepted

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

ABC.append([0,0,0])

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

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 ] + ABC[ i ][ k ])

print(max(dp[N][0],dp[N][1],dp[N][2]))

Submission status: Accepted

Solution code:
n = int(input())
a = []
d = []
for _ in range(n):
    a.append(list(map(int,input().split())))
    d.append([0,0,0])
d[0]=a[0]
for i in range(1,n):
    for j in range(3):
        d[i][j]=max(d[i-1][(j+1)%3],d[i-1][(j+2)%3])+a[i][j]
print(max(d[-1]))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# Vacation

'''
3
10 40 70
20 50 80
30 60 90
'''

N = int(input())

dp = [[None] * 3 for _ in range(N)]
dp[0] = list(map(int, input().split()))
# print(dp)

for i in range(1, N):
    data = list(map(int, input().split()))
    # print(data)

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

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

Submission status: Accepted

Solution code:
n = int(input())
choice = [list(map(int, input().split())) for _ in range(n)]
dp = [[0 for _ in range(3)] for __ in range(n)]
dp[0] = choice[0]

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

Submission status: Accepted

Solution code:
from sys import stdin
input = stdin.readline

def main():
    N = int(input())
    reward = [list(map(int, input().split())) for _ in range(N)]
    
    dp = [[0] * 3 for _ in range(N)]
    dp[0] = reward[0]
    
    for i in range(1, N):
        dp[i][0] = max(dp[i - 1][1], dp[i - 1][2]) + reward[i][0]
        dp[i][1] = max(dp[i - 1][0], dp[i - 1][2]) + reward[i][1]
        dp[i][2] = max(dp[i - 1][1], dp[i - 1][0]) + reward[i][2]
    
    print(max(dp[-1]))
    
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n=int(input())
a=[0]*(n)
b=[0]*(n)
c=[0]*(n)
for _ in range(n):
    a[_],b[_],c[_]=map(int,input().split())

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

dp[0] = [a[0],b[0],c[0]]

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

Submission status: Accepted

Solution code:
N = int(input())

dpa,dpb,dpc = 0,0,0
for n in range(N):
  a,b,c = map(int, input().split())  
  dpa,dpb,dpc = max(dpb+a,dpc+a),max(dpa+b,dpc+b),max(dpa+c,dpb+c)
  
ans = max(dpa,dpb,dpc)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
ans = [0,0,0]
temp = [0,0,0]
for i in range(n):
    a = list(map(int,input().split()))
    temp[0] = max(ans[1],ans[2])+a[0]
    temp[1] = max(ans[0],ans[2])+a[1]
    temp[2] = max(ans[0],ans[1])+a[2]
    for i in range(3):
        ans[i] = temp[i]
print(max(ans))

Submission status: Accepted

Solution code:
N = int(input())
lst = []
for i in range(N):
    lst.append([int(x) for x in input().split()])
dp = [[0] * 3 for i in range(N + 1)]
for i in range(N):
    dp[i + 1][0] = max(dp[i][1], dp[i][2]) + lst[i][0]
    dp[i + 1][1] = max(dp[i][0], dp[i][2]) + lst[i][1]
    dp[i + 1][2] = max(dp[i][1], dp[i][0]) + lst[i][2]

print(max(dp[-1]))

Submission status: Accepted

Solution code:
n = int(input())
alist = [0]*n#umi
blist = [0]*n#yama
clist = [0]*n#HW
for i in range(n):
  alist[i],blist[i],clist[i] = map(int,input().split())

dpa = [alist[0]] + [0]*(n-1)
dpb = [blist[0]] + [0]*(n-1)
dpc = [clist[0]] + [0]*(n-1)
for i in range(1,n):
  dpa[i] = max(dpb[i-1], dpc[i-1]) + alist[i]
  dpb[i] = max(dpa[i-1], dpc[i-1]) + blist[i]
  dpc[i] = max(dpb[i-1], dpa[i-1]) + clist[i]
print(max(dpa[-1], dpb[-1], dpc[-1]))

Submission status: Accepted

Solution code:
N = int(input())

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

DP = [[0]*3 for i in range(N+1)]

for i in range(N):
    DP[i+1][0] = A[i][0] + max(DP[i][1],DP[i][2])
    DP[i+1][1] = A[i][1] + max(DP[i][0],DP[i][2])
    DP[i+1][2] = A[i][2] + max(DP[i][0],DP[i][1])

print(max(DP[N]))

Submission status: Accepted

Solution code:
N = int(input())

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

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

for i, abc in enumerate(ABC):
  for k in range(3):
    if (i == 0):
      dp[i][k] = abc[k]
    else:
      max_dp_i = dp[i][k]
      for j in range(3):
        if (k == j):
          continue
        if (max_dp_i < (dp[i-1][j] + abc[k])):
          max_dp_i = (dp[i-1][j] + abc[k])
      
      dp[i][k] = max_dp_i

print(max(dp[-1]))

Submission status: Accepted

Solution code:
N = int(input())
table = [ list(map(int,input().split())) for i in range(N)]
dp = [[0 for i in range(3)] for j in range(N)]

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

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

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

Submission status: Accepted

Solution code:
N = int(input())

la,lb,lc = 0,0,0

for _ in range(N):
    a,b,c = map(int,input().split())
    la, lb, lc = a+max(lb,lc), b+max(lc,la), c+max(la,lb)
print(max(la,lb,lc))

Submission status: Accepted

Solution code:
def solve(n, acts):
    fa = [0] * n
    fb = [0] * n
    fc = [0] * n
    fa[0] = acts[0][0]
    fb[0] = acts[0][1]
    fc[0] = acts[0][2]

    for i in range(1, n):
        fa[i] = acts[i][0] + max(fb[i-1], fc[i-1])
        fb[i] = acts[i][1] + max(fc[i-1], fa[i-1])
        fc[i] = acts[i][2] + max(fa[i-1], fb[i-1])

    return max([fa[-1], fb[-1], fc[-1]])


def main():
    inp = lambda: [int(x) for x in input().split()]
    n = int(input())
    acts = [inp() for _ in range(n)]
    print(solve(n, acts))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
utility = [tuple(map(int, input().split())) for _ in range(N)]
dp = [[0] * 3 for _ in range(N+1)]
for i, u in enumerate(utility):
    a, b, c = u
    dp[i+1][0] = a + max(dp[i][1], dp[i][2])
    dp[i+1][1] = b + max(dp[i][0], dp[i][2])
    dp[i+1][2] = c + max(dp[i][0], dp[i][1])
print(max(dp[N]))

Submission status: Accepted

Solution code:
n=int(input())
l=[]
for i in range(n):
    l.append(list(map(int,input().split())))
dp=[]
for i in range(n):
    dp.append([0]*3)
    
dp[n-1][0]=l[n-1][0]
dp[n-1][1]=l[n-1][1]
dp[n-1][2]=l[n-1][2]
for i in range(n-2,-1,-1):

    dp[i][0]=l[i][0]+max(dp[i+1][1],dp[i+1][2])
    dp[i][1]=l[i][1]+max(dp[i+1][0],dp[i+1][2])
    dp[i][2]=l[i][2]+max(dp[i+1][1],dp[i+1][0])
print(max(max(dp[0][0],dp[0][1]),dp[0][2]))

Submission status: Accepted

Solution code:
n=int(input())
score=[[]for i in range(n)]
for i in range(n):
    x,y,z=map(int,input().split())
    score[i]=(x,y,z)
dp=[[0]*3 for i in range(n)]
for i in range(3):
    dp[0][i]=score[0][i]
#dp[i][j] i日目にjを選ぶ時ありうる最大のスコア
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]+score[i][j])
print(max(dp[-1]))