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():
    n = int(input())
    a = 0
    b = 0
    c = 0

    for i in range(n):
        aa,bb,cc = map(int,input().split())
        a,b,c = max(b,c)+aa, max(a,c)+bb, max(a,b)+cc

    print(max(a,b,c))

main()

Submission status: Accepted

Solution code:
def main():
  n = int(input())
  a = [0]
  b = [0]
  c = [0]

  for i in range(n):
      aa,bb,cc = map(int,input().split())
      a.append(max(b[i] + aa, c[i] + aa))
      b.append(max(a[i] + bb, c[i] + bb))
      c.append(max(a[i] + cc, b[i] + cc))

  print(max(a[-1], b[-1], c[-1]))

main()

Submission status: Accepted

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

for i in range(n):
    aa,bb,cc = map(int,input().split())
    a.append(max(b[i] + aa, c[i] + aa))
    b.append(max(a[i] + bb, c[i] + bb))
    c.append(max(a[i] + cc, b[i] + cc))
    
print(max(a[-1], b[-1], c[-1]))

Submission status: Accepted

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

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

for j in range(1, N+1):
    for k in range(3):
        if j == 1:
            dp[j][k] = abc[0][k]
        else:
            box = []
            for l in range(3):
                if l == k:
                    continue
                else:
                    box.append(dp[j-1][l] + abc[j-1][k])
            dp[j][k] = max(dp[j][k], max(box))
print(max(dp[N]))

Submission status: Accepted

Solution code:
N=int(input())
numbers=[]
for _ in range(N):
    a,b,c=input().split()
    numbers.append([int(a),int(b),int(c)])
dp = [[0 for i in range(3)] for j in range(N+1)]
def chmax(a,b):
    if a>=b:
        return a
    else:
        return b
for i in range(1,N+1):
    for j in range(3):
        K=[a for a in range(3) if a!=j]
        for k in K:
            dp[i][k]=chmax(dp[i][k],dp[i-1][j]+numbers[i-1][k])
print(max(dp[-1]))

Submission status: Accepted

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

print(max(dp[n]))

Submission status: Accepted

Solution code:
import sys
sample = sys.stdin.read().splitlines()
N = int(sample[0])
points = [list(map(int, sample[current_day].split())) for current_day in range(1, N+1)]
dp = points[0]
ans = max(dp)
for point in points[1:]:
	for idx, num in enumerate(point):
		point[idx] = point[idx]+max(dp[(idx+1)%len(point)], dp[(idx+2)%len(point)])
	dp = point
	ans = max(ans, max(point))
print(ans)

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]

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

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

Submission status: Accepted

Solution code:
def main():
    N=int(input())
    dp=[[0]*3 for _ in range(N+1)]
    abc=[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]+abc[i][k])
    print(max(dp[N]))
    
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N=int(input())
dp=[[0]*3 for _ in range(N+1)]
abc=[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]+abc[i][k])
print(max(dp[N]))

Submission status: Accepted

Solution code:
from sys import stdin
#from collections import deque
#from math import sqrt, floor, ceil, log, log2, log10, pi, gcd, sin, cos, asin
def ii(): return int(stdin.readline())
def fi(): return float(stdin.readline())
def mi(): return map(int, stdin.readline().split())
def fmi(): return map(float, stdin.readline().split())
def li(): return list(mi())
def lsi():
    x=list(stdin.readline())
    x.pop()
    return x
def si(): return stdin.readline()
############# CODE STARTS HERE #############
n=ii()
x, y, z=mi()
for _ in range(n-1):
    a, b, c=mi()
    a+=max(y, z)
    b+=max(x, z)
    c+=max(y, x)
    x, y, z=a, b, c
print(max(x, y, z))

Submission status: Accepted

Solution code:
n = int(input())
l = []

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

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

dp[0][0] = dp[0][1] = dp[0][2] = 0
dp[1][0] = l[0][0] 
dp[1][1] = l[0][1]
dp[1][2] = l[0][2]

for j in range(2,n+1):
  for k in range(3):
    dp[j][k] = max(dp[j-1][(k+1)%3] + l[j-1][k],dp[j-1][(k+2)%3] + l[j-1][k])

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# edpc c

import itertools
from math import factorial
import sys
import heapq
from collections import Counter
from collections import deque
from fractions import gcd
from functools import reduce

sys.setrecursionlimit(4100000)
INF = 1 << 60
MOD = 10 ** 9 + 7

# ここから書き始める
n = int(input())
x = [[] for i in range(n)]
for i in range(n):
    x[i] = list(map(int, input().split()))
