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 = 3
ARR = [
    [10, 40, 70],
    [20, 50, 80],
    [30, 60, 90]
]

N = 1
ARR = [
    [
        100, 10, 1
    ]
]


N = 7
ARR = [
    [6, 7, 8],
    [8, 8, 3],
    [2, 5, 2],
    [7, 8, 6],
    [4, 6, 8],
    [2, 3, 4],
    [7, 5, 1]
]

N = int(input())
ARR = []

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


def calculate(n, arr):
    result = []



    for index, value in enumerate(arr):
        if index == 0:
            result.append(value)
        else:
            s0 = max(value[0] + result[index - 1][1], value[0] + result[index - 1][2])
            s1 = max(value[1] + result[index - 1][0], value[1] + result[index - 1][2])
            s2 = max(value[2] + result[index - 1][0], value[2] + result[index - 1][1])
            result.append([s0, s1, s2])


    print(max(result[-1]))


calculate(N, ARR)

Solution (Python):
n = int(input())
c = [[],[],[]]
for x in range(n):
    a_,b_,c_ = [int(x) for x in input().split(" ")]
    c[0].append(a_)
    c[1].append(b_)
    c[2].append(c_)

dp = [0 for x in range(3)]
for i in range(n):
    newdp = [0 for x in range(3)]
    for x in range(3):
        for y in range(3):
            if x != y:
                newdp[y] = max(newdp[y], dp[x] + c[y][i])
    
    dp[0] = newdp[0]
    dp[1] = newdp[1]
    dp[2] = newdp[2]

print(max(dp))

Solution (Python):
import numpy as np


N = int(input())
a = np.array([[int(i) for i in input().split()] for j in range(N)]).T


dp = np.zeros(3*(N+1), dtype=int).reshape(3, (N+1))


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

ans = 0
for j in [0, 1, 2]:
    ans = max(ans, dp[j, N])
print(ans)

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


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


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

    dp_a = [0] * (N + 1)
    dp_b = [0] * (N + 1)
    dp_c = [0] * (N + 1)
    for i in range(1, N + 1):
        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]))


if __name__ == "__main__":
    main()

Solution (Python):
N = int(input())
A = []
for i in range(N):
    a,b,c = [int(_n) for _n in input().split()]
    A.append((a,b,c))
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], dp[i-1][2])+A[i][0]
    dp[i][1] = max(dp[i-1][0], dp[i-1][2])+A[i][1]
    dp[i][2] = max(dp[i-1][0], dp[i-1][1])+A[i][2]
print(max(dp[N-1][0], dp[N-1][1], dp[N-1][2]))

Solution (Python):




















N = int(input())
ARR = []

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



def calculate(n, arr):
    result = []
    for index, ar in enumerate(arr):
        if index == 0:
            result.append(ar)
        else:
            s1 = max(result[-1][1] + ar[0], result[-1][2] + ar[0])
            s2 = max(result[-1][0] + ar[1], result[-1][2] + ar[1])
            s3 = max(result[-1][0] + ar[2], result[-1][1] + ar[2])
            result.append([s1, s2, s3])

    print(max(result[-1]))


calculate(N, ARR)

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

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

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

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

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

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

Solution (Python):
import sys


def I():
	return int(sys.stdin.readline().rstrip())

def LI():
	return list(map(int, sys.stdin.readline().rstrip().split()))

def main():
	N = I()
	activity = []
	for i in range(N):
		activity.append(LI())
	
	
	dp = [[0, 0, 0] for _ in range(N)]
	dp[0][0] = activity[0][0]
	dp[0][1] = activity[0][1]
	dp[0][2] = activity[0][2]
	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[-1]))

if __name__ == "__main__":
	main()

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

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

for i in range(3):
    dp[1][i] = katsudou[1][i]

for i in range(2, N + 1):
    for j in range(3):
        if j == 0:
            dp[i][j] = max(dp[i - 1][1] + katsudou[i][j], 
            dp[i - 1][2] + katsudou[i][j])
        elif j == 1:
            dp[i][j] = max(dp[i - 1][0] + katsudou[i][j], 
            dp[i - 1][2] + katsudou[i][j])
        else:
            dp[i][j] = max(dp[i - 1][0] + katsudou[i][j], 
            dp[i - 1][1] + katsudou[i][j])

