Problem:


Score : 100 points

Problem Statement

Taro's summer vacation starts tomorrow, and he has decided to make plans for it now.

The vacation consists of N days.
For each i (1 \leq i \leq N), Taro will choose one of the following activities and do it on the i-th day:

A: Swim in the sea. Gain a_i points of happiness.

B: Catch bugs in the mountains. Gain b_i points of happiness.

C: Do homework at home. Gain c_i points of happiness.

As Taro gets bored easily, he cannot do the same activities for two or more consecutive days.

Find the maximum possible total points of happiness that Taro gains.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq a_i, b_i, c_i \leq 10^4

Input

Input is given from Standard Input in the following format:

N
a_1 b_1 c_1
a_2 b_2 c_2
:
a_N b_N c_N

Output

Print the maximum possible total points of happiness that Taro gains.

Sample Input 1

3
10 40 70
20 50 80
30 60 90

Sample Output 1

210

If Taro does activities in the order C, B, C, he will gain 70 + 50 + 90 = 210 points of happiness.

Sample Input 2

1
100 10 1

Sample Output 2

100

Sample Input 3

7
6 7 8
8 8 3
2 5 2
7 8 6
4 6 8
2 3 4
7 5 1

Sample Output 3

46

Taro should do activities in the order C, A, B, A, C, B, A.



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


Sample output:
210

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    n = I()
    aa = [LI() for _ in range(n)]
    r = [0,0,0]
    for a in aa:
        r = [a[i] + max(r[i-1], r[i-2]) for i in range(3)]

    return max(r)


print(main())

Submission status: Accepted

Solution code:

import sys

sys.setrecursionlimit(100000)
N = int(raw_input())
act = [map(int, raw_input().split()) for _ in range(N)]
memo = [[-1]*3 for _ in range(N)]

def happy(n, i):
  if memo[n][i] >= 0:
    return memo[n][i]
  if n == 0:
    h = act[n][i]
  else:
    h = -1
    for j in range(3):
      if j == i:
        continue
      c = happy(n-1,j)+act[n][i]
      h = h if h > c else c
  memo[n][i] = h
  return h
print max(happy(N-1,0),happy(N-1,1),happy(N-1,2))

Submission status: Accepted

Solution code:
import sys
import numpy as np

N = int(sys.stdin.readline().strip())

a_list = [None for _ in range(N)]
b_list = [None for _ in range(N)]
c_list = [None for _ in range(N)]

for i in range(N):
    a, b, c = list(map(int, sys.stdin.readline().strip().split(" ")))
    a_list[i] = a
    b_list[i] = b
    c_list[i] = c
    
tmp = [a_list, b_list, c_list]
happy = [[0 for _ in range(3)] for __ in range(N)]

happy[0][0] = a_list[0]
happy[0][1] = b_list[0]
happy[0][2] = c_list[0]

for i in range(1, N):
    for j in range(3):
        #今日なにするか
        max_h = 0
        for k in range(3):
            #昨日なにしてたか
            if k == j:
                continue
            #print(i, j, k)
            #print(tmp)
            max_h = max(max_h, happy[i-1][k] + tmp[j][i])
        happy[i][j] = max_h

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

