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())
src = [tuple(map(int,input().split())) for i in range(N)]

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

Submission status: Accepted

Solution code:
# ANSHUL GAUTAM
# IIIT-D

from math import *
from copy import *
from string import *					# alpha = ascii_lowercase
from random import *
from sys import stdin
from itertools import *
from collections import Counter			# z = dict(Counter(l))

def maxHappiness(A,B,C,n):
	dp = [0,0,0]
	for i in range(3):
		dp[i] = [0]*n
	dp[0][0] = A[0]
	dp[1][0] = B[0]
	dp[2][0] = C[0]
	for i in range(1,n):
		dp[0][i] = A[i] + max(dp[1][i-1], dp[2][i-1])
		dp[1][i] = B[i] + max(dp[2][i-1], dp[0][i-1])
		dp[2][i] = C[i] + max(dp[0][i-1], dp[1][i-1])
	ans = max(dp[0][-1],dp[1][-1],dp[2][-1])
	return ans


A,B,C = [],[],[]
T = int(input())
for _ in range(T):
	a,b,c = list(map(int, stdin.readline().rstrip().split()))
	A.append(a)
	B.append(b)
	C.append(c)

ans = maxHappiness(A,B,C,T)
print(ans)

Submission status: Accepted

Solution code:


n = input()
a = []
b = []
c = []
for i in range(n):
    ai, bi, ci = map(int, raw_input().split())
    a.append(ai)
    b.append(bi)
    c.append(ci)

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

print(max([dpa[0], dpb[0], dpc[0]]))

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    a = [0]
    b = [0]
    c = [0]
    for _ 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 + 1)]

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

    print(max(dp[N]))


main()

Submission status: Accepted

Solution code:
from sys import stdin

n = input()
inp = stdin.readlines()

score = [[0, 0, 0] for i in xrange(n)]
dp = [[-1 for i in xrange(3)] for j in xrange(n)]

i = 0
for line in inp:
    score[i] = map(int, line.strip().split())

    if i == 0:
        dp[0] = score[0]
    else:
        for j in xrange(3):
            for k in xrange(1, 3): dp[i][j] = max(dp[i][j], dp[i - 1][(j + k) % 3])
            dp[i][j] += score[i][j]
    i += 1

print max(dp[n - 1])

Submission status: Accepted

Solution code:
N = int(input())

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

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

for i in range(3):
    DP[0][i] = ABC[0][i]

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

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
act = [["dammy"]]
for i in range(n):
    act.append(list(map(int, input().split())))
dp = [[0]*3 for i in range(n+1)]

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

Submission status: Accepted

Solution code:
A, B, C = 0, 1, 2
N = int(input())
H =[[0 for _ in range(3)] for _ in range(N)]
H[0] = list(map(int, input().split()))
for i in range(1, N):
    a, b, c = map(int, input().split())
    H[i][A] = a + max(H[i-1][B], H[i-1][C])
    H[i][B] = b + max(H[i-1][C], H[i-1][A])
    H[i][C] = c + max(H[i-1][A], H[i-1][B])
print(max(H[N-1]))

Submission status: Accepted

Solution code:
from sys import stdin

n = int(input())
a = stdin.read().splitlines()
b = [[int(i) for i in line.split()] for line in a]

d, f, g = b[0]  # -> ends with activity 1,2,3 respectively

for i in range(1, n):
    d, f, g = max(f, g) + b[i][0], max(d, g) + b[i][1], max(d, f) + b[i][2]

print(max(d, f, g))

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
N=int(input())
F=[]
for m in range(0,N):
  F.append([0,0,0])
F[0]=  list(map(int,input().split(" ")))
#print(F[0])

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python

from collections import deque
import itertools as it
import sys
import math

sys.setrecursionlimit(1000000)

INF = 10 ** 18

