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())
T = [list(map(int,input().split())) for _ in range(N)]
dp = [[0]*3 for i in range(N)]
dp[0] = T[0]
for i in range(1,N):
  for j in [0,1,2]:
    dp[i][j] = max([dp[i-1][j1]+T[i][j] for j1 in [0,1,2] if j1 != j])
print(max(dp[N-1]))

Solution (Python):
import itertools
import math
import string
import collections
from collections import Counter
from collections import deque
import sys
sys.setrecursionlimit(2*10**5)
INF = 2**60


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


def nCr(n, r):
    return math.factorial(n)//(math.factorial(n-r)*math.factorial(r))


def has_duplicates2(seq):
    seen = []
    for item in seq:
        if not(item in seen):
            seen.append(item)
    return len(seq) != len(seen)


def divisor(n):
    divisor = []
    for i in range(1, n+1):
        if n % i == 0:
            divisor.append(i)
    return divisor



dx = [-1, -1, -1, 0, 0, 1, 1, 1]
dy = [-1, 0, 1, -1, 1, -1, 0, 1]

N = int(input())
a = []
dp = [0]*(N+1)
for i in range(N+1):
    dp[i] = [0]*3

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

dp[0][0] = 0

for i in range(0, 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())
A,B,C=map(int,input().split())
for x in range(n-1):
    a,b,c=map(int,input().split())
    a+=[B,C][B<C]
    b+=[A, C][A < C]
    c+=[B, A][B < A]
    A,B,C=a,b,c
print(max([A,B,C]))

Solution (Python):
from sys import stdin


def main():
    input = lambda: stdin.readline()[:-1]
    N = int(input())
    ABC = [list(map(int, input().split())) for _ in [0] * N]

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


main()

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

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


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

dp[0][0]=arr[0][0]
dp[0][1]=arr[0][1]
dp[0][2]=arr[0][2]


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

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

Solution (Python):
def main():
    n, *abc = map(int, open(0).read().split())
    x, y, z = 0, 0, 0
    for (a, b, c) in zip(*[iter(abc)] * 3):
        x, y, z = a + max(y, z), b + max(z, x), c + max(x, y)

    ans = max([x, y, z])
    print(ans)


if __name__ == '__main__':
    main()

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

dp_a = [0]*(N+1)
dp_b = [0]*(N+1)
dp_c = [0]*(N+1)

for i in range(N):
    a, b, c = map(int,input().split())
    dp_a[i+1] = max(dp_b[i],dp_c[i]) + a
    dp_b[i+1] = max(dp_c[i],dp_a[i]) + b
    dp_c[i+1] = max(dp_a[i],dp_b[i]) + c

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

Solution (Python):
def main():
    n, *abc = map(int, open(0).read().split())
    x, y, z = 0, 0, 0
    for i, (a, b, c) in enumerate(zip(*[iter(abc)] * 3), 1):
        x, y, z = a + max(y, z), b + max(z, x), c + max(x, y)
    
    ans = max([x, y, z])
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    n, *abc = map(int, open(0).read().split())
    dp = [[0, 0, 0] for _ in range(n + 1)]
    for i, (a, b, c) in enumerate(zip(abc[::3], abc[1::3], abc[2::3]), 1):
        x, y, z = dp[i - 1]
        dp[i] = [a + max(y, z), b + max(z, x), c + max(x, y)]

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


if __name__ == '__main__':
    main()

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

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

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

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

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

    print(max(dp[-1]))

if __name__ == '__main__':
    main()

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

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

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

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

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

Solution (Python):




inf = 10**9+7

n = int(input())
dp_a = [0 for i in range(n+1)]
dp_b = [0 for i in range(n+1)]
dp_c = [0 for i in range(n+1)]

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

Solution (Python):
N = int(input())
A, B, C = map(int, input().split())
dp = [[0] * 3 for _ in range(N)]
dp[0] = [A, B, C]

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

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

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

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

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


if __name__ == "__main__":
    main()

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

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

print(ans)

Solution (Python):

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

N = int(input())

