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

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

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

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

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

Solution (Python):
N = int(input())
lists = []

for i in range(N):
    lists.append(list(map(int,input().split())))

dp = [[0 for i in range(3)] for j in range(N)]
dp[0] = lists[0]
for i in range(1,N):
    for j in [0,1,2]:

        if j == 0:
            dp[i][j] = lists[i][0] + max(dp[i - 1][1], dp[i - 1][2])
        elif j == 1:
            dp[i][j] = lists[i][1] + max(dp[i - 1][0], dp[i - 1][2])
        else:
            dp[i][j] = lists[i][2] + max(dp[i - 1][0], dp[i - 1][1])

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

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

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

for i in range(n):
    for j in range(3):
        DP[i+1][j] = max(DP[i][k] + A[i][j] for k in range(3) if j != k)

print(max(DP[n]))

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

Solution (Python):
n = int(input())
import numpy as np
abc = np.array([[int(x) for x in input().split()] for y in range(n)],dtype=int)
dp = np.zeros((n,3),dtype=int)
dp[0] = abc[0]
for p in range(1,n):
    dp[p][0] = max(dp[p-1][1],dp[p-1][2]) + abc[p][0]
    dp[p][1] = max(dp[p-1][0],dp[p-1][2]) + abc[p][1]
    dp[p][2] = max(dp[p-1][0],dp[p-1][1]) + abc[p][2]
print(max(dp[n-1]))

Solution (Python):







INF = 1000000000+5

def get_res(N, A):
    DPA = [0]*N
    DPB = [0]*N
    DPC = [0]*N
    a0, b0, c0 = A[0]
    DPA[0] = a0
    DPB[0] = b0
    DPC[0] = c0
    for i in range(1, N):
        
        ai, bi, ci = A[i]
        DPA[i] = ai + max(DPB[i-1], DPC[i-1])
        DPB[i] = bi + max(DPA[i-1], DPC[i-1])
        DPC[i] = ci + max(DPA[i-1], DPB[i-1])
    return max(DPA[N-1], DPB[N-1], DPC[N-1])

N = int(input())
A = []
for _ in range(N):
    L = [int(s) for s in input().split(" ")]
    A.append(L)
result = get_res(N, A)
print("{}".format(result))

Solution (Python):

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

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

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

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

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

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

Solution (Python):

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

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

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

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

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

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

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

Solution (Python):
N=int(input())
inputs=[]
dp=[]
for _ in range(N):
    inputs.append([int(x) for x in input().split()])
    dp.append([0]*3)

for j in range(3):
    dp[0][j]=inputs[0][j]

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



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

Solution (Python):
from sys import stdin
n = int(stdin.readline().rstrip())
abc = []
for i in range(n):
    abc.append([int(x) for x in stdin.readline().rstrip().split()])
dp = [[0] * (n + 1) for i in range(3)]
dp[0][0] = abc[0][0]
dp[1][0] = abc[0][1]
dp[2][0] = abc[0][2]
for i in range(n - 1):
    for j in range(3):
        for k in range(3):
            if j != k:
                renew = dp[k][i] + abc[i + 1][j]
                if dp[j][i + 1] < renew:
                    dp[j][i + 1] = renew
ans = 0
for i in range(3):
    if ans < dp[i][n-1]:
        ans = dp[i][n-1]
print(ans)

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

print(max(dp[n]))

Solution (Python):
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict, deque, OrderedDict
from copy import deepcopy
from functools import lru_cache, reduce
from math import ceil, floor

import heapq
import itertools
import operator


inf = float('inf')


def get_int():
    return int(input())


def get_str():
    return input().strip()


def get_list_of_int():
    return [int(i) for i in input().split()]


def get_char_list():
    return list(input().strip())



N = 0
A, B, C = [0], [0], [0]


def set_inputs():
    global N, A, B, C
    N = get_int()
    a, b, c = [], [], []
    for _ in range(N):
        x, y, z = get_list_of_int()
        a.append(x)
        b.append(y)
        c.append(z)
    A, B, C = a, b, c


