Problem:
 Score : 100 points    Problem Statement Taro's summer vacation starts tomorrow, and he has decided to make plans for it now.  The vacation consists of N days.
For each i ( 1 \leq i \leq N ), Taro will choose one of the following activities and do it on the i -th day:   A: Swim in the sea. Gain a_i points of happiness.  B: Catch bugs in the mountains. Gain b_i points of happiness.  C: Do homework at home. Gain c_i points of happiness.   As Taro gets bored easily, he cannot do the same activities for two or more consecutive days.  Find the maximum possible total points of happiness that Taro gains.      Constraints  All values in input are integers.  1 \leq N \leq 10^5  1 \leq a_i, b_i, c_i \leq 10^4         Input Input is given from Standard Input in the following format:  N  a_1  b_1  c_1  a_2  b_2  c_2  :  a_N  b_N  c_N       Output Print the maximum possible total points of happiness that Taro gains.        Sample Input 1 3
10 40 70
20 50 80
30 60 90      Sample Output 1 210  If Taro does activities in the order C, B, C, he will gain 70 + 50 + 90 = 210 points of happiness.       Sample Input 2 1
100 10 1      Sample Output 2 100       Sample Input 3 7
6 7 8
8 8 3
2 5 2
7 8 6
4 6 8
2 3 4
7 5 1      Sample Output 3 46  Taro should do activities in the order C, A, B, A, C, B, A.

Solution (Python):
N = int(input())
a, 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[N - 1]))

Solution (Python):

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

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

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

Solution (Python):
def examA():
    N = I()
    H = LI()
    dp = [inf]*(N)
    dp[0] = 0
    for i in range(N-1):
        dp[i+1] = min(dp[i+1],dp[i]+abs(H[i+1]-H[i]))
        if i<N-2:
            dp[i + 2] = min(dp[i + 2], dp[i]+abs(H[i + 2] - H[i]))
    ans = dp[-1]
    print(ans)
    return

def examB():
    N, K = LI()
    H = LI()
    dp = [inf]*N
    dp[0] = 0
    for i in range(N):
        for k in range(1,K+1):
            if i+k>=N:
                break
            if dp[i+k]>dp[i]+abs(H[i+k]-H[i]):
                dp[i+k] = dp[i]+abs(H[i+k]-H[i])
    ans = dp[-1]
    print(ans)
    return

def examC():
    N = I()
    A = [0]*N; B = [0]*N; C = [0]*N
    for i in range(N):
        A[i],B[i],C[i] = LI()
    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][1], dp[i][0]) + C[i]
    ans = max(dp[N])
    print(ans)
    return

def examD():
    ans = 0
    print(ans)
    return

def examE():
    ans = 0
    print(ans)
    return

def examF():
    ans = 0
    print(ans)
    return

import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
alphabet = [chr(ord('a') + i) for i in range(26)]

if __name__ == '__main__':
    examC()

"""

"""

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

Solution (Python):
def main():
    n = int(input())
    hpy = []
    for _ in range(n):
        tmp = list(map(int,input().split()))
        hpy.append(tmp)
    
    dp = [[0] * 3 for i in range(n) ]
    dp[0][0] = hpy[0][0]
    dp[0][1] = hpy[0][1]
    dp[0][2] = hpy[0][2]
    for i, abc in enumerate(dp):
        if i ==n-1:
            break
        a = max(abc[1], abc[2]) + hpy[i+1][0]
        b = max(abc[2], abc[0]) + hpy[i+1][1]
        c = max(abc[0], abc[1]) + hpy[i+1][2]
        dp[i+1][0] = a
        dp[i+1][1] = b
        dp[i+1][2] = c
    print(max(dp[n-1]))


if __name__=='__main__':
    main()

Solution (Python):
def main():
    n = int(input())
    hpy = []
    for _ in range(n):
        tmp = list(map(int,input().split()))
        hpy.append(tmp)
    
    dp = [[0] * 3 for i in range(n) ]
    dp[0][0] = hpy[0][0]
    dp[0][1] = hpy[0][1]
    dp[0][2] = hpy[0][2]
    for i, abc in enumerate(dp):
        if i ==n-1:
            break
        a = max(abc[1], abc[2]) + hpy[i+1][0]
        b = max(abc[2], abc[0]) + hpy[i+1][1]
        c = max(abc[0], abc[1]) + hpy[i+1][2]
        dp[i+1][0] = a
        dp[i+1][1] = b
        dp[i+1][2] = c
    print(max(dp[n-1]))


