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):
import sys
input = sys.stdin.readline
n = int(input())
abc = [list(map(int,input().split())) for i in range(n)]
f_min = 0 
dp = [[f_min] * 3 for _ in range(n)]
def chmin(a, b):
    if a > b:
        return b
    else:
        return a
def chmax(a,b):
    if a < b:
        return b
    else:
        return a
def main():
    dp[0][0], dp[0][1], dp[0][2] = abc[0][0], abc[0][1], abc[0][2]
    for i in range(1,n):
            a,b,c = abc[i][0], abc[i][1], abc[i][2]
            dp[i][0] = chmax(dp[i][0], chmax(dp[i-1][1] + a, dp[i-1][2] +a))
            dp[i][1] = chmax(dp[i][1], chmax(dp[i-1][0] + b, dp[i-1][2] +b))
            dp[i][2] = chmax(dp[i][2], chmax(dp[i-1][0] + c, dp[i-1][1] +c))
    print(chmax(dp[n-1][0], chmax(dp[n-1][1],dp[n-1][2])))

if __name__ == "__main__":
    main()

Solution (Python):
import sys
input = sys.stdin.readline
n = int(input())
abc = [list(map(int,input().split())) for i in range(n)]
f_min = 0 
dp = [[f_min] * 3 for _ in range(n)]
def chmin(a, b):
    if a > b:
        return b
    else:
        return a
def chmax(a,b):
    if a < b:
        return b
    else:
        return a
def main():
    dp[0][0], dp[0][1], dp[0][2] = abc[0][0], abc[0][1], abc[0][2]
    for i in range(1,n):
            a,b,c = abc[i][0], abc[i][1], abc[i][2]
            dp[i][0] = chmax(dp[i][0], chmax(dp[i-1][1] + a, dp[i-1][2] +a))
            dp[i][1] = chmax(dp[i][1], chmax(dp[i-1][0] + b, dp[i-1][2] +b))
            dp[i][2] = chmax(dp[i][2], chmax(dp[i-1][0] + c, dp[i-1][1] +c))
    print(chmax(dp[n-1][0], chmax(dp[n-1][1],dp[n-1][2])))

if __name__ == "__main__":
    main()

Solution (Python):


import sys
 
stdin = sys.stdin
inf = 1 << 60
mod = 1000000007
 
ni = lambda: int(ns())
nin = lambda y: [ni() for _ in range(y)]
na = lambda: list(map(int, stdin.readline().split()))
nan = lambda y: [na() for _ in range(y)]
nf = lambda: float(ns())
nfn = lambda y: [nf() for _ in range(y)]
nfa = lambda: list(map(float, stdin.readline().split()))
nfan = lambda y: [nfa() for _ in range(y)]
ns = lambda: stdin.readline().rstrip()
nsn = lambda y: [ns() for _ in range(y)]
ncl = lambda y: [list(ns()) for _ in range(y)]
nas = lambda: stdin.readline().split()

N = ni()
abc = nan(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][j] = max(dp[i + 1][j], dp[i][k] + abc[i][j])

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

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

dp = [[0] * 3 for _ in range(100010)]
for i in range(1, n + 1):
    for j in range(3):
        for k in range(3):
            if j == k:
                continue
            if dp[i][k] < dp[i - 1][j] + a[i - 1][k]:
                dp[i][k] = dp[i - 1][j] + a[i - 1][k]
print(max(dp[n]))

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

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

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

print(solve())

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

Solution (Python):
import sys

N = int(input())

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

dp[0][0], dp[0][1], dp[0][2] = map(int, sys.stdin.readline().split())

tmp = [0, 0, 0]

for i in range(1, N):
    tmp[0], tmp[1], tmp[2] = map(int, sys.stdin.readline().split())
    for j in range(3):
        dp[i][j] = max(dp[i-1][(j+1)%3]+tmp[j], dp[i-1][(j+2)%3]+tmp[j])

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

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

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

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

print(max(dp[N]))

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

Solution (Python):

import sys


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


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

