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.



Sample input:
3
10 40 70
20 50 80
30 60 90


Sample output:
210

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**5 + 9)
from collections import defaultdict
dp=defaultdict(lambda:0)
n=int(input())
ar,br,cr=[0]*n,[0]*n,[0]*n
for i in range(n):
	ar[i],br[i],cr[i]=map(int,input().split())
def points(i,pv):
	if dp[(i,pv)] != 0:
		return dp[(i,pv)]
	if pv==1:
		x,y=br[i],cr[i]
		a,b=2,3
	elif pv==2:
		x,y=ar[i],cr[i]
		a,b=1,3
	else:
		x,y=ar[i],br[i]
		a,b=1,2
	if i==n-1:
		return max(x,y)
	ax=points(i+1,a)+x
	by=points(i+1,b)+y
	ans=max(by,ax)
	dp[(i,pv)]=ans
	return ans
p=points(1,1)+ar[0]
q=points(1,2)+br[0]
r=points(1,3)+cr[0]
print(max(p,q,r))

Submission status: Runtime Error

Solution code:
10
1 3 5
4 5 6
3 4 2
3 4 4
3 4 5
3 4 6
3 2 1
3 2 2
2 1 5
1 3 9

Submission status: Runtime Error

Solution code:
a = int(input())
m = []
for i in range(0, a):
  t = [int(v) for v in input().split()]
  m.append(t)

maxtim = 0
maxluu = 0

def xuly(d, c):
    global maxtim
    global maxluu
    if d >= a:
       if maxtim > maxluu: 
           maxluu = maxtim
    else:
        for i in range(0, 3):
            if i != c:
                maxtim += m[d][i]
                xuly(d + 1, i)
                maxtim -= m[d][i]
    return(maxluu)

xuly(0,-1)  
print(maxluu)

Submission status: Runtime Error

Solution code:
a = int(input())
m = []
for i in range(0, a):
  t = [int(v) for v in input().split()]
  m.append(t)

print(m)

maxtim = 0
maxluu = 0

def xuly(d, c):
    global maxtim
    global maxluu
    if d >= a:
       if maxtim > maxluu: 
           maxluu = maxtim
    else:
        for i in range(0, 3):
            if i != c:
                maxtim += m[d][i]
                xuly(d + 1, i)
                maxtim -= m[d][i]
    return(maxluu)

xuly(0,-1)  
print(maxluu)

Submission status: Runtime Error

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

happy_list = [[0, 0, 0]] # n日目にa|b|cを選んだときの幸福のそこまでの最大値
# before_act = -1 # a: 0, b: 1, c: 2
for n in range(N):
    before_happy_a, before_happy_b, before_happy_c = happy_list[-1]
    a, b, c = abc_list[n]
    tmp_happy_a = max(before_happy_b, before_happy_c) + a
    tmp_happy_b = max(before_happy_c, before_happy_a) + b
    tmp_happy_c = max(before_happy_a, before_happy_b) + c
    happy_list.append([tmp_happy_a, tmp_happy_b, tmp_happy_c])

print(max(happy_list[-1]))s

Submission status: Runtime Error

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

dp = [0,0,0]
for a in arr:
	dp = [a[0]+max(dp[1],dp[2]),a[1]+max(dp[0],dp[2]),a[2]+max(dp[0],dp[1])]
return max(dp)

Submission status: Runtime Error

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

dp = [[0,0]]*(N+1)
m = max(a[0])
j = a[0].index(m)
dp[0][0] = m
dp[0][1] = j
for i in range(N):
	prev = dp[i-1][1]
	m = 0
	for j in range(3):
		if j!=prev:
			m = max(a[i],m)
	j = a[i].index(m)
	dp[i][0] = dp[i-1][0] + m
	dp[i][1] = j
print(dp[N-1][0])

Submission status: Runtime Error

Solution code:
N = int(input())
a = []*N
for _ in range(N):
	a.append(list(map(int,input().split())))