if __name__=='__main__':
    main()

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

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

print(max(dp[-1]))

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

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

print(max(dp[-1]))

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

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:
                happy =  dp[i-1][k] + S[i-1][j]
                if dp[i][j] < happy:
                    dp[i][j] = happy

print(int(max(dp[N])))

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

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:
                happy =  dp[i-1][k] + S[i-1][j]
                if dp[i][j] < happy:
                    dp[i][j] = happy

print(int(max(dp[N])))

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

import numpy as np


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:
                happy =  dp[i-1][k] + S[i-1][j]
                if dp[i][j] < happy:
                    dp[i][j] = happy

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

dp = [[0 for i in range(N)] for j 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]+A[i],dp[2][i-1]+A[i])
    dp[1][i] = max(dp[0][i-1]+B[i],dp[2][i-1]+B[i])
    dp[2][i] = max(dp[0][i-1]+C[i],dp[1][i-1]+C[i])
    
print(max(dp[0][-1],dp[1][-1],dp[2][-1]))

Solution (Python):


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

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

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

    ans = max(dp[n])
    print(ans)
main()

Solution (Python):
N = int(input())
H = [[int(x) for x in input().split()] for _ in range(N)]
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(N-1):
    dp[i+1][0] = max(dp[i][1] + H[i+1][0], dp[i][2] + H[i+1][0])
    dp[i+1][1] = max(dp[i][0] + H[i+1][1], dp[i][2] + H[i+1][1])
    dp[i+1][2] = max(dp[i][0] + H[i+1][2], dp[i][1] + H[i+1][2])

print(max(dp[-1]))

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

Solution (Python):
from sys import stdin as I
N = int(I.readline())
H = [[None,None,None]] + [list(map(int,I.readline().split())) for i in range(N)]
DP = H[1]
for i in range(2,N+1):DP = [max(DP[1],DP[2])+H[i][0],max(DP[0],DP[2])+H[i][1],max(DP[0],DP[1])+H[i][2]]
print(max(DP))

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

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

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

Solution (Python):

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

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


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

Solution (Python):
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate
import sys
import bisect
import string
import math
import time


def I(): return int(input())


def MI(): return map(int, input().split())


def S(): return input()


def MS(): return map(str, input().split())


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


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


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


def show(*inp, end='\n'):
    if show_flg:
        print(*inp, end=end)


YN = {False: 'No', True: 'Yes'}
MOD = 10**9+7
inf = float('inf')
IINF = 10**10
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']


show_flg = True
show_flg = False


def main():
    N = I()
    abc = [[0, 0, 0]] * N
    dp = [0] * 3
    dp[0] = 0

    for i in range(N):
        abc[i] = LI()

    for i in range(N):
        dp_next = [0] * 3
        for k in range(3):
            dp_next[k] = max(dp_next[k], dp[k])
            for j in range(3):
                if j != k:
                    dp_next[j] = max(dp[k] + abc[i][j], dp_next[j])
        dp = dp_next
        

    print(max(dp))


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

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

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

for i in range(n):
    p,q,r = map(int,input().split())
    a.append([p,q,r])

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

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

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

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

for i in range(0,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())
a=[[int(i) for i in input().split()]for i in range(n)]
dpa=[0]*(n+1)
dpb=[0]*(n+1)
dpc=[0]*(n+1)

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

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

Solution (Python):
def chmax(a, b):
    if a < b:
        return b
    return a

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

dp = [[-float('inf') for i in range(3)] for j in range(N)]

dp[0] = H[0]

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

print(max(dp[-1]))

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

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

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

    print(max(DP[-1]))

if __name__ == '__main__':
    main()

Solution (Python):
def main():
    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):
        for j in range(3):
            tmp = [abc[i][j] + DP[i-1][k] for k in range(3) if k != j]
            DP[i][j] = max(tmp)

    print(max(DP[-1]))

main()

Solution (Python):
n = int(input())
satis = []
for i in range(n):
    satis.append([int(j) for j in input().split()])
"""
dp[i][j] = 幸福度
i : 何日目終了時点(0~n)
j : 直前に選んだもの(0 : A,1 : B,2 : C)
"""

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

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


for when in range(1,n+1):
    
    for yesterday in range(3):
        if when != 1:
            for today in range(3):
                if today != yesterday:
                    dp[when][today] = max(dp[when][today], dp[when-1][yesterday] + satis[when-1][today])
        else:
            dp[when][yesterday] = satis[when-1][yesterday]

print(max(dp[n]))

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