dp = [[0 for _ in range(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] + DATA[i][k])

res = 0

for v in range(3):
    res = max(res, dp[N][v])

print(res)

Solution (Python):
import sys

input = sys.stdin.readline

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

dp = [[0 for i in range(3)] for j in range(vacation+1)]
def chmax(a, b):
    if a > b:
        return a
    else:
        return b

for i in range(vacation):
    for j in range(3):
        for k in range(3):
            if j == k:
                continue
            dp[i+1][k] = chmax(dp[i+1][k], dp[i][j] + lst_happiness[i][k])
ans = 0
for i in range(3):
    ans = chmax(ans, dp[vacation][i])
print(ans)

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

x, y, z = 0, 0, 0
for a, b, c in h:
    x, y, z = max(y,z) + a, max(x,z) + b, max(x,y) + c

print(max(x, y, z))

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

a, b, c = 0, 0, 0
for x, y, z in abc:
    a, b, c = max(b, c) + x, max(a, c) + y, max(a, b) + z

print(max(a, b, c))

Solution (Python):
dpa = [-float('inf') for _ in range(100010)]
dpb = [-float('inf') for _ in range(100010)]
dpc = [-float('inf') for _ in range(100010)]


N = int(input())
a = []
b = []
c = []

for i in range(N):
    a1,b1,c1 = map(int,input().split())
    a.append(a1)
    b.append(b1)
    c.append(c1)


dpa[0] = a[0]
dpb[0] = b[0]
dpc[0] = c[0]


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

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

Solution (Python):
def main():
    N=int(input())
    abc=[0]*N
    for i in range(N):
        xyz=list(map(int,input().split()))
        abc[i]=xyz
    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-2)%3]+abc[i-1][(j-1)%3])

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

if __name__=="__main__":
    main()

Solution (Python):
n = int(input())
arr = []
for i in range(n):
	arr.append(list(map(int,input().split())))
ans = [arr[0][::]]
for i in range(1,n):
	temp = ans[-1]
	x = max(temp[1]+arr[i][0],temp[2]+arr[i][0])
	y = max(temp[0]+arr[i][1],temp[2]+arr[i][1])
	z = max(temp[0]+arr[i][2],temp[1]+arr[i][2])
	ans.append([x,y,z])
print(max(ans[-1]))

Solution (Python):





import sys

ipti = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)

def main():
    n = int(input())
    h = [[0] * 3 for _ in range(n)]

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

    dp = [[0] * 3 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):
        for j in range(3):
            dp[i][j] = max(dp[i-1][(j+1)%3] + h[i][j], dp[i-1][(j+2)%3] + h[i][j])

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

if __name__ == '__main__':
    main()

Solution (Python):
import sys
import itertools
from operator import itemgetter 
from fractions import gcd 
from math import ceil, floor, sqrt, isinf 
from copy import deepcopy 
from collections import Counter, deque 
import heapq
import numpy as np
from functools import reduce
sys.setrecursionlimit(200000)

input = sys.stdin.readline


def retmax(a, b):
    if a > b:
        return a
    else:
        return b

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

    dp = np.zeros([3, n+1])

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

    print(int(np.max(dp[:, n], axis=0)))


if __name__ == "__main__":
    main()

Solution (Python):
import sys
import itertools
from operator import itemgetter 
from fractions import gcd 
from math import ceil, floor, sqrt, isinf 
from copy import deepcopy 
from collections import Counter, deque 
import heapq
import numpy as np
from functools import reduce
sys.setrecursionlimit(200000)

input = sys.stdin.readline


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

    a, b, c = 0, 0, 0
    for x, y, z in abc:
        a, b, c = max(b, c) + x, max(a, c) + y, max(a, b) + z

    print(max(a, b, c))



if __name__ == "__main__":
    main()

Solution (Python):
import sys
import itertools
from operator import itemgetter 
from fractions import gcd 
from math import ceil, floor, sqrt, isinf 
from copy import deepcopy 
from collections import Counter, deque 
import heapq
import numpy as np
from functools import reduce
sys.setrecursionlimit(200000)

input = sys.stdin.readline


