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.

Solution (Python):
n = int(input())
activity = {'a': [0], 'b': [0], 'c': [0]}
for _ in range(n):
    a, b, c = (map(int, input().split()))
    activity['a'].append(a)
    activity['b'].append(b)
    activity['c'].append(c)

lev_of_hap = {'a': [0]*(n+1), 'b': [0]*(n+1), 'c': [0]*(n+1)}

for i in range(1, n+1):
    lev_of_hap['a'][i] = max(lev_of_hap['b'][i-1], lev_of_hap['c'][i-1]) + activity['a'][i]
    lev_of_hap['b'][i] = max(lev_of_hap['a'][i - 1], lev_of_hap['c'][i - 1]) + activity['b'][i]
    lev_of_hap['c'][i] = max(lev_of_hap['a'][i - 1], lev_of_hap['b'][i - 1]) + activity['c'][i]

ans = max(lev_of_hap['a'][n], lev_of_hap['b'][n], lev_of_hap['c'][n])
print(ans)

Solution (Python):
import sys
import numpy as np
input = sys.stdin.readline
N = int(input())
w = np.zeros((N, 3), dtype=int)
for i in range(N):
  w[i, :] = list(map(int, input().split()))
dp = np.zeros((N+1, 3), dtype=int)
for i in range(1,N+1):
  dp[i][0] = max([dp[i][0], dp[i-1][1]+w[i-1][0], dp[i-1][2]+w[i-1][0]]) 
  dp[i][1] = max([dp[i][1], dp[i-1][2]+w[i-1][1], dp[i-1][0]+w[i-1][1]]) 
  dp[i][2] = max([dp[i][2], dp[i-1][0]+w[i-1][2], dp[i-1][1]+w[i-1][2]]) 
print(max(dp[N, :]))

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


if __name__ == '__main__':
    main()

Solution (Python):
N = int(input().strip())
W = [list(map(int, input().strip().split(' '))) for _ in range(N)]
dp = [[0,0,0] for _ in range(N)] 
dp[0] = W[0]
for i in range(1,N):
  for kind in range(3):
    dp[i][kind] = max(dp[i-1][(kind+1)%3],dp[i-1][(kind+2)%3]) + W[i][kind]

print(max(dp[-1]))

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


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

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

for i in range(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[-1]))

Solution (Python):
n = int(input())
HappyList = [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 k==j:
                continue
            dp[i+1][k] = max(dp[i+1][k], dp[i][j]+HappyList[i][k])

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

Solution (Python):

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


dp = [[0]*3 for _ in range(N)]
dp[0][0] = abc[0][0]
dp[0][1] = abc[0][1]
dp[0][2] = abc[0][2]
for n in range(1,N):
	dp[n][0] = abc[n][0]+max(dp[n-1][1],dp[n-1][2])
	dp[n][1] = abc[n][1]+max(dp[n-1][2],dp[n-1][0])
	dp[n][2] = abc[n][2]+max(dp[n-1][0],dp[n-1][1])

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

Solution (Python):
n = int(input())
l = []
for i in range(n):
    a = list(map(int,input().split()))
    l.append(a)
dpa = [l[0][0]]
dpb = [l[0][1]]
dpc = [l[0][2]]
for i in range(1,n):
    dpa.append(l[i][0]+max(dpb[i-1],dpc[i-1]))
    dpb.append(l[i][1]+max(dpa[i-1],dpc[i-1]))
    dpc.append(l[i][2]+max(dpb[i-1],dpa[i-1]))
print(max([dpa[n-1],dpb[n-1],dpc[n-1]]))

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

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


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

Solution (Python):
n = int(input())
arr = [[int(i) for i in input().split()] for x in range(n)]

for i in range(n - 2, -1, -1):
    for j in range(2, -1, -1):
        q = -1
        if j == 0:
            arr[i][j] += max(arr[i + 1][1],arr[i+1][2])
        elif j == 2:
            arr[i][j] += max(arr[i + 1][1],arr[i+1][0])
        else:
            arr[i][j] += max(arr[i + 1][0], arr[i+1][2])

print(max(arr[0]))

Solution (Python):
N = int(input())
a = [[ int(y) for y in input().split(' ')] for x in range(N)]

h = [[0 for y in range(3)] for x in range(N)]

def max_h(i, j):
    n = [0, 0, 0]
    for k in range(3):
        if k != j:
            n[k] = h[i][k]
    return max(n)

for i in range(N):
    for j in range(3):
        if i == 0:
            h[i][j] = a[i][j]
        else:
            h[i][j] = max_h(i-1, j) + a[i][j]

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

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

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

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

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

Solution (Python):
N = int(raw_input())
H = []
 
for _ in xrange(N):
    a, b, c = map(int, raw_input().split())
    H.append([a, b, c])
 
dp = [[0]*3 for __ in xrange(N)]
 
for i in xrange(0, N):
    for j in xrange(3):
        if i==0:
            dp[i][j] = H[0][j]
        else:
            dp[i][j] = max(dp[i-1][j-1]+H[i][j], dp[i-1][j-2]+H[i][j])
 
print max(dp[N-1])

Solution (Python):
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 for i in range(3)] for j in range(n)]
dp[0][0] = a[0]
dp[0][1] = b[0]
dp[0][2] = c[0]

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

