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:
def main_C1():

  N = int(input())
  a = [[int(item) for item in input().split()] for i in range(N)]
  
  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:
          continue
        dp[i+1][k] = max(dp[i+1][k], dp[i][j] + a[i][k])
  
  print(max(dp[-1]))

if __name__ == "__main__":
  main_C1()
  # print(list(input_gen()))

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

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
abc = []
for i in range(N):
    abc.append(list(map(int, input().split())))
    
dp = np.zeros(shape=(N, 3), dtype=int)
dp[0, :] = abc[0]
for i in range(N-1):
    dp[i+1, 0] = max(dp[i+1, 0], dp[i, 1] + abc[i+1][0], dp[i, 2] + abc[i+1][0])
    dp[i+1, 1] = max(dp[i+1, 1], dp[i, 0] + abc[i+1][1], dp[i, 2] + abc[i+1][1])
    dp[i+1, 2] = max(dp[i+1, 2], dp[i, 0] + abc[i+1][2], dp[i, 1] + abc[i+1][2])
print(max(dp[N-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)
def vac(n):
    if n==1:
        return max(A[0],B[0],C[0])
    else:
        dp=[[0]*3 for i 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],dp[i][2])+A[i+1]
            dp[i+1][1]=max(dp[i][0],dp[i][2])+B[i+1]
            dp[i+1][2]=max(dp[i][0],dp[i][1])+C[i+1]
        return max(dp[n-1])
print(vac(n))

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = []
    B = []
    C = []
    for _ in range(N):
        a, b, c = [int(x) for x in input().split()]
        A.append(a)
        B.append(b)
        C.append(c)
    
    dp = [[0 for i in range(3)] for i 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] = 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][1], dp[i - 1][0])
    
    print(max(dp[N - 1]))
  
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# O(N)

N = int(input())
# abc[k]: (k+1)日目の行動
abc = [[int(i) for i in input().split()] for j in range(N)]

# dp[i][j]: i日目の行動jで得られる幸福度の総和の最大値
# (j= 0:a, 1:b, 2:c)
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])

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

Submission status: Accepted

Solution code:
def main():
    n=int(input())
    a=[]
    for _ in range(n):
        a.append(list(map(int,input().split())))
    dp=[]
    dp.append([a[0][0],a[0][1],a[0][2]])
    for i in range(1,n):
        tmpa=max(dp[i-1][1],dp[i-1][2])+a[i][0]
        tmpb=max(dp[i-1][0],dp[i-1][2])+a[i][1]
        tmpc=max(dp[i-1][0],dp[i-1][1])+a[i][2]
        dp.append([tmpa,tmpb,tmpc])
    print(max(dp[n-1]))
if __name__=='__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
lst=[]
for i in range(n):
    lst.append([int(i) for i in input().split()])
dp=[[0,0,0] for i in range(n)]
dp[0]=lst[0]

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

Submission status: Accepted

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

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


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

        
print(max(dp[-1]))

Submission status: Accepted

Solution code:
import numpy as np
n=int(input())
ABC=[list(map(int,input().split())) for _ in range(n)]

dp=np.zeros((n,3),int)
for i in range(3):
  dp[0,i]=ABC[0][i]
for i in range(1,n):
  for j in range(3):
    dp[i,j]=max(dp[i-1,(j+1)%3]+ABC[i][j],dp[i-1,(j+2)%3]+ABC[i][j])
print(max(dp[-1]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
n = int(input())
abc_list = []

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

pre_suma,pre_sumb,pre_sumc = abc_list[0][0],abc_list[0][1],abc_list[0][2]

if n == 1:
    print(max(pre_suma,pre_sumb,pre_sumc))
    sys.exit()

for i in range(1,n):
    a,b,c = abc_list[i][0],abc_list[i][1],abc_list[i][2]
    suma = a + max(pre_sumb,pre_sumc)
    sumb = b + max(pre_suma,pre_sumc)
    sumc = c + max(pre_suma,pre_sumb)
    pre_suma,pre_sumb,pre_sumc = suma,sumb,sumc
    # print(suma,sumb,sumc)
print(max(suma,sumb,sumc))

Submission status: Accepted

Solution code:
if __name__ == '__main__':

	n = int(input())
	res = [[0 for i in range(0, 3)] for i in range(0, n+1)]

	for i in range(1,n+1):
		x, y, z =  map(int,input().split())
		res[i][0] = max(res[i-1][1], res[i-1][2])
		res[i][1] = max(res[i-1][0], res[i-1][2])
		res[i][2] = max(res[i-1][0], res[i-1][1])
		res[i][0]+=x
		res[i][1]+=y
		res[i][2]+=z

	r = max(res[-1])
	print(r)

Submission status: Accepted

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


def main():
    N = int(input())
    A = []
    B = []
    C = []
    for _ in range(N):
        a, b, c = [int(x) for x in input().split()]
        A.append(a)
        B.append(b)
        C.append(c)
    
    dp = [[0 for i in range(3)] for i 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] = 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][1], dp[i - 1][0])
    
    print(max(dp[N - 1]))