# print(x)
dp = [[-INF for j in range(3)] for i in range(n)]
dp[0][0] = x[0][0]
dp[0][1] = x[0][1]
dp[0][2] = x[0][2]
for i in range(1, n):
    a = x[i][0]
    b = x[i][1]
    c = x[i][2]
    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][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 = [[None]*3 for _ in range(n)]
dp[0] = abc[0]
m = [[0, [1, 2]], [1, [0, 2]], [2, [0, 1]]]
for i in range(1, n):
    abci = abc[i]
    for j, k in m:
        dp[i][j] = max(dp[i-1][k[0]]+abci[j], dp[i-1][k[1]]+abci[j])
print(max(dp[-1]))

Submission status: Accepted

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

# dp[i][j] i日目にjを選んだ場合の幸福度
dp = [[0]*3 for _ in range(N+1)]

for i, a in enumerate(A):
  for j, h in enumerate(a):
    dp[i+1][j] = max(dp[i][(1+j)%3], dp[i][(2+j)%3]) + h
    
print(max(dp[N]))

Submission status: Accepted

Solution code:
n=int(input())
li1=0
li2=0
li3=0
for i in range(0,n):
    a,b,c=[int(x) for x in input().split()]
    li11=a+max(li2,li3)
    li22=b+max(li1,li3)
    li33=c+max(li1,li2)
    li1=li11
    li2=li22
    li3=li33
print(max(li1,li2,li3))

Submission status: Accepted

Solution code:
n = int(input())
#a,b,c = [0]*n,[0]*n,[0]*n
list_act = [[0 for i in range(3)] for j in range(n)]

#list_act_2 = [[0 for i in range(n)] for j in range(3)]
'''
for j in range(n):
    list_act_2[j][0],list_act_2[j][1],list_act_2[j][2] = list(map(int,input().rstrip().split(' ')))

print(list_act_2)
'''

for j in range(n):
    #list_act[day][act]

    #list_act[0][j],list_act[1][j],list_act[2][j] = list(map(int,input().rstrip().split(' ')))
    list_act[j][0],list_act[j][1],list_act[j][2] = list(map(int,input().rstrip().split(' ')))

#list_act_to_yesterday = [list_act[0][0],list_act[1][0],list_act[2][0]]
list_act_to_yesterday = [list_act[0][0],list_act[0][1],list_act[0][2]]

#list_act_to_today = [-1]*3
list_act_to_today = [-1,-1,-1]

import numpy as np

for day in range(1,n):
    for act in range(3):
        acts_yes = [0,1,2]
        del acts_yes[act]
        lst = np.array(list_act_to_yesterday)

        list_act_to_today[act] = max(list_act_to_yesterday[act_yes] for act_yes in acts_yes) + list_act[day][act]
    #list_act_to_yesterday  = list_act_to_today
    list_act_to_yesterday = [list_act_to_today[0],list_act_to_today[1],list_act_to_today[2]]

ans = max(list_act_to_yesterday[act] for act in range(3))

print(ans)

Submission status: Accepted

Solution code:
import itertools
from collections import deque,defaultdict,Counter
from itertools import accumulate
import bisect
from heapq import heappop,heappush,heapify
from fractions import gcd
from copy import deepcopy
import math
import queue
import numpy as np
#import sympy as syp(素因数分解とか)
Mod = 1000000007
import sys
sys.setrecursionlimit(100000) 
  
def sieve_of_eratosthenes(n):
    if not isinstance(n,int):
        raise TypeError("n is not int")
    if n<2:
        raise ValueError("n is not effective")
    prime = [1]*(n+1)
    for i in range(2,int(math.sqrt(n))+1):
        if prime[i] == 1:
            for j in range(2*i,n+1):
                if j%i == 0:
                    prime[j] = 0
    res = []
    for i in range(2,n+1):
        if prime[i] == 1:
            res.append(i)
    return res

 
class UnionFind:
    def __init__(self,n):
        self.parent = [i for i in range(n+1)]
        self.rank = [0 for i in range(n+1)]
    
    def findroot(self,x):
        if x == self.parent[x]:
            return x
        else:
            y = self.parent[x]
            y = self.findroot(self.parent[x])
            return y
    
    def union(self,x,y):
        px = self.findroot(x)
        py = self.findroot(y)
        if px < py:
            self.parent[py] = px
        else:
            self.parent[px] = py
 
    def same_group_or_no(self,x,y):
        return self.findroot(x) == self.findroot(y)

def main(): #startline-------------------------------------------
    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])
    print(max(dp[n]))