N = input()
DP = [0, 0, 0]
for _ in range(N):
    a, b, c = map(int, raw_input().split())
    DP_ = [0, 0, 0]
    DP_[0] = a + max(DP[1], DP[2])
    DP_[1] = b + max(DP[0], DP[2])
    DP_[2] = c + max(DP[0], DP[1])
    DP = DP_

print max(DP)

Submission status: Accepted

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

abc[0][0]

ahappiness = [0 for i in range(N)]
bhappiness = [0 for i in range(N)]
chappiness = [0 for i in range(N)]
# syoki
ahappiness[0] = abc[0][0]
bhappiness[0] = abc[0][1]
chappiness[0] = abc[0][2]

#loop
for i in range(1,N):
    ahappiness[i] = max(bhappiness[i-1],chappiness[i-1])+abc[i][0]
    bhappiness[i] = max(ahappiness[i-1],chappiness[i-1])+abc[i][1]
    chappiness[i] = max(ahappiness[i-1],bhappiness[i-1])+abc[i][2]
    
print(max(ahappiness[-1],bhappiness[-1],chappiness[-1]))

Submission status: Accepted

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

##
## C - Vacation
## https://atcoder.jp/contests/dp/tasks/dp_c
##

import sys

def solve(N, tuples):
    xa, xb, xc = 0, 0, 0
    for a, b, c in tuples:
        ya = max(xb, xc) + a
        yb = max(xc, xa) + b
        yc = max(xa, xb) + c
        xa, xb, xc = ya, yb, yc
    return max(xa, xb, xc)

def load(input):
    line = input.readline()
    N = int(line.strip())
    tuples = [ tuple( int(s) for s in line.split() )
                   for line in input ]
    return N, tuples

def main():
    N, tuples = load(sys.stdin)
    result = solve(N, tuples)
    print(result)

main()

Submission status: Accepted

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

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

ans=dp[n-1][0]
for i in range(1,3):
    if ans < dp[n-1][i]:
        ans = dp[n-1][i]
        
print(ans)

Submission status: Accepted

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

dp = [[0 for _ in range(3)] for _ in range(N + 1)]
# dp[日付, 1-indexed][その日の行動0/1/2]

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] + A[i][j])

print(max(dp[N]))

Submission status: Accepted

Solution code:
a = int(input())
x = []
for i in range(a):
    c,d,e = map(int, input().split())#a,b,c,d or n,k
    x.append([c,d,e])
dp =[[0, 0, 0] for _ in [0]*(a+1)]
dp[0][0] = x[0][0]
dp[0][1] = x[0][1]
dp[0][2] = x[0][2]
for n in range(3):
    for i,h in enumerate(x):
        for n in range(3):
            if i !=0:
                dp[i][n] = max(dp[i-1][(n+1)%3],dp[i-1][(n+2)%3])+h[n]
        #print(dp)
#print(dp)
print(max(dp[a-1]))

Submission status: Accepted

Solution code:
n=int(input())
l=[list(map(int,input().split()))]
for i in range(1,n):
  a,b,c=map(int,input().split())
  x=a+max(l[-1][1:])
  y=b+max(l[-1][0],l[-1][2])
  z=c+max(l[-1][:2])
  l.append([x,y,z])
print(max(l[-1]))

Submission status: Accepted

Solution code:
N = int(input())

ABC = []

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

dp = []

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

dp[0] = ABC[0]

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

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

Submission status: Accepted

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

Submission status: Accepted

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

import sys, re
from collections import deque, defaultdict, Counter
from math import sqrt, hypot, factorial, pi, sin, cos, radians
if sys.version_info.minor >= 5: from math import gcd
else: from fractions import gcd 
from heapq import heappop, heappush, heapify, heappushpop
from bisect import bisect_left, bisect_right
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from functools import reduce, partial
from fractions import Fraction
from string import ascii_lowercase, ascii_uppercase, digits