def retmax(a, b):
    if a > b:
        return a
    else:
        return b

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

    dp = np.zeros([3, n+1])

    for i, a_l in enumerate(a):
        for j in range(3):
            for k in range(3):
                if j != k:
                    dp[k, i+1] = retmax(dp[k, i+1], dp[j, i] + a_l[k])

    print(int(np.max(dp[:, n], axis=0)))


if __name__ == "__main__":
    main()

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

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

dp[0] = abc[0]

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

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

Solution (Python):
import sys
import numpy as np

input = sys.stdin.readline


n = int(input())


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


dp = np.zeros([3, n+1])

def retmax(a, b):
    if a > b:
        return a
    return b

for i, a_l in enumerate(act):
    
    for j in range(3):
        
        for k in range(3):
            
            if j != k:
                
                dp[k, i+1] = retmax(dp[k, i+1], dp[j, i]+a_l[k])
print(int(np.max(dp[:, n], axis=0)))

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

for i in range(1,N+1):
    for j in range(3):
        for k in range(3):
            if(j!=k):
                dp[i][j]=max(dp[i][j],dp[i-1][k]+act[i-1][j])
                
print(max([dp[N][0],dp[N][1],dp[N][2]]))

Solution (Python):
days = int(input())
activities = []

for _ in range(days):
	activities.append([int(x) for x in input().split()])

a, b, c = 0, 0, 0
for x, y, z in activities:
	a, b, c = max(b, c) + x, max(a, c) + y, max(a, b) + z
print(max(a, b, c))

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

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

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

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

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

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

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

print(max(dp[-1]))

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

    
    

    return N, ABC


def solve(N, ABC):
    x, y = N + 1, 3
    DP = [[0 for _ in range(y)] for _ in range(x)]

    for i in range(N):
        for a in range(3):
            if a == 0:
                DP[i + 1][1] = max(DP[i + 1][1], DP[i][a] + ABC[i][1])
                DP[i + 1][2] = max(DP[i + 1][2], DP[i][a] + ABC[i][2])
            elif a == 1:
                DP[i + 1][0] = max(DP[i + 1][0], DP[i][a] + ABC[i][0])
                DP[i + 1][2] = max(DP[i + 1][2], DP[i][a] + ABC[i][2])
            elif a == 2:
                DP[i + 1][0] = max(DP[i + 1][0], DP[i][a] + ABC[i][0])
                DP[i + 1][1] = max(DP[i + 1][1], DP[i][a] + ABC[i][1])

    
    return max(DP[-1])


if __name__ == '__main__':
    res = solve(*args())
    print(res)

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

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

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

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

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

Solution (Python):

n=int(input())
a=[0 for i in range(n)]
b=[0 for i in range(n)]
c=[0 for i in range(n)]
for i in range(n):
    x,y,z=map(int,input().split())
    a[i]=x
    b[i]=y
    c[i]=z
max_a=[0 for i in range(n+1)]
max_b=[0 for i in range(n+1)]
max_c=[0 for i in range(n+1)]


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

Solution (Python):
N = int(input())
A = {}
B = {}
C = {}

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

dp = {}

dp[1] = {}
dp[1][0] = A[1]
dp[1][1] = B[1]
dp[1][2] = C[1]

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

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

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

inf = 10**4 * 10**5

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

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

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

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

INF = 10**10
dp = []
for i in range(N):
    dp.append([0,0,0])

dp[0] = act[0]

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

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

Solution (Python):
from sys import stdin

N = int(stdin.readline().rstrip())
vac = []
for i in range(N):
    vac.append([int(x) for x in stdin.readline().rstrip().split()])
dp = [[0]*3 for _ in range(N+1)]
for i in range(N):
    for j in range(3):
        for k in range(3):
            if j == k:
                continue
            dp[i+1][k] = max(dp[i+1][k], dp[i][j] + vac[i][k])
print(max(dp[N]))

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

dp_a[0], dp_b[0], dp_c[0] = abc[0]

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

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

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

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

    for i in range(1, N):
        prev = dp[i-1]
        preva = prev[0]
        prevb = prev[1]
        prevc = prev[2]
        abci = abc[i]
        a = abci[0]
        b = abci[1]
        c = abci[2]
        dp[i] = [max(prevb+a, prevc+a), max(prevc+b, preva+b), max(preva+c, prevb+c)]

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

