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:
import sys
import itertools
from operator import itemgetter #sortedの対象を決めたい
from fractions import gcd #最大公約数
from math import ceil, floor, sqrt, isinf #小数点切り上げ、切り捨て、平方根
from copy import deepcopy #参照で影響されないコピー
from collections import Counter, deque #要素ごとの出現回数、双方向アクセス可能データ型
import heapq
import numpy as np
from functools import reduce
sys.setrecursionlimit(200000)

input = sys.stdin.readline
# template

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

    a, b, c = 0, 0, 0
    for x, y, z in abc:
        a, b, c = max(b, c) + x, max(a, c) + y, max(a, b) + z

    print(max(a, b, c))



if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
import itertools
from operator import itemgetter #sortedの対象を決めたい
from fractions import gcd #最大公約数
from math import ceil, floor, sqrt, isinf #小数点切り上げ、切り捨て、平方根
from copy import deepcopy #参照で影響されないコピー
from collections import Counter, deque #要素ごとの出現回数、双方向アクセス可能データ型
import heapq
import numpy as np
from functools import reduce
sys.setrecursionlimit(200000)

input = sys.stdin.readline
# template

def retmax(a, b):
    if a > b:
        return a
    else:
        return b

def main():
    n = int(input())
    a = [list(map(int, input().split())) for _ in range(n)]

    dp = np.zeros([3, n+1])

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

    print(int(np.max(dp[:, n], axis=0)))


if __name__ == "__main__":
    main()

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) ]

dp[0] = abc[0]

for i in range(1,n):
    # print("i={}".format(i))
    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:
import sys
import numpy as np

input = sys.stdin.readline

# N 夏休みの日数
n = int(input())

# 太郎君のとる行動
act = [list(map(int, input().split())) for ii in range(n)]

# 最大値なので0に初期化
dp = np.zeros([3, n+1])

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

for i, a_l in enumerate(act):
    # i 夏休みの日数分
    for j in range(3):
        # j 前日にとった行動
        for k in range(3):
            # k 今日とる行動
            if j != k:
                # 前日と同じ鼓動はしない。前日と違う行動のみ。
                dp[k, i+1] = retmax(dp[k, i+1], dp[j, i]+a_l[k])
print(int(np.max(dp[:, n], axis=0)))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
days = int(input())
activities = []

for _ in range(days):
	activities.append([int(x) for x in input().split()])

a, b, c = 0, 0, 0
for x, y, z in activities:
	a, b, c = max(b, c) + x, max(a, c) + y, max(a, b) + z
print(max(a, b, c))

Submission status: Accepted

Solution code:
n = int(input())
a,b,c = [0]*n,[0]*n,[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[i][j] i~n j~2
dp[0] = [a[0],b[0],c[0]] #初期値代入
for i in range(n-1):
    #print()
    dp[i+1][0] = max(dp[i+1][0], dp[i][1]+a[i+1], dp[i][2]+a[i+1])
    dp[i+1][1] = max(dp[i+1][1], dp[i][0]+b[i+1], dp[i][2]+b[i+1])
    dp[i+1][2] = max(dp[i+1][2], dp[i][0]+c[i+1], dp[i][1]+c[i+1])
    
    #for day in dp:
        #print(*day)
print(max(dp[-1]))

Submission status: Accepted

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

dp=[[-1]*3 for i in range(n)]
dp[0][0]=action[0][0]
dp[0][1]=action[0][1]
dp[0][2]=action[0][2]

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

print(max(dp[-1]))

Submission status: Accepted

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

    # print(N)
    # print(ABC)

    return N, ABC


def solve(N, ABC):
    x, y = N + 1, 3
    DP = [[0 for _ in range(y)] for _ in range(x)]

    for i in range(N):
        for a in range(3):
            if a == 0:
                DP[i + 1][1] = max(DP[i + 1][1], DP[i][a] + ABC[i][1])
                DP[i + 1][2] = max(DP[i + 1][2], DP[i][a] + ABC[i][2])
            elif a == 1:
                DP[i + 1][0] = max(DP[i + 1][0], DP[i][a] + ABC[i][0])
                DP[i + 1][2] = max(DP[i + 1][2], DP[i][a] + ABC[i][2])
            elif a == 2:
                DP[i + 1][0] = max(DP[i + 1][0], DP[i][a] + ABC[i][0])
                DP[i + 1][1] = max(DP[i + 1][1], DP[i][a] + ABC[i][1])

    # print(DP)
    return max(DP[-1])


if __name__ == '__main__':
    res = solve(*args())
    print(res)

Submission status: Accepted

Solution code:
N = int(input())
A, B, C = [], [], []
for n in range(N):
    a, b, c = map(int, input().split())
    A.append(a)
    B.append(b)
    C.append(c)
a, b, c = 0, 0, 0
for n in range(N):
    a, b, c = A[n] + max(b, c), B[n] + max(c, a), C[n] + max(a, b)
print(max(a, b, c))

Submission status: Accepted

Solution code:
n = int(input())
a =[]
b =[]
c =[]
for i in range(n):
    x1, y1, z1 = [int(i) for i in input().split()]
    a.append(x1)
    b.append(y1)
    c.append(z1)

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

dp[0][0] = a[0]
dp[0][1] = b[0]
dp[0][2] = 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())
dp = [int(i) for i in input().split()]
for i in range(n-1):
    a, b, c = map(int, input().split())
    a0 = dp[0]
    b0 = dp[1]
    c0 = dp[2]
    
    dp[0] = max(b0, c0) + a
    dp[1] = max(c0, a0) + b
    dp[2] = max(a0, b0) + c