"""

happy = [0 for _ in range(N)]
select = [None for _ in range(N)]

ind = np.argmax([a_list[0], b_list[0], c_list[0]])
select[0] = [0, 1, 2][ind]
happy[0] = [a_list[0], b_list[0], c_list[0]][ind]

if N == 1:
    print(happy[0])
    sys.exit()

ind = np.argmax([a_list[0] + b_list[1], a_list[0] + c_list[1], b_list[0] + a_list[1], b_list[0] + c_list[1], c_list[0] + a_list[1], c_list[0] + b_list[1]])
happy[1] = [a_list[0] + b_list[1], a_list[0] + c_list[1], b_list[0] + a_list[1], b_list[0] + c_list[1], c_list[0] + a_list[1], c_list[0] + b_list[1]][ind]
select[1] = [1, 2, 0, 2, 0, 1][ind]

if N == 2:
    print(happy[1])
    sys.exit()

for i in range(2, N):
    max_h = 0
    for j in range(3):
        for k in range(3):
            if k == j:
                continue
            if j == select[i-2]:
                continue
            if max_h < tmp[k][i] + tmp[j][i-1] + happy[i-2]:
                max_h = tmp[k][i] + tmp[j][i-1] + happy[i-2]
                select[i] = k
            #print(tmp[k][i-1], tmp[j][i-2])
    happy[i] = max_h

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

Submission status: Accepted

Solution code:
import sys
N = int(input())
x, y, z = 0, 0, 0
for i, e in enumerate(sys.stdin):
    a, b, c = map(int, e.split())
    nx = max(y, z) + a
    ny = max(x, z) + b
    nz = max(x, y) + c
    x, y, z = nx, ny, nz
print(max(x, y, z))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
nb_days = int(input())
now_a, now_b, now_c = 0, 0, 0
for _ in range(nb_days):
    a, b, c = [int(x) for x in input().split()]
    next_a = a + max(now_b, now_c)
    next_b = b + max(now_a, now_c)
    next_c = c + max(now_a, now_b)
    now_a, now_b, now_c = next_a, next_b, next_c
print(max(next_a, next_b, next_c))

Submission status: Accepted

Solution code:
in_list = lambda: list(map(int,input().split()))

n = int(input())

lis_pnt = []

for _ in range(n):
	lis_pnt.append(in_list())

cost = []

for _ in range(n+1):
	cost.append([0,0,0])

for i in range(n):
	for bef in range(3):
		for aft in range(3):
			if bef != aft:
				cost[i+1][aft] = max(cost[i][bef]+lis_pnt[i][aft],cost[i+1][aft])
print(max(cost[n]))

Submission status: Accepted

Solution code:
import sys
raw_input = sys.stdin.readline
N = int(raw_input())
dp = [[0,0,0] for x in range(N+1)]
for i in range(N):
    li = map(int,raw_input().split())
    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]+li[k])
print max(dp[N])

Submission status: Accepted

Solution code:
# coding: utf-8
import sys
input = sys.stdin.readline

def dp_happiness(N):
    today_a, today_b, today_c = 0, 0, 0
    for _ in range(N):
        a, b, c = map(int, input().split())

        tommorow_a = a + max(today_b, today_c)
        tommorow_b = b + max(today_c, today_a)
        tommorow_c = c + max(today_a, today_b)

        today_a = tommorow_a
        today_b = tommorow_b
        today_c = tommorow_c
    return max(tommorow_a, tommorow_b, tommorow_c)


N = int(input())
print(dp_happiness(N))

Submission status: Accepted

Solution code:
# coding: utf-8
import sys
input = sys.stdin.readline

def dp_happiness(N):
    yesterday_a, yesterday_b, yesterday_c = 0, 0, 0
    for _ in range(N):
        a, b, c = map(int, input().split())

        today_a = a + max(yesterday_b, yesterday_c)
        today_b = b + max(yesterday_c, yesterday_a)
        today_c = c + max(yesterday_a, yesterday_b)

        yesterday_a = today_a
        yesterday_b = today_b
        yesterday_c = today_c
    return max(today_a, today_b, today_c)

N = int(input())
print(dp_happiness(N))

Submission status: Accepted

Solution code:
# coding: utf-8
# import sys
# input = sys.stdin.readline

def dp_happiness(N):
    yesterday_a, yesterday_b, yesterday_c = 0, 0, 0
    for _ in range(N):
        a, b, c = map(int, input().split())

        today_a = a + max(yesterday_b, yesterday_c)
        today_b = b + max(yesterday_c, yesterday_a)
        today_c = c + max(yesterday_a, yesterday_b)

        yesterday_a = today_a
        yesterday_b = today_b
        yesterday_c = today_c
    return max(today_a, today_b, today_c)

N = int(input())
print(dp_happiness(N))

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    prev = [0] * 3
    current = [0] * 3
    for _ in range(N):
        a, b, c = map(int, input().split())
        current[0] = a + max(prev[1], prev[2])
        current[1] = b + max(prev[0], prev[2])
        current[2] = c + max(prev[0], prev[1])
        prev = list(current)
    print(max(current))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# coding:utf-8

import sys

INF = float('inf')
MOD = 10 ** 9 + 7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def S(): return input()


n = I()

dp = [[0] * (n + 1) for _ in range(3)]
for i in range(n):
    H = LI()
    for k in range(3):
        k1, k2 = (k + 1) % 3, (k + 2) % 3
        dp[k1][i + 1] = max(dp[k1][i + 1], dp[k][i] + H[k1])
        dp[k2][i + 1] = max(dp[k2][i + 1], dp[k][i] + H[k2])

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

Submission status: Accepted

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

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

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

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

Submission status: Accepted

Solution code:
n=int(input())
a,b,c=[],[],[]
for _ in range(n):
    t=map(int,input().split())
    a.append(t.__next__())
    b.append(t.__next__())
    c.append(t.__next__())
dpA,dpB,dpC=[a[0]],[b[0]],[c[0]]
for i,(A,B,C)in enumerate(zip(a,b,c)):
    if not i:continue
    ta,tb,tc=dpA[-1],dpB[-1],dpC[-1]
    dpA.append(max(tb,tc)+A)
    dpB.append(max(tc,ta)+B)
    dpC.append(max(ta,tb)+C)
print(max(dpA[-1],dpB[-1],dpC[-1]))

Submission status: Accepted

Solution code:
n = int(input())
a,b,c = [],[],[]
for i in range(n):
    tmp = list(int(i) for i in input().split())
    a.append(tmp[0])
    b.append(tmp[1])
    c.append(tmp[2])

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

Submission status: Accepted

Solution code:
import itertools

n = int(input())
happiness = [list(map(int, input().split())) for _ in range(n)]
dp = [[0, 0, 0] for _ in range(n + 1)] 

tpls = tuple((i, j) for i, j in itertools.product(range(3), repeat=2) if i != j)
for x in range(n):
    for i, j in tpls:
        dp[x + 1][j] = max(dp[x + 1][j], dp[x][i] + happiness[x][i])

print(max(dp[n]))

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 = x + max(b,c), y + max(c,a), z + max(a,b)

print(max(a,b,c))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from itertools import combinations

N=int(input())

activity = []

for i in range(N):
    activity.append(list(map(int,input().split())))

h = [0]*3

for i in range(N):
    if i==0:
        for j in range(3):
            h[j]=activity[0][j]
    else:
        tmp=[0]*3
        for j in range(3):
            tmp[j]=activity[i][j]+max([h[k] for k in range(3) if k!=j])
        for j in range(3):
            h[j]=tmp[j]

print(max(h))

Submission status: Accepted

Solution code:
N = int(input())

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

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

for i in range(1, N + 1):
    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][0], dp[i - 1][2])
    dp[i][2] = abc[i][2] + 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())
H = [list(map(int,input().split())) for i in range(N)]

dpA=[0]*N
dpB=[0]*N
dpC=[0]*N

dpA[0]=H[0][0]
dpB[0]=H[0][1]
dpC[0]=H[0][2]

for num in range(1,N):
  dpA[num]=max(dpB[num-1],dpC[num-1])+H[num][0]
  dpB[num]=max(dpC[num-1],dpA[num-1])+H[num][1]
  dpC[num]=max(dpA[num-1],dpB[num-1])+H[num][2]

print(max(dpA[N-1],dpB[N-1],dpC[N-1]))

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.readline
n = int(input())
dp = list(list(map(int, readline().split())) for _ in range(n))
for i in range(1, n):
    dp[i][0] += max(dp[i - 1][1], dp[i - 1][2]);
    dp[i][1] += max(dp[i - 1][2], dp[i - 1][0]);
    dp[i][2] += max(dp[i - 1][0], dp[i - 1][1]);
print(max(dp[n - 1]))

Submission status: Accepted

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

Submission status: Accepted

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

dpA = [0 for _ in range(N)]
dpB = [0 for _ in range(N)]
dpC = [0 for _ in range(N)]

for i in range(N):
    if i == 0:
        dpA[0] = ABC[i][0]
        dpB[0] = ABC[i][1]
        dpC[0] = ABC[i][2]
    else:
        dpA[i] =  max(dpB[i-1], dpC[i-1]) + ABC[i][0]
        dpB[i] =  max(dpA[i-1], dpC[i-1]) + ABC[i][1]
        dpC[i] =  max(dpA[i-1], dpB[i-1]) + ABC[i][2]

print(max(dpA[-1],dpB[-1],dpC[-1]))

Submission status: Accepted

Solution code:
import sys
N = int(sys.stdin.readline())
a = []
b = []
c = []
for i in range(N):
    tmpa, tmpb, tmpc = map(int, sys.stdin.readline().split())
    a.append(tmpa)
    b.append(tmpb)
    c.append(tmpc)

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

Submission status: Accepted

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

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

Submission status: Accepted

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

# i日目に選んだ行動別の幸福度の最大値
dp = [[0, 0, 0] for _ in range(N+1)]

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

print(max(dp[-1]))

Submission status: Accepted

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

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

dp_a[0] = A[0]
dp_b[0] = B[0]
dp_c[0] = C[0]

for i in range(1, n):
    dp_a[i] = max(dp_b[i-1], dp_c[i-1]) + A[i]
    dp_b[i] = max(dp_a[i-1], dp_c[i-1]) + B[i]
    dp_c[i] = max(dp_b[i-1], dp_a[i-1]) + C[i]

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import copy

def main():
	n = int(input())
	a = []
	for _ in range(0, n):
		x = [int(x) for x in input().split()]
		a.append(x)
	
	cur = a[0]
	nxt = [0] * 3

	for i in range(1, len(a)):
		nxt[0] = max(a[i][0] + cur[1], a[i][0] + cur[2])
		nxt[1] = max(a[i][1] + cur[0], a[i][1] + cur[2])
		nxt[2] = max(a[i][2] + cur[1], a[i][2] + cur[0])

		cur = copy.deepcopy(nxt)

	print(max(cur[0], cur[1], cur[2]))

if __name__ == "__main__":
	main()

Submission status: Accepted

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

if N==1:
	print max( A[-1] )
	quit()

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

#Initialize
a,b,c=A[0][0],A[0][1],A[0][2]

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


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

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

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

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

print  max( dp[-1] )

Submission status: Accepted

Solution code:
N = int(input())
a_list = []
b_list = []
c_list = []
a_append = a_list.append
b_append = b_list.append
c_append = c_list.append
for i in range(N):
    a, b, c = map(int, input().split())
    a_append(a)
    b_append(b)
    c_append(c)

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

for i in range(1, N + 1):

    if i == 1:
        dp[i][0] = a_list[i - 1]
        dp[i][1] = b_list[i - 1]
        dp[i][2] = c_list[i - 1]
    else:
        dp[i][0] = max(dp[i][0], dp[i - 1][1] + a_list[i - 1])
        dp[i][0] = max(dp[i][0], dp[i - 1][2] + a_list[i - 1])
        dp[i][1] = max(dp[i][1], dp[i - 1][0] + b_list[i - 1])
        dp[i][1] = max(dp[i][1], dp[i - 1][2] + b_list[i - 1])
        dp[i][2] = max(dp[i][2], dp[i - 1][0] + c_list[i - 1])
        dp[i][2] = max(dp[i][2], dp[i - 1][1] + c_list[i - 1])

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
from sys import stdin

s_in = lambda: stdin.readline()[:-1]  # s = s_in()
d_in = lambda: int(stdin.readline())  # N = d_in()
ds_in = lambda: list(map(int, stdin.readline().split()))  # List = ds_in()


N = d_in()

happiness = [0] * 3
for _ in range(N):
    today = ds_in()
    pre = happiness.copy()
    happiness[0] = max(pre[1], pre[2]) + today[0]
    happiness[1] = max(pre[0], pre[2]) + today[1]
    happiness[2] = max(pre[1], pre[0]) + today[2]

print(max(happiness))

Submission status: Accepted

Solution code:
N=int(raw_input())
A=[]
for i in range(N):
    A.append([int(x) for x in raw_input().split()])
C=[[0,0,0] for x in range(N)]
C[0]=[A[0][0],A[0][1],A[0][2]]
for i in range(1,N):
    for j in range(3):
        C[i][j]=A[i][j] + max([ C[i-1][k] for k in range(3) if k != j])
print max(C[-1])

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def makelist(n, m):
    return [[0 for i in range(m)] for j in range(n)]


N = int(input())
hoge = makelist(N+1, 3)
for i in range(1, N+1):
    a, b, c = map(int, input().split())
    hoge[i][0], hoge[i][1], hoge[i][2] = a, b, c


dp = makelist(N+1, 3)
dp[1][0] = hoge[1][0]
dp[1][1] = hoge[1][1]
dp[1][2] = hoge[1][2]

for i in range(2, N+1):
    for act in range(3):
        now = 0
        for sel in range(3):
            if act != sel:
                now = max(now, dp[i-1][sel])
        dp[i][act] = now + hoge[i][act]
ans = max(dp[N][0], max(dp[N][1], dp[N][2]))

print(ans)

Submission status: Accepted

Solution code:
a = int(input())

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

c = [[0,0,0] for i in range(a+1)]

for i in range(a):
	for j in range(3):
		for k in range(3):
			if j == k:
				continue
			else:
				c[i+1][k] = max(c[i+1][k],c[i][j] + b[i][k])

print(max(c[-1]))

Submission status: Accepted

Solution code:
n=int(input())
l=[]
for i in range(n):
    a=[int(j) for j in input().split()]
    if i==0:
        l.append(a)
    else:
        li=[]
        li.append(a[0]+max(l[-1][1],l[-1][2]))
        li.append(a[1]+max(l[-1][0],l[-1][2]))
        li.append(a[2]+max(l[-1][0],l[-1][1]))
        l.append(li)

print(max(l[-1]))

Submission status: Accepted

Solution code:
N = int(input())
L = [list(map(int,input().split())) for i in range(N)]
NOW = [0]*3
NEXT = [None]*3
for i in range(N) :
    NEXT[0] = max(NOW[1],NOW[2]) + L[i][0]
    NEXT[1] = max(NOW[2],NOW[0]) + L[i][1]
    NEXT[2] = max(NOW[0],NOW[1]) + L[i][2]    
    NOW = NEXT.copy()
print(max(NOW))

Submission status: Accepted

Solution code:
N = int(input())
L = [list(map(int,input().split())) for i in range(N)]
NOW = [0,0,0]
NEXT = [None,None,None]

for i in range(N) :
    NEXT[0] = max(NOW[1],NOW[2]) + L[i][0]
    NEXT[1] = max(NOW[2],NOW[0]) + L[i][1]
    NEXT[2] = max(NOW[0],NOW[1]) + L[i][2]
    
    NOW = NEXT.copy()
print(max(NOW))

Submission status: Accepted

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

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

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

Submission status: Accepted

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

for i in range(n):
    A = [int(i) for i in input().split()]
    dp = [max(dp[1], dp[2]) + A[0], max(dp[0], dp[2]) + A[1], max(dp[0], dp[1]) + A[2]]
    
print(max(dp))

Submission status: Accepted

Solution code:
n = int(input())
d = []
for i in range(n):#h:高さ
    d.append([int(m) for m in input().split()])
#dp = [None] * n
dp = []
for i in range(n + 1):
    dp.append([None, None, None])
dp[0][0] = d[0][0]#"A"
dp[0][1] = d[0][1]#"B"
dp[0][2] = d[0][2]#"C"
#dp[1][0] = d[0][0]#"A"
#dp[1][1] = d[0][1]#"B"
#dp[1][2] = d[0][2]#"C"
#dp[i] = max(dp[i - 2]["A"] + d[i - 1][1], dp[i - 2]["A"] + d[i - 1][2], dp[i - 2]["B"] + d[i - 1][0], dp[i - 2]["B"] + d[i - 1][2], dp[i - 2]["C"] + d[i - 1][0], dp[i - 2]["C"] + d[i - 1][1])
for i in range(1, n):
    dp[i][0] = max(dp[i - 1][1] + d[i][0], dp[i - 1][2] + d[i][0])
    dp[i][1] = max(dp[i - 1][0] + d[i][1], dp[i - 1][2] + d[i][1])
    dp[i][2] = max(dp[i - 1][0] + d[i][2], dp[i - 1][1] + d[i][2])
print(max(dp[n - 1][0], dp[n - 1][1], dp[n - 1][2]))

Submission status: Accepted

Solution code:
N = int(input())
L = [list(map(int,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] + L[i][k])
print(max(dp[-1]))

Submission status: Accepted

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

n = int(input())

for i in range(n):
    if i == 0:
        dp = [int(i) for i in input().split()]
    else:
        abc = [int(i) for i in input().split()]
        dp[0], dp[1], dp[2] = abc[0] + max(dp[1], dp[2]), abc[1] + max(dp[0], dp[2]), abc[2] + max(dp[0], dp[1])
    
print(max(dp))

Submission status: Accepted

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

n = int(input())
abc = []
for i in range(n):
    abc.append([int(i) for i in input().split()])
dp = [[float("inf") for i in range(3)] for i in range(n)]

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

d = {0:a, 1:b, 2:c}
dp = [[0]*3 for _ in range(N+1)]

for i in range(N):
    for cur in [0,1,2]:
        for prev in [0,1,2]:
            if prev == cur:
                continue
            dp[i+1][cur] = max(dp[i+1][cur],
                               dp[i][prev] + d[cur][i])

print(max(dp[N]))

Submission status: Accepted

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

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

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

print(max(dp))

Submission status: Accepted

Solution code:
n = int(input())
a, b, c = [], [], []
for i in range(n):
    s = [int(i) for i in input().split()]
    a.append(s[0])
    b.append(s[1])
    c.append(s[2])


def happydc(n):
    hdc = {}
    hdc[0] = {"A": a[0], "B": b[0], "C": c[0]}
    
    for i in range(1, n):
        ai, bi, ci = a[i], b[i], c[i]
        lastdc = hdc[i-1]
        al, bl, cl = lastdc["A"], lastdc["B"], lastdc["C"]
        hdc[i] =  {"A": max(bl,cl)+ai, "B": max(al,cl)+bi, "C": max(al,bl)+ci}
    return hdc

print(max(happydc(n)[n-1].values()))

Submission status: Accepted

Solution code:
n=int(input())
x=[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):
        dp[i+1][j]=max(dp[i][(j+1)%3],dp[i][(j+2)%3])+x[i][j]
print(max(dp[-1]))

Submission status: Accepted

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

Submission status: Accepted

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

MaxTotalHappiness = [[0, 0, 0] for n in range(N)]
MaxTotalHappiness[0] = Happiness[0]

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

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

Submission status: Accepted

Solution code:
n = int(input())
dp = [[1]*3]
dp.append(list(map(int,input().split())))
for i in range(n-1):
    n1,n2,n3 = map(int,input().split())
    ls = [n1,n2,n3]
    ap = [None]*3
    for j in range(3):
        num = 0
        c = 0
        for k in range(3):
            if j == k:continue
            if c == 0:
                num = dp[i+1][k] + ls[j]
                c += 1
            elif c == 1:ap[j] = max(num,dp[i+1][k] + ls[j])
    dp.append(ap)
print(max(dp[-1]))

Submission status: Accepted

Solution code:
#!/mnt/c/Users/moiki/bash/env/bin/python
N = int(input())
# info = [ tuple(zip(tuple(map(int, input().split())), ("a","b","c"))) for i in range(N)]
info = [list(map(int, input().split())) for i in range(N)]
dp = [[0,0,0] for i in range(N+1)]
dp[1] = info[0]
for i in range(N):
    for j in range(3):
        for k in [x for x in range(3) if x != j]:
            if dp[i][k] + info[i][j] > dp[i+1][j]: # dp[i+1][j]?
                dp[i+1][j] = dp[i][k] + info[i][j]

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

Submission status: Accepted

Solution code:
N = int(input())
info = [list(map(int,input().split())) for i in range(N)]
# dp[i+1][j] = max(dp[i][k(!=j)]) + info[i][j]
dp = [[0,0,0] for i in range(N+1)]
dp[1] = info[0]
for i in range(1,N):
    for j in range(3):
        for k in [_ for _ in range(3) if _ != j]:
            if dp[i][k] + info[i][j] > dp[i+1][j]:
                dp[i+1][j] = dp[i][k] + info[i][j]
print(max(dp[N]))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
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(n):
    dp[i+1][0] = max(dp[i][1], dp[i][2]) + abc[i][0]
    dp[i+1][1] = max(dp[i][0], dp[i][2]) + abc[i][1]
    dp[i+1][2] = max(dp[i][0], dp[i][1]) + abc[i][2]

print(max(dp[n]))

Submission status: Accepted

Solution code:
n = int(input())
abc = [tuple(map(int, input().split())) for _ in range(n)]
ans = [[0, 0, 0] for _ in range(n+1)]

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

Submission status: Accepted

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

# dp[i+1日][最終日の活動 0:A, 1:B, 2:C]
dp = np.zeros((N, 3), np.int64)
dp[0] = abc[0]
for i in range(1, N):
    for j in range(3):
        dp[i, j] = max([dp[i - 1, k] + abc[i][j] for k in range(3) if k != j])
print(dp[-1].max())