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())
a, b, c = map(int, input().split())
dp_a = [a]
dp_b = [b]
dp_c = [c]

for _ in range(N-1):
    a, b, c = map(int, input().split())
    former_a, former_b, former_c = dp_a[-1], dp_b[-1], dp_c[-1]
    dp_a.append(max(former_b+a, former_c+a))
    dp_b.append(max(former_c+b, former_a+b))
    dp_c.append(max(former_a+c, former_b+c))

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

Solution (Python):
ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))

n = ii()
a = [tuple(int(i) for i in input().split()) for _ in range(n)]

dp = [[0]*3 for i in range(n)]
dp[0][0] = a[0][0]
dp[0][1] = a[0][1]
dp[0][2] = a[0][2]
for i in range(1,n):
    for j in range(3):
        comp = []
        for k in range(3):
            if k==j:
                continue
            comp.append(dp[i-1][k] + a[i][j])

        dp[i][j] = max(comp)
print(max(dp[n-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())


import numpy as np
d = np.array((a, b, c))

dp = np.zeros((3, N), int)
dp[:, 0] = d[:, 0]

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

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

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

for i in range(1, 1+N):
    for frm_action in range(3):
        for to_action in range(3):
            if frm_action == to_action:
                dp[i][to_action] = max(dp[i][to_action], dp[i-1][frm_action])
            else:
                dp[i][to_action] = max(
                    dp[i][to_action], dp[i-1][frm_action] + A[i-1][to_action])


print(max(dp[-1]))

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

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

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

print(max(res[n]))

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

print(max(aa[-1],bb[-1],cc[-1]))

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

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

Solution (Python):
N = int(input())
a, b, c = 0, 0, 0
for n in range(N):
    an, bn, cn = map(int, input().split())
    a, b, c = an + max(b, c), bn + max(c, a), cn + max(a, b)
print(max(a, b, c))

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 6)

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

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

Solution (Python):
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][2], dp[i-1][0])
    dp[i][2] = c+max(dp[i-1][0], dp[i-1][1])
print(max(dp[N-1]))

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

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

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


submit()

Solution (Python):


N=int(input())

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

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

dp[0][0]=pit[0][0]
dp[0][1]=pit[0][1]
dp[0][2]=pit[0][2]

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

print(max(dp[-1]))

Solution (Python):
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[n-1]))

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





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


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

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

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

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

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

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

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

Solution (Python):
n = int(input())
l = []
for i in range(n):
    x,y,z = map(int,input().split())
    l.append([x,y,z])

dp = []
for _ in range(n):
    dp.append([0,0,0])

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


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

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

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

if __name__ == '__main__':
    main()

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

if __name__ == '__main__':
    main()

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

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

Solution (Python):
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())
dp = [[0] * 3 for _ in range(N)]

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

Solution (Python):

import numpy as np
n = int(input())
a = [list(map(int,input().split())) for _ in range(n)]
dp = [[0 for i in range(3)] for j in range(n+1)]


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

def main():
    
    for i in range(n):
        
        for j in range(3):
            
            for k in range(3):
                if (j == k):
                    continue
                else:
                    dp[i+1][k] = chmax(dp[i+1][k], dp[i][j]+ a[i][k])

    res = 0
    for i in range(3):
        res = chmax(res, dp[n][i])

    print(res)

if __name__ == '__main__' :
    main()

Solution (Python):
N = int(input())
la, lb, lc = [0] * 3

for i in range(N):
    a, b, c = [int(x) for x in input().split()]
    aa = a + max(lb, lc)
    bb = b + max(lc, la)
    cc = c + max(lb, la)
    la = aa
    lb = bb
    lc = cc

print(max(la, lb, lc))

Solution (Python):
def main():
    n = int(input())
    a = 0
    b = 0
    c = 0

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

    print(max(a,b,c))

main()

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

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

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

main()

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

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

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] * 3)

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

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

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

print(max(dp[n]))

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

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

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

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

dp[0][0]=act[0][0]
dp[0][1]=act[0][1]
dp[0][2]=act[0][2]

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

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