if __name__ == '__main__':
    main()

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)]
dp[0][0] = abc[0][0]
dp[0][1] = abc[0][1]
dp[0][2] = abc[0][2]
for n in range(1, N):
    dp[n][0] = max(dp[n-1][1]+abc[n][0], dp[n-1][2]+abc[n][0])
    dp[n][1] = max(dp[n-1][0]+abc[n][1], dp[n-1][2]+abc[n][1])
    dp[n][2] = max(dp[n-1][0]+abc[n][2], dp[n-1][1]+abc[n][2])
print(max(dp[-1]))

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

Submission status: Accepted

Solution code:
#C
N = int(input())
abc = [list(map(int,input().split())) for _ in range(N)]
dp = [[0]*N for _ in range(3)] #i日目の活動をa,b,cで終えた幸福度のmax
dp[0][0] = abc[0][0]
dp[1][0] = abc[0][1]
dp[2][0] = abc[0][2]

def chmax(a,b):
    if a < b:
        return b
    else:
        return a 

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


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

Submission status: Accepted

Solution code:
import numpy as np
n=int(input())
ABC=[list(map(int,input().split())) for _ in range(n)]

dp=np.zeros((n,3),int)
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][(j+1)%3]+ABC[i+1][j], dp[i][(j+2)%3]+ABC[i+1][j])
print(max(dp[-1]))

Submission status: Accepted

Solution code:
n=int(input())
abc=[list(map(int,input().split())) for _ in range(n)]
dp=[abc[0]]
for i in range(1,n):
    dp.append([])
    dp[-1].append(max(dp[i-1][1]+abc[i][0],dp[i-1][2]+abc[i][0]))
    dp[-1].append(max(dp[i-1][0]+abc[i][1],dp[i-1][2]+abc[i][1]))
    dp[-1].append(max(dp[i-1][0]+abc[i][2],dp[i-1][1]+abc[i][2]))
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] * N
for idx, val in enumerate(abc):
    a, b, c = val
    if idx == 0:
        dp[0] = val
    else:
        tmp = []
        tmp.append(max(dp[idx - 1][1] + a, dp[idx - 1][2] + a))
        tmp.append(max(dp[idx - 1][0] + b, dp[idx - 1][2] + b))
        tmp.append(max(dp[idx - 1][1] + c, dp[idx - 1][0] + c))
        dp[idx] = tmp
print(max(dp[-1]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
N = int(input())
summer = []
for i in range(N):
    summer.append(list(map(int, sys.stdin.readline().rstrip().split(" "))))

INF = float('inf')
DP = [[-INF for i in range(3)] for i in range(N)]

for i in range(N):
    if i == 0:
        DP[i] = summer[i]
    else:
        DP[i][0] = max(DP[i-1][1], DP[i-1][2]) + summer[i][0]
        DP[i][1] = max(DP[i-1][0], DP[i-1][2]) + summer[i][1]
        DP[i][2] = max(DP[i-1][0], DP[i-1][1]) + summer[i][2]

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

Submission status: Accepted

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

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

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

#print(n, active, dp)

def chmin(arr, index_a, val):
  if arr[index_a] > val:
    arr[index_a] = val
    return True
  return False

def chmax(arr, index_a, val):
  if arr[index_a] < val:
    arr[index_a] = val
    return True
  return False

ans = 0
pre_active = None
for i in range(n):
  a, b, c = active[i]
  dp_i = i + 1 # dpの添字はi日目なので、i = 0 = 初期状態, この場合 i が前日になる
  
  # i日目に a を選ぶ
  dp[dp_i][0] = max(dp[i][1] + a, dp[i][2] + a)
  # i日目に b を選ぶ
  dp[dp_i][1] = max(dp[i][0] + b, dp[i][2] + b)
  # i日目に a を選ぶ
  dp[dp_i][2] = max(dp[i][0] + c, dp[i][1] + c)

#print(dp)
print(max(dp[n]))

Submission status: Accepted

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

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] + A[i][k])
print(max(dp[N]))