A, B, C = 0, 0, 0 
for _ in range(N):
    a, b, c = map(int, input().split())
    A, B, C = max(B, C) + a, max(C, A) + b, max(A, B) + c
ans = max(A, B, C)

print(ans)

Solution (Python):
def resolve():
    n = int(input())
    happy = [[0]*3 for i in range(n)]
    for i in range(n):
        a, b, c = map(int, input().split())
        if i == 0:
            happy[0][0] = a
            happy[0][1] = b
            happy[0][2] = c
        else:
            happy[i][0] = a + max(happy[i-1][1], happy[i-1][2])
            happy[i][1] = b + max(happy[i - 1][0], happy[i - 1][2])
            happy[i][2] = c + max(happy[i - 1][0], happy[i - 1][1])
    print(max(happy[n-1]))
resolve()

Solution (Python):
def resolve():
    n = int(input())
    happy = [[0]*3 for i in range(n)]
    for i in range(n):
        a, b, c = map(int, input().split())
        if i == 0:
            happy[0][0] = a
            happy[0][1] = b
            happy[0][2] = c
        else:
            happy[i][0] = a + max(happy[i-1][1], happy[i-1][2])
            happy[i][1] = b + max(happy[i - 1][0], happy[i - 1][2])
            happy[i][2] = c + max(happy[i - 1][0], happy[i - 1][1])
    print(max(happy[n-1]))
resolve()

Solution (Python):


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

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

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

dp = [[-float('inf')]*3 for _ in range(N+1)]
for j in range(3):
  dp[0][j] = 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]+A[i][k])

print(max(dp[N]))

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


dp = [[0, 0, 0] for _ in range(n+1)]
for i in range(n):
    for place_y in range(3):
        for place in range(3):
            if place_y != place:
                dp[i+1][place] = max(
                    dp[i+1][place], dp[i][place_y]+abc[i][place]
                )

print(max(dp[n]))

Solution (Python):






"""Module docstring
"""

import sys


def solve(values, _nb):
    ca, cb, cc = 0, 0, 0
    for a, b, c in values:
        ca, cb, cc = max(cb + b, cc + c), max(ca + a, cc + c), max(ca + a, cb + b)
    return max(ca, cb, cc)


def do_job():
    "Do the work"
    
    N = int(input().strip())
    values = []
    for _ in range(N):
        values.append(map(int, input().split()))
    result = solve(values, N)
    print(result)