def input(): return sys.stdin.readline().strip()
def ceil(a, b=1): return int(-(-a // b))
def round(x): return int((x*2+1) // 2)
def fermat(x, y, MOD): return x * pow(y, MOD-2, MOD) % MOD
def lcm(x, y): return (x * y) // gcd(x, y)
def lcm_list(nums): return reduce(lcm, nums, 1)
def gcd_list(nums): return reduce(gcd, nums, nums[0])
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N = INT()

# dp[日数][今回ABCのどれにしたか] = その時点での最大値
dp = [[0] * 3 for i in range(N)]
dp[0][0], dp[0][1], dp[0][2] = MAP()

for i in range(N-1):
    abc = LIST()
    # 送り元ループ
    for j in range(3):
        # 送り先ループ
        for k in range(3):
            # 今回と同じ所には送れない
            if j != k:
                dp[i+1][k] = max(dp[i][j] + abc[k], dp[i+1][k])
# 最終日にABCどれにしたかの最大を取る
print(max(dp[N-1]))

Submission status: Accepted

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

for i in range(n):
    ai,bi,ci=list(map(int,input().split()))
    a.append(ai)
    b.append(bi)
    c.append(ci)

last_a=[0 for i in range(n)]
last_b=[0 for i in range(n)]
last_c=[0 for i in range(n)]

last_a[0]=a[0]
last_b[0]=b[0]
last_c[0]=c[0]

for i in range(1,n):
    last_a[i]=max(last_b[i-1]+a[i],last_c[i-1]+a[i])
    last_b[i]=max(last_a[i-1]+b[i],last_c[i-1]+b[i])
    last_c[i]=max(last_a[i-1]+c[i],last_b[i-1]+c[i])

print(max([last_a[n-1],last_b[n-1],last_c[n-1]]))

Submission status: Accepted

Solution code:
n=int(input())

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

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

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

n=n-1

print(max(a[n], b[n], c[n]))

Submission status: Accepted

Solution code:
n = int(input())
arr = [list(map(int, input().split(' '))) for _ in range(n)]
INF = 0 # int(1e9)
dp = [[INF, INF, INF] for _ in range(n)]
# init
dp[0] = [i for i in arr[0]]

for i in range(1, n):
    for j in range(3):
        for k in range(3):
            if j != k:
                tmp = dp[i-1][k] + arr[i][j]
                if dp[i][j] < tmp:
                    dp[i][j] = tmp

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

Submission status: Accepted

Solution code:
import math
import sys
from operator import itemgetter

# N, K = map(int, input().split())
# h = list(map(int, input().split()))
# S = [input() for _ in range(H)]
# X = [[0 for j in range(100)] for i in range(200)]


### input
N = int(input())
a = [0]*100001
b = [0]*100001
c = [0]*100001

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


### solve
hpns = [[0 for j in range(100001)] for i in range(3)]

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

Submission status: Accepted

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

a = [0]*n
b = [0]*n
c = [0]*n

#a[n]は、n日目にaをした場合のn日間の幸福度の最大値

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


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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
f=[0]*3
for i in range(n):
	g=list(map(int,input().split()))
	h=[0]*3
	for j in range(3):
		for k in range(3):
			if j!=k:
				h[j]=max(h[j],f[k]+g[j])
	f=h
print(max(f))

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

Submission status: Accepted

Solution code:
import sys
INF = 99999999999999
N = int(input())
a = [0]*N
b = [0]*N
c = [0]*N
memo = [[0 for _ in range(3)]for _ in range(N)]
for i in range(N):
    a[i],b[i],c[i] = [int(n) for n in input().split()]

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

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

Submission status: Accepted

Solution code:
from sys import stdin, stdout

INF = float('inf')
n = int(stdin.readline())
dp = [[-INF for j in range(3)] for i in range(n)]
price = []

for i in range(n):
    price.append(list(map(int, stdin.readline().split())))

dp[0] = price[0]

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

stdout.write(str(max(dp[n - 1])))

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n=int(input())
k=3
l=[]
for _ in range(n):
    l.append(list(map(int,input().split())))
dp=[l[0]]
for i,s in enumerate(l):
    if not i:continue
    t=[t for t in dp[-1]]
    d=[]
    for i in range(k):
        d.append(max(t[j]for j in range(k)if i!=j)+s[i])
    dp.append(d)
print(max(dp[-1]))

Submission status: Accepted

Solution code:
n=int(input())
l=[list(map(int,input().split()))for _ in[0]*n]
dp=l[0]
for i,s in enumerate(l):
    if not i:continue
    d=[]
    for i,t in enumerate(s):
        d.append(max(t for j,t in enumerate(dp)if i!=j)+t)
    dp=d
print(max(dp))

Submission status: Accepted

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

infty = 10**10

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

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

def solve():
    for i in range(1,N):
        for j in range(3):
            tmp = [0,1,2]
            tmp.remove(j)
            dp[i][j] = max(dp[i-1][tmp[0]],dp[i-1][tmp[1]]) + abc[i][j]

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

Submission status: Accepted

Solution code:
def main():
  N = int(input())
  Data = []
  for i in range(N):
    Data.append(list(map(int, input().split())))
  Data = tuple(Data)
  
  DP = [[0, 0, 0] for i in range(N)]
  DP[0] = Data[0]
  for i in range(1, N):
    DP[i][0] = Data[i][0] + max(DP[i - 1][1], DP[i - 1][2])
    DP[i][1] = Data[i][1] + max(DP[i - 1][2], DP[i - 1][0])
    DP[i][2] = Data[i][2] + max(DP[i - 1][0], DP[i - 1][1])
    
  print(max(DP[N-1]))
  
main()

Submission status: Accepted

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


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

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


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

Submission status: Accepted

Solution code:

def a():return input()#s
def ai():return int(input())#a,n or k
def ma():return map(int, input().split())#a,b,c,d or n,k
def ms():return map(str, input().split())#,a,b,c,d
def lma():return list(map(int, input().split()))#x or y
def lms():return list(map(str, input().split()))#x or y
a = int(input())
l =[]
for i in range(a):
    c,d,e = ma()
    l.append([c,d,e])
dp =[[0, 0, 0] for _ in [0]*(a+1)]
for i in range(a):
    for j in range(3):
        for k in range(3):
            if j == k:continue
            if i==0:dp[i][j] = l[i][j]
            else:dp[i][j] = max(dp[i][j],dp[i-1][k]+l[i][j])
print(max(dp[a-1]))

Submission status: Accepted

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

happiness = [0]*3

for i in range(n):
    act = list(map(int, input().split()))
    tmp1 = [happiness[(i+1)%3]+act[i] for i in range(3)]
    tmp2 = [happiness[(i+2)%3]+act[i] for i in range(3)]
    happiness = [max(tmp1[i], tmp2[i]) for i in range(3)]

print(max(happiness))

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

dpa = [0]*N
dpb = [0]*N
dpc = [0]*N
dpa[0] = a[0]
dpb[0] = b[0]
dpc[0] = c[0]

for i in range(1,N):
    dpa[i] = 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:
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(input())
def S(): return input()

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np
n= int(input())
a = np.zeros(n+2)
b = np.zeros(n+2)
c = np.zeros(n+2)
dp = np.zeros(3)
for i in range(n):
 a[i],b[i],c[i] = (int(j) for j in input().split())
 dp = [a[i]+max(dp[1],dp[2]),b[i]+max(dp[0],dp[2]),c[i]+max(dp[0],dp[1])]

print(int(max(dp)))

Submission status: Accepted

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

Submission status: Accepted

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

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 = [A[0] + max(dp[1], dp[2]), A[1] + max(dp[0], dp[2]), A[2] + max(dp[0], dp[1])]

print(max(dp))

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)

NOW = [[-1,-1,-1] for i in range(N)]
NOW[0] = [A[0],B[0],C[0]]

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

ans = max(NOW[-1])
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

x,y,z = 0,0,0

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

print(max(x,y,z))

Submission status: Accepted

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

dp = [[10**18]*3 for i in range(N)]
for i in range(3):
    dp[0][i] = days[0][i]

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

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

Submission status: Accepted

Solution code:
N = int(input())

dp = [[0 for _ in range(3)] for _ in range(N + 1)]
# dp[day][a/b/c] = day日にa/b/cを行った場合の最大幸福度

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

Submission status: Accepted

Solution code:
#https://atcoder.jp/contests/dp/tasks/dp_c
#2019-01-13
N = int(input())
abc = [list(map(int, input().split())) for i in range(N)]

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

Submission status: Accepted

Solution code:
n = int(input())
ma,mb,mc=0,0,0
for i in range(n):
    a,b,c = map(int,input().split())
    ma1, mb1, mc1 = ma,mb,mc
    ma = max(mb1 + b, mc1 + c)
    mb = max(ma1 + a, mc1 + c)
    mc = max(mb1 + b, ma1 + a)
print(max(ma,mb,mc))

Submission status: Accepted

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

for i in range(n-1):
    a, b, c = map(int, input().split())
    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())
h = [tuple(map(int,input().split())) for i in range(N)]
inf = float('inf')
DP = [[inf for i in range(3)] for i in range(N)]
DP[0][0]=h[0][0]
DP[0][1]=h[0][1]
DP[0][2]=h[0][2]

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

print(max(DP[-1]))

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys, math


def main():
    N = input()
    days = [map(int, raw_input().split()) for i in range(N)]

    dpMax = [[0, 0, 0] for i in range(N)]
    dpMax[0] = days[0]
    
    for i in range(1, N):
        for j in range(3):
            for k in range(3):
                if j != k:
                    dpMax[i][j] = max(dpMax[i][j], dpMax[i - 1][k] + days[i][j])
            
    print max(max(dpMax[N - 1][0], dpMax[N - 1][1]), dpMax[N - 1][2])


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())