if __name__ == '__main__':
    resolve()

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

Solution (Python):
def main():
    N = int(input())
    work = []
    for _ in range(N):
        a, b, c = map(int, input().split())
        work.append((a,b,c))
    
    dp = [[0] * 3 for _ in range(N)]
    for i in range(N):
        for j in range(3):
            if i == 0:
                dp[i][j] = work[i][j]
                continue
            if j == 0:
                dp[i][0] = max(dp[i-1][1], dp[i-1][2]) + work[i][0]
            elif j == 1:
                dp[i][1] = max(dp[i-1][0], dp[i-1][2]) + work[i][1]
            elif j == 2:
                dp[i][2] = max(dp[i-1][1], dp[i-1][0]) + work[i][2]

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

if __name__ == "__main__":
    main()

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

a = list()
b = list()
c = list()

for i in range(N):
    tmp_a, tmp_b, tmp_c = map(int,input().split())
    a.append(tmp_a)
    b.append(tmp_b)
    c.append(tmp_c)

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

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

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

Solution (Python):
import math
import fractions
import bisect
import collections
import itertools
import heapq
import string
import sys
import copy
from collections import deque
sys.setrecursionlimit(10**7)
MOD = 10**9+7
def gcd(a,b):return fractions.gcd(a,b) 
def lcm(a,b):return (a*b) // fractions.gcd(a,b) 
def iin(): return int(input()) 
def ifn(): return float(input()) 
def isn(): return input().split() 
def imn(): return map(int, input().split()) 
def fmn(): return map(float, input().split()) 
def iln(): return list(map(int, input().split())) 
def iln_s(): return sorted(iln()) 
def iln_r(): return sorted(iln(), reverse=True) 
def fln(): return list(map(float, input().split())) 
def join(l, s=''): return s.join(l) 
def perm(l, n): return itertools.permutations(l, n) 
def perm_count(n, r): return math.factorial(n) // math.factorial(n-r) 
def comb(l, n): return itertools.combinations(l, n) 
def comb_count(n, r): return math.factorial(n) // (math.factorial(n-r) * math.factorial(r)) 
def two_distance(a, b, c, d): return ((c-a)**2 + (d-b)**2)**.5 
def m_add(a,b): return (a+b) % MOD

N = iin()
abc = [iln() for _ in range(N)]

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

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

print(max(dp[N]))

Solution (Python):
import sys
input = sys.stdin.readline
n = int(input())
abc = [tuple(map(int,input().split())) for i in range(n)]

dp_a = [0]*n
dp_b = [0]*n
dp_c = [0]*n

a,b,c = abc[0]
dp_a[0] = a
dp_b[0] = b
dp_c[0] = c

for i in range(1,n):
    a, b, c = abc[i]
    dp_a[i] = max(dp_b[i-1]+a, dp_c[i-1]+a)
    dp_b[i] = max(dp_c[i-1]+b, dp_a[i-1]+b)
    dp_c[i] = max(dp_a[i-1]+c, dp_b[i-1]+c)

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

Solution (Python):
import sys
input = sys.stdin.readline
n = int(input())
abc = [tuple(map(int,input().split())) for i in range(n)]

dp_a = [0]*n
dp_b = [0]*n
dp_c = [0]*n

a,b,c = abc[0]
dp_a[0] = a
dp_b[0] = b
dp_c[0] = c

for i in range(1,n):
    a, b, c = abc[i]
    dp_a[i] = max(dp_b[i-1]+a, dp_c[i-1]+a)
    dp_b[i] = max(dp_c[i-1]+b, dp_a[i-1]+b)
    dp_c[i] = max(dp_a[i-1]+c, dp_b[i-1]+c)

print(max(dp_a[-1], dp_b[-1], dp_c[-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] = [int(j) for j in input().split()]

ABC = [A, B, C]
dp = [[0]*3 for _ in range(N+1)]

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

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

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

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

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

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

    dp = [0] * 3

    for i in range(N):
        a, b, c = table[i]

        a0 = dp[0]
        b0 = dp[1]
        c0 = dp[2]

        dp[0] = max(b0, c0) + a
        dp[1] = max(c0, a0) + b
        dp[2] = max(a0, b0) + c

    print(max(dp))

main()

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

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



for i in range(len(plan)-1):
    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] + plan[i+1][k])