def prob(N,a):
	if N==1:
		ans = max(a[0][0],a[0][1],a[0][2])
		if ans==a[0][0]:
			click = 0
		elif ans==a[0][1]:
			click = 1
		elif ans==a[0][2]:
			click = 2
		return [ans, click]

	rec = prob(N-1, a)
	if rec[1] == 0:
		ans = rec[0] + max(a[N-1][1],a[N-1][2])
		if a[N-1][1]>a[N-1][2]:
			click = 1
		else:
			click = 2
		return [ans,click]

	if rec[1] == 1:
		ans = rec[0] + max(a[N-1][0],a[N-1][2])
		if a[N-1][0]>a[N-1][2]:
			click = 0
		else:
			click = 2
		return [ans,click]

	if rec[1] == 2:
		ans = rec[0] + max(a[N-1][0],a[N-1][1])
		if a[N-1][0]>a[N-1][1]:
			click = 0
		else:
			click = 1
		return [ans,click]

l = prob(N,a)
print(l[0])

Submission status: Runtime Error

Solution code:

#B- Frog 2

def solution(nums):
    if not nums:return 0
    INF = float('-inf')
    n = len(nums[0])

    def dp(pre,cur,total = set([0,1,2])):
        if cur == n:
            return 0
        take = total - set([pre])
        res = INF
        for x in take:
            cand =  nums[x][cur] + dp(x,cur+1)
            if cand > res:
                res = cand
        return res
    return dp(-1,0)

N = int(input())
nums = [[],[],[]]
for _ in range(N):    
    a,b,c = list(map(int,input().split()))
    nums[0].append(a)
    nums[1].append(b)
    nums[2].append(c)
res = solution(nums)
print(res)

Submission status: Runtime Error

Solution code:
from functools import lru_cache
N = int(input())
hap_list = [list(map(int, input().rstrip().split(' '))) for _ in range(N)]
@lru_cache(maxsize=None)
def happiness(days=N):
  if not days:
    return 0, 0, 0
  last_result = happiness(days-1)
  choice_a = hap_list[days-1][0] + max(last_result[1], last_result[2])
  choice_b = hap_list[days-1][1] + max(last_result[0], last_result[2])
  choice_c = hap_list[days-1][2] + max(last_result[0], last_result[1])
  return choice_a, choice_b, choice_c
print(max(happiness(N)))

Submission status: Runtime Error

Solution code:
N = int(input())
hap_list = [list(map(int, input().rstrip().split(' '))) for _ in range(N)]
def happiness(days=N):
  if not days:
    return 0, 0, 0
  last_result = happiness(days-1)
  choice_a = hap_list[days-1][0] + max(last_result[1], last_result[2])
  choice_b = hap_list[days-1][1] + max(last_result[0], last_result[2])
  choice_c = hap_list[days-1][2] + max(last_result[0], last_result[1])
  return choice_a, choice_b, choice_c
print(max(happiness(N)))

Submission status: Runtime Error

Solution code:
# https://atcoder.jp/contests/dp/tasks/dp_c
N = int(input())
Max_List = [[0] * 3 for i in range(N)]
for i in range(N):
    Current_List = list(map(int,input().split()))  
    if i == 0:
        Max_List[i] = Current_List
    else:
        Search_List = Max_List[i-1]
        for j in range(3):
            Current_Search_List = Search_List[1:3]
            Max_List[i][j] = max(Current_Search_List) + Current_List[j]
            Search_List.append(Search_List[0])
            del Search_List[0]
        
        Max_List[i] = Current_Max_List

        
print(max(Max_List[-1]))

Submission status: Runtime Error

Solution code:
# https://atcoder.jp/contests/dp/tasks/dp_c
N = int(input())
Max_List = [[0] * 3 for i in range(N)]
for i in range(N):
    Current_List = list(map(int,input().split()))  
    if i == 0:
        Max_List[i] = Current_List
    else:
        Search_List = Max_List[i-1]
        for j in range(3):
            Current_Search_List = Search_List[1:3]
            Max_List[i][j] = (max(Current_Search_List) + Current_List[j])
            Search_List.append(Search_List[0])
            del Search_List[0]
        
        Max_List[i] = Current_Max_List

        
print(max(Max_List[-1]))

Submission status: Runtime Error