print(max(dp))

Submission status: Accepted

Solution code:
#vacation
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):
    x,y,z=map(int,input().split())
    a[i]=x
    b[i]=y
    c[i]=z
max_a=[0 for i in range(n+1)]
max_b=[0 for i in range(n+1)]
max_c=[0 for i in range(n+1)]

#max_a[i]=i日めにちょうどaの過ごし方をしたときの、考えうる最小の値
max_a[1]=a[0]
max_b[1]=b[0]
max_c[1]=c[0]
for i in range(2,n+1):
    max_a[i]=max(max_b[i-1]+a[i-1],max_c[i-1]+a[i-1])
    max_b[i]=max(max_a[i-1]+b[i-1],max_c[i-1]+b[i-1])
    max_c[i]=max(max_a[i-1]+c[i-1],max_b[i-1]+c[i-1])
print(max(max_a[-1],max_b[-1],max_c[-1]))

Submission status: Accepted

Solution code:
N = int(input())
A = {}
B = {}
C = {}

for i in range(N):
    A[i+1], B[i+1], C[i+1] = map(int, input().split())

dp = {}

dp[1] = {}
dp[1][0] = A[1]
dp[1][1] = B[1]
dp[1][2] = C[1]

for i in range(2, N+1):
    dp[i] = {}
    dp[i][0] = A[i]+max(dp[i-1][1],dp[i-1][2])
    dp[i][1] = B[i]+max(dp[i-1][0],dp[i-1][2])
    dp[i][2] = C[i]+max(dp[i-1][0],dp[i-1][1])

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

Submission status: Accepted

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

inf = 10**4 * 10**5

dp = [[inf] * 3 for i in range(n)] 
dp[0][0] = p[0][0]
dp[0][1] = p[0][1]
dp[0][2] = p[0][2]

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

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

Submission status: Accepted

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

INF = 10**10
dp = []
for i in range(N):
    dp.append([0,0,0])

dp[0] = act[0]

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

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

Submission status: Accepted

Solution code:
from sys import stdin