def main():
    set_inputs()
    happiness = [(A[0], B[0], C[0])]
    for i in range(1, N):
        h = happiness[i-1]
        a = max(h[1], h[2]) + A[i]
        b = max(h[0], h[2]) + B[i]
        c = max(h[0], h[1]) + C[i]
        happiness.append((a, b, c))
    print(max(happiness[N-1]))


if __name__ == '__main__':
    main()

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

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

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


print(max(dp[N]))

Solution (Python):

"""
Created on Sat Feb 16 09:48:04 2019

@author: maezawa
"""

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

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

Solution (Python):
N= int(input())
X = [list(map(int, input().split())) for _ in range(N)]
L = 3
dp = [[0 for _ in range(3)] for _ in range(N+1)]

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

print(max(dp[-1]))

Solution (Python):


n = int(input())

x = 0
y = 0
z = 0
for i in range(n):
    a,b,c = map(int,input().split())
    nx = max(y,z) + a
    ny = max(x,z) + b
    nz = max(x,y) + c
    x = nx
    y = ny
    z = nz

print(max(x,y,z))

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

x = 0
y = 0
z = 0
for i in range(n):
    a,b,c = map(int,input().split())
    nx = max(y,z) + a
    ny = max(x,z) + b
    nz = max(x,y) + c
    x = nx
    y = ny
    z = nz

print(max(x,y,z))

Solution (Python):
N = int(input())
happy = [[int(n) for n in input().split()] 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 not j == k:
                dp[i + 1][k] = max(dp[i + 1][k], dp[i][j] + happy[i][k])

print(max(dp[N]))

Solution (Python):
N = int(input())
happy = [[int(n) for n in input().split()] 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 not j == k:
                dp[i + 1][k] = max(dp[i + 1][k], dp[i][j] + happy[i][k])

print(max(dp[N]))

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

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

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

for i in range(n-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] + hap[i+1][k])

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

print(ans)

Solution (Python):
def inpl(): return list(map(int, input().split()))
N = int(input())
sa, sb, sc = 0, 0, 0
for i in range(N):
    a, b, c = inpl()
    sa, sb, sc = max(sb+a, sc+a), max(sa+b, sc+b), max(sa+c, sb+c)
print(max(sa, sb, sc))

Solution (Python):
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**8) 
 
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

n = ni()
abc = [list(li()) for _ in range(n)]

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

for day in range(n):
    ai,bi,ci = abc[day]
    dp[day+1][0] = max(dp[day][1], dp[day][2]) + ai
    dp[day+1][1] = max(dp[day][2], dp[day][0]) + bi
    dp[day+1][2] = max(dp[day][0], dp[day][1]) + ci
    
print(max(dp[n]))

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

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

Solution (Python):



from math import *
from copy import *
from string import *					
from random import *
from sys import stdin
from itertools import *
from collections import Counter			

def maxHappiness(A,B,C,n):
	dp = [0,0,0]
	for i in range(3):
		dp[i] = [0]*n
	dp[0][0] = A[0]
	dp[1][0] = B[0]
	dp[2][0] = C[0]
	for i in range(1,n):
		dp[0][i] = A[i] + max(dp[1][i-1], dp[2][i-1])
		dp[1][i] = B[i] + max(dp[2][i-1], dp[0][i-1])
		dp[2][i] = C[i] + max(dp[0][i-1], dp[1][i-1])
	ans = max(dp[0][-1],dp[1][-1],dp[2][-1])
	return ans


A,B,C = [],[],[]
T = int(input())
for _ in range(T):
	a,b,c = list(map(int, stdin.readline().rstrip().split()))
	A.append(a)
	B.append(b)
	C.append(c)

ans = maxHappiness(A,B,C,T)
print(ans)

Solution (Python):


n = input()
a = []
b = []
c = []
for i in range(n):
    ai, bi, ci = map(int, raw_input().split())
    a.append(ai)
    b.append(bi)
    c.append(ci)

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