Solution code:
# https://atcoder.jp/contests/dp/tasks/dp_c
N = int(input())
Max_List=[]
N_List=[i for i in range(3)]
for i in range(N):
    Current_Max_List = []
    Current_List=list(map(int,input().split())) 
    if i == 0:
        Max_List.append(Current_List)
    else:
        for j in range(3):
            Index_List=np.array(Max_List[i-1])
            ind = np.ones(3, dtype=bool)
            ind[j] = False
            Current_Max_List.append(max(Index_List[ind])+Current_List[j])
            
        Max_List.append(Current_Max_List)
 
print(max(Max_List[-1]))

Submission status: Runtime Error

Solution code:
# https://atcoder.jp/contests/dp/tasks/dp_c
N = int(input())
Max_List=[]
N_List=[i for i in range(3)]
for i in range(N):
    Current_Max_List = []
    Current_List=list(map(int,input().split())) 
    if i == 0:
        Max_List.append(Current_List)
    else:
        for j in range(3):
            Index_List=np.array(Max_List[i-1])
            ind = np.ones(3, dtype=bool)
            ind[j] = False
            Current_Max_List.append(max(Index_List[ind])+Current_List[j])
            
        Max_List.append(Current_Max_List)

print(max(Max_List[-1]))

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import numpy as np

N, W = [int(x) for x in input().split()]
weight = np.zeros((N,2))
for x in range(N):
    weight[x] = [int(y) for y in input().split()]
#topdown
memo = np.zeros((N+1,W+1))
for x in range(N+1):
    memo[x] = [-1]*(W+1)

def dp(n,w):
    if w < 0:
        return -float('inf')
    elif n == 0 or w == 0:
        return 0
    elif memo[n][w] != -1:
        return memo[n][w]
    else:
        memo[n][w] = max(dp(n-1,w), dp(n-1,w - int(weight[n-1][0])) + weight[n-1][1])
        return memo[n][w]

print(int(dp(N,W)))

Submission status: Runtime Error

Solution code:
idmx = []
def vocations(arr, id = 0, cost=0) :
    if id < len(arr) :
        max = 0
        iid = 0
        for i in range(len(arr[id])) :
            if len(idmx) < 1 :
                if max < arr[id][i] :
                    max = arr[id][i]
                    iid = i
            else :
                lastId = idmx[-1]
                if i != lastId :
                    if max < arr[id][i] :
                        max = arr[id][i]
                        iid = i
        cost += max
        id += 1
        idmx.append(iid)
        if len(idmx) > 1 :
            del idmx[0]
        return vocations(arr, id, cost)
    else :
        return cost

def main() :

    n = int(input())
    allArr = []

    for _ in range(n) :
        temp = [int(i) for i in input().split(' ')]
        allArr.append(temp)

    print(vocations(allArr))
if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
idmx = []
def vocations(arr, id = 0, cost=0) :
    if id < len(arr) :
        max = 0
        iid = 0
        for i in range(len(arr[id])) :
            if len(idmx) < 1 :
                if max < arr[id][i] :
                    max = arr[id][i]
                    iid = i
            else :
                lastId = idmx[-1]
                if i != lastId :
                    if max < arr[id][i] :
                        max = arr[id][i]
                        iid = i
        cost += max
        id += 1
        idmx.append(iid)
        if len(idmx) > 1 :
            del idmx[0]
        return vocations(arr, id, cost)
    else :
        return cost

def main() :

    n = int(input())
    allArr = []

    for _ in range(n) :
        temp = [int(i) for i in input().split(' ')]
        allArr.append(temp)

    print(vocations(allArr))
if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
idmx = []
def vocations(arr, id = 0, cost=0) :
    if id < len(arr) :
        max = 0
        iid = 0
        for i in range(len(arr[id])) :
            if len(idmx) < 1 :
                if max < arr[id][i] :
                    max = arr[id][i]
                    iid = i
            else :
                lastId = idmx[-1]
                if i != lastId :
                    if max < arr[id][i] :
                        max = arr[id][i]
                        iid = i
        cost += max
        id += 1
        idmx.append(iid)
        return vocations(arr, id, cost)
    else :
        return cost