N = int(stdin.readline().rstrip())
vac = []
for i in range(N):
    vac.append([int(x) for x in stdin.readline().rstrip().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:
                continue
            dp[i+1][k] = max(dp[i+1][k], dp[i][j] + vac[i][k])
print(max(dp[N]))

Submission status: Accepted

Solution code:
n = int(input())
abc = [tuple(map(int, input().split())) for _ in range(n)]
dp_a = [0]*n
dp_b = [0]*n
dp_c = [0]*n

dp_a[0], dp_b[0], dp_c[0] = abc[0]

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

Submission status: Accepted

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip() 

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

    # dp[i]: i日目にa,b,cを選んだ場合のまでの最大幸福度
    dp = [[0, 0, 0] for _ in range(N)]
    dp[0] = [abc[0][0], abc[0][1], abc[0][2]]

    for i in range(1, N):
        prev = dp[i-1]
        preva = prev[0]
        prevb = prev[1]
        prevc = prev[2]
        abci = abc[i]
        a = abci[0]
        b = abci[1]
        c = abci[2]
        dp[i] = [max(prevb+a, prevc+a), max(prevc+b, preva+b), max(preva+c, prevb+c)]

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

if __name__ == '__main__':
    resolve()

Submission status: Accepted

Solution code:
N=int(input())
A=[]
B=[]
C=[]
for i in range(N):
  l1,l2,l3=map(int,input().split())
  A+=[l1]
  B+=[l2]
  C+=[l3]
dp=[[0,0,0] for i in range(N)]#dp[i][j]はi+1日目にアクティビティjを選んだ時の活動終了時の幸福量
dp[0][0]=A[0]
dp[0][1]=B[0]
dp[0][2]=C[0]
for i in range(1,N):
  dp[i][1]=max(dp[i-1][2]+B[i],dp[i-1][0]+B[i])
  dp[i][0]=max(dp[i-1][1]+A[i],dp[i-1][2]+A[i])
  dp[i][2]=max(dp[i-1][1]+C[i],dp[i-1][0]+C[i])
ans=max(dp[N-1])
print(ans)

Submission status: Accepted

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

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

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())

a = list()
b = list()
c = list()

for i in range(N):
    tmp_a, tmp_b, tmp_c = map(int,input().split())
    a.append(tmp_a)
    b.append(tmp_b)
    c.append(tmp_c)

dp = [[0 for i in range(3)] for j 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(dp[i-1][1]+a[i], dp[i-1][2]+a[i])
    dp[i][1] = max(dp[i-1][0]+b[i], dp[i-1][2]+b[i])
    dp[i][2] = max(dp[i-1][0]+c[i], dp[i-1][1]+c[i])

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

Submission status: Accepted

Solution code:
import math
import fractions
import bisect
import collections
import itertools
import heapq
import string
import sys
import copy
from collections import deque
sys.setrecursionlimit(10**7)
MOD = 10**9+7
def gcd(a,b):return fractions.gcd(a,b) #最大公約数
def lcm(a,b):return (a*b) // fractions.gcd(a,b) #最小公倍数
def iin(): return int(input()) #整数読み込み
def ifn(): return float(input()) #浮動小数点読み込み
def isn(): return input().split() #文字列読み込み
def imn(): return map(int, input().split()) #整数map取得
def fmn(): return map(float, input().split()) #浮動小数点map取得
def iln(): return list(map(int, input().split())) #整数リスト取得
def iln_s(): return sorted(iln()) # 昇順の整数リスト取得
def iln_r(): return sorted(iln(), reverse=True) # 降順の整数リスト取得
def fln(): return list(map(float, input().split())) # 浮動小数点リスト取得
def join(l, s=''): return s.join(l) #リストを文字列に変換
def perm(l, n): return itertools.permutations(l, n) # 順列取得
def perm_count(n, r): return math.factorial(n) // math.factorial(n-r) # 順列の総数
def comb(l, n): return itertools.combinations(l, n) # 組み合わせ取得
def comb_count(n, r): return math.factorial(n) // (math.factorial(n-r) * math.factorial(r)) #組み合わせの総数
def two_distance(a, b, c, d): return ((c-a)**2 + (d-b)**2)**.5 # 2点間の距離
def m_add(a,b): return (a+b) % MOD

N = iin()
abc = [iln() for _ in range(N)]

dp = [[0 for _ in range(3)] 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][k] = max(dp[i+1][k], dp[i][j]+abc[i][k])

print(max(dp[N]))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
n = int(input())
abc = [tuple(map(int,input().split())) for i in range(n)]

dp_a = [0]*n
dp_b = [0]*n
dp_c = [0]*n

a,b,c = abc[0]
dp_a[0] = a
dp_b[0] = b
dp_c[0] = c

for i in range(1,n):
    a, b, c = abc[i]
    dp_a[i] = max(dp_b[i-1]+a, dp_c[i-1]+a)
    dp_b[i] = max(dp_c[i-1]+b, dp_a[i-1]+b)
    dp_c[i] = max(dp_a[i-1]+c, dp_b[i-1]+c)

print(max(dp_a[-1], dp_b[-1], dp_c[-1]))

Submission status: Accepted

Solution code:
N = int(input())
A, B, C = [0]*N, [0]*N, [0]*N 
for i in range(N):
    A[i], B[i], C[i] = [int(j) for j in input().split()]

ABC = [A, B, C]
dp = [[0]*3 for _ in range(N+1)]

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

print(max(dp[-1]))

Submission status: Accepted

Solution code:
N = int(input())
A, B, C = [0]*N, [0]*N, [0]*N 
for i in range(N):
    A[i], B[i], C[i] = [int(j) for j in input().split()]
    
dp = [[0]*3 for _ in range(N+1)]

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

Submission status: Accepted

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

Submission status: Accepted

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

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

    dp = [0] * 3

    for i in range(N):
        a, b, c = table[i]

        a0 = dp[0]
        b0 = dp[1]
        c0 = dp[2]

        dp[0] = max(b0, c0) + a
        dp[1] = max(c0, a0) + b
        dp[2] = max(a0, b0) + c

    print(max(dp))

main()

Submission status: Accepted

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

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



for i in range(len(plan)-1):
    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] + plan[i+1][k])