print(max(dp[N]))

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


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

print( max( dpa, dpb, dpc ) )

Solution (Python):
n = int(input())
abcs = [None for _ in range(n)]


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

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

Solution (Python):

n = int(input())
abc = []
for _ in range(n):
  abc.append(list(map(int, input().split())))
hap = abc[0]
for day in range(1, n):
  a = abc[day][0] + max(hap[1], hap[2])
  b = abc[day][1] + max(hap[0], hap[2])
  c = abc[day][2] + max(hap[1], hap[0])
  hap = [a,b,c]
print(max(hap))

Solution (Python):
from sys import stdin, stdout
rl = lambda: stdin.readline()
rll = lambda: stdin.readline().split()
rli = lambda: map(int, stdin.readline().split())

INF, NINF = float('inf'), float('-inf')

def main():
	n = int(rl())
	A, B, C = [], [], []
	for _ in range(n):
		a, b, c = rli()
		A.append(a)
		B.append(b)
		C.append(c)
	LA, LB, LC = A[0], B[0], C[0]
	for i in range(1, n):
		nLA = A[i] + max(LB, LC)
		nLB = B[i] + max(LA, LC)
		nLC = C[i] + max(LB, LA)
		LA, LB, LC = nLA, nLB, nLC
	print(max(LA, LB, LC))
	stdout.close()

if __name__ == "__main__":
	main()

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

costs = [[0, 0, 0] for _ in range(N + 1)]  
for n in range(1, N + 1):
    a, b, c = map(int, input().split())
    if n == 1:
        costs[1][0] = a
        costs[1][1] = b
        costs[1][2] = c
        continue
    max_value = 0
    for i in range(3):
        if i == 0:
            tmp_max_b_a = costs[n - 1][1] + a
            tmp_max_c_a = costs[n - 1][2] + a
            max_value = max(tmp_max_b_a, tmp_max_c_a)
            costs[n][0] = max_value
        if i == 1:
            tmp_max_a_b = costs[n - 1][0] + b
            tmp_max_c_b = costs[n - 1][2] + b
            max_value = max(tmp_max_a_b, tmp_max_c_b)
            costs[n][1] = max_value
        if i == 2:
            tmp_max_a_c = costs[n - 1][0] + c
            tmp_max_b_c = costs[n - 1][1] + c
            max_value = max(tmp_max_a_c, tmp_max_b_c)
            costs[n][2] = max_value
print(max(costs[-1]))

Solution (Python):


def maximizeVacation(swimming, bugs, homework):
    '''
    Each of swimming, bugs, and homework represents a list of values for that activity on day i of the list.
    Return the maximum value possible such that an activity cannot be performed consecutively.
    '''
    n = len(swimming) 
    '''
    justSwam =      [0] * n
    justBugged =    [0] * n
    justDidHw =     [0] * n

    justSwam[0] = swimming[0]
    justBugged[0] = bugs[0]
    justDidHw[0] = homework[0]
    '''
    justSwam = swimming[0]
    justBugged = bugs[0]
    justDidHw = homework[0]

    for i in range(1, n):
        tmpSwam, tmpBugged, tmpHw = justSwam, justBugged, justDidHw
        justSwam = max(tmpBugged, tmpHw) + swimming[i]
        justBugged = max(tmpSwam, tmpHw) + bugs[i]
        justDidHw = max(tmpSwam, tmpBugged) + homework[i]

        '''
        justSwam[i] = max(justBugged[i-1], justDidHw[i-1]) + swimming[i]
        justBugged[i] = max(justSwam[i-1], justDidHw[i-1]) + bugs[i]
        justDidHw[i] = max(justSwam[i-1], justBugged[i-1]) + homework[i]
        '''

        '''
        justSwam[i] = max(justBugged[i-1], justDidHw[i-1]) + justSwam[i-1] 
        justBugged[i] = max(justSwam[i-1], justDidHw[i-1]) + justBugged[i-1]
        justDidHw[i] = max(justSwam[i-1], justBugged[i-1]) + justDidHw[i-1]
        '''

    return max(justSwam, justBugged, justDidHw)