y = [[0 for j in i] for i in x]


for i in range(len(x)):
    if i - 1 < 0:
        y[i] = [k for k in x[i]]    
        continue

    for j in range(len(x[i])):
        x_1 = max([y[i - 1][k] for k in range(len(x[i - 1])) if k != j])
        y[i][j] = x[i][j] + x_1

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

Solution (Python):

import numpy as np
n = int(input())
abc = [input() for _ in range(n)]
dp = [[0 for _ in range(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][k] for k in range(3)if k != j ]) +int(abc[i-1].split()[j])

print(max( [dp[n][i] for i in range(3)] ) )

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

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


a, b, c = map(int, input().strip().split())
dp[0][0]=a
dp[0][1]=b
dp[0][2]=c

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

Solution (Python):
from functools import lru_cache
import sys 
sys.setrecursionlimit(2147483647)

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


def dfs(d,pre):
    if (d,pre) in dic: return dic[(d,pre)]
    if d < 0:
        dic[(d,pre)] = 0 
        return 0
    ma = 0 
    for idx,a in enumerate(A[d]):
        if idx != pre:
            res = dfs(d - 1,idx) + a 
            ma = max(res,ma)
    dic[(d,pre)] = ma
    return dic[(d,pre)]
dic = {}
res = dfs(N-1,-1)
print(res)

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

activity =[]

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

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

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

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

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

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

dp = [[0 for _ in range(3)] for _ in range(N)]
for i in range(N):
    for j in range(3):
        if i == 0:
            dp[i][j]= A[i][j]
        else:
            tmp = [0] 
            for k in range(3):
                if j !=k:
                    tmp.append(dp[i-1][k])
            dp[i][j] = max(tmp) + A[i][j]
print(max(dp[-1]))

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

activities = []

for i in range(N):
  activities.append([int(x) for x in input().split()])
  
memo = [[0] * N for _ in range(3)]

for day in range(N):
  for no in range(3):
    
    if day == 0:
      memo[no][0] = activities[0][no]
      continue
    
    memo[no][day] = max(
      memo[(no + 1) % 3][day - 1], 
      memo[no - 1][day - 1]
    ) + activities[day][no]
    
print(max(memo[x][N -1] for x in range(3)))

Solution (Python):
N = int(input())
dp = [[0 for i in range(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] + 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)
ans = max(dp[N][0],dp[N][1],dp[N][2])
print(ans)

Solution (Python):
N = int(input())
dp = [[0 for i in range(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] + 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)
ans = max(dp[N][0],dp[N][1],dp[N][2])
print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(10**6)
from math import floor,ceil,sqrt,factorial,log
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from operator import itemgetter
from fractions import gcd
mod = 10 ** 9 + 7

def ii(): return int(sys.stdin.readline().rstrip()) 
def mii(): return map(int,sys.stdin.readline().rstrip().split())
def limii(): return list(mii())
def lin(n:int): return [ii() for _ in range(n)]
def llint(n: int): return [limii() for _ in range(n)]

def ss(): return sys.stdin.readline().rstrip() 
def mss(): return sys.stdin.readline().rstrip().split()
def limss(): return list(mss())
def lst(n:int): return [ss() for _ in range(n)]
def llstr(n: int): return [limss() for _ in range(n)]

n = ii()
arr = llint(n)
dp = [[0]*3 for _ in range(n)]
dp[0][0],dp[0][1],dp[0][2]=arr[0][0],arr[0][1],arr[0][2]
for i in range(1,n):
    for abc in [0,1,2]:
        if abc==0:
            dp[i][0] = max(arr[i][0]+dp[i-1][1],arr[i][0]+dp[i-1][2])
        elif abc==1:
            dp[i][1] = max(arr[i][1]+dp[i-1][0],arr[i][1]+dp[i-1][2])
        else:
            dp[i][2] = max(arr[i][2]+dp[i-1][1],arr[i][2]+dp[i-1][0])

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

Solution (Python):
N=int(input())
abc=[[] for _ in range(N)]

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

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

print(max(dp[-1]))

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

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

    dp = newDP[::]

ans = max(dp)
print(ans)

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  n=I()
  l=[LI() for _ in range(n)]

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

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


print(main())

Solution (Python):

import numpy as np
def fnSolv():
  iNum = int(input())
  naABC = np.zeros((iNum, 3), dtype = int)
  naDP  = [0]*3
  
  for iI in range(iNum):
    naABC[iI] = list(map(int, input().split()))
  for iA, iB, iC in naABC:
    naDPa = max([naDP[1] + iA, naDP[2] + iA])
    naDPb = max([naDP[2] + iB, naDP[0] + iB])
    naDPc = max([naDP[0] + iC, naDP[1] + iC])
    naDP = [naDPa, naDPb, naDPc]
    
  print(max(naDP))

if __name__ == '__main__':
  fnSolv()

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

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

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

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] = [a[0], b[0], c[0]]

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

Solution (Python):
def main():
  n = int(input())
  dp = [0, 0, 0]
  
  for activities in [list(map(int, input().split())) for _ in range(n)]:
    new_dp = [0, 0, 0]
    
    for i in range(3):
      offset = dp[i]
      for j in range(3):
        if i == j: continue
          
        new_dp[j] = max(new_dp[j], offset + activities[j])
    
    dp = new_dp
  
  print(max(dp))

if __name__ == "__main__":
  main()

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

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

Solution (Python):
import itertools

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

n = int(input())
ls = [[int(_) for _ in input().split()] for _ in range(n)]

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

for i in range(1, n):
	for j, k in itertools.product(range(3), range(3)):
		if j == k:
			continue
		else:
			dp[i][k] = chmax(dp[i][k], dp[i - 1][j] + ls[i][k])

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

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

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

    if not dp:
        dp.append([a, b, c])
    else:
        dp.append([a + max(dp[-1][1], dp[-1][2]),
                   b + max(dp[-1][0], dp[-1][2]),
                   c + max(dp[-1][0], dp[-1][1])])

print(max(dp[-1]))

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

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)
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 
MOD = 10**9+7

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

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