Solution (Python):
def main():
    N=int(input())
    dp=[[0]*3 for _ in range(N+1)]
    abc=[list(map(int,input().split())) for _ in range(N)]
    for i in range(N):
        for j in range(3): 
            for k in range(3): 
                if j==k: continue
                dp[i+1][k] = max(dp[i+1][k], dp[i][j]+abc[i][k])
    print(max(dp[N]))
    
if __name__ == '__main__':
    main()

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

Solution (Python):
from sys import stdin


def ii(): return int(stdin.readline())
def fi(): return float(stdin.readline())
def mi(): return map(int, stdin.readline().split())
def fmi(): return map(float, stdin.readline().split())
def li(): return list(mi())
def lsi():
    x=list(stdin.readline())
    x.pop()
    return x
def si(): return stdin.readline()

n=ii()
x, y, z=mi()
for _ in range(n-1):
    a, b, c=mi()
    a+=max(y, z)
    b+=max(x, z)
    c+=max(y, x)
    x, y, z=a, b, c
print(max(x, y, z))

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

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

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

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

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

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

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

Solution (Python):


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

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


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

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

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

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


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

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

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


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

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

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

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

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


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

print(list_act_2)
'''

for j in range(n):
    

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


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


list_act_to_today = [-1,-1,-1]

import numpy as np

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

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

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

print(ans)

Solution (Python):
import itertools
from collections import deque,defaultdict,Counter
from itertools import accumulate
import bisect
from heapq import heappop,heappush,heapify
from fractions import gcd
from copy import deepcopy
import math
import queue
import numpy as np

Mod = 1000000007
import sys
sys.setrecursionlimit(100000) 
  
def sieve_of_eratosthenes(n):
    if not isinstance(n,int):
        raise TypeError("n is not int")
    if n<2:
        raise ValueError("n is not effective")
    prime = [1]*(n+1)
    for i in range(2,int(math.sqrt(n))+1):
        if prime[i] == 1:
            for j in range(2*i,n+1):
                if j%i == 0:
                    prime[j] = 0
    res = []
    for i in range(2,n+1):
        if prime[i] == 1:
            res.append(i)
    return res

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

def main(): 
    n = int(input())
    abc = [list(map(int,input().split())) for i in range(n)]
    dp = [[0]*3 for i in range(n+1)]
    for i in range(n):
        for j in range(3):
            for k in range(3):
                if j == k:continue
                dp[i+1][k] = max(dp[i+1][k],dp[i][j]+abc[i][k])
    print(max(dp[n]))

if __name__ == "__main__":
    main()

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

Solution (Python):
def main():
    N = int(input())
    a = [[0]*3 for _ in range(N)]
    for i in range(N):
        a[i][0], a[i][1], a[i][2] = map(int, input().split())
    
    DP = [[0]*3 for _ in range(N+1)]
    
    for i in range(N):
        for j in range(3):
            for k in range(3):
                if(j == k):
                    continue
                DP[i+1][k] = max(DP[i][j]+a[i][k], DP[i+1][k])
                
    ans = 0
    for m in range(3):
        ans = max(ans, DP[N][m])
    
    print(ans)
    
main()

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

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

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

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

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

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

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

print(max(dp[N]))

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

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

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

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

print(max(dp[N]))

Solution (Python):
from itertools import product


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

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

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

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

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

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

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

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

print(max(dp))

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

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

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

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

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

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

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

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

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

dp = [[0 for i in range(3)] for j in range(n)]
dp[0][0] = a[0]
dp[0][1] = b[0]
dp[0][2] = c[0]

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

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

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

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

print(max(dp[N]))

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

arr = [0] * MAX_N
for i in range(N):
    arr[i] = list(map(int, input().split()))
for i in range(N):
    for j in range(3):
        for k in range(3):
            if j == k:
                continue
            dp[i+1][k] = max(dp[i+1][k], dp[i][j] + arr[i][k])
print(max(dp[N]))

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

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

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

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

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

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

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

print(max(dp[n]))

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

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

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

print(max(dp[n]))

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

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

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

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

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

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

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

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

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

if __name__ == '__main__':
    main()

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

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

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

if __name__ == '__main__':
    main()

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

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

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

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