def main():
    '''
    print("Vacation dynamic programming problem")
    print(maximizeVacation([10, 20, 30], [40, 50, 60], [70, 80, 90]))
    print(maximizeVacation([100], [10], [1]))
    print(maximizeVacation([6, 8, 2, 7, 4, 2, 7], [7, 8, 5, 8, 6, 3, 5], [8, 3, 2, 6, 8, 4, 1]))
    '''
    n = int(input())

    swimming = [0] * n
    bugs = [0] * n
    homework = [0] * n

    for i in range(n):
        values = input()
        values = values.split()
        swimming[i] = int(values[0])
        bugs[i] = int(values[1])
        homework[i] = int(values[2])

    print(maximizeVacation(swimming, bugs, homework))

if __name__ == '__main__':
    main()

Solution (Python):


def maximizeVacation(swimming, bugs, homework):
    '''
    Each of swimming, bugs, and homework represents a list of values for that activity on day i of the list.
    Return the maximum value possible such that an activity cannot be performed consecutively.
    '''
    n = len(swimming) 
    justSwam =      [0] * n
    justBugged =    [0] * n
    justDidHw =     [0] * n

    justSwam[0] = swimming[0]
    justBugged[0] = bugs[0]
    justDidHw[0] = homework[0]

    for i in range(1, n):
        justSwam[i] = max(justBugged[i-1], justDidHw[i-1]) + swimming[i]
        justBugged[i] = max(justSwam[i-1], justDidHw[i-1]) + bugs[i]
        justDidHw[i] = max(justSwam[i-1], justBugged[i-1]) + homework[i]

        '''
        justSwam[i] = max(justBugged[i-1], justDidHw[i-1]) + justSwam[i-1] 
        justBugged[i] = max(justSwam[i-1], justDidHw[i-1]) + justBugged[i-1]
        justDidHw[i] = max(justSwam[i-1], justBugged[i-1]) + justDidHw[i-1]
        '''

    return max(justSwam[n-1], justBugged[n-1], justDidHw[n-1])

def main():
    '''
    print("Vacation dynamic programming problem")
    print(maximizeVacation([10, 20, 30], [40, 50, 60], [70, 80, 90]))
    print(maximizeVacation([100], [10], [1]))
    print(maximizeVacation([6, 8, 2, 7, 4, 2, 7], [7, 8, 5, 8, 6, 3, 5], [8, 3, 2, 6, 8, 4, 1]))
    '''
    n = int(input())

    swimming = [0] * n
    bugs = [0] * n
    homework = [0] * n

    for i in range(n):
        values = input()
        values = values.split()
        swimming[i] = int(values[0])
        bugs[i] = int(values[1])
        homework[i] = int(values[2])

    print(maximizeVacation(swimming, bugs, homework))

if __name__ == '__main__':
    main()

Solution (Python):

import sys
import math
from bisect import bisect_left, bisect_right, insort_left, insort_right
from collections import defaultdict, deque, Counter
from copy import deepcopy
from fractions import gcd
from functools import lru_cache, reduce
from heapq import heappop, heappush
from itertools import accumulate, groupby, product, permutations, combinations, combinations_with_replacement
from math import ceil, floor, factorial, log, sqrt, sin, cos
from operator import itemgetter
from string import ascii_lowercase, ascii_uppercase, digits
sys.setrecursionlimit(10**6)
INF = float('inf')
MOD = 10 ** 9 + 7
def rs(): return sys.stdin.readline().rstrip()
def ri(): return int(rs())
def rf(): return float(rs())
def rs_(): return [_ for _ in rs().split()]
def ri_(): return [int(_) for _ in rs().split()]
def rf_(): return [float(_) for _ in rs().split()]
def divisors(n, sortresult=True):
    div = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            div.append(i)
            if i != n // i:
                div.append(n//i)
    if sortresult:
        div.sort()
    return div

N = ri()
a = [0] * N
b = [0] * N
c = [0] * N
for i in range(N):
    a[i], b[i], c[i] = ri_()
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], 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]))

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