if __name__ == "__main__":
    main() #endline===============================================

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    a = [[0]*3 for _ in range(N)]
    for i in range(N):
        a[i][0], a[i][1], a[i][2] = 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):
                    continue
                DP[i+1][k] = max(DP[i][j]+a[i][k], DP[i+1][k])
                
    ans = 0
    for m in range(3):
        ans = max(ans, DP[N][m])
    
    print(ans)
    
main()

Submission status: Accepted

Solution code:
n = int(input())
li = []
for _ in range(n):
    li.append([int(i) for i in input().split()])
dp = [[-1,-1,-1] for x in range(n)]
for i in range(3):
    dp[0][i] = li[0][i]

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

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

Submission status: Accepted

Solution code:
N = int(input())

A = [0] * (N + 1)
B = [0] * (N + 1)
C = [0] * (N + 1)

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

dp = [[0] * 3 for _ in range(N + 1)]#N+1
for i in range(1, N + 1):
    dp[i][0] = A[i] + max(dp[i-1][1], dp[i-1][2])#dp[i][0]=i日目でaを選んだ時の最大値＝i-1日目でbかcを選んだ時の最大値+ai
    dp[i][1] = B[i] + max(dp[i-1][2], dp[i-1][0])
    dp[i][2] = C[i] + max(dp[i-1][0], dp[i-1][1])

print(max(dp[N]))#Nの時の最大

Submission status: Accepted

Solution code:
N = int(input())

A = [0] * (N + 1)
B = [0] * (N + 1)
C = [0] * (N + 1)

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

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

print(max(dp[N]))

Submission status: Accepted

Solution code:
from itertools import product


def read():
    N = int(input())
    happiness = list()
    for i in range(N):
        a, b, c = list(map(int, input().split()))
        happiness.append([a, b, c])
    return N, happiness

def solve(N, happiness):
    dp = [[0 for j in range(3)] for i in range(N+1)]
    for i, j in product(range(N), range(3)):
        dp[i+1][j] = max(dp[i][(j+1)%3], dp[i][(j+2)%3]) + happiness[i][j]
    return max(dp[N])

if __name__ == '__main__':
    inputs = read()
    print("%s" % solve(*inputs))

Submission status: Accepted

Solution code:
n=int(input())
a,b,c=[],[],[]
for i in range(n):
    x,y,z=map(int,input().split(" "))
    a.append(x)
    b.append(y)
    c.append(z)
dp=[[0,0,0] for i in range(n)]
for i in range(n):
    if(i==0):
        dp[i][0]=a[0]
        dp[i][1]=b[0]
        dp[i][2]=c[0]
    else:
        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][0],max(dp[n-1][1],dp[n-1][2])))

Submission status: Accepted

Solution code:
n = int(input())

dp = list(map(int, input().split()))

for i in range(1,n):
    arr = list(map(int, input().split()))

    dp[0],dp[1],dp[2] = arr[0] + max(dp[1],dp[2]),arr[1] + max(dp[0],dp[2]),arr[2] + max(dp[0],dp[1])

print(max(dp))

Submission status: Accepted

Solution code:
N = int(input())
dp = []
h0 = list(map(int, input().split()))
dp.append(h0)
for i in range(1, N):
  a, b, c = map(int, input().split())
  prev = dp[-1]
  ha = a + max(prev[1], prev[2])
  hb = b + max(prev[0], prev[2])
  hc = c + max(prev[0], prev[1])
  dp.append([ha, hb, hc])
#print(dp)
ans = max(dp[N-1])
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

arr = [0] * MAX_N
for i in range(N):
    arr[i] = list(map(int, input().split()))

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

print(max(dp[N]))

Submission status: Accepted

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

arr = [0] * MAX_N
for i in range(N):
    arr[i] = list(map(int, input().split()))
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] + arr[i][k])
print(max(dp[N]))

Submission status: Accepted

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

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

for i in range(n):
    for p in range(3):
        if p == 0:
            q, r = p+1, p+2
        elif p == 1:
            q, r = p+1, p-1
        else:
            q, r = p-2, p-1
        dp[p][i+1] = max(dp[q][i] + h[i][p], dp[r][i] + h[i][p])

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

Submission status: Accepted

Solution code:
n = int(input())
abc = []
for _ in range(n):
    t = [ int(x) for x in input().split() ]
    abc.append(t)

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][j] = max(dp[i+1][j], dp[i][k] + abc[i][j])

print(max(dp[n]))

Submission status: Accepted

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

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

def f(list,step,choice):
    global dp
    if step == 0:
        dp[0][choice] = list[0][choice]
    else:
        temp = 0
        for i in range(3):
            if i == choice:
                continue
            spend = dp[step-1][i] + list[step][choice]
            if spend >= temp:
                temp = spend
        dp[step][choice] = temp

