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

Solution (Python):
import sys
readline = sys.stdin.buffer.readline
def even(n): return 1 if n%2==0 else 0

n = int(readline())
dp = [[0]*3 for _ in range(n)]
lst1 = list(map(int,readline().split()))
for i in range(3):
	dp[0][i] = lst1[i]
for i in range(n-1):
	lst1 = list(map(int,readline().split()))
	dp[i+1][0] = max(dp[i][1]+lst1[0],dp[i][2]+lst1[0])
	dp[i+1][1] = max(dp[i][0]+lst1[1],dp[i][2]+lst1[1])
	dp[i+1][2] = max(dp[i][0]+lst1[2],dp[i][1]+lst1[2])

print(max(dp[-1]))

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

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

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

for day, abc in enumerate(ABC):
  for t_act in range(3):
    if (day == 0):
      dp[day][t_act] = abc[t_act]
      continue
    t_max = dp[day][t_act] 
    for y_act in range(3):
      if(t_act == y_act):
        continue
      happy = dp[day - 1][y_act] + abc[t_act]
      if (t_max < happy):
        t_max = happy
    
    dp[day][t_act] = t_max

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

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

Solution (Python):
n = int(input())
a, b, c = 0, 0, 0
for i in range(n):
    ai, bi, ci = map(int, input().split())
    a, b, c = ai + max(b, c), bi + max(c, a), ci + max(a, b)
print(max(a, b, c))

Solution (Python):
import sys
input=sys.stdin.readline
n=int(input())
l=[[float("INF")]*3 for i in range(n)]
l[0]=list(map(int,input().split()))
for day in range(1,n):
    f=list(map(int,input().split()))
    for choice in range(3):
        l[day][choice]=max(l[day-1][j]+f[choice] for j in range(3) if j!=choice)
print(max(l[-1]))

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

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

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

dp[0][0], dp[0][1], dp[0][2] = hapiness[0][0], hapiness[0][1], hapiness[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]) + hapiness[i][j]
        
        elif j == 1:
            dp[i][j] = max(dp[i-1][0], dp[i-1][2]) + hapiness[i][j]
        
        else:
            dp[i][j] = max(dp[i-1][1], dp[i-1][0]) + hapiness[i][j]

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

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

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):
    lis=list(map(int,input().split()))
    for j in range(3):
        DP[i][j]=lis[j]+max(DP[i-1][(j+1)%3],DP[i-1][(j+2)%3])
print(max(DP[N-1]))

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

for i in range(N):
  for P in range(3):
    for PY in range(3):
      if P==PY:
        continue
      elif i==0:
        dp[i][P]=L[i][P]
      else:
        dp[i][P]=max(dp[i][P],dp[i-1][PY]+L[i][P])
ans=0
for i in range(3):
  ans=max(ans,dp[N-1][i])
print(ans)

Solution (Python):
N=int(input())
L = [list(map(int, input().split())) for i in range(N)]
dp=[[0 for j in range(3)] for i in range(N)]
ans=0
for i in range(N):
  for P in range(3):
    for PY in range(3):
      if P==PY:
        continue
      elif i==0:
        dp[i][P]=L[i][P]
        ans=max(ans,dp[i][P])
      else:
        dp[i][P]=max(dp[i][P],dp[i-1][PY]+L[i][P])
        ans=max(ans,dp[i][P])
'''
for i in range(3):
  ans=max(ans,dp[N-1][i])
'''
print(ans)

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

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

ABC.append([0,0,0])

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

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

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

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

Solution (Python):



'''
3
10 40 70
20 50 80
30 60 90
'''

N = int(input())

dp = [[None] * 3 for _ in range(N)]
dp[0] = list(map(int, input().split()))


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

    dp[i][0] = max(dp[i-1][1], dp[i-1][2]) + data[0]
    dp[i][1] = max(dp[i-1][0], dp[i-1][2]) + data[1]
    dp[i][2] = max(dp[i-1][0], dp[i-1][1]) + data[2]


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