Submission status: Accepted

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

for i in range(N):
    A = list(map(int, input().split()))
    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] + A[k])

print(max(dp[N]))

Submission status: Accepted

Solution code:

import sys

if sys.platform =='ios':
	sys.stdin=open('Untitled.txt')

n = int(input())
a = [[]] * n
for i in range(n):
	a[i] = [int(x) for x in input().split()]

dp = [[]]
for i in range(n):
	if i == 0:
		dp[i] = a[i]
	else:
		dp.append([0,0,0])
		dp[i][0] = max(dp[i-1][1], dp[i-1][2]) + a[i][0]
		dp[i][1] = max(dp[i-1][0], dp[i-1][2]) + a[i][1]
		dp[i][2] = max(dp[i-1][0], dp[i-1][1]) + a[i][2]

print(max(dp[n-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=[max(dp[1],dp[2])+a,max(dp[0],dp[2])+b,max(dp[0],dp[1])+c]
print(max(dp))

Submission status: Accepted

Solution code:
n = int(input())

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

dp = [[0]*3 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):
  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(i,j,(j+1)%3,(j+2)%3)
    
#print(dp)
ans = max(dp[n-1])
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8

# import numpy as np

"""
20191118
"""


# def max_pair(abc, start, unavailable=None):
#     pair = [0, 0]
#     for i in range(3):
#         if unavailable is not None:
#             if i == unavailable:
#                 continue
#         for j in range(3):
#             if j != i and abc[i][start] + abc[j][start+1] > abc[pair[0]][start] + abc[pair[1]][start+1]:
#                 pair = [i, j]

#     return pair


# def main():
#     N = int(input())
#     a = [None for _ in range(N)]
#     b = [None for _ in range(N)]
#     c = [None for _ in range(N)]
#     for i in range(N):
#         a[i], b[i], c[i] = map(int, input().split())
#     abc = [a, b, c]

#     if N == 1:
#         print(max(a[0], b[0], c[0]))
#         return None

#     first_pair = max_pair(abc, 0)

#     # first_pair = [0, 0]
#     # for i in range(3):
#     #     for j in range(3):
#     #         if j != i and abc[i][0] + abc[j][1] > abc[first_pair[0]][0] + abc[first_pair[1]][0]:
#     #             first_pair = [i, j]

#     max_sat = [None for _ in range(N)]
#     max_sat[0] = {"sat": max(a[0], b[0], c[0]), "use": np.argmax([a[0], b[0], c[0]])}
#     max_sat[1] = {"sat": abc[first_pair[0]][0] + abc[first_pair[1]][1], "use": first_pair[1]}

#     for i in range(2, N):
#         pre = max_sat[i-1]["use"]
#         available = [0, 1, 2]
#         available.remove(pre)

#         if i == 2:
#             pair = max_pair(abc, i-1)
#         else:
#             pair = max_pair(abc, i-1, max_sat[i-3]["use"])

#         candidates = [max_sat[i-1]["sat"]+abc[k][i] for k in available]
#         candidates.append(max_sat[i-2]["sat"]+abc[pair[0]][i-1]+abc[pair[1]][i])
#         max_idx = np.argmax(candidates)
#         if max_idx == 2:  # i-2から変更
#             use = pair[1]
#             max_sat[i-2] = {"sat": max_sat[i-3]["sat"]+abc[pair[0]][]}
#         else:
#             use = available[max_idx]

#         max_sat[i] = {
#             "sat": candidates[max_idx],
#             "use": use
#         }
#     print(max_sat[-1]["sat"])
#     for i in range(N):
#         print(max_sat[i])


def main():
    N = int(input())
    a = [None for _ in range(N)]
    b = [None for _ in range(N)]
    c = [None for _ in range(N)]
    for i in range(N):
        a[i], b[i], c[i] = map(int, input().split())

    satisfaction = [[0 for _ in range(N)] for _ in range(3)]
    satisfaction[0][0] = a[0]
    satisfaction[1][0] = b[0]
    satisfaction[2][0] = c[0]

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

    return max(satisfaction[0][-1], satisfaction[1][-1], satisfaction[2][-1])


if __name__ == "__main__":
    print(main())

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def chmax(a,b):
    if a > b:
        return a
    else:
        return b

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

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

dp[0][0] = A[0][0]
dp[0][1] = A[0][1]
dp[0][2] = A[0][2]

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

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

Submission status: Accepted

Solution code:
# --------------------------------------
# ライブラリ読み込み
# --------------------------------------
from sys import stdin
input = stdin.readline

# --------------------------------------
# 関数定義
# --------------------------------------


def chmax(a, b):
    """
    引数２つの値のうち、大きい値を返す

    Args:
        a (int): 比較対象１
        b (int): 比較対象２

    Returns:
        (int): a,bのうち大きい値
    """
    if a < b:
        a = b
    return a


# --------------------------------------
# メイン処理
# --------------------------------------
N = int(input())
abc = [list(map(int, input().split())) for _ in range(N)]

# メモ用テーブル初期化
dp = [[0] * 3 for _ in range(N)]
for i in range(3):
    dp[0][i] = abc[0][i]

for now_day in range(1, N):
    for select_yesterday in range(3):
        for select_today in range(3):
            if select_today == select_yesterday:
                continue
            dp[now_day][select_today] = chmax(dp[now_day][select_today], dp[now_day - 1][select_yesterday] + abc[now_day][select_today])

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

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 _ in range(N)]
for i in range(3):
    dp[0][i] = abc[0][i]
for i in range(1, N):
    a, b, c = abc[i]
    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][i] for i in range(3)))