for i in range(n):
    for j in range(3):
        f(hap,i,j)

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

Submission status: Accepted

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

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

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

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

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

if __name__ == '__main__':
    main()

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)]
dp[0] = a[0]

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def rr(): return input().rstrip()
def rri(): return int(rr())
def rrl(): return list(map(int, rr().split()))
def rrt(): return tuple(map(int, rr().split()))
from collections import defaultdict
def mus(d=lambda: 0): return defaultdict(lambda: defaultdict(d))
def dd0(d=lambda: 0): return defaultdict(d)
def ms(x, y, d=0): return [[0]*y for i in range(x)]
def ar(x, d=0): return [d]*x
def ppm(m, n=0, x=0, y=0): print("\n".join(("\t".join((str(m[j][i]) for j in range(y or n))) for i in range(x or n))))
def ppa(a, n): print("\t".join(map(str, a[0:n])))
def ppl(a=0, x=[1]): x[0]=a or (x[0]+1); print("-"+"- -"*14+"-# "+str(x[0]-2)+" #-"+"- -"*14+"-")
def fltn(a): (i for sub in a for i in sub)
def yn(x): return "YES" if x else "NO"
from itertools import accumulate
from itertools import groupby
def grp(x): return ((i, sum(1 for _ in g)) for i, g in groupby(x))
import math
#def rnar(): return (*rrl(), rrl())
#def rn(): return (*rrl(),)
def dpp(a, b=""): print(")#| {} |#(o) ? {} ? (".format(a, b))

def read():
    n = rri()
    l = [rrt() for _ in range(n)]

    return n, l
    return (0,)

def solve(n, l):
    dp0 = ar(n)
    dp1 = ar(n)
    dp2 = ar(n)
    #dpp(l)

    dp0[0] = l[0][0]
    dp1[0] = l[0][1]
    dp2[0] = l[0][2]

    for i in range(1, n):
        dp0[i] = max(dp1[i-1], dp2[i-1]) + l[i][0]
        dp1[i] = max(dp0[i-1], dp2[i-1]) + l[i][1]
        dp2[i] = max(dp1[i-1], dp0[i-1]) + l[i][2]

    #dpp(dp0)
    #dpp(dp1)
    #dpp(dp2)

    ans = max(dp0[-1], dp1[-1], dp2[-1])
    return ans

if __name__ == "__main__":
    #test_count = rri()
    test_count = 1
    for _ in range(test_count):
        input_data = read()
        result = solve(*input_data)
        print(result)

Submission status: Accepted

Solution code:
N=int(input())
l=[]
for i in range(N):
  l.append(list(map(int,input().split())))
def pat(j):
  if j==0:
    return [1,2]
  elif j==1:
    return [0,2]
  else:
    return [0,1]
dp=[[0]*3 for _ in range(N+1)]
for i in range(1,N+1):
  for j in range(3):
    dp[i][j]=l[i-1][j]+max(dp[i-1][k] for k in pat(j))
print(max(dp[-1]))

Submission status: Accepted

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

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


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

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

Submission status: Accepted

Solution code:
N=int(input())
A=list()
B=list()
C=list()
DP_A=[0]
DP_B=[0]
DP_C=[0]
for i in range(N):
    a,b,c=input().split()
    A.append(int(a))
    B.append(int(b))
    C.append(int(c))
for i in range(1,N+1):
    if DP_B[i-1] + A[i-1] > DP_C[i-1] + A[i-1]:
        DP_A.append(DP_B[i-1] + A[i-1])
    else:
        DP_A.append(DP_C[i-1] + A[i-1])

    if DP_C[i-1] + B[i-1] > DP_A[i-1] + B[i-1]:
        DP_B.append(DP_C[i-1] + B[i-1])
    else:
        DP_B.append(DP_A[i-1] + B[i-1])

    if DP_A[i-1] + C[i-1] > DP_B[i-1] + C[i-1]:

        DP_C.append(DP_A[i-1] + C[i-1])
    else:
        DP_C.append(DP_B[i-1] + C[i-1])
print(max(DP_A[N], DP_B[N], DP_C[N]))

Submission status: Accepted

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

dp = [[float("inf") for _ in range(3)] for _ in range(N + 1)]
dp[0] = [0, 0, 0]

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