Solution (Python):
N = int(input())

h = []

for _ in range(N):
    h.append(tuple(map(int, input().split())))

dp = [[0 for j 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):
    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]+h[i+1][j])

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

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

Solution (Python):
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())

inf = -float('inf')
dp = [[inf] * N for _ in range(3)]
dp[0][0] = a[0]
dp[1][0] = b[0]
dp[2][0] = c[0]

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

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

Solution (Python):
n = int(input())
dp = [list(map(int, input().split()))] + [[0, 0, 0]] * (n-1)

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

print(max(dp[-1]))

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

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

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

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

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

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

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

Solution (Python):
n= int(input())
ABC = [0]*n
for i in range(n):
  ABC[i]= list(map(int, input().split()))

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

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

Solution (Python):

import sys


def solve(N: int, a: "List[int]", b: "List[int]", c: "List[int]"):
    hs = [(a1, b1, c1) for a1, b1, c1 in zip(a, b, c)]
    dp = [[0]*3 for _ in range(N)]
    dp[0][0] = hs[0][0]
    dp[0][1] = hs[0][1]
    dp[0][2] = hs[0][2]

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



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    a = [int()] * (N)  
    b = [int()] * (N)  
    c = [int()] * (N)  
    for i in range(N):
        a[i] = int(next(tokens))
        b[i] = int(next(tokens))
        c[i] = int(next(tokens))
    print(solve(N, a, b, c))

if __name__ == '__main__':
    main()

Solution (Python):
n=int(input())
l=[list(map(int,input().split())) for _ in range(n)]
ans=[0,0,0]
for i in l:
    ans[0],ans[1],ans[2]=max(ans[1],ans[2])+i[0],max(ans[0],ans[2])+i[1],max(ans[0],ans[1])+i[2]
print(max(ans))

Solution (Python):
n=int(input())
l=[list(map(int,input().split())) for _ in range(n)]
ans=[0,0,0]
for i in l:
    ans[0],ans[1],ans[2]=max(ans[1],ans[2])+i[0],max(ans[0],ans[2])+i[1],max(ans[0],ans[1])+i[2]
print(max(ans))

Solution (Python):
n,*l=map(int,open(0).read().split())
A=B=C=0;M=max
for i in range(n):
	A,B,C=l[3*i]+M(B,C),l[3*i+1]+M(C,A),l[3*i+2]+M(A,B)
print(M(A,B,C))

Solution (Python):
n,*l=map(int,open(0).read().split())
a=l[::3];b=l[1::3];c=l[2::3]
A,B,C=a[0],b[0],c[0]
M=max
for i in range(1,n):
	A,B,C=a[i]+M(B,C),b[i]+M(C,A),c[i]+M(A,B)
print(M(A,B,C))

Solution (Python):
n,*l=map(int,open(0).read().split())
a=l[::3]
b=l[1::3]
c=l[2::3]
dp=[[0]*3 for _ in range(n)]
dp[0]=[a[0],b[0],c[0]]
A,B,C=dp[0]
for i in range(1,n):
	dp[i]=[ a[i]+max(B,C) , b[i]+max(C,A) , c[i]+max(A,B) ]
	A,B,C=dp[i]
print(max(dp[-1]))

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

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

Solution (Python):
N=int(input())
dp=[[0]*3 for i in range(N+1)]
for i in range(1,N+1):
  a=list(map(int,input().split()))
  for j in range(3):
    tmp=[]
    for k in range(3):
      if j!=k:
        dp[i][k]=max(dp[i][k],dp[i-1][j]+a[k])  