def main() :

    n = int(input())
    allArr = []

    for _ in range(n) :
        temp = [int(i) for i in input().split(' ')]
        allArr.append(temp)

    print(vocations(allArr))
if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
import math
from functools import lru_cache
def Frog1():
    n = int(input())
    h = list(map(int,input().split(' ')))
    @lru_cache(None)
    def Solve(i):
        if i==n-1:
            return 0
        if i==n-2:
            return abs(h[i]-h[i+1])
        return min(Solve(i+1)+abs(h[i]-h[i+1]),Solve(i+2)+abs(h[i]-h[i+2]))
    dp = [0]*n
    for i in reversed(range(n)):
        if i==n-1:
            dp[i] = 0
        elif i==n-2:
            dp[i] = abs(h[i]-h[i+1])
        else:
            dp[i] = min(dp[i+1]+abs(h[i]-h[i+1]),dp[i+2]+abs(h[i]-h[i+2]))
    print(dp[0])
def Frog2():
    n,k = input().split(' ')
    n,k = int(n),int(k)
    h = list(map(int, input().split(' ')))
    @lru_cache(None)
    def Solve(i):
        if i==n-1:
            return 0
        ans = math.inf
        for j in range(1,k+1):
            new_i = i+j
            if new_i<=n-1:
                ans = min(ans , Solve(new_i)+abs(h[i]-h[new_i]))
            else:
                break
        return ans

    dp = [0]*n
    for i in reversed(range(n)):
        if i==n-1:
            dp[i] = 0
        else:
            ans = math.inf
            for j in range(1,k+1):
                new_i = i+j
                if new_i<=n-1:
                    ans = min(ans,dp[new_i]+abs(h[i]-h[new_i]))
                else:
                    break
            dp[i] = ans
    print(dp[0])

def Vacation():
    n = int(input())
    gain = []
    for i in range(n):
        a = list(map(int,input().split(' ')))
        gain.append(a)
    @lru_cache(None)
    def Solve(i,prevj):
        if i==n:
            return 0
        ans = -math.inf
        m = list({0,1,2} - {prevj})
        for j in m:
            ans = max(ans,Solve(i+1,j)+gain[i][j])
        return ans

    print(Solve(0,-1))
Vacation()

Submission status: Runtime Error

Solution code:
class Data:
    def __init__(self,n):
        self.n = n
        self.abc = []
        self.sum = 0
        self.history = [-1]*n
        self.ava = [[1,2],[0,2],[0,1]]

    def change_his(self,i):
        a = self.abc[i-1][self.history[i]] + max(self.abc[i][self.ava[self.history[i]][0]],self.abc[i][self.ava[self.history[i]][1]])
        b = max(self.abc[i-1][self.ava[self.history[i]][0]],self.abc[i-1][self.ava[self.history[i]][1]]) + self.abc[i][self.history[i]]
        if a>b:
            self.history[i] = self.abc[i].index( max(self.abc[i][self.ava[self.history[i]][0]],self.abc[i][self.ava[self.history[i]][1]]))
            
        elif a==b:
            self.history[i] = self.abc[i].index( max(self.abc[i][self.ava[self.history[i]][0]],self.abc[i][self.ava[self.history[i]][1]]))
        else:
            self.history[i-1] = self.abc[i].index(max(self.abc[i-1][self.ava[self.history[i]][0]],self.abc[i-1][self.ava[self.history[i]][1]]))
            if(self.history[i-1]==self.history[i-2]):
                self.change_his(i-1)
        return

    def input_abc(self):
        for i in range(self.n):
            self.abc.append(list(map(int,input().split())))

    def init(self):
        #print(self.abc)
        self.history[0] = self.abc[0].index(max(self.abc[0]))
    
    def count(self):
        for i in range(self.n):
            self.sum += self.abc[i][self.history[i]]

def main():
    n = int(input())
    data = Data(n)
    data.input_abc()
    data.init()
    for i in range(1,n):
        data.history[i] = data.abc[i].index(max(data.abc[i]))
        if(data.history[i-1] == data.history[i]):
            data.change_his(i)
    data.count()
    print(data.sum)

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
n = int(input())

arrays = []
for _ in range(n):
  day = input()
  day = day.split()
  arrays.append(day)