print(max([dpa[0], dpb[0], dpc[0]]))

Solution (Python):
def main():
    N = int(input())
    a = [0]
    b = [0]
    c = [0]
    for _ 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 + 1)]

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


main()

Solution (Python):
from sys import stdin

n = input()
inp = stdin.readlines()

score = [[0, 0, 0] for i in xrange(n)]
dp = [[-1 for i in xrange(3)] for j in xrange(n)]

i = 0
for line in inp:
    score[i] = map(int, line.strip().split())

    if i == 0:
        dp[0] = score[0]
    else:
        for j in xrange(3):
            for k in xrange(1, 3): dp[i][j] = max(dp[i][j], dp[i - 1][(j + k) % 3])
            dp[i][j] += score[i][j]
    i += 1

print max(dp[n - 1])

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

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

DP = [[0,0,0] for i in range(N)]

for i in range(3):
    DP[0][i] = ABC[0][i]

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

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

print(ans)

Solution (Python):
n = int(input())
act = [["dammy"]]
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):
    dp[i][0] = max(dp[i-1][1],dp[i-1][2]) + act[i][0]
    dp[i][1] = max(dp[i-1][0],dp[i-1][2]) + act[i][1]
    dp[i][2] = max(dp[i-1][0],dp[i-1][1]) + act[i][2]
    
print(max(dp[n]))

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

Solution (Python):
from sys import stdin

n = int(input())
a = stdin.read().splitlines()
b = [[int(i) for i in line.split()] for line in a]

d, f, g = b[0]  

for i in range(1, n):
    d, f, g = max(f, g) + b[i][0], max(d, g) + b[i][1], max(d, f) + b[i][2]

print(max(d, f, g))

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

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

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

print(ans)

Solution (Python):
N=int(input())
F=[]
for m in range(0,N):
  F.append([0,0,0])
F[0]=  list(map(int,input().split(" ")))


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

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

Solution (Python):


from collections import deque
import itertools as it
import sys
import math

sys.setrecursionlimit(1000000)

INF = 10 ** 18

N = input()
DP = [0, 0, 0]
for _ in range(N):
    a, b, c = map(int, raw_input().split())
    DP_ = [0, 0, 0]
    DP_[0] = a + max(DP[1], DP[2])
    DP_[1] = b + max(DP[0], DP[2])
    DP_[2] = c + max(DP[0], DP[1])
    DP = DP_

print max(DP)

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

abc[0][0]

ahappiness = [0 for i in range(N)]
bhappiness = [0 for i in range(N)]
chappiness = [0 for i in range(N)]

ahappiness[0] = abc[0][0]
bhappiness[0] = abc[0][1]
chappiness[0] = abc[0][2]


for i in range(1,N):
    ahappiness[i] = max(bhappiness[i-1],chappiness[i-1])+abc[i][0]
    bhappiness[i] = max(ahappiness[i-1],chappiness[i-1])+abc[i][1]
    chappiness[i] = max(ahappiness[i-1],bhappiness[i-1])+abc[i][2]
    
print(max(ahappiness[-1],bhappiness[-1],chappiness[-1]))

Solution (Python):







import sys

def solve(N, tuples):
    xa, xb, xc = 0, 0, 0
    for a, b, c in tuples:
        ya = max(xb, xc) + a
        yb = max(xc, xa) + b
        yc = max(xa, xb) + c
        xa, xb, xc = ya, yb, yc
    return max(xa, xb, xc)

def load(input):
    line = input.readline()
    N = int(line.strip())
    tuples = [ tuple( int(s) for s in line.split() )
                   for line in input ]
    return N, tuples

def main():
    N, tuples = load(sys.stdin)
    result = solve(N, tuples)
    print(result)

main()

Solution (Python):
n=int(input())
e=[[int(i) for i in input().split()] for j in range(n)]
dp=[[0,0,0] for i in range(n)]
for i in range(3):
    dp[0][i]=e[0][i]

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

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

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

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