print(max(dp[-1]))

Solution (Python):
import sys
import numpy as np

def solve(dpa, dpb, dpc, a, b, c):
    dpa[N - 1] = a[N - 1]
    dpb[N - 1] = b[N - 1]
    dpc[N - 1] = c[N - 1]
    for n in range(N - 2, -1, -1):
        dpa[n] = max(dpb[n + 1], dpc[n + 1]) + a[n]
        dpb[n] = max(dpc[n + 1], dpa[n + 1]) + b[n]
        dpc[n] = max(dpa[n + 1], dpb[n + 1]) + c[n]

nin = np.fromstring(sys.stdin.read(), dtype=np.int64, sep=' ')
N = nin[0]
abc = nin[1: 3 * N + 1].reshape(N, 3).T
a = abc[0]
b = abc[1]
c = abc[2]
dpa = np.zeros_like(a)
dpb = np.zeros_like(b)
dpc = np.zeros_like(c)
solve(dpa, dpb, dpc, a, b, c)
print(max(dpa[0], dpb[0], dpc[0]))

Solution (Python):
N = int(input())
ABC = list(list(map(int,input().split(' '))) for i in range(N))
DP = list([0,0,0] for i in range(N+1))

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

Solution (Python):

import sys
input = sys.stdin.readline

def main():
    n = int(input())
    A = tuple(tuple(map(int, input().rstrip().split())) for _ in range(n))
    dp = [[0] * 3 for _ in range(n)]
    dp[0] = A[0]
    for i in range(1, n):  
        for j in range(3):  
            for k in range(3):  
                if j != k:
                    dp[i][j] = max(dp[i][j], dp[i - 1][k] + A[i][j])
    print(max(dp[-1]))


if __name__ == "__main__":
    main()

Solution (Python):


import sys
input = sys.stdin.readline

def main():
    n = int(input())
    A = tuple(tuple(map(int, input().rstrip().split())) for _ in range(n))
    a, b, c = A[0]
    for x, y, z in A[1:]:
        a, b, c = x + max(b, c), y + max(a, c), z + max(a, b)
    print(max(a, b, c))


if __name__ == "__main__":
    main()

Solution (Python):


def main():
    n = int(input())
    A = tuple(tuple(map(int, input().rstrip().split())) for _ in range(n))
    a, b, c = A[0]
    for x, y, z in A[1:]:
        a, b, c = x + max(b, c), y + max(a, c), z + max(a, b)
    print(max(a, b, c))


if __name__ == "__main__":
    main()

Solution (Python):


import sys
input = sys.stdin.readline

def main():
    n = int(input())
    A = tuple(tuple(map(int, input().rstrip().split())) for _ in range(n))
    dp = [[0] * 3 for _ in range(n)]
    dp[0] = A[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] + A[i + 1][k])
    print(max(dp[-1]))


if __name__ == "__main__":
    main()

Solution (Python):


import sys
input = sys.stdin.readline

def main():
    n = int(input())
    A = tuple(tuple(map(int, input().rstrip().split())) for _ in range(n))
    dp = [[0] * 3 for _ in range(n)]
    dp[0] = A[0]
    for i in range(1, n):  
        for j in range(3):  
            for k in range(3):  
                if j != k:
                    dp[i][k] = max(dp[i][k], dp[i - 1][j] + A[i][k])
    print(max(dp[-1]))


if __name__ == "__main__":
    main()

Solution (Python):


import sys
input = sys.stdin.readline

def main():
    n = int(input())
    A = tuple(tuple(map(int, input().rstrip().split())) for _ in range(n))
    dp = [[0] * 3 for _ in range(n)]
    dp[0] = A[0]
    for i in range(1, n):  
        for j in range(3):  
            for k in range(3):  
                if j != k:
                    dp[i][k] = max(dp[i][k], dp[i - 1][j] + A[i][k])
    print(max(dp[-1]))


if __name__ == "__main__":
    main()

Solution (Python):
n = int(input())
a, b, c = list(map(int, input().split()))

if n > 1:
    for _ in range(n-1):
        a_, b_, c_ = list(map(int, input().split()))
        a_ += max(b, c)
        b_ += max(a, c)
        c_ += max(a, b)
        a, b, c = a_, b_, c_