result = []
for i in range(len(arrays)):
    if i == 0:
        result.append([max(arrays[i]), arrays[i].index(max(arrays[i]))])
    elif i!=0:
        hasil = result[i-1][1]
        if (arrays[i].index(max(arrays[i])))!= hasil:
            result.append([max(arrays[i]), arrays[i].index(max(arrays[i]))])
        else:
            arrays[i].remove(max(arrays[i]))
            result.append([max(arrays[i]), arrays[i].index(max(arrays[i]))])
jumlah = 0
for i in result:
    jumlah+=i[0]
print(jumlah)

Submission status: Runtime Error

Solution code:
def solve(a,lvl,prev,dp):
    if (prev,lvl) in dp: return dp[(prev,lvl)]
    if lvl==len(a)-1:
        if prev==0: return max(a[-1][1],a[-1][2])
        if prev==1: return max(a[-1][0],a[-1][2])
        if prev==2: return max(a[-1][0],a[-1][1])
    if prev==-1:
        p=a[0][0]+solve(a,lvl+1,0,dp)
        q=a[0][1]+solve(a,lvl+1,1,dp)
        r=a[0][2]+solve(a,lvl+1,2,dp)
        return max(p,q,r)
    dp[(prev,lvl)]=max(a[lvl][(prev+1)%3]+solve(a,lvl+1,(prev+1)%3,dp),a[lvl][(prev-1)%3]+solve(a,lvl+1,(prev-1)%3,dp))
    return dp[(prev,lvl)]
if __name__=='__main__':
    n=int(input())
    a=[]
    for i in range(n):
        a.append([int(i) for i in input().split()])
    if n==1:
        print(max(a[0]))
    else:
        print(solve(a,0,-1,{}))

Submission status: Runtime Error

Solution code:
def solve(a,lvl,prev,dp,curr):
    if lvl>len(a): return
    if (prev,lvl) in dp: return dp[(prev,lvl)]
    if prev==-1:
        return max(solve(a,lvl+1,0,dp,curr+a[lvl][0]),solve(a,lvl+1,1,dp,curr+a[lvl][1]),solve(a,lvl+1,2,dp,curr+a[lvl][2]))
    if lvl==len(a):
        if prev==0: return curr+max(a[-1][1],a[-1][2])
        if prev==1: return curr+max(a[-1][0],a[-1][2])
        if prev==2: return curr+max(a[-1][0],a[-1][1])
    dp[(prev,lvl)]=max(solve(a,lvl+1,(prev+1)%3,dp,curr+a[lvl][(prev+1)%3]),solve(a,lvl+1,(prev-1)%3,dp,curr+a[lvl][(prev-1)%3]))
    return dp[(prev,lvl)]
if __name__=='__main__':
    n=int(input())
    a=[]
    for i in range(n):
        a.append([int(i) for i in input().split()])
    print(solve(a,0,-1,{},0))

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(100000)

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

def dp(i, a):
    if i == 0:
        return action[0][a]
    else:
        return max(dp(i-1, (a+1)%3), dp(i-1, (a+2)%3)) + action[i][a]

print(max([dp(n-1,a) for a in range(3)]))

Submission status: Runtime Error

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

@lru_cache(maxsize=1000000)
def dp(i, a):
    if i == 0:
        return action[0][a]
    else:
        return max(dp(i-1, (a+1)%3), dp(i-1, (a+2)%3)) + action[i][a]

import sys
sys.setrecursionlimit(1000000)
print(max([dp(n-1,a) for a in range(3)]))

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(100000)

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

memo = []
for i in range(n):
    memo.append([-1]*3)

def dp(i, a):
    if i == 0:
        return action[0][a]
    elif memo[i][a] != -1:
        return memo[i][a]
    else:
        memo[i][a] = max(dp(i-1, (a+1)%3), dp(i-1, (a+2)%3)) + action[i][a]
        return memo[i][a]

print(max([dp(n-1,a) for a in range(3)]))

Submission status: Runtime Error

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

memo = []
for i in range(n):
    memo.append([-1]*3)