Solution (Python):

"""
Created on Tue Jan 21 18:19:39 2020

@author: matsui
"""

N=int(input())
dp=dict()
dp['a']=[0]*N
dp['b']=[0]*N
dp['c']=[0]*N

a,b,c=input().split()
dp['a'][0]=int(a)
dp['b'][0]=int(b)
dp['c'][0]=int(c)

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

Solution (Python):
import sys
from collections import defaultdict

sys.setrecursionlimit(10**7)

def max_happiness(n, data, dp, a, b, c):
  string = str(n) + str(a) + str(b) + str(c)

  if dp[string] is not None:
    return dp[string]

  if n == 0:
    if a and b:
      dp[string] = max(data[n][0], data[n][1])
      return dp[string]
    elif b and c:
      dp[string] = max(data[n][1], data[n][2])
      return dp[string]
    elif c and a:
      dp[string] = max(data[n][2], data[n][0])
      return dp[string]

  max_a = -1
  max_b = -1
  max_c = -1

  if a:
    max_a = data[n][0] + max_happiness(n-1, data, dp, a=0, b=1, c=1)
  if b:
    max_b = data[n][1] + max_happiness(n-1, data, dp, a=1, b=0, c=1)
  if c:
    max_c = data[n][2] + max_happiness(n-1, data, dp, a=1, b=1, c=0)

  dp[string] = max(max_a, max_b, max_c)
  return dp[string]

N = int(input())
data = []
dp = defaultdict(lambda: None)

for _ in range(N):
  data.append([int(num) for num in input().split()])

print(max_happiness(N-1, data, dp, a=1, b=1, c=1))

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

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

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

Solution (Python):
import sys
input = sys.stdin.readline
N = int(input())
table = []
dp = [[0]*3 for i in range(N)]

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

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

Solution (Python):
def solve():
    N, *abc = map(int, open(0).read().split())
    a = abc[::3]
    b = abc[1::3]
    c = abc[2::3]
    
    dp = [[10 ** 10] * 3 for _ in range(N)]
    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]))


if __name__ == '__main__':
    solve()

Solution (Python):

N = int(input().split()[0])
abc_list = []

for _ in range(N):
    a, b, c = list(map(int, input().split()))
    abc_list.append(dict(a=a, b=b, c=c))

dp_list = [dict(a=0, b=0, c=0) for _ in range(N + 1)]

for i in range(1, N + 1):
    
    scores = abc_list[i - 1]
    
    dp_list[i]["a"] = scores["a"] + max(dp_list[i - 1]["b"], dp_list[i - 1]["c"])
    
    dp_list[i]["b"] = scores["b"] + max(dp_list[i - 1]["a"], dp_list[i - 1]["c"])
    
    dp_list[i]["c"] = scores["c"] + max(dp_list[i - 1]["a"], dp_list[i - 1]["b"])

ans = max(dp_list[-1].values())

print(ans)