print(max(dp[-1]))

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

Solution (Python):
N=int(input())
C=[[0]*N for i in range(3)]
S=list(map(int,input().split()))
C[0][0]=S[0]
C[1][0]=S[1]
C[2][0]=S[2]

for i in range(1,N):
    H=list(map(int,input().split()))
    C[0][i]=max(C[1][i-1],C[2][i-1])+H[0]
    C[1][i]=max(C[0][i-1],C[2][i-1])+H[1]
    C[2][i]=max(C[0][i-1],C[1][i-1])+H[2]

ans=C[0][N-1]
if(ans<C[1][N-1]):
    ans=C[1][N-1]
if(ans<C[2][N-1]):
    ans=C[2][N-1]
print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(100002)


N = int(input())
act = [list(map(int, input().split())) for _ in range(N)]
dp = [[-float('inf') for _ in range(3)] for _ in range(N)]


def dp_rec(day, choice):

    if dp[day][choice] > -float('inf'):
        return dp[day][choice]

    v = -float('inf')
    for c in range(3):
        if c != choice:
            v = max(v, dp_rec(day - 1, c) + act[day][choice])

    dp[day][choice] = v
    return v


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

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

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

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



for i in range(n):
    for j in range(3):
        
        maxValue = 0
        for k in range(3):
            if k== j:
                continue
            maxValue = max(maxValue, dp[i][k])
            
        
        dp[i+1][j] = maxValue + li[i][j]
        
print(max(dp[n][0],dp[n][1], dp[n][2]))

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

print(max(dp[n]))

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
dpB = [0]*n
dpC = [0]*n

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

table = []
for _ in range(n):
    table.append([int(v) for v in input().split()])

dp = [0] * 3

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

    a, b, c = table[i]

    a0 = dp[0]
    b0 = dp[1]
    c0 = dp[2]

    dp_n[0] = max(b0, c0) + a
    dp_n[1] = max(c0, a0) + b
    dp_n[2] = max(a0, b0) + c

    dp = dp_n

print(max(dp))

Solution (Python):
n = int(input())
s = []
for i in range(n):
      s.append(tuple(map(int,input().split())))
dp = [s[0][0],s[0][0],s[0][1],s[0][1],s[0][2],s[0][2]]
sel = (0,0,1,1,2,2)
pre = ((2,3),(4,5),(0,1),(4,5),(0,1),(2,3))
p = list(dp)
for i in range(1,n):
      for j in range(6):
            dp[j] = max(p[pre[j][0]],p[pre[j][1]]) + s[i][sel[j]]
      p = list(dp)
print(max(dp))

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

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



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


'''
dp[j][i] = dp[k][i-1] + j *jは幸福度が最大となるようなi+1日目の行動、j ￢= k
'''
for i in range(n):
    for j in range(3):
        for k in range(3):
            if k == j:continue
            dp[k][i+1] = max(dp[k][i+1],dp[j][i] + happy[i][k])

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

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

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

c = [0, 1, 2]   
for i in range(1, N):
    for j in range(3):
        c.remove(j)
        if dp[i - 1][c[0]] > dp[i - 1][c[1]]:
            dp[i][j] = dp[i - 1][c[0]] + act[i][j]
        else:
            dp[i][j] = dp[i - 1][c[1]] + act[i][j]
        c.append(j)

print(max(dp[-1]))

Solution (Python):
import itertools
import math
import sys
from collections import Counter
from fractions import gcd
from functools import reduce
import sys
sys.setrecursionlimit(4100000)

INF = 1 << 60


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

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

print(max(dp[-1]))

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

dp = [[0] * 3 for _ in range(N+1)]
for i, v in enumerate(ABC):
  for j in range(3): 
    for k in range(3): 
      if j == k:
        continue
      dp[i][k] = max(dp[i-1][j] + v[k], dp[i][k])
print(max(dp[N-1]))

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

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

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

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

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