def dp(i, a):
    if i == 0:
        return action[0][a]
    elif memo[i][a] != -1:
        return memo[i][a]
    else:
        memo[i][a] = max(dp(i-1, (a+1)%3), dp(i-1, (a+2)%3)) + action[i][a]
        return memo[i][a]

import sys
sys.setrecursionlimit(n*3)
print(max([dp(n-1,a) for a in range(3)]))

Submission status: Runtime Error

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

memo = []
for i in range(n):
    memo.append([-1]*3)

def dp(i, a):
    if i == 0:
        return action[0][a]
    elif memo[i][a] != -1:
        return memo[i][a]
    else:
        memo[i][a] = max(dp(i-1, (a+1)%3), dp(i-1, (a+2)%3)) + action[i][a]
        return memo[i][a]

print(max([dp(n-1,a) for a in range(3)]))

Submission status: Runtime Error

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

memo = []
for i in range(n):
    memo.append([-1]*3)

def dp(i, a):
    global action
    global memo
    if i == 0:
        return action[0][a]
    elif memo[i][a] != -1:
        return memo[i][a]
    else:
        memo[i][a] = max(dp(i-1, (a+1)%3), dp(i-1, (a+2)%3)) + action[i][a]
        return memo[i][a]

print(max([dp(n-1,a) for a in range(3)]))

Submission status: Runtime Error

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

memo = []
for i in range(n):
    memo.append([-1]*3)

def dp(i, a):
    global memo
    if i == 0:
        return action[0][a]
    elif memo[i][a] != -1:
        return memo[i][a]
    else:
        memo[i][a] = max(dp(i-1, (a+1)%3), dp(i-1, (a+2)%3)) + action[i][a]
        return memo[i][a]

print(max([dp(n-1,a) for a in range(3)]))

Submission status: Runtime Error

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

memo = []
for i in range(n):
    memo.append([-1]*3)

def dp(i, a):
  	global memo
    if i == 0:
        return action[0][a]
    elif memo[i][a] != -1:
        return memo[i][a]
    else:
        memo[i][a] = max(dp(i-1, (a+1)%3), dp(i-1, (a+2)%3)) + action[i][a]
        return memo[i][a]

print(max([dp(n-1,a) for a in range(3)]))

Submission status: Runtime Error

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

memo = []
for i in range(n):
    memo.append([-1]*3)

def dp(i, a):
    if i == 0:
        return action[0][a]
    elif memo[i][a] != -1:
        return memo[i][a]
    else:
        memo[i][a] = max(dp(i-1, (a+1)%3), dp(i-1, (a+2)%3)) + action[i][a]
        return memo[i][a]

print(max([dp(n-1,a) for a in range(3)]))

Submission status: Runtime Error

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

memo = []
for i in range(n):
    memo.append([-1]*3)

def dp(i, a):
    if i == 0:
        return action[0][a]
    else:
        memo[i][a] = max(dp(i-1, (a+1)%3), dp(i-1, (a+2)%3)) + action[i][a]
        return memo[i][a]

print(max([dp(n-1,a) for a in range(3)]))

Submission status: Runtime Error

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

memo = []
for i in range(n):
    memo.append([-1]*3)
def dp(i, a):
    if i == 0:
        return action[0][a]
    elif memo[i][a] != -1:
        return memo[i][a]
    else:
        return memo[i][a] = max(dp(i-1, (a+1)%3), dp(i-1, (a+2)%3)) + action[i][a]

print(max([dp(n-1,a) for a in range(3)]))

Submission status: Runtime Error

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

def dp(i, a):
    if i == 0:
        return action[0][a]
    else:
        return max(dp(i-1, (a+1)%3), dp(i-1, (a+2)%3)) + action[i][a]

print(max([dp(n-1,a) for a in range(3)]))

Submission status: Runtime Error

Solution code:
def funcion():
    n = int(input())
    indice_anterior = -1
    total = 0
    
    

    for i in range(n):
        mayor = 0
        cadena = input()
        lista = cadena.split()
        for j in lista:
            jnum = int(j)
            indice_actual = lista.index(j)
            if (jnum > mayor) and indice_actual != indice_anterior:
                mayor = jnum
        indice_anterior = lista.index(str(mayor))     
        total = total + mayor
    print(total)