def print_output(testcase, result) -> None:
    "Formats and print result"
    if result is None:
        result = "IMPOSSIBLE"
    print("Case 


def main(argv=None):
    "Program wrapper."
    if argv is None:
        argv = sys.argv[1:]
    do_job()
    return 0


if __name__ == "__main__":
    import doctest

    doctest.testmod()
    sys.exit(main())

Solution (Python):






"""Module docstring
"""

import sys


def solve(values, _nb):
    ca, cb, cc = 0, 0, 0
    for a, b, c in values:
        new_ca = max(cb + b, cc + c)
        new_cb = max(ca + a, cc + c)
        new_cc = max(ca + a, cb + b)
        ca, cb, cc = new_ca, new_cb, new_cc
    return max(ca, cb, cc)


def do_job():
    "Do the work"
    
    N = int(input().strip())
    values = []
    for _ in range(N):
        values.append(map(int, input().split()))
    result = solve(values, N)
    print(result)


def print_output(testcase, result) -> None:
    "Formats and print result"
    if result is None:
        result = "IMPOSSIBLE"
    print("Case 


def main(argv=None):
    "Program wrapper."
    if argv is None:
        argv = sys.argv[1:]
    do_job()
    return 0


if __name__ == "__main__":
    import doctest

    doctest.testmod()
    sys.exit(main())

Solution (Python):
def chmax_a(l,B,C,i):
	return max(B+l[i][1],C+l[i][2])

def chmax_b(l,A,C,i):
	return max(A+l[i][0],C+l[i][2])

def chmax_c(l,A,B,i):
	return max(B+l[i][1],A+l[i][0])

if __name__=='__main__':
	N=int(input())
	happiness=[]
	for i in range(N):
		cost=list(map(int,input().split()))
		happiness.append(cost)
	answers=[]
	A=0
	B=0
	C=0
	for i in range(N-1,-1,-1):
		a=chmax_a(happiness,B,C,i)
		b=chmax_b(happiness,A,C,i)
		c=chmax_c(happiness,A,B,i)
		A=a
		B=b
		C=c
	print(max(A,B,C))

Solution (Python):
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines


def main():
    N = int(readline())
    abc = map(int, read().split())
    x, y, z = 0, 0, 0
    for a, b, c in zip(abc, abc, abc):
        x, y, z = max(y, z) + a, max(x, z) + b, max(x, y) + c

    print(max(x, y, z))


if __name__ == '__main__':
    main()

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

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

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

print(max(dp[n]))

Solution (Python):
n = int(input())
a = [[int(x) for x in 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:
        dp[i+1][j] = max(dp[i+1][j], dp[i][k] + a[i][j])
print(max(dp[n][0], dp[n][1], dp[n][2]))

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

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

print(max(dp[-1]))

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

dp = [[0] * 3 for i in range(n)]
dp[0] = list(map(int, input().split()))

for i in range(1, n):
    a, b, c = map(int, input().split())
    dp[i][0] = max(dp[i - 1][1] + a, dp[i - 1][2] + a)
    dp[i][1] = max(dp[i - 1][2] + b, dp[i - 1][0] + 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_ls = [0] * n
b_ls = [0] * n
c_ls = [0] * n
for i in range(n):
    a,b,c = map(int,input().split())
    a_ls[i] = a
    b_ls[i] = b
    c_ls[i] = c

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

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

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

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

Solution (Python):
n=int(input())
mat=[]
dp=[]

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

dp[0][0]=mat[0][0]
dp[0][1]=mat[0][1]
dp[0][2]=mat[0][2]

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

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

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

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


for i in range(0, N):
    if i == 0:
        table[i] = ABC[i]
    else:
        for j in range(3): 
            table[i][j] = max(
                [table[i-1][j1] + ABC[i][j] for j1 in range(3) if not j1 == j ]
                 )

        
print(max(table[-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)

dpa=[0]*(N+1)
dpb=[0]*(N+1)
dpc=[0]*(N+1)

for i in range(N):
  dpa[i]=max(dpb[i-1],dpc[i-1])+A[i]
  dpb[i]=max(dpa[i-1],dpc[i-1])+B[i]
  dpc[i]=max(dpa[i-1],dpb[i-1])+C[i]

print(str(max(dpa[N-1],dpb[N-1],dpc[N-1])))

Solution (Python):
import numpy as np

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

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

print(max(dp))

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

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

Solution (Python):
def resolve():
    N = int(input())
    abc = []
    for i in range(N):
        abc.append([int(i) for i in input().split()])
        if i == 0:
            dp = [abc[0]]
        else:
            dp.append(
                [
                    abc[i][j] + max(dp[-1][k] for k in range(3) if j != k)
                    for j in range(3)
                ]
            )
    print(max(dp[-1]))


if __name__ == "__main__":
    resolve()

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

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

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

Solution (Python):


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


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

Solution (Python):


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


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

Solution (Python):


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


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

Solution (Python):

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

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):
import sys
input = sys.stdin.readline
n = int(input())
dp = [[0]*3 for _ in range(n+1)]
for i in range(n):
    a, b, c = map(int, input().split())
    dp[i+1][0] = max(dp[i][1], dp[i][2])+a
    dp[i+1][1] = max(dp[i][0], dp[i][2])+b
    dp[i+1][2] = max(dp[i][0], dp[i][1])+c
print(max(dp[-1]))

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

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

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


for i, abc in enumerate(abc_list):
    for j in range(3):
        temp = dp[i][:]
        temp.pop(j)
        dp[i+1][j] = max(temp) + abc[j]

print(max(dp[-1]))

Solution (Python):
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])
result = max(dp[n][0], dp[n][1], dp[n][2])
print(result)

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

Solution (Python):

import sys


def solve(N: int, a: "List[int]", b: "List[int]", c: "List[int]"):

    dp = []
    for _ in range(N):
        l = [0]*3
        dp.append(l)

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

    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())
a = [[int(i) for i in input().split()] for _ in range(N)]
dp = [[0]*3 for __  in range(N)]
dp[0] = a[0]
for i in range(1,N):
    for j in range(3):
        dp[i][j] = max(dp[i-1][(j+1)%3],dp[i-1][(j+2)%3])+a[i][j]
print(max(dp[N-1]))

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

dp[0][0] = potential_plans[0][0]
dp[0][1] = potential_plans[0][1]
dp[0][2] = potential_plans[0][2]

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

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

Solution (Python):
n=int(input())
mat=[]
dp=[]

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

dp[0][0]=mat[0][0]
dp[0][1]=mat[0][1]
dp[0][2]=mat[0][2]

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

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

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

Solution (Python):



def main():
    n = int(input())
    work_list = [tuple(map(int, input().split())) for i in range(n)]

    
    
    
    
    
    
    
    
    dp_pre = [0] * 3
    dp_now = [0] * 3
    for i, work in enumerate(work_list):
        for i_now in range(3):
            for i_pre in range(3):
                if i_now == i_pre:
                    continue

                dp_now[i_now] = max(dp_now[i_now], dp_pre[i_pre] + work[i_now])

        dp_pre[0] = dp_now[0]
        dp_pre[1] = dp_now[1]
        dp_pre[2] = dp_now[2]

    print(max(dp_now))


if __name__ == '__main__':
    main()

Solution (Python):



def main():
    n = int(input())
    work_list = [tuple(map(int, input().split())) for i in range(n)]

    dp = [[0] * 3 for i in range(n + 1)]
    for i, work in enumerate(work_list):
        for i_now in range(3):
            for i_pre in range(3):
                if i_now == i_pre:
                    continue

                dp[i + 1][i_now] = max(dp[i + 1][i_now], dp[i][i_pre] + work[i_now])

    print(max(dp[-1]))


if __name__ == '__main__':
    main()

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

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

print(ans)

Solution (Python):



def main():
    n = int(input())
    work_list = [tuple(map(int, input().split())) for i in range(n)]

    dp = [[0] * 3 for i in range(n)]
    for i, work in enumerate(work_list):
        for i_now in range(3):
            for i_pre in range(3):
                if i_now == i_pre:
                    continue

                if i - 1 >= 0:
                    dp[i][i_now] = max(dp[i][i_now], dp[i - 1][i_pre] + work[i_now])
                else:
                    dp[i][i_now] = work[i_now]

    print(max(dp[-1]))


if __name__ == '__main__':
    main()

Solution (Python):
n=int(input())
happy=[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(1,n+1):
  for place in range(3):
    for placeY in range(3):
      if place==placeY:
        continue
      dp[i][place]=max(dp[i][place],
                       dp[i-1][placeY]+happy[i-1][place])
      
ans=0
for place in range(3):
  ans=max(ans,dp[n][place])
print(ans)

Solution (Python):


n = int(input())

A = [[]]
for _ in range(n):
    A += [[int(x) for x in input().split()]]

B = [[0, 0, 0] for _ in range(n + 1)]

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

print(max(B[n]))

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

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

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

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

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

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

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

dp[0][0],dp[0][1],dp[0][2] = map(int,input().split())

for i in range(1,n):
    abc = map(int,input().split())
    for j,v in enumerate(abc):
        for k in range(3):
            if j != k:
                dp[i][j] = max(dp[i][j],dp[i-1][k] + v)

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

Solution (Python):
import operator as op

def main():
	N   = int(input())
	abc = [tuple(map(int, input().split())) for _ in range(N)]

	a = list( map(op.itemgetter(0), abc) )
	b = list( map(op.itemgetter(1), abc) )
	c = list( map(op.itemgetter(2), abc) )

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

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

main()

Solution (Python):
n=int(input())
x=[[0]*3 for i in range(n)]

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

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

for _ in range(n):
    a = [int(i) for i in input().split()]
    c = [0] * 3
    for i in range(3):
        for j in range(3):
            if i == j:
                continue
            c[i] = max(c[i], b[j] + a[i])
    b = c
print(max(b))