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):
def rr(): return input().rstrip()
def rri(): return int(rr())
def rrl(): return list(map(int, rr().split()))
def rrt(): return tuple(map(int, rr().split()))
from collections import defaultdict
def mus(d=lambda: 0): return defaultdict(lambda: defaultdict(d))
def dd0(d=lambda: 0): return defaultdict(d)
def ms(x, y, d=0): return [[0]*y for i in range(x)]
def ar(x, d=0): return [d]*x
def ppm(m, n=0, x=0, y=0): print("\n".join(("\t".join((str(m[j][i]) for j in range(y or n))) for i in range(x or n))))
def ppa(a, n): print("\t".join(map(str, a[0:n])))
def ppl(a=0, x=[1]): x[0]=a or (x[0]+1); print("-"+"- -"*14+"-
def fltn(a): (i for sub in a for i in sub)
def yn(x): return "YES" if x else "NO"
from itertools import accumulate
from itertools import groupby
def grp(x): return ((i, sum(1 for _ in g)) for i, g in groupby(x))
import math


def dpp(a, b=""): print(")

def read():
    n = rri()
    l = [rrt() for _ in range(n)]

    return n, l
    return (0,)

def solve(n, l):
    dp0 = ar(n)
    dp1 = ar(n)
    dp2 = ar(n)
    

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

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

    
    
    

    ans = max(dp0[-1], dp1[-1], dp2[-1])
    return ans

if __name__ == "__main__":
    
    test_count = 1
    for _ in range(test_count):
        input_data = read()
        result = solve(*input_data)
        print(result)

Solution (Python):
N=int(input())
l=[]
for i in range(N):
  l.append(list(map(int,input().split())))
def pat(j):
  if j==0:
    return [1,2]
  elif j==1:
    return [0,2]
  else:
    return [0,1]
dp=[[0]*3 for _ in range(N+1)]
for i in range(1,N+1):
  for j in range(3):
    dp[i][j]=l[i-1][j]+max(dp[i-1][k] for k in pat(j))
print(max(dp[-1]))

Solution (Python):
N = int(input())
l = [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):
    for j in range(3):
        for k in range(3):
            if j != k:
                dp[i][j] = max(dp[i][j], dp[i-1][k]+l[i-1][k])

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

Solution (Python):
N=int(input())
A=list()
B=list()
C=list()
DP_A=[0]
DP_B=[0]
DP_C=[0]
for i in range(N):
    a,b,c=input().split()
    A.append(int(a))
    B.append(int(b))
    C.append(int(c))
for i in range(1,N+1):
    if DP_B[i-1] + A[i-1] > DP_C[i-1] + A[i-1]:
        DP_A.append(DP_B[i-1] + A[i-1])
    else:
        DP_A.append(DP_C[i-1] + A[i-1])

    if DP_C[i-1] + B[i-1] > DP_A[i-1] + B[i-1]:
        DP_B.append(DP_C[i-1] + B[i-1])
    else:
        DP_B.append(DP_A[i-1] + B[i-1])

    if DP_A[i-1] + C[i-1] > DP_B[i-1] + C[i-1]:

        DP_C.append(DP_A[i-1] + C[i-1])
    else:
        DP_C.append(DP_B[i-1] + C[i-1])
print(max(DP_A[N], DP_B[N], DP_C[N]))

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

dp = [[float("inf") for _ in range(3)] for _ in range(N + 1)]
dp[0] = [0, 0, 0]

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

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

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

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

Solution (Python):
n=int(input())
a=[0]*n
b=[0]*n
c=[0]*n
ka=[-1]*n
kb=[-1]*n
kc=[-1]*n
for i in range(n):
  a[i],b[i],c[i]=map(int,input().split())

ka[0]=a[0]
kb[0]=b[0]
kc[0]=c[0]

for i in range(1, n):
  ka[i]=max(kb[i-1],kc[i-1])+a[i]
  kb[i]=max(ka[i-1],kc[i-1])+b[i]
  kc[i]=max(kb[i-1],ka[i-1])+c[i]

print(max(ka[n-1],kb[n-1],kc[n-1]))

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

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

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

print(max(dp[n]))

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

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
            else:
                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())
a_b_c_list=[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(n):

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

Solution (Python):
import sys

input = sys.stdin.readline

days = int(input())

prev = [0, 0, 0]
while days:
    nxt = [0, 0, 0]
    a, b, c = map(int, input().split())
    nxt[0] = a + max(prev[1], prev[2])
    nxt[1] = b + max(prev[0], prev[2])
    nxt[2] = c + max(prev[0], prev[1])
    prev = nxt
    days -= 1

print(max(prev))

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

prev = [0, 0, 0]
while days:
    nxt = [0, 0, 0]
    a, b, c = map(int, input().split())
    nxt[0] = a + max(prev[1], prev[2])
    nxt[1] = b + max(prev[0], prev[2])
    nxt[2] = c + max(prev[0], prev[1])
    prev = nxt
    days -= 1

print(max(prev))

Solution (Python):
n = int(input())
dpa = 0
dpb = 0
dpc = 0
for i in range(n):
    a,b,c = map(int,input().split())
    dpat,dpbt,dpct = dpa,dpb,dpc
    dpa = max(dpbt,dpct) + a
    dpb = max(dpct,dpat) + b
    dpc = max(dpat,dpbt) + c
print(max(dpa,dpb,dpc))

Solution (Python):
def DP_C() :
    N = int(input())
    a = []
    b = []
    c = []
    for i in range(N) :
        ai, bi, ci = map(int, input().split())
        a.append(ai)
        b.append(bi)
        c.append(ci)

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

    return max(dp[N-1])

print(DP_C())

Solution (Python):
N = int(input())
A = list()
for n in range(N):
	A.append(list(map(int, input().split())))
dp = [[-1 for i in range(3)] for j in range(N)]
dp[0] = [A[0][0], A[0][1], 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):
from sys import stdin,stdout
n=input()
l=[]
for i in range(n):
    l1=list(map(int,stdin.readline().split()))
    l.append(l1)
a,b,c=l[0][0],l[0][1],l[0][2]
for i in range(1,n):
    x,y,z=a,b,c
    a=l[i][0]+max(y,z)
    b=l[i][1]+max(x,z)
    c=l[i][2]+max(x,y)
stdout.write(str(max(a,b,c))+'\n')

Solution (Python):
def main():
    N=int(input())
    dp=[[0]*(3) 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]=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][1],dp[i-1][0])
    print(max(dp[-1][0],dp[-1][1],dp[-1][2]))