Solution (Python):
n = int(input())
choice = [list(map(int, input().split())) for _ in range(n)]
dp = [[0 for _ in range(3)] for __ in range(n)]
dp[0] = choice[0]

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

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

def main():
    N = int(input())
    reward = [list(map(int, input().split())) for _ in range(N)]
    
    dp = [[0] * 3 for _ in range(N)]
    dp[0] = reward[0]
    
    for i in range(1, N):
        dp[i][0] = max(dp[i - 1][1], dp[i - 1][2]) + reward[i][0]
        dp[i][1] = max(dp[i - 1][0], dp[i - 1][2]) + reward[i][1]
        dp[i][2] = max(dp[i - 1][1], dp[i - 1][0]) + reward[i][2]
    
    print(max(dp[-1]))
    
if __name__ == '__main__':
    main()

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

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

dpa,dpb,dpc = 0,0,0
for n in range(N):
  a,b,c = map(int, input().split())  
  dpa,dpb,dpc = max(dpb+a,dpc+a),max(dpa+b,dpc+b),max(dpa+c,dpb+c)
  
ans = max(dpa,dpb,dpc)
print(ans)

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

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

Solution (Python):
N = int(input())
lst = []
for i in range(N):
    lst.append([int(x) for x in input().split()])
dp = [[0] * 3 for i in range(N + 1)]
for i in range(N):
    dp[i + 1][0] = max(dp[i][1], dp[i][2]) + lst[i][0]
    dp[i + 1][1] = max(dp[i][0], dp[i][2]) + lst[i][1]
    dp[i + 1][2] = max(dp[i][1], dp[i][0]) + lst[i][2]

print(max(dp[-1]))

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

dpa = [alist[0]] + [0]*(n-1)
dpb = [blist[0]] + [0]*(n-1)
dpc = [clist[0]] + [0]*(n-1)
for i in range(1,n):
  dpa[i] = max(dpb[i-1], dpc[i-1]) + alist[i]
  dpb[i] = max(dpa[i-1], dpc[i-1]) + blist[i]
  dpc[i] = max(dpb[i-1], dpa[i-1]) + clist[i]
print(max(dpa[-1], dpb[-1], dpc[-1]))

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

print(max(DP[N]))

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

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

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

for i, abc in enumerate(ABC):
  for k in range(3):
    if (i == 0):
      dp[i][k] = abc[k]
    else:
      max_dp_i = dp[i][k]
      for j in range(3):
        if (k == j):
          continue
        if (max_dp_i < (dp[i-1][j] + abc[k])):
          max_dp_i = (dp[i-1][j] + abc[k])
      
      dp[i][k] = max_dp_i

print(max(dp[-1]))

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

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

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

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

Solution (Python):
import sys
sys.setrecursionlimit(10000000)
 
N = int(input())
H = list(list(map(int,input().split())) for _ in range(N))

memo = {}
def rec(i, choice):
  if (i, choice) in memo:
    return memo[(i, choice)]
  if i == len(H):
    return 0
  m = max(H[i][j] + rec(i+1,j) for j in range(3) if j != choice)
  memo[(i, choice)] = m
  return m
print(max(rec(0, j) for j in range(3)))

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

la,lb,lc = 0,0,0

for _ in range(N):
    a,b,c = map(int,input().split())
    la, lb, lc = a+max(lb,lc), b+max(lc,la), c+max(la,lb)
print(max(la,lb,lc))

Solution (Python):
def solve(n, acts):
    fa = [0] * n
    fb = [0] * n
    fc = [0] * n
    fa[0] = acts[0][0]
    fb[0] = acts[0][1]
    fc[0] = acts[0][2]

    for i in range(1, n):
        fa[i] = acts[i][0] + max(fb[i-1], fc[i-1])
        fb[i] = acts[i][1] + max(fc[i-1], fa[i-1])
        fc[i] = acts[i][2] + max(fa[i-1], fb[i-1])

    return max([fa[-1], fb[-1], fc[-1]])