def solve():
  N = int(input())

  A, B, C = 0, 0, 0
  for _ in range(N):
      a, b, c = map(int, input().split())
      A, B, C = max(B, C) + a, max(C, A) + b, max(A, B) + c
  return max(A, B, C)
 
if __name__ == "__main__":
	print(solve())

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

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

print(ans)

Solution (Python):
import sys

input = lambda :sys.stdin.readline().rstrip()

def main():
    n = int(input())
    A, B, C = [0 for _ in range(n)], [0 for _ in range(n)], [0 for _ in range(n)]
    for i in range(n):
        a, b, c = map(int, input().split())
        A[i], B[i], C[i] = a, b, c
    ans = solve(n, A, B, C)
    print(ans)


def solve(n, A, B, C):
    a, b, c = 0, 1, 2
    dp = [[0 for _ in range(3)] for _ in range(n)]
    dp[0][a], dp[0][b], dp[0][c] = A[0], B[0], C[0]
    for i in range(1, n):
        dp[i][a] = max(dp[i - 1][b], dp[i - 1][c]) + A[i]
        dp[i][b] = max(dp[i - 1][c], dp[i - 1][a]) + B[i]
        dp[i][c] = max(dp[i - 1][a], dp[i - 1][b]) + C[i]
    
    return max(dp[n - 1])


if __name__ == '__main__':
    main()

Solution (Python):
import itertools,sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
N = I()
abc = [LI() for _ in range(N)]
dp = [[0,0,0] for _ in range(N)]
dp[0] = abc[0]
for i,j,k in itertools.product(range(1,N),[0,1,2],[0,1,2]):
    if j==k:
        continue
    dp[i][j] = max(dp[i][j],dp[i-1][k]+abc[i][j])
print(max(dp[-1]))

Solution (Python):
import itertools,sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
N = I()
abc = [LI() for _ in range(N)]
dp = [[0,0,0] for _ in range(N)]
dp[0] = abc[0]
for i,j,k in itertools.product(range(N),[0,1,2],[0,1,2]):
    if j==k:
        continue
    if i-1>=0:
        dp[i][j] = max(dp[i][j],dp[i-1][k]+abc[i][j])
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)]
for i in range(3):
    dp[0][i] = abc[0][i]
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[-1]))

Solution (Python):
n = int(input())
abc = [list(map(int, input().split())) for _ in range(n)]
dp = [[0]*3 for _ in range(n)]
for i in range(3):
    dp[0][i] = abc[0][i]
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[-1]))

Solution (Python):

import sys
sys.setrecursionlimit(500000) 
input=sys.stdin.readline

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

inf=float('inf')
dp=[[0]*3 for j in range(N+1)]

def chmax(a,b):
    return max(a,b)

for n in range(N):
    for i in range(3):
        for m in range(3):
                if (i == m):
                    continue
                dp[n+1][m]=chmax(dp[n+1][m], dp[n][i] + A[n][m])


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

Solution (Python):

import sys
sys.setrecursionlimit(500000) 
input=sys.stdin.readline

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

inf=float('inf')
dp=[[0]*3 for j in range(N+1)]

def chmax(a,b):
    return max(a,b)

for n in range(N):
    for i in range(3):
        for m in range(3):
                if (i == m):
                    continue
                dp[n+1][m]=chmax(dp[n+1][m], dp[n][i] + A[n][m])


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

Solution (Python):

import sys
sys.setrecursionlimit(500000) 
input=sys.stdin.readline

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

inf=float('inf')
dp=[[0]*3 for j in range(N+1)]

def chmax(a,b):
    return max(a,b)

def rec(n,m):
    if n==0:
        return 0
    if dp[n][m]>0:
        return dp[n][m]

    for i in range(3):
            if (i == m):
                continue
            dp[n][m]=chmax(dp[n][m], rec(n-1,i) + A[n-1][m])
    
    return dp[n][m]

re=max(rec(N,0),rec(N,1),rec(N,2))
print(re)

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

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