print(max(dp[-1]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
C=[[0]*N for i in range(3)]
S=list(map(int,input().split()))
C[0][0]=S[0]
C[1][0]=S[1]
C[2][0]=S[2]

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

ans=C[0][N-1]
if(ans<C[1][N-1]):
    ans=C[1][N-1]
if(ans<C[2][N-1]):
    ans=C[2][N-1]
print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(100002)


N = int(input())
act = [list(map(int, input().split())) for _ in range(N)]
dp = [[-float('inf') for _ in range(3)] for _ in range(N)]


def dp_rec(day, choice):

    if dp[day][choice] > -float('inf'):
        return dp[day][choice]

    v = -float('inf')
    for c in range(3):
        if c != choice:
            v = max(v, dp_rec(day - 1, c) + act[day][choice])

    dp[day][choice] = v
    return v


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

Submission status: Accepted

Solution code:
n = int(input())
li = list()

for i in range(n):
    li.append(list(map(int, input().split())))

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



for i in range(n):
    for j in range(3):
        #dp[i+1][j]を更新する
        maxValue = 0
        for k in range(3):
            if k== j:
                continue
            maxValue = max(maxValue, dp[i][k])
            
        
        dp[i+1][j] = maxValue + li[i][j]
        
print(max(dp[n][0],dp[n][1], dp[n][2]))

Submission status: Accepted

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

print(max(dp[n]))

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
dpB = [0]*n
dpC = [0]*n

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())

table = []
for _ in range(n):
    table.append([int(v) for v in input().split()])

dp = [0] * 3

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

    a, b, c = table[i]

    a0 = dp[0]
    b0 = dp[1]
    c0 = dp[2]

    dp_n[0] = max(b0, c0) + a
    dp_n[1] = max(c0, a0) + b
    dp_n[2] = max(a0, b0) + c

    dp = dp_n

print(max(dp))

Submission status: Accepted

Solution code:
n = int(input())
s = []
for i in range(n):
      s.append(tuple(map(int,input().split())))
dp = [s[0][0],s[0][0],s[0][1],s[0][1],s[0][2],s[0][2]]
sel = (0,0,1,1,2,2)
pre = ((2,3),(4,5),(0,1),(4,5),(0,1),(2,3))
p = list(dp)
for i in range(1,n):
      for j in range(6):
            dp[j] = max(p[pre[j][0]],p[pre[j][1]]) + s[i][sel[j]]
      p = list(dp)
print(max(dp))

Submission status: Accepted

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

dp = [[0] * (n+1) for _ in range(3)]
#dp[k][i]:=i日目までの最大の幸福度、ただしkはi日目の選んだ行動

#初期条件 dpテーブルを作るときに０で作っているのでいらない
dp[0][0] = dp[1][0] = dp[2][0] = 0

#漸化式
'''
dp[j][i] = dp[k][i-1] + j *jは幸福度が最大となるようなi+1日目の行動、j ￢= k
'''
for i in range(n):
    for j in range(3):
        for k in range(3):
            if k == j:continue
            dp[k][i+1] = max(dp[k][i+1],dp[j][i] + happy[i][k])

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

Submission status: Accepted

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

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

c = [0, 1, 2]   # choice
for i in range(1, N):
    for j in range(3):
        c.remove(j)
        if dp[i - 1][c[0]] > dp[i - 1][c[1]]:
            dp[i][j] = dp[i - 1][c[0]] + act[i][j]
        else:
            dp[i][j] = dp[i - 1][c[1]] + act[i][j]
        c.append(j)

print(max(dp[-1]))

Submission status: Accepted

Solution code:
import itertools
import math
import sys
from collections import Counter
from fractions import gcd
from functools import reduce
import sys
sys.setrecursionlimit(4100000)

INF = 1 << 60

# ここから書き始める
n = int(input())
x = [[int(j) for j in input().split()] for i in range(n)]
# print(x)
dp = [[-INF for j in range(3)] for i in range(n)]
for i in range(3):
    dp[0][i] = x[0][i]
for i in range(1, n):
    dp[i][0] = max(dp[i][0], dp[i - 1][1] + x[i][0])
    dp[i][0] = max(dp[i][0], dp[i - 1][2] + x[i][0])
    dp[i][1] = max(dp[i][1], dp[i - 1][0] + x[i][1])
    dp[i][1] = max(dp[i][1], dp[i - 1][2] + x[i][1])
    dp[i][2] = max(dp[i][2], dp[i - 1][0] + x[i][2])
    dp[i][2] = max(dp[i][2], dp[i - 1][1] + x[i][2])

print(max(dp[-1]))

Submission status: Accepted

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

dp = [[0] * 3 for _ in range(N+1)]
for i, v in enumerate(ABC):
  for j in range(3): # prev
    for k in range(3): # cand
      if j == k:
        continue
      dp[i][k] = max(dp[i-1][j] + v[k], dp[i][k])
print(max(dp[N-1]))

Submission status: Accepted

Solution code:
N = int(input())

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

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

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

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

Submission status: Accepted

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


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys

N = int(sys.stdin.readline())
ABC = [[None]*3 for i in range(N)]

for i in range(N):
    ABC[i][0], ABC[i][1], ABC[i][2] = map(int, sys.stdin.readline().split())

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

for i in range(N):
    for j in range(3):
        happiness[i+1][j] = max(happiness[i][j-1] + ABC[i][j], happiness[i][j-2] + ABC[i][j])

print(max(happiness[N]))

Submission status: Accepted

Solution code:
N = int(input())
ABC = [[None]*3 for i in range(N)]

for i in range(N):
    ABC[i][0], ABC[i][1], ABC[i][2] = map(int, input().split())

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

for i in range(N):
    for j in range(3):
        happiness[i+1][j] = max(happiness[i][j-1] + ABC[i][j], happiness[i][j-2] + ABC[i][j])

print(max(happiness[N]))

Submission status: Accepted

Solution code:
# dp[i日目][i-1日目にどれを選んだか] = i日目までの最大の幸福度
# これはループでやってみたバージョン

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

dp = [[-1 for _ in range(3)] for _ in range(n + 1)]
# 初期値、最初間違えて0にしてた
dp[0][0] = a[0]
dp[0][1] = b[0]
dp[0][2] = c[0]
for i in range(1, n):
    for j in range(3):
        if j == 0:
            dp[i][j] = a[i] + max(dp[i - 1][1], dp[i - 1][2])
        elif j == 1:
            dp[i][j] = b[i] + max(dp[i - 1][0], dp[i - 1][2])
        elif j == 2:
            dp[i][j] = c[i] + max(dp[i - 1][1], dp[i - 1][0])
print(max(dp[n - 1][0], dp[n - 1][1], dp[n - 1][2]))

Submission status: Accepted

Solution code:
# dp[i日目][i-1日目にどれを選んだか] = i日目までの最大の幸福度
import sys
sys.setrecursionlimit(10 ** 8)

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

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


def rec(i, choice):
    if dp[i][choice] > 0:
        return dp[i][choice]
    if i == n:
        return 0
    ret = 0  # ループじゃなくてifでやってるのでそれぞれ確実に大きい値が入る（ので更新する必要がない）
    if choice == 0:
        ret = a[i] + max(rec(i + 1, 1), rec(i + 1, 2))
    elif choice == 1:
        ret = b[i] + max(rec(i + 1, 0), rec(i + 1, 2))
    elif choice == 2:
        ret = c[i] + max(rec(i + 1, 0), rec(i + 1, 1))
    dp[i][choice] = ret
    return ret


print(max(rec(0, 0), rec(0, 1), rec(0, 2)))

Submission status: Accepted

Solution code:
n = int(input())

days = []
for _ in range(n):
  a = list(map(int,input().split()))
  days.append(a)
  
dp = [0]*3

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

for cur_day in range(1,n):
  m = [0]*3
  for i in range(3):
    for j in range(3):
      if i!=j:
        m[i] = max(m[i],dp[j]+days[cur_day][i])
  dp = m

print(max(dp))

Submission status: Accepted

Solution code:
n = int(input())


days = [] 
for _ in range(n):
  temp = list(map(int,input().split()))
  days.append(temp)
  
dp = [0]*3

for i in range(3):
  dp[i]= days[0][i]
#print(dp) 
for cur_day in range(1,n):
  m = [0]*3
  for i in range(3):
    for j in range(3):
      if i!=j:
        m[i] = max(m[i],dp[j]+days[cur_day][i])
  dp = m
#  print(dp)
  
print(max(dp))

Submission status: Accepted

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

dp = [[-1, -1, -1] for _ in range(N)]
dp[0] = [abc[0][0], abc[0][1], abc[0][2]]

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

Submission status: Accepted

Solution code:
INF = float('inf')
MAX_N = 10**5

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

# dp table
# dp[i][j] := 状態jにおけるi日目に得る幸福度の総和の最大値
dp = [[0]*3 for _ in range(N)]

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

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

Submission status: Accepted

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

Submission status: Accepted

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

dp = [[0, 0, 0] for _ in range(n+1)]
dp[0] = [List[0][0], List[0][1], List[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]+List[i][j])

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

Submission status: Accepted

Solution code:
n = int(input())

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

INF = -10 ** 18

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

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

print(max(dp[-1]))

Submission status: Accepted

Solution code:
#1:初期化
N = int(input())
a = []
r_append = a.append
for i in range(N):
    r_append(list(map(int,input().split())))
dp = [[0]*3 for _ in range (N+1)]
#print(dp, sep="\n")
#print(a, sep="\n")

#2:初期値(元からゼロなので記述無し)

#3:dp実行部(貰うDP)
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][j]) #dpは[1]からだが，aは[0]からに注意