def main():
    return(funcion())

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
from time import time

def funcion():
    n = int(input())
    indice_anterior = -1
    total = 0

    for i in range(n):
        mayor = 0
        cadena = input()
        lista = cadena.split()
        cont = 0
        for j in lista:
            jnum = int(j)
            if (jnum > mayor) and cont != indice_anterior:
                mayor = jnum
            cont = cont + 1
        indice_anterior = lista.index(str(mayor))
        total = total + mayor
    print(total, tiempo_ejecucion)

def main():
    return(funcion())

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
def funcion():
    n = int(input())
    indice_anterior = -1
    total = 0
    
    for i in range(n):
        mayor = 0
        cadena = input()
        lista = cadena.split()
        for j in lista:
            jnum = int(j)
            indice_actual = lista.index(j)
            if (jnum >= mayor) and indice_actual != indice_anterior:
                mayor = jnum
        indice_anterior = lista.index(str(mayor))     
        total = total + mayor

    
    
    print(total)
def main():
    return(funcion())

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
def funcion():
    n = int(input())
    m = []
    indice_anterior = -1
    total = 0

    for i in range(n):
        cadena = input()
        lista = cadena.split()
        m.append(lista)
    
    for i in range(n):
        mayor = 0
        for j in m[i]:
            jnum = int(j)
            indice_actual = m[i].index(j)
            if (jnum > mayor) and indice_actual != indice_anterior:
                mayor = jnum
        indice_anterior = m[i].index(str(mayor))     
        total = total + mayor
    
    print(total)
def main():
    return(funcion())

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
n=int(input())
abc = [list(map(int, input().split())) for i in range(n)]
#[[10,40,70],[20,50,80],[30,60,90]]
ABC=[[0,0,0]]
#A = [0]
#B = [0]
#C = [0]
for i in range(n):
  a = abc[i][0] + max(ABC[-1][1], ABC[-1][2])#10+max(0,0),20+max(40,70)=90,30+max(120,120)=150
  b = abc[i][1] + max(ABC[-1][0], ABC[-1][2])#40+max(0,0),50+max(10,70)=120,60+max(90,120)=180
  c = abc[i][2] + max(ABC[-1][0], ABC[-1][1])#70+mac(0,0),80+max(10,40)=120,90+max(90,120)=210
  ABC += [[a,b,c]]
  #A += [a]#0,10,90,150
  #B += [b]#0,40,120,180
  #C += [c]#0,70,120,210