print(max(dp[N]))

Solution (Python):
a = int(input())
x = []
for i in range(a):
    c,d,e = map(int, input().split())
    x.append([c,d,e])
dp =[[0, 0, 0] for _ in [0]*(a+1)]
dp[0][0] = x[0][0]
dp[0][1] = x[0][1]
dp[0][2] = x[0][2]
for n in range(3):
    for i,h in enumerate(x):
        for n in range(3):
            if i !=0:
                dp[i][n] = max(dp[i-1][(n+1)%3],dp[i-1][(n+2)%3])+h[n]
        

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

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

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

ABC = []

for i in range(N):
	abc = list(map(int,input().split()))
	ABC.append(abc)

dp = []

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

dp[0] = ABC[0]

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

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

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

Solution (Python):


import sys, re
from collections import deque, defaultdict, Counter
from math import sqrt, hypot, factorial, pi, sin, cos, radians
if sys.version_info.minor >= 5: from math import gcd
else: from fractions import gcd 
from heapq import heappop, heappush, heapify, heappushpop
from bisect import bisect_left, bisect_right
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from functools import reduce, partial
from fractions import Fraction
from string import ascii_lowercase, ascii_uppercase, digits

def input(): return sys.stdin.readline().strip()
def ceil(a, b=1): return int(-(-a // b))
def round(x): return int((x*2+1) // 2)
def fermat(x, y, MOD): return x * pow(y, MOD-2, MOD) % MOD
def lcm(x, y): return (x * y) // gcd(x, y)
def lcm_list(nums): return reduce(lcm, nums, 1)
def gcd_list(nums): return reduce(gcd, nums, nums[0])
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()


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

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

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

Solution (Python):


import sys, re
from collections import deque, defaultdict, Counter
from math import sqrt, hypot, factorial, pi, sin, cos, radians
if sys.version_info.minor >= 5: from math import gcd
else: from fractions import gcd 
from heapq import heappop, heappush, heapify, heappushpop
from bisect import bisect_left, bisect_right
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from functools import reduce, partial
from fractions import Fraction
from string import ascii_lowercase, ascii_uppercase, digits

def input(): return sys.stdin.readline().strip()
def ceil(a, b=1): return int(-(-a // b))
def round(x): return int((x*2+1) // 2)
def fermat(x, y, MOD): return x * pow(y, MOD-2, MOD) % MOD
def lcm(x, y): return (x * y) // gcd(x, y)
def lcm_list(nums): return reduce(lcm, nums, 1)
def gcd_list(nums): return reduce(gcd, nums, nums[0])
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()


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

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

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

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

for i in range(n):
    ai,bi,ci=list(map(int,input().split()))
    a.append(ai)
    b.append(bi)
    c.append(ci)

last_a=[0 for i in range(n)]
last_b=[0 for i in range(n)]
last_c=[0 for i in range(n)]

last_a[0]=a[0]
last_b[0]=b[0]
last_c[0]=c[0]

for i in range(1,n):
    last_a[i]=max(last_b[i-1]+a[i],last_c[i-1]+a[i])
    last_b[i]=max(last_a[i-1]+b[i],last_c[i-1]+b[i])
    last_c[i]=max(last_a[i-1]+c[i],last_b[i-1]+c[i])

print(max([last_a[n-1],last_b[n-1],last_c[n-1]]))

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

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

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

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

n=n-1

print(max(a[n], b[n], c[n]))

Solution (Python):
n = int(input())
arr = [list(map(int, input().split(' '))) for _ in range(n)]
INF = 0 
dp = [[INF, INF, INF] for _ in range(n)]

dp[0] = [i for i in arr[0]]

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

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

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

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

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

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

n=n-1

print(max(a[n], b[n], c[n]))

Solution (Python):
import math
import sys
from operator import itemgetter








N = int(input())
a = [0]*100001
b = [0]*100001
c = [0]*100001

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



hpns = [[0 for j in range(100001)] for i in range(3)]

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

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

a = [0]*n
b = [0]*n
c = [0]*n



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


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

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

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

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

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, 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][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[i][0], dp[i][1], dp[i][2]))

Solution (Python):
import sys
INF = 99999999999999
N = int(input())
a = [0]*N
b = [0]*N
c = [0]*N
memo = [[0 for _ in range(3)]for _ in range(N)]
for i in range(N):
    a[i],b[i],c[i] = [int(n) for n in input().split()]

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

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

Solution (Python):
from sys import stdin, stdout

INF = float('inf')
n = int(stdin.readline())
dp = [[-INF for j in range(3)] for i in range(n)]
price = []

for i in range(n):
    price.append(list(map(int, stdin.readline().split())))

dp[0] = price[0]

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

stdout.write(str(max(dp[n - 1])))

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

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

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

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

Solution (Python):
n=int(input())
k=3
l=[]
for _ in range(n):
    l.append(list(map(int,input().split())))
dp=[l[0]]
for i,s in enumerate(l):
    if not i:continue
    t=[t for t in dp[-1]]
    d=[]
    for i in range(k):
        d.append(max(t[j]for j in range(k)if i!=j)+s[i])
    dp.append(d)
print(max(dp[-1]))

Solution (Python):
n=int(input())
l=[list(map(int,input().split()))for _ in[0]*n]
dp=l[0]
for i,s in enumerate(l):
    if not i:continue
    d=[]
    for i,t in enumerate(s):
        d.append(max(t for j,t in enumerate(dp)if i!=j)+t)
    dp=d
print(max(dp))

Solution (Python):
n=int(input())
l=[list(map(int,input().split()))for _ in[0]*n]
dp=l[0]
for i,s in enumerate(l):
    if not i:continue
    d=[]
    for i,t in enumerate(s):
        d.append(max(t for j,t in enumerate(dp)if i!=j)+t)
    dp=d
print(max(dp))

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

infty = 10**10

dp = [[infty, infty, infty] for i in range(N)]

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

def solve():
    for i in range(1,N):
        for j in range(3):
            tmp = [0,1,2]
            tmp.remove(j)
            dp[i][j] = max(dp[i-1][tmp[0]],dp[i-1][tmp[1]]) + abc[i][j]

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

Solution (Python):
def main():
  N = int(input())
  Data = []
  for i in range(N):
    Data.append(list(map(int, input().split())))
  Data = tuple(Data)
  
  DP = [[0, 0, 0] for i in range(N)]
  DP[0] = Data[0]
  for i in range(1, N):
    DP[i][0] = Data[i][0] + max(DP[i - 1][1], DP[i - 1][2])
    DP[i][1] = Data[i][1] + max(DP[i - 1][2], DP[i - 1][0])
    DP[i][2] = Data[i][2] + max(DP[i - 1][0], DP[i - 1][1])
    
  print(max(DP[N-1]))
  
main()

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


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

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


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

Solution (Python):

def a():return input()
def ai():return int(input())
def ma():return map(int, input().split())
def ms():return map(str, input().split())
def lma():return list(map(int, input().split()))
def lms():return list(map(str, input().split()))
a = int(input())
l =[]
for i in range(a):
    c,d,e = ma()
    l.append([c,d,e])
dp =[[0, 0, 0] for _ in [0]*(a+1)]
for i in range(a):
    for j in range(3):
        for k in range(3):
            if j == k:continue
            if i==0:dp[i][j] = l[i][j]
            else:dp[i][j] = max(dp[i][j],dp[i-1][k]+l[i][j])
print(max(dp[a-1]))

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

Solution (Python):
import numpy as np

n = int(input())

happiness = [0]*3

for i in range(n):
    act = list(map(int, input().split()))
    tmp1 = [happiness[(i+1)%3]+act[i] for i in range(3)]
    tmp2 = [happiness[(i+2)%3]+act[i] for i in range(3)]
    happiness = [max(tmp1[i], tmp2[i]) for i in range(3)]

print(max(happiness))