Submission status: Accepted

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

DP[1][0] = Data[1][0]
DP[1][1] = Data[1][1]
DP[1][2] = Data[1][2]

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

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

Submission status: Accepted

Solution code:
n = int(input())
dp = [[0, 0, 0] 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][2], dp[i-1][0]) + b
    dp[i][2] = max(dp[i-1][0], dp[i-1][1]) + c

print(max(dp[i]))

Submission status: Accepted

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

    dp = [0] * 3
    n_dp = [0] * 3

    for _ in range(N):
        a = list(map(int, input().split()))
        for j in range(3):
            n_dp[j] = max(dp[j-2] + a[j], dp[j-1] + a[j])
        dp = n_dp.copy()

    print(max(dp))


main()

Submission status: Accepted

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

dp = [[0]*3]*n

dp[0]= abc[0]

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

Submission status: Accepted

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

table = [a[0]]
def dp(n, i):
  for j in range(len(table), n+1):
    table.append([0]*3)
    for k in range(3):
      h = [table[j-1][l]+a[j][k] for l in range(3) if k != l]
      table[j][k] = max(h)
  return table[n][i]

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

# dp[i][j] := i日目までにjを選んだときのmax幸福度
# j={A=0,B=1,C=2}の添え字を使うことにする
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:  # 前日と同じActivityは除外
                continue
            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=[0]*n
b=[0]*n
c=[0]*n
for i in range(n):
    a[i],b[i],c[i]=list(map(int,input().split()))
dpa=[100000000]*n
dpb=[100000000]*n
dpc=[100000000]*n
dpa[0]=a[0]
dpb[0]=b[0]
dpc[0]=c[0]
for i in range(n-1):
    dpa[i+1]=max(dpb[i]+a[i+1],dpc[i]+a[i+1])
    dpb[i+1]=max(dpa[i]+b[i+1],dpc[i]+b[i+1])
    dpc[i+1]=max(dpa[i]+c[i+1],dpb[i]+c[i+1])


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

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())
dpa=[0 for i in range(n+1)]
dpb=[0 for i in range(n+1)]
dpc=[0 for i in range(n+1)]

for i in range(n):
    dpa[i+1]=max(dpb[i]+a[i],dpc[i]+a[i])
    dpb[i+1]=max(dpa[i]+b[i],dpc[i]+b[i])
    dpc[i+1]=max(dpa[i]+c[i],dpb[i]+c[i])
#print(dpa,dpb,dpc)
print(max(dpa[n],dpb[n],dpc[n]))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np
n=int(input())
ABC=[list(map(int,input().split())) for _ in range(n)]