#4:出力
dpmax = 0
for i in range(3):
    dpmax= max(dp[N][i], dpmax)
#print(*dp, sep="\n")
print(dpmax)

Submission status: Accepted

Solution code:
#1:初期化
N = int(input())
a = [None]*N
for i in range(N):
    a[i] = list(map(int,input().split()))
dp = [[0]*3 for _ in range (N+1)]
#print(dp, sep="\n")
#print(a, sep="\n")

#2:初期値(元からゼロなので記述無し)

#3:dp実行部(貰うDP)
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][j]) #dpは[1]からだが，aは[0]からに注意

#4:出力
dpmax = 0
for i in range(3):
    dpmax= max(dp[N][i], dpmax)
#print(*dp, sep="\n")
print(dpmax)

Submission status: Accepted

Solution code:
#1:初期化
N = int(input())
a = []
for i in range(N):
    a.append(list(map(int,input().split())))
dp = [[0]*3 for _ in range (N+1)]
#print(dp, sep="\n")
#print(a, sep="\n")

#2:初期値(元からゼロなので記述無し)

#3:dp実行部(貰うDP)
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][j]) #dpは[1]からだが，aは[0]からに注意

#4:出力
dpmax = 0
for i in range(3):
    dpmax= max(dp[N][i], dpmax)
#print(*dp, sep="\n")
print(dpmax)

Submission status: Accepted

Solution code:
A=B=C=0;M=max
for i in range(int(input())):
    a,b,c=map(int,input().split())
    A,B,C=a+M(B,C),b+M(C,A),c+M(A,B)
print(M(A,B,C))

Submission status: Accepted

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