print(max(ABC[-1])

Submission status: Runtime Error

Solution code:
n=int(input())
abc = [list(map(int, input().split())) for i in range(n)]
#[[10,40,70],[20,50,80],[30,60,90]]
ABC=[[0,0,0]]
#A = [0]
#B = [0]
#C = [0]
for i in range(n):
  a = abc[i][0] + max(B[-1], C[-1])#10+max(0,0),20+max(40,70)=90,30+max(120,120)=150
  b = abc[i][1] + max(A[-1], C[-1])#40+max(0,0),50+max(10,70)=120,60+max(90,120)=180
  c = abc[i][2] + max(A[-1], B[-1])#70+mac(0,0),80+max(10,40)=120,90+max(90,120)=210
  ABC += [[a,b,c]]
  #A += [a]#0,10,90,150
  #B += [b]#0,40,120,180
  #C += [c]#0,70,120,210
print(max(ABC[-1])

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**6)

n = int(input())
a, b, c = [], [], []
# dp = [[None] * 3 for _ in range(n)]
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(n):
    v1, v2, v3 = map(int, input().split())
    a.append(v1)
    b.append(v2)
    c.append(v3)

def solve(idx, ptn):
    if dp[idx][ptn] > 0:
        return dp[idx][ptn]

    # Select A(0)
    if ptn == 0:
        if idx == 0:
            return a[0]
        a1 = solve(idx - 1, 1) + a[idx]
        a2 = solve(idx - 1, 2) + a[idx]
        dp[idx][0] = max(a1, a2)
        return dp[idx][0]

    # Select B(1)
    if ptn == 1:
        if idx == 0:
            return b[0]
        b1 = solve(idx - 1, 0) + b[idx]
        b2 = solve(idx - 1, 2) + b[idx]
        dp[idx][0] = max(b1, b2)
        return dp[idx][1]

    # Select C(2)
    if ptn == 2:
        if idx == 0:
            return c[0]
        c1 = solve(idx - 1, 0) + c[idx]
        c2 = solve(idx - 1, 1) + c[idx]
        dp[idx][0] = max(c1, c2)
        return dp[idx][2]

ans1 = solve(n-1, 0)
ans2 = solve(n-1, 1)
ans3 = solve(n-1, 2)
print(max(ans1, ans2, ans3))

Submission status: Runtime Error

Solution code:
n = int(input()
arr = [[int(i) for i in list(input().split())] for j in range(n)]
for i in range(1,n):
	arr[i][0] += max(arr[i-1][1], arr[i-1][2])
	arr[i][2] += max(arr[i-1][1], arr[i-1][0])
	arr[i][1] += max(arr[i-1][0], arr[i-1][2])
print(max(arr[n-1]))

Submission status: Runtime Error

Solution code:
N = int(input())
 
li = [list(map(int,input().split())) for i in range(N)]
 
from sys import setrecursionlimit
setrecursionlimit(pow(10,6))
from functools import lru_cache
 
 
#i回目にa..0,b..1,c..2 を選んだ時の最大幸福値
@lru_cache(None)
def func(i,num):
    s = num%3
    if i==0:
        return max(li[0][(s+1)%3],li[0][(s+2)%3])
    return max(func(i-1,(s+1)%3),func(i-1,(s+2)%3)) + li[i][num]
 
print(max( func(N-1,0), func(N-1,1), func(N-1,2) ))
N = int(input())

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

from sys import setrecursionlimit
setrecursionlimit(pow(10,6))
from functools import lru_cache


#i回目にa..0,b..1,c..2 を選んだ時の最大幸福値
@lru_cache(None)
def func(i,num):
    s = num%3
    if i==0:
        return max(li[0][(s+1)%3],li[0][(s+2)%3])
    return max(func(i-1,(s+1)%3),func(i-1,(s+2)%3)) + li[i][num]

print(max( func(N-1,0), func(N-1,1), func(N-1,2) ))

Submission status: Runtime Error

Solution code:
N = int(input())
abc = [[int(j) for j in input().split()] for i in range(n)]
dp = [[0] * 3]
for day in range(N):
    dp += [[max([dp[-1][p] + abc[day][n] for p in range(3) if p != n]) for n in range(3)]]
print(max(dp[-1]))

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
h = [int(i) for i in input().split()]
dp = [0]
for i in range(1, n):
    dp += [min([dp[-j] + abs(h[i] - h[i - j]) for j in range(1, min(i, k) + 1)])]
print(dp[-1])

Submission status: Runtime Error

Solution code:
def dp(idx,f,a,cache):
    if idx==len(a):
        return 0
    if cache[idx][f]!=-1:
        return cache[idx][f]
    ans=0
    for i in range(3):
        if i==f:
            continue
        ans=max(ans,dp(idx+1,i,a,cache))
    ans+=a[idx][f]
    cache[idx][f]=ans
    return ans
a=[]
n=int(input())
for i in range(n):
    x1,x2,x3=map(int,input().split())
    a.append([x1,x2,x3])
cache=[[-1]*3 for i in range(n)]
ans=0
for i in range(3):
  ans=max(ans,dp(0,i,a,cache))
print(ans)

Submission status: Runtime Error

Solution code:
def dp(idx,f,a,cache):
    if idx==len(a):
        return 0
    if cache[idx][f]!=-1:
        return cache[idx][f]
    ans=0
    for i in range(3):
        if i==f:
            continue
        ans=max(ans,dp(idx+1,i,a,cache))
    ans+=a[idx][f]
    cache[idx][f]=ans
    return ans
a=[]
n=int(input())
for i in range(n):
    a.append(list(map(int,input().split())))
cache=[[-1,-1,-1] for i in range(n)]
ans=0
for i in range(3):
  ans=max(ans,dp(0,i,a,cache))
print(ans)