main()

Solution (Python):
N = int(input())
a = [[0]]*(N)
for i in range(N):
  a[i] = list(map(int, input().split()))
DP = [[0] * 3 for _ in range(N+3)]
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):
import sys
sys.setrecursionlimit(10 ** 7)

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N = int(readline())
ABC = [[int(x) for x in readline().split()] for _ in range(N)]

dp = [[0] * 3 for _ in range(10 ** 5 + 10)]

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

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

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

Solution (Python):
def main():
    n = int(input())
    abc = [list(map(int, input().split())) for _ in range(n)]
    a, b, c = zip(*abc)
    srs_a = {-1: 0}
    srs_b = {-1: 0}
    srs_c = {-1: 0}
    def dp_a(i):
        return max(srs_b[i-1], srs_c[i-1]) + a[i]
    def dp_b(i):
        return max(srs_c[i-1], srs_a[i-1]) + b[i]
    def dp_c(i):
        return max(srs_a[i-1], srs_b[i-1]) + c[i]
    for i in range(n):
        srs_a[i] = dp_a(i)
        srs_b[i] = dp_b(i)
        srs_c[i] = dp_c(i)
    print(max(srs_a[n-1], srs_b[n-1], srs_c[n-1]))


main()

Solution (Python):
n = int(input())
da, db, dc = 0, 0, 0