def main():
    inp = lambda: [int(x) for x in input().split()]
    n = int(input())
    acts = [inp() for _ in range(n)]
    print(solve(n, acts))


if __name__ == '__main__':
    main()

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

Solution (Python):
n=int(input())
l=[]
for i in range(n):
    l.append(list(map(int,input().split())))
dp=[]
for i in range(n):
    dp.append([0]*3)
    
dp[n-1][0]=l[n-1][0]
dp[n-1][1]=l[n-1][1]
dp[n-1][2]=l[n-1][2]
for i in range(n-2,-1,-1):

    dp[i][0]=l[i][0]+max(dp[i+1][1],dp[i+1][2])
    dp[i][1]=l[i][1]+max(dp[i+1][0],dp[i+1][2])
    dp[i][2]=l[i][2]+max(dp[i+1][1],dp[i+1][0])
print(max(max(dp[0][0],dp[0][1]),dp[0][2]))

Solution (Python):
n=int(input())
score=[[]for i in range(n)]
for i in range(n):
    x,y,z=map(int,input().split())
    score[i]=(x,y,z)
dp=[[0]*3 for i in range(n)]
for i in range(3):
    dp[0][i]=score[0][i]

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]+score[i][j])
print(max(dp[-1]))

Solution (Python):
n=int(input())
score=[[]for i in range(n)]
for i in range(n):
    x,y,z=map(int,input().split())
    score[i]=(x,y,z)
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:
                dp[i+1][j]=max(dp[i+1][j],dp[i][k]+score[i][j])
print(max(dp[-1]))

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

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