X = [[0]*3 for i in range(N)]
for i in range(N):
    a, b, c = map(int, input().split())
    if i == 0:
        X[i][0] = a
        X[i][1] = b
        X[i][2] = c
    else:
        X[i][0] = max(X[i-1][1], X[i-1][2]) + a
        X[i][1] = max(X[i-1][0], X[i-1][2]) + b
        X[i][2] = max(X[i-1][0], X[i-1][1]) + c

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

Submission status: Accepted

Solution code:
N = int(input())
happy_list = [list(map(int, input().split())) for n in range(N)]
def chmax(a, b):
    if a > b:
        return a
    else:
        return b
dp_list = [[0, 0, 0]]
for n in range(1, N+1):
    tmp = [0, 0, 0]
    tmp[1] = chmax(tmp[1], dp_list[n-1][0] + happy_list[n-1][1])
    tmp[2] = chmax(tmp[2], dp_list[n-1][0] + happy_list[n-1][2])
    tmp[0] = chmax(tmp[0], dp_list[n-1][1] + happy_list[n-1][0])
    tmp[2] = chmax(tmp[2], dp_list[n-1][1] + happy_list[n-1][2])
    tmp[0] = chmax(tmp[0], dp_list[n-1][2] + happy_list[n-1][0])
    tmp[1] = chmax(tmp[1], dp_list[n-1][2] + happy_list[n-1][1])
    dp_list.append(tmp)
print(max(tmp))

Submission status: Accepted

Solution code:
N=int(input())

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

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

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

Submission status: Accepted

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


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

print(max(dp[N]))

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
N=int(input())
a0,b0,c0=map(int,input().split())
for _ in range(1,N):
    a,b,c=map(int,input().split())
    a0,b0,c0=a+max(b0,c0),b+max(a0,c0),c+max(a0,b0)
print(max(a0,b0,c0))

Submission status: Accepted

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

dpa = [0] * N
dpb = [0] * N
dpc = [0] * N

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