Solution (Python):
N=int(input())
l=[]
dp=[[0 for i in range(3)]for j in range(N+2)]
for i in range(N):
    a,b,c=[int(x) for x in input().rstrip().split()]
    l.append([a,b,c])


for i in range(1,N+1):
    now=l[i-1]
    for j in range(3):
        if j==0:
            dp[i][0]=max(dp[i-1][1]+now[0],dp[i-1][2]+now[0])
        elif j==1:
            dp[i][1]=max(dp[i-1][2]+now[1],dp[i-1][0]+now[1])
        else:
            dp[i][2]=max(dp[i-1][0]+now[2],dp[i-1][1]+now[2])
print(max(dp[N]))

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline

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

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

    for i in range(N-1):
        a, b, c = ABC[i+1]

        dp[i+1][0] = a + max(dp[i][1], dp[i][2])
        dp[i+1][1] = b + max(dp[i][0], dp[i][2])
        dp[i+1][2] = c + max(dp[i][0], dp[i][1])

    print(max(dp[-1]))


if __name__ == "__main__":
    main()

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline

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

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

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

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


if __name__ == "__main__":
    main()

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

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

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

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

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

print(max(max_list[-1]))

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

dp = [[0]*3 for i in range(n+1)]
for i in range(1,n+1):
    for j in range(3):
        add = active[i-1][j]
        if(j==0):
            dp[i][j] = max(dp[i-1][2]+add,dp[i-1][1]+add)
        elif(j==1):
            dp[i][j] = max(dp[i-1][0]+add,dp[i-1][2]+add)
        else:
            dp[i][j] = max(dp[i-1][0]+add,dp[i-1][1]+add)

print(max(dp[-1]))

Solution (Python):
import itertools,sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
N = I()
abc = [LI() for _ in range(N)]
dp = [[0,0,0] for _ in range(N+1)]
for i,j,k in itertools.product(range(N),[0,1,2],[0,1,2]):
    if j==k:
        continue
    if i+1<=N:
        dp[i+1][j] = max(dp[i+1][j],dp[i][k]+abc[i][j])
print(max(dp[-1]))

Solution (Python):
t=int(input())
arr=[]
for x in range(t):
    a=list(map(int,input().split()))
    arr.append(a)
dp=[[0 for x in range(3)]for y in range(t)]
dp[0][0]=arr[0][0]
dp[0][1]=arr[0][1]
dp[0][2]=arr[0][2]
for x in range(1,t):
    dp[x][0]=arr[x][0]+max(dp[x-1][1],dp[x-1][2])
    dp[x][1]=arr[x][1]+max(dp[x-1][0],dp[x-1][2])
    dp[x][2]=arr[x][2]+max(dp[x-1][0],dp[x-1][1])



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

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

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

Solution (Python):
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] * 3 for _ in range(N + 5)]
for i in range(3):
    dp[0][i] = 0

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

ans = 0
for i in range(N + 1):
    for j in range(3):
        ans = max(ans, dp[i][j])
print(ans)

Solution (Python):

import sys
input = sys.stdin.readline
def chmax(dp, i, k, b):
    if dp[i][k] < b:
        dp[i][k] = b
        return True
    return False
def main():
    N = int(input())
    activities = []
    for _ in range(N):
        activities.append(list(map(int, input().split())))
    dp = [[0 for _ in range(3)] for _ in range(N)]
    dp[0] = activities[0]
    for i in range(1, N):
        for j in range(3):
            for k in range(3):
                if j != k:chmax(dp, i, k, dp[i-1][j]+activities[i][k])
    print(max(dp[-1][0], dp[-1][1], dp[-1][2]))
if __name__ == "__main__":
    main()

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

Solution (Python):
import sys
input=sys.stdin.readline
def main():
    n=int(input())
    
    dp=[[-10**9]*3 for _ in range(n)]
    
    for i in range(n):
        a,b,c=map(int,input().split())
        t=[a,b,c]
        if i==0:
            dp[0][0],dp[0][1],dp[0][2]=a,b,c 
        else:
            for j in range(3):
                dp[i][j]=max(dp[i-1][j-1]+t[j],dp[i-1][j-2]+t[j])
    

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

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

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