dp = [[0, 0, 0] for _ 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(1, N):
    t = i-1
    a = H[i][0]
    b = H[i][1]
    c = H[i][2]
    dp[i][0] = max(dp[t][1] + a, dp[t][2] + a)
    dp[i][1] = max(dp[t][0] + b, dp[t][2] + b)
    dp[i][2] = max(dp[t][0] + c, dp[t][1] + c)

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

Solution (Python):
N = int(input())
a,b,c = list(map(int,input().split()))
dpa = a
dpb = b
dpc = c
for i in range(1,N):
    dpap = dpa
    dpbp = dpb
    dpcp = dpc
    a,b,c = list(map(int,input().split()))
    dpa = max(dpbp + a, dpcp + a)
    dpb = max(dpap + b, dpcp + b)
    dpc = max(dpap + c, dpbp + c)
print(max(dpa,dpb,dpc))

Solution (Python):
N = int(input())
dp = [[0 for _ in range(3)] for i in range(N)]
a,b,c = list(map(int,input().split()))
dpa = a
dpb = b
dpc = c
for i in range(1,N):
    dpap = dpa
    dpbp = dpb
    dpcp = dpc
    a,b,c = list(map(int,input().split()))
    dpa = max(dpbp + a, dpcp + a)
    dpb = max(dpap + b, dpcp + b)
    dpc = max(dpap + c, dpbp + c)
print(max(dpa,dpb,dpc))

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

Solution (Python):
N=int(input())
l1=[[] for i in range(N)]
for i in range(N):
    l=list(map(int,input().split()))
    for j in range(len(l)):
        l1[i].append(l[j])
l2=[[] for i in range(N)]
for j in l1[0]:
    l2[0].append(j)
for j in range(1,N):
    l2[j].append(l1[j][0]+max(l2[j-1][1],l2[j-1][2]))
    l2[j].append(l1[j][1]+max(l2[j-1][0],l2[j-1][2]))
    l2[j].append(l1[j][2]+max(l2[j-1][0],l2[j-1][1]))

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

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

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


if __name__ == '__main__':
    main()

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

Solution (Python):
def chmax(a, b):
  if a > b:
    return a
  else:
    return b
n = int(input())
h = [list(map(int, input().split())) for _ in range(n)]
dp = [[0]*3 for _ in range(n)]
dp[0] = h[0]
for i in range(n-1):
  for j in range(3):
    for k in range(3):
      if j == k:
        continue
      else:
        dp[i+1][j] = chmax(dp[i+1][j], dp[i][k] + h[i+1][j]) 
      
print(max(dp[n-1]))

Solution (Python):


N = int(input())

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



inf = float("inf")
n_pattern = 3
dp = [[-inf for _ in range(n_pattern)] for i in range(N + 1)]

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

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

print(max(dp[N]))

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

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

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

f[0] = points[0]
for i in range(1, n):
    for j in range(0, 3):
        for k in range(0, 3):
            if j == k: continue
            f[i][j] = max(f[i][j], f[i - 1][k] + points[i][j])

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

Solution (Python):
from itertools import permutations
N = int(input())
dp = [[0]*3 for i in range(N)]
dp[0] = list(map(int, input().split()))
for i in range(1,N):
    li = list(map(int, input().split()))
    for x, y in permutations([0,1,2], 2):
        dp[i][x] = max(dp[i][x],dp[i-1][y]+li[x])
print(max(dp[N-1]))

Solution (Python):
def zeros(h,*w): return [[0] * w[0]  for _ in range(h)] if w else [[0] * h  for _ in range(h)]

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

dp = zeros(n+1,3)

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

ans = max( dp[n] )

print(ans)

Solution (Python):
def zeros(h,*w): return [[0] * w[0]  for _ in range(h)] if w else [[0] * h  for _ in range(h)]

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

dp = zeros(n+1,3)

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

ans = max( dp[n] )

print(ans)

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

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

Solution (Python):

n=int(input())
N=100100
happy=[[0]*3 for i in range(N)]
dp=[[0]*3 for i in range(N)]
for i in range(1,n+1):
    x,y,z=map(int,input().split())
    happy[i][0]=x
    happy[i][1]=y
    happy[i][2]=z
ans=0
for i in range(1,n+1):
    for place in range(3):
        for place_y in range(3):
            if place==place_y:
                continue
            else:
                dp[i][place]=max(dp[i][place],dp[i-1][place_y]+happy[i][place])

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

Solution (Python):
from bisect import bisect_left as bl
from bisect import bisect_right as br
from heapq import heappush,heappop
import math
from collections import *
from functools import reduce,cmp_to_key
import sys
input = sys.stdin.readline

M = mod = 10**9 +7
def factors(n):return sorted(set(reduce(list.__add__, ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0))))
def inv_mod(n):return pow(n, mod - 2, mod)

def li():return [int(i) for i in input().rstrip('\n').split()]
def st():return input().rstrip('\n')
def val():return int(input().rstrip('\n'))
def li2():return [i for i in input().rstrip('\n')]
def li3():return [int(i) for i in input().rstrip('\n')]
def li4():return [float(i) for i in input().rstrip('\n').split(' ')]





n = val()
l = []
for i in range(n):l.append(li())
dp = [[0]*3 for i in range(n)]
dp[0] = l[0][:]
for i in range(1,n):
    for j in range(3):
        dp[i][j] = l[i][j] + max(dp[i-1][j-1],dp[i-1][j-2])
print(max(dp[-1]))

Solution (Python):
from collections import defaultdict, deque
import sys
import heapq
import bisect
import math
import itertools
import string
import queue
import copy
import time

from fractions import gcd

sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7


def inp(): return int(sys.stdin.readline())


def inp_list(): return list(map(int, sys.stdin.readline().split()))


def lcm(x, y): return (x * y) // gcd(x, y)


N = inp()

a = [0] * N

for i in range(N):
    a[i] = inp_list()



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

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



print(ans)

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):
happiness = [list(map(int, input().split())) for _ in range(int(input()))]

n = len(happiness)

for i in range(1, n):
    for j in range(3):
        cur = float("-inf")
        for k in range(3):
            if j != k:
                cur = max(happiness[i - 1][k], cur)
        happiness[i][j] += cur