print(max(a, b, c))
"""
ちゃんと解いたVer
"""

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

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

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

print(max(dp[-1]))

Solution (Python):
n = int(input())
d = [[int(e) for e in input().split()] for i in range(n)]
f = [None for i in range(n)]
f[0] = d[0]
for i in range(1, n):
  f[i] = [max(f[i - 1][1] + d[i][0], f[i - 1][2] + d[i][0]), max(f[i - 1][0] + d[i][1], f[i - 1][2] + d[i][1]), max(f[i - 1][0] + d[i][2], f[i - 1][1] + d[i][2])]
print(max(f[n - 1]))

Solution (Python):
n = int(input())
a,b,c =map(int, input().split())
if n > 1:
    for _ in range(1, n):
        _a, _b, _c =map(int, input().split())
        _a = _a + max(b , c)
        _b = _b + max(c , a)
        _c = _c + max(a , b)
        a = _a
        b = _b
        c = _c
print(max(a,b,c))

Solution (Python):

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


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

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

Solution (Python):
n = int(input())
play_list = [0, 0, 0]

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

Solution (Python):
n = int(input())

aa = [[int(n) for n in input().split()] for i in range(n)]

s = [0] * 3

for a in aa:
    s = [max(s[1], s[2]) + a[0], 
        max(s[0], s[2]) + a[1],
        max(s[0], s[1]) + a[2]
    ]

print(max(s))

Solution (Python):
n = int(input())

aa = [[int(n) for n in input().split()] for i in range(n)]

s = [0] * 3

for a in aa:
    s = [max(s[1], s[2]) + a[0], 
        max(s[0], s[2]) + a[1],
        max(s[0], s[1]) + a[2]
    ]

print(max(s))

Solution (Python):
def solve():
    list_of_abc = read()
    result = think(list_of_abc)
    write(result)


def read():
    n = read_int(1)[0]
    list_of_abc = []
    for i in range(n):
        list_of_abc.append(read_int(3))
    return list_of_abc


def read_int(n):
    return list(map(lambda x: int(x), read_line().split(' ')))[:n]


def read_line(n=0):
    if n == 0:
        return input().rstrip()
    else:
        return input().rstrip()[:n]


def think(list_of_abc):
    dp = [[0 for abc in range(3)] for n in range(len(list_of_abc))]
    dp[0][0] = list_of_abc[0][0]
    dp[0][1] = list_of_abc[0][1]
    dp[0][2] = list_of_abc[0][2]

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

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


def write(result):
    print(result)


if __name__ == '__main__':
    solve()

Solution (Python):
N = int(input())
A = []
B = []
C = []
for i in range(N):
    tmp_a,tmp_b,tmp_c = map(int,input().split())
    A.append(tmp_a)
    B.append(tmp_b)
    C.append(tmp_c)

dp = [[0 for j in range(3)] for i in range(N)]
dp[0][0] = A[0]
dp[0][1] = B[0]
dp[0][2] = C[0]

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

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

Solution (Python):
def main():
    n = int(input())
    dp = [[0 for i in range(3)] for j in range(n)]

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


    print(max(dp[-1]))


main()

Solution (Python):
import sys
input = sys.stdin.readline


def main():
    N = int(input())
    h = [ list(map(int, input().split())) for _ in range(N) ]
    dp = [ [0] * 3 for _ in range(N) ]
    for i in range(3):
        dp[0][i] = h[0][i]

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

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


if __name__ == "__main__":
    main()

Solution (Python):
n = int(input())

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

Solution (Python):
import sys
input = sys.stdin.readline

N = int(input())
M = [None] * N

for i in range(N):
    M[i] = [int(v) for v in input().split()]
    
S = [[0 for _ in range(3)] for _ in range(N)]
S[0][0] = M[0][0]
S[0][1] = M[0][1]
S[0][2] = M[0][2]

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

print(max(S[-1]))

Solution (Python):
import sys
input = sys.stdin.readline

N = int(input())

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

dp = a = [[0 for i in range(3)] for j in range(N)]
dp[0][0] = grid[0][0]
dp[0][1] = grid[0][1]
dp[0][2] = grid[0][2]


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

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

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

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

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

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

print(max(dp[-1]))

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

Solution (Python):
N = int(input())
abc = 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):
    for j in range(3):
        DP[i+1][j] = max(DP[i][:j]+DP[i][j+1:])+abc[i][j]