dp = [[0,0,0] for i in range(N+1)]

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

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

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 5 + 10)
def input(): return sys.stdin.readline().strip()

def resolve():
    
    
    
    
    INF = 10**10  
    
    N = int(input())
    L=[list(map(int,input().split())) for i in range(N+1)]
    
    W=3
    dp = [[-1]*3 for _ in range(N+1)]
    """
    初期化の値は，
        最小化問題：INF, 
        最大化問題：-INF（0以上の値しか取らないなら-1, あるいは0でもいいかも)
        数え上げ問題：0
        確率問題：0
        Yes/No判定問題:False
    """
    
    
    for j in range(W):
        dp[0][j] = 0
    
    
    for i in range(1,N+1):
        for j in range(W):
    
            """
            dp[from] の値の更新が完了していればノード from からノード to への緩和を行っていい．貰うdpであれ配るdpであれ．
            両者の違いを意識すべきケースはcf. 
            https://qiita.com/drken/items/ace3142967c4f01d42e9
    
            
            """
            for k in range(W):
                if j!=k:
                    dp[i][j]=max(dp[i][j],dp[i-1][k]+L[i-1][k])
    
    
    print(max(dp[N]))
    
    
    
resolve()

Solution (Python):
n = int(input())
dp = [[0 for _ in range(3)]for _ in range(n)]
cost = []
for i in range(n):
    cost.append(list(map(int,input().split())))
ans = 0
dp[0][0] = cost[0][0]
dp[0][1] = cost[0][1]
dp[0][2] = cost[0][2]
if n == 1:
    print(max(dp[0]))
    exit()
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]+cost[i+1][k])
if dp[-1][0] < dp[-1][1]:
    if dp[-1][1] < dp[-1][2]:
        print(dp[-1][2])
    else:
        print(dp[-1][1])
else:
    if dp[-1][0] < dp[-1][2]:
        print(dp[-1][2])
    else:
        print(dp[-1][0])

Solution (Python):
N=int(input())
a=[0]*N
b=[0]*N
c=[0]*N
hpa=[0]*N
hpb=[0]*N
hpc=[0]*N
hp=[0]*N

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

hpa[0]=a[0]
hpb[0]=b[0]
hpc[0]=c[0]
hp[0]=max(hpa[0],hpb[0],hpc[0])

for i in range(N-1):
    hpa[i+1]=max(hpb[i]+a[i+1],hpc[i]+a[i+1])
    hpb[i+1]=max(hpa[i]+b[i+1],hpc[i]+b[i+1])
    hpc[i+1]=max(hpa[i]+c[i+1],hpb[i]+c[i+1])
    hp[i+1]=max(hpa[i+1],hpb[i+1],hpc[i+1])

print(hp[-1])

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

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

for i, (a, b, c) in enumerate(ABC):
  dp_a[i+1] = a + max(dp_b[i], dp_c[i])
  dp_b[i+1] = b + max(dp_a[i], dp_c[i])
  dp_c[i+1] = c + max(dp_a[i], dp_b[i])
print(max(dp_a[N], dp_b[N], dp_c[N]))

Solution (Python):

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



dp[0] = l[0]


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

Solution (Python):
N = int(input())
dpa = [0] * N
dpb = [0] * N
dpc = [0] * N

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

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

Solution (Python):
import numpy as np

n = int(input())
ac = []
for i in range(n):
    i = list(map(int,input().split()))
    ac.append(i)
dp = [[0,0,0] for _ in range(n)]
a,b,c = 0,1,2
dp[0] = ac[0][:]

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

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

Solution (Python):
import numpy as np


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

dp = np.zeros((n, 3))
dp[0] = 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][2], dp[i-1][0]) + b[i]
    dp[i][2] = max(dp[i-1][0], dp[i-1][1]) + c[i]
ans = max(dp[-1])
print(int(ans))

Solution (Python):
import math
from math import sqrt
import sys
from collections import deque, defaultdict
from heapq import heappop,heappush
from copy import deepcopy

INF=10**18
MOD=10**9+7
MAX=10**5+7



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

if __name__=='__main__':
  main()