dp=np.zeros((n,3),int)
for i in range(n):
  for j in range(3):
    dp[i][j]=ABC[0][j] if i==0 else max(dp[i-1][(j+1)%3]+ABC[i][j], dp[i-1][(j+2)%3]+ABC[i][j])
print(max(dp[-1]))

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())
dpa=[0 for i in range(n+1)]
dpb=[0 for i in range(n+1)]
dpc=[0 for i in range(n+1)]

for i in range(n):
    dpa[i+1]=max(dpb[i]+a[i],dpc[i]+a[i])
    dpb[i+1]=max(dpa[i]+b[i],dpc[i]+b[i])
    dpc[i+1]=max(dpa[i]+c[i],dpb[i]+c[i])
#print(dpa,dpb,dpc)
print(max(dpa[n],dpb[n],dpc[n]))

Submission status: Accepted

Solution code:
import sys
n = int(sys.stdin.readline())

L = [list(map(int, sys.stdin.readline().split())) for _ in range(n)]
dp = [[0 for i in range(3)] for j in range(n)]
for col in range(3):
    dp[0][col] = L[0][col]
#print(dp)

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

Submission status: Accepted

Solution code:
n = int(input())
a,b,c = 0,0,0
for _ in range(n):
    x, y, z = map(int, input().split())
    a,b,c = max(c,b)+x,max(a,c)+y,max(a,b)+z

print(max(a,b,c))

Submission status: Accepted

Solution code:
dp = [[0] * 3 for _ in range(10 ** 5 + 2)]

N = int(input())
for i in range(1, N + 1):
    inp = [int(_) for _ in input().split()]
    for j in range(3):
        dp[i][j] = max(dp[i - 1][(j + 1) % 3], dp[i - 1][(j + 2) % 3]) + inp[j]

print(max(dp[N]))

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys


def solve():
    input = sys.stdin.readline
    mod = 10 ** 9 + 7
    n = int(input().rstrip('\n'))
    dp = [[0] * 3 for _ in range(n)]
    for i in range(n):
        a, b, c = list(map(int, input().rstrip('\n').split()))
        if i == 0:
            dp[i][0] = a
            dp[i][1] = b
            dp[i][2] = c
        else:
            dp[i][0] = max(dp[i-1][1] + a, dp[i-1][2] + a)
            dp[i][0] = max(dp[i-1][1] + a, dp[i-1][2] + a)

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

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


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
def main():
    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()) 
    dp=[[0 for _ in range(3)] for _ in range(n+1)]
    for i in range(n):
        dp[i+1][0]=max(dp[i][1]+b[i],dp[i][2]+c[i])
        dp[i+1][1]=max(dp[i][0]+a[i],dp[i][2]+c[i])
        dp[i+1][2]=max(dp[i][0]+a[i],dp[i][1]+b[i])
    print(max(dp[n][0],dp[n][1],dp[n][2]))
    

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import numpy as np
n=int(input())
ABC=[list(map(int,input().split())) for _ in range(n)]

dp=np.zeros((n,3),int)
for i in range(n):
  for j in range(3):
    dp[i][j]=ABC[0][j] if i==0 else max(dp[i-1][(j+1)%3]+ABC[i][j], dp[i-1][(j+2)%3]+ABC[i][j])
  
print(max(dp[-1]))

Submission status: Accepted

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

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

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]=list(map(int,input().split()))
dpa=[100000000]*n
dpb=[100000000]*n
dpc=[100000000]*n
dpa[0]=a[0]
dpb[0]=b[0]
dpc[0]=c[0]
for i in range(n-1):
    dpa[i+1]=max(dpb[i]+a[i+1],dpc[i]+a[i+1])
    dpb[i+1]=max(dpa[i]+b[i+1],dpc[i]+b[i+1])
    dpc[i+1]=max(dpa[i]+c[i+1],dpb[i]+c[i+1])


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

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
L = [list(map(int, input().split())) for i in range(N)]
dp = np.zeros((N, 3), dtype=int)

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

print(max(dp[-1]))

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N = int(readline())
m = map(int,read().split())
ABC = zip(m,m,m)

x,y,z = 0,0,0 # 最後の活動 -> 幸福度のmax
for a,b,c in ABC:
    x,y,z = max(y,z)+a,max(x,z)+b,max(x,y)+c

answer = max(x,y,z)
print(answer)

Submission status: Accepted

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

Submission status: Accepted

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