print(max(DP[-1]))

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

dp0=[0]*n
dp1=[0]*n
dp2=[0]*n

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

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

print(max(dp0[n-1],dp1[n-1],dp2[n-1]))

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

dp0=[0]*n
dp1=[0]*n
dp2=[0]*n

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

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

print(max(dp0[n-1],dp1[n-1],dp2[n-1]))

Solution (Python):
N = int(input())
inputs = [[int(i) for i in input().split()] for _ in range(N)]

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

    print(max(dp[-1]))

Solution (Python):
def number():
    return int(input())


def space():
    return list(map(int, input().split()))


N = number()
ABC = [space() for _ in range(N)]
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]) + ABC[i-1][0]
    dp[i][1] = max(dp[i-1][0], dp[i-1][2]) + ABC[i-1][1]
    dp[i][2] = max(dp[i-1][0], dp[i-1][1]) + ABC[i-1][2]
print(max(dp[-1]))

Solution (Python):
n = int(input())

tasks = []

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

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

for i in range(1,n):
    for j in range(3):
        temp = []
        for k in range(3):
            if j != k:
                temp.append(dp[i-1][k] + tasks[i][j])
        dp[i][j] = max(temp)

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

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

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

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

Solution (Python):
n = int(input())
ad = []
for i in range(n):
    ad.append([int(i) for i in input().split()])




dp = [[0,0,0]]



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

"""
for i in range(3): 
    for j in range(1,n+1): 
        if j == 1:
            dp.append(dp[j-1]+ad[j-1][i])
            former = i
        else:
            if dp[j-1]+ad[j-1][dic[former][0]] <  dp[j-1]+ad[j-1][dic[former][1]]:
                dp.append(dp[j-1]+ad[j-1][dic[former][1]])
                former = dic[former][1]
            else:
                dp.append(dp[j-1]+ad[j-1][dic[former][0]])
                former = dic[former][0]
        
    
    ls.append(dp[n])
    dp = [0]
print(max(ls))
"""
"""
for i in range(3):
    for j in range(2):
        for k in range(1,n+1): 
            if k == 1:
                dp.append(dp[k-1]+ad[k-1][i])
            elif k == 2:
                dp.append(dp[k-1]+ad[k-1][dic[i][j]])
            else:
                if k == 3:
                    former = dic[i][j]
                if dp[k-1]+ad[k-1][dic[former][0]] <  dp[k-1]+ad[k-1][dic[former][1]]:
                    dp.append(dp[k-1]+ad[k-1][dic[former][1]])
                    former = dic[former][1]
                else:
                    dp.append(dp[k-1]+ad[k-1][dic[former][0]])
                    former = dic[former][0]
        
        
        ls.append(dp[n])
        dp = [0]
print(max(ls))
"""
"""



dp = [[0,-1]]
ads = []
tmp = []

former = [[-1,-1]]
dic = {0:[1,2],1:[0,2],2:[0,1]}
ls = []
for i in range(3):
    for k in range(1,n+1): 
        if k == 1:
            ads.append(dp[k-1][0]+ad[k-1][i])
            former.append([i,i])
        elif k != n:
            
            alpha = dic[former[k-1][0]]
            beta  = dic[former[k-1][1]]
            former.append([alpha,beta])
            if dp[k-1] + ad[alpha] + ad[dic[alpha][0]] < dp[k-1] + ad[alpha] + ad[dic[alpha][1]]):
                ads.append()
            if dp[k-1] + ad[beta] + ad[dic[beta][0]] < dp[k-1] + ad[beta] + ad[dic[beta][1]]:

            print(dp)
            dp.append(ads)
            ads = []

            tmp = []



        
        
        ls.append(max(dp[n]))
        dp = [[0,0]]
print(max(ls))
"""

Solution (Python):
N = int(input())
values = [[int(i) for i in input().split()] for _ in  range(N)]
dp = [values[0]]
for i in range(N-1):
  dp.append([max(dp[i][1],dp[i][2])+values[i+1][0],max(dp[i][2],dp[i][0])+values[i+1][1],max(dp[i][0],dp[i][1])+values[i+1][2]])

print(max(dp[-1]))

Solution (Python):
import sys
import numpy as np

input = sys.stdin.readline

N = int(input())

a = [0]*N
b = [0]*N
c = [0]*N
dp = [[0 for i in range(N)] for j in range(3)]

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

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

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

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