print(max(happiness[-1]))

Solution (Python):
import numpy as np
N = int(input())
M = np.zeros((N, 3), dtype='int32')
for i in range(N):
    M[i, 0:3] = list(map(int, input().split()))
H = np.zeros((N, 3), dtype='int32')
H[N - 1, 0] = M[N - 1, 0]
H[N - 1, 1] = M[N - 1, 1]
H[N - 1, 2] = M[N - 1, 2]
for i in range(1, N):
    current_index = N - 1 - i
    H[current_index, 0] = max(M[current_index, 0] +
                              H[current_index + 1, 1], M[current_index, 0] + H[current_index + 1, 2])
    H[current_index, 1] = max(M[current_index, 1] +
                              H[current_index + 1, 0], M[current_index, 1] + H[current_index + 1, 2])
    H[current_index, 2] = max(M[current_index, 2] +
                              H[current_index + 1, 0], M[current_index, 2] + H[current_index + 1, 1])
print(max(H[0, :]))

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product, islice
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')

N = INT()
abc = [LIST() for _ in range(N)]

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]+abc[i-1][(j-1)%3], dp[i-1][(j+1)%3]+abc[i-1][(j+1)%3])
print(max(dp[N]))

Solution (Python):











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

dp = [[0 for j in range(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][j] = max(dp[i][j], dp[i - 1][k] + ABC[i - 1][j])
print(max(dp[-1]))

Solution (Python):
n = int(input())
A, B, C = 0, 0, 0
for _ in range(n):
  a, b, c = map(int, input().split())
  A, B, C = a + max(B, C), b + max(C, A), c + max(A, B)
print(max(A, B, C))

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

print(max(dp[-1]))

Solution (Python):
def main():
    n = int(input())
    x = []
    for _ in range(n):
        a, b, c = map(int, input().split())
        x.append((a, b, c))
    dp0 = [0]*(n+1)
    dp1 = [0]*(n+1)
    dp2 = [0]*(n+1)
    for i in range(1,n+1):
        a, b, c = x[i-1]
        dp0[i] = max(dp1[i-1]+a, dp2[i-1]+a)
        dp1[i] = max(dp0[i-1]+b, dp2[i-1]+b)
        dp2[i] = max(dp0[i-1]+c, dp1[i-1]+c)
    print(max(dp0[n], dp1[n], dp2[n]))

if __name__ == "__main__":
    main()

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

if __name__ == "__main__":
    main()

Solution (Python):

import numpy as np

N = int(input())
value = np.zeros((N, 3))
for i in range(N):
    value[i, :] = list(map(int, input().split()))
value = value.tolist()

dp = [0]*3

for a, b, c in value:
    dp = [max(dp[1], dp[2])+a, max(dp[0], dp[2])+b, max(dp[0], dp[1])+c]
    
print(int(max(dp)))

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

for i in range(1, N+1):
    for place in range(3):
        for yes in range(3):
            if place == yes:
                continue
            dp[i][place] = max(dp[i][place], dp[i-1][yes] + happy[i-1][place])

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

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

for i in range(1, N+1):
    for place in range(3):
        for yes in range(3):
            if place == yes:
                continue
            dp[i][place] = max(dp[i][place], dp[i-1][yes] + happy[i-1][place])

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

Solution (Python):
n=int(input())
arr=[]
for i in range(n):
    a,b,c=map(int,input().split())
    arr.append([a,b,c])
dp0=[0]*n
dp1=[0]*n
dp2=[0]*n
dp0[0]=arr[0][0]
dp1[0]=arr[0][1]
dp2[0]=arr[0][2]
for i in range(1,n):
    dp0[i]=arr[i][0]+max(dp1[i-1],dp2[i-1])
    dp1[i]=arr[i][1]+max(dp0[i-1],dp2[i-1])
    dp2[i]=arr[i][2]+max(dp0[i-1],dp1[i-1])
print(max(dp0[n-1],dp1[n-1],dp2[n-1]))

Solution (Python):
n=int(input())
arr=[]
for i in range(n):
    a,b,c=map(int,input().split())
    arr.append([a,b,c])
dp0=[0]*n
dp1=[0]*n
dp2=[0]*n
dp0[0]=arr[0][0]
dp1[0]=arr[0][1]
dp2[0]=arr[0][2]
for i in range(1,n):
    dp0[i]=arr[i][0]+max(dp1[i-1],dp2[i-1])
    dp1[i]=arr[i][1]+max(dp0[i-1],dp2[i-1])
    dp2[i]=arr[i][2]+max(dp0[i-1],dp1[i-1])
print(max(dp0[n-1],dp1[n-1],dp2[n-1]))

Solution (Python):
N = int(input())
h = []
for i in range(N):
    abc = [int(x) for x in input().split()]
    h.append(abc)
aDP = [0]*N
bDP = [0]*N
cDP = [0]*N
aDP[0] = h[0][0]
bDP[0] = h[0][1]
cDP[0] = h[0][2]
for i in range(1,N):
    aDP[i] = h[i][0] + max(bDP[i-1],cDP[i-1])
    bDP[i] = h[i][1] + max(aDP[i-1],cDP[i-1])
    cDP[i] = h[i][2] + max(bDP[i-1],aDP[i-1])
print(max(aDP[N-1],bDP[N-1],cDP[N-1]))

Solution (Python):
N = int(input())
h = []
for i in range(N):
    abc = [int(x) for x in input().split()]
    h.append(abc)
aDP = [0]*N
bDP = [0]*N
cDP = [0]*N
aDP[0] = h[0][0]
bDP[0] = h[0][1]
cDP[0] = h[0][2]
for i in range(1,N):
    aDP[i] = h[i][0] + max(bDP[i-1],cDP[i-1])
    bDP[i] = h[i][1] + max(aDP[i-1],cDP[i-1])
    cDP[i] = h[i][2] + max(bDP[i-1],aDP[i-1])
print(max(aDP[N-1],bDP[N-1],cDP[N-1]))

Solution (Python):
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)
dp = [[0 for _ in range(3)] for _ in range(N)]
dp[0][0] = A[0]
dp[0][1] = B[0]
dp[0][2] = C[0]
for i in range(1, N):
    dp[i][0] = max(dp[i-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], dp[N-1][1], dp[N-1][2]))

Solution (Python):

import sys


def solve(N: int, a: "List[int]", b: "List[int]", c: "List[int]"):
    dp = [(0,0,0)]*(N+1)
    for i in range(1,N+1):
        A = max(dp[i-1][1],dp[i-1][2])+a[i-1]
        B = max(dp[i-1][0],dp[i-1][2])+b[i-1]
        C = max(dp[i-1][0],dp[i-1][1])+c[i-1]
        dp[i]=(A,B,C)
    print(max(dp[-1]))
    return



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))
    solve(N, a, b, c)

if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
abc = []
for i in range(N):
    abc.append(list(map(int, input().split())))
    
happy = [abc[0]]
for i in range(1,N):
    tmp = happy[i-1]
    max_happy = max(tmp)
    max_happy_index = tmp.index(max_happy)
    
    
    tmp.remove(max_happy)
    if max(tmp)==max_happy:
        max_happy_index = -1
        
    happy_i = [0,0,0]
    for j in range(3):
        if j==max_happy_index:
            happy_i[j] = max(tmp) + abc[i][j]
        else:
            happy_i[j] = max_happy + abc[i][j]
    happy.append(happy_i)

print(max(happy[-1]))

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

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

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


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

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


for i in range(n):
    
    for late_day in range(3):
        for next_day in range(3):
            if late_day != next_day:
                dp[i][next_day] = chmax(dp[i][next_day], dp[i-1][late_day] + a[i][next_day])

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