print(max(dp[-1][0], dp[-1][1], dp[-1][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] = map(int,input().split())
dp = [[0]*3 for _ in range(n)]
dp[0][:] = [a[0],b[0],c[0]]
for i in range(1,n):
    for j in range(3): # 今日やること
        for k in range(3):# 前日やったこと
            if j == k:
                continue
            if j == 0:
                dp[i][j] = max(dp[i][j], dp[i-1][k] + a[i])
            if j == 1:
                dp[i][j] = max(dp[i][j], dp[i-1][k] + b[i])
            if j == 2:
                dp[i][j] = max(dp[i][j], dp[i-1][k] + c[i])
print(max(dp[n-1][i] for i in range(3)))
#print(dp)

Submission status: Accepted

Solution code:
n = int(input())
abc = []
for i in range(n):
    abc.append(list(map(int,input().split())))
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):
        for k in [1,2]:#j以外を選ぶ
            dp[i][j] = max(dp[i][j],\
            dp[i-1][(j+k)%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
ka=[-1]*n
kb=[-1]*n
kc=[-1]*n
for i in range(n):
  a[i],b[i],c[i]=map(int,input().split())

ka[0]=a[0]
kb[0]=b[0]
kc[0]=c[0]

for i in range(1, n):
  ka[i]=max(kb[i-1],kc[i-1])+a[i]
  kb[i]=max(ka[i-1],kc[i-1])+b[i]
  kc[i]=max(kb[i-1],ka[i-1])+c[i]

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

print(max(dp[n]))

Submission status: Accepted

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

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
            else:
                dp[i+1][k] = max(dp[i+1][k], dp[i][j]+a[i][k])

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

Submission status: Accepted

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

# dp用の空行列（[a,b,c]×[0～n]の行列）
dp = [[0 for i in range(3)] for j in range(n+1)]

# 「1番荷物(i=0)」......「n番荷物(i=n-1)」から何番目までを使うかの場合分け
for i in range(n):
#     for j in range(3):
    dp[i+1][0] = max(dp[i][1],dp[i][2])+a_b_c_list[i][0]
    dp[i+1][1] = max(dp[i][0],dp[i][2])+a_b_c_list[i][1]
    dp[i+1][2] = max(dp[i][0],dp[i][1])+a_b_c_list[i][2]
    
print(max(dp[n]))

Submission status: Accepted

Solution code:
import sys
# read = sys.stdin.read
input = sys.stdin.readline
# readlines = sys.stdin.readlines
days = int(input())

prev = [0, 0, 0]
while days:
    nxt = [0, 0, 0]
    a, b, c = map(int, input().split())
    nxt[0] = a + max(prev[1], prev[2])
    nxt[1] = b + max(prev[0], prev[2])
    nxt[2] = c + max(prev[0], prev[1])
    prev = nxt
    days -= 1

print(max(prev))

Submission status: Accepted

Solution code:
days = int(input())

prev = [0, 0, 0]
while days:
    nxt = [0, 0, 0]
    a, b, c = map(int, input().split())
    nxt[0] = a + max(prev[1], prev[2])
    nxt[1] = b + max(prev[0], prev[2])
    nxt[2] = c + max(prev[0], prev[1])
    prev = nxt
    days -= 1

print(max(prev))

Submission status: Accepted

Solution code:
n = int(input())
dpa = 0
dpb = 0
dpc = 0
for i in range(n):
    a,b,c = map(int,input().split())
    dpat,dpbt,dpct = dpa,dpb,dpc
    dpa = max(dpbt,dpct) + a
    dpb = max(dpct,dpat) + b
    dpc = max(dpat,dpbt) + c
print(max(dpa,dpb,dpc))

Submission status: Accepted

Solution code:
def DP_C() :
    N = int(input())
    a = []
    b = []
    c = []
    for i in range(N) :
        ai, bi, ci = map(int, input().split())
        a.append(ai)
        b.append(bi)
        c.append(ci)

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

    return max(dp[N-1])

print(DP_C())

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin,stdout
n=input()
l=[]
for i in range(n):
    l1=list(map(int,stdin.readline().split()))
    l.append(l1)
a,b,c=l[0][0],l[0][1],l[0][2]
for i in range(1,n):
    x,y,z=a,b,c
    a=l[i][0]+max(y,z)
    b=l[i][1]+max(x,z)
    c=l[i][2]+max(x,y)
stdout.write(str(max(a,b,c))+'\n')

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
a = [[0]]*(N)
for i in range(N):
  a[i] = list(map(int, input().split()))
DP = [[0] * 3 for _ in range(N+3)]
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:
import sys
sys.setrecursionlimit(10 ** 7)

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N = int(readline())
ABC = [[int(x) for x in readline().split()] for _ in range(N)]

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

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

ans = max(dp[N])
print(ans)