for i in range(n):
    a, b, c = map(int, input().split())
    da, db, dc = max(db, dc) + a, max(da, dc) + b, max(da, db) + c

print(max(da, db, dc))

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

Solution (Python):





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


for i in range(n):
    if i == 0:
        t1 = score[i][0]
        t2 = score[i][1]
        t3 = score[i][2]
    else:
        x1 = t1
        x2 = t2
        x3 = t3

        t1 = max(x2,x3) + score[i][0]
        t2 = max(x1,x3) + score[i][1]
        t3 = max(x1,x2) + score[i][2]

print(max(t1,t2,t3))

Solution (Python):
import sys
input=sys.stdin.readline
a=int(input())
ans=[]
total=0
for i in range(a):
    z=list(map(int,input().split()))
    ans.append(z)
for i in range(a):
    if(i==0):
        t1=ans[0][0]
        t2=ans[0][1]
        t3=ans[0][2]
        
    else:
        x1=t1
        x2=t2
        x3=t3
        t1=max(x2,x3)+ans[i][0]
        t2=max(x1,x3)+ans[i][1]
        t3=max(x1,x2)+ans[i][2]
        
    
print(max(t1,t2,t3))

Solution (Python):
N = int(input())
DP = [[0]*3 for i in range(N+1)]
for i in range(1,N+1):
    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[-1]))

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][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):
        for k in range(3):
            if j != k:
                dp[i][j] = max(dp[i][j],dp[i-1][k]+A[i][j])

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

Solution (Python):
"""
@author:  wangquaxiu
@time:  2020/3/7 19:47
"""

import numpy as np
n = int(input())
dp = np.zeros((n,3),dtype=np.int)
happy = []
for i in range(n):
    d = list(map(int,input().split()))
    happy.append(d)

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

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


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

Solution (Python):
N = int(input())
l = [list(map(int,input().split())) for _ in [0]*N]

dp_a = [0]*N
dp_b = [0]*N
dp_c = [0]*N
dp_a[0] = l[0][0]
dp_b[0] = l[0][1]
dp_c[0] = l[0][2]

for i in range(1,N):
    dp_a[i] = max(dp_b[i-1],dp_c[i-1])+l[i][0]
    dp_b[i] = max(dp_a[i-1],dp_c[i-1])+l[i][1]
    dp_c[i] = max(dp_b[i-1],dp_a[i-1])+l[i][2]

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

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

res = h[0]
for i in range(1, n):
    tmp = res[:]
    for j in range(3):
        res[j] = h[i][j] + max(tmp[(j + 1) % 3], tmp[j - 1])
print(max(res))

Solution (Python):
dp=[[0]*3 for _ in range(10**5+1)]
n=int(input())
abc=[[int(i) for i in input().split()] for _ in range(n)]

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

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

dp = nums[0]

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

print(max(dp))

Solution (Python):
def max_ex(target, iex):
  ret = 0
  i_ret = 0
  for v,i in enumerate(range(len(target))):
    if i == iex:
      continue
    if v > ret:
      ret = v
      i_ret = i
  
  return [ret,i_ret]

n = int(input())
abc = []
dp = []
for i in range(n):
  abc.append(list(map(int, input().split())))
  
  if i == 0:
    dp.append(abc[0])
  else:
    dpa = abc[i][0] + max(dp[i-1][1],dp[i-1][2])
    dpb = abc[i][1] + max(dp[i-1][0],dp[i-1][2])
    dpc = abc[i][2] + max(dp[i-1][0],dp[i-1][1])
    dp.append([dpa,dpb,dpc])
    
print('{}'.format(max(dp[n-1])))

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

if __name__ == "__main__":
    main()

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

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())

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[n-1]))

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

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

print(max(x,y,z))

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

dp[0][0], dp[0][1], dp[0][2] = list(map(int, input().split()))
for i in range(1,N):
  abc = list(map(int, input().split()))
  for j,k,l in ((0,1,2), (1,0,2), (2, 0, 1)):
    dp[i][j] = abc[j] + max(dp[i-1][k], dp[i-1][l])

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

Solution (Python):








def resolve():
    n=int(input())

    [A,B,C]=[[0]*n for _ in range(3)]
    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]+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]))



resolve()

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

for i in range(n):
    inp.append(list(map(lambda x: int(x), input().split(" "))))

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

for i in range(n):
    for j in range(3):
        if (i<n-1):
            m1 = dp[i][j] + inp[i+1][(j+1)%3]
            m2 = dp[i][j] + inp[i+1][(j+2)%3]
            if (dp[i+1][(j+1)%3] < m1):
                dp[i+1][(j+1)%3] = m1
            if (dp[i+1][(j+2)%3] < m2):
                dp[i+1][(j+2)%3] = m2

print(max(dp[-1]))

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

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

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

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

Solution (Python):

def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10000000)
    from collections import Counter, deque
    
    from itertools import combinations, permutations
    
    from bisect import bisect_left,bisect_right
    from math import floor, ceil
    

    

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

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


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10000000)
    from collections import Counter, deque
    
    from itertools import combinations, permutations
    
    from bisect import bisect_left,bisect_right
    from math import floor, ceil
    

    

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

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


if __name__ == '__main__':
    main()

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] = map(int,input().split())

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

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] = map(int,input().split())

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

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10000000)
    from collections import Counter, deque
    
    from itertools import combinations, permutations
    
    from bisect import bisect_left,bisect_right
    from math import floor, ceil
    

    

    N = int(input())
    l = [list(map(int, input().split())) for _ in range(N)]
    dp = [[0]*3 for _ in range(N+1)]
    dp[1] = l[0]
    
    for i in range(1, N):
        for j in range(3):
            a = (j+1)%3
            b = (j+2)%3
            dp[i+1][j] = max(dp[i][a], dp[i][b]) + l[i][j]
    print(max(dp[N]))

if __name__ == '__main__':
    main()

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10000000)
    from collections import Counter, deque
    
    from itertools import combinations, permutations
    
    from bisect import bisect_left,bisect_right
    from math import floor, ceil
    

    

    N = int(input())
    l = [list(map(int, input().split())) for _ in range(N)]
    dp = [[0]*3 for _ in range(N+1)]
    dp[1][0] = l[0][0]
    dp[1][1] = l[0][1]
    dp[1][2] = l[0][2]
    
    for i in range(1, N):
        for j in range(3):
            a = (j+1)%3
            b = (j+2)%3
            dp[i+1][j] = max(dp[i][a], dp[i][b]) + l[i][j]
    print(max(dp[N]))


if __name__ == '__main__':
    main()

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

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

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

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

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):
    for j in range(3):
        sc = 0
        for k in range(3):
            if j == k:
                t = dp[i-1][k]
            else:
                t = dp[i-1][k] + abc[i][j]
            if sc < t:
                sc = t
        dp[i][j] = sc
print(max(dp[N-1]))

Solution (Python):
import sys


def input():
    return sys.stdin.readline().strip()


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


dp = [[0 for j in range(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[-1]))

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


dp = [[0 for j in range(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[-1]))

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

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

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

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

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


if __name__ == '__main__':
    main()

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

happiness = [tuple(map(int, input().split())) for _ in range(n)]
dp = [[0]*3 for _ in range(n)]

dp[0][0] = happiness[0][0]
dp[0][1] = happiness[0][1]
dp[0][2] = happiness[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] + happiness[i][j])
print(max(dp[n-1]))

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

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


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

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

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

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

Solution (Python):
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd

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()))



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

N = INT()
L = [LIST() for i in range(N)]

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


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

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

Solution (Python):
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd

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()))



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

N = INT()
L = [LIST() for i in range(N)]

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


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

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

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