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:
num = int(input())
actions = []
for i in range(num):
    actions.append(list(map(int, input().split())))

dp = [[0 if j != 0 else actions[i][j] for i in range(3)] for j in range(num)]
for i in range(num):
    dp[i][0] = max(dp[i-1][1], dp[i-1][2]) + actions[i][0]
    dp[i][1] = max(dp[i-1][0], dp[i-1][2]) + actions[i][1]
    dp[i][2] = max(dp[i-1][0], dp[i-1][1]) + actions[i][2]

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

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline()
N = int(input())
abc = [[0,0,0]]+[list(map(int,input().split())) for _ in range(N)]
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 j == k:continue
            #print('i={},j={},k={}'.format(i,j,k))
            #print(dp[i][j],abc[i][j],abc[i-1][k])
            dp[i][j] = max(dp[i][j],abc[i][j]+dp[i-1][k])
print(max(dp[-1]))

Submission status: Runtime Error

Solution code:
import sys
read = sys.stdin.readline


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_matrix(H):
    '''
    H is number of rows
    '''
    return [list(map(int, read().split())) for _ in range(H)]


def read_map(H):
    '''
    H is number of rows
    文字列で与えられた盤面を読み取る用
    '''
    return [read() for _ in range(H)]


def read_col(H, n_cols):
    '''
    H is number of rows
    n_cols is number of cols

    A列、B列が与えられるようなとき
    '''
    ret = [[] for _ in range(n_cols)]
    for _ in range(H):
        tmp = list(map(int, read().split()))
        for col in range(n_cols):
            ret[col].append(tmp[col])

    return ret

# 全探索だと
# 3^N通りになってTLEだこれ

# 制限がなければ、iごとにmaxを取っておわりO(N)

# iの時の最善の状態が[i,j](jは行動)のとき、i+1最善の状態は[i+1,k](k!=j)となる
# これはDPで実装できそう！


# import numpy as np

N = read_a_int()
ABC = np.array(read_matrix(N))
# A, B, C = read_col(N, 3)

dp = np.full((N, 3), -float('inf'))
# 0->a,1->b,2->c

dp[0, :] = ABC[0]

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

# print(dp)
print(int(dp[N-1].max()))

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
def Solve(acts,first,second,third,index):
    if index == len(acts):
        return 0
    
    if first:
        val1 =  acts[index][0]+Solve(acts,False,True,True,index+1)
    else:
        val1 = 0
    if second:
        val2 = acts[index][1]+Solve(acts,True,False,True,index+1)
    else:
        val2 = 0
    if third:
        val3 =  acts[index][2]+Solve(acts,True,True,False,index+1)
    else:
        val3 = 0

    return max(val1,val2,val3)

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

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

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

    print(Solve(acts,True,True,True,0))

main()

Submission status: Runtime Error

Solution code:
def Solve(acts,first,second,third,index):
    if index == len(acts):
        return 0
    
    if first:
        val1 =  acts[index][0]+Solve(acts,False,True,True,index+1)
    else:
        val1 = 0
    if second:
        val2 = acts[index][1]+Solve(acts,True,False,True,index+1)
    else:
        val2 = 0
    if third:
        val3 =  acts[index][2]+Solve(acts,True,True,False,index+1)
    else:
        val3 = 0

    return max(val1,val2,val3)

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

    dp = [[0 for x in range(3)] for y in range(n)]
    dp[0][0] = acts[0][0]
    dp[0][1] = acts[0][1]
    dp[0][2] = acts[0][2]

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

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

    print(Solve(acts,True,True,True,0))

main()

Submission status: Runtime Error

Solution code:



def do(act, day):
    if day >= n:
        return 0
    if happy_days[day][act] == None:
        happiness = -1
        for i in [0,1,2]:
            if i != act:
                happiness = max(happiness, do(i,day+1))
        
        happy_days[day][act] = happiness + activity[day][act]
    return happy_days[day][act]

n = int(input())
activity = []
happy_days = [[None] * 3 for i in range(n)] 
for i in range(n):
    activity.append(list(map(int,input().split())))

print(max(do(0,0), do(1,0), do(2,0)))

Submission status: Runtime Error

Solution code:
N = int(input())
util = [[int(i) for i in input().split()] for _ in range(N)]
memo = [0 for _ in range(N + 1)]
def dp(i, u, rem_i):
    if i == N:
        return u
    elif i == 0:
        ret = max(dp(i + 1, u+util[i][0], 0), dp(i + 1, u+util[i][1], 1), dp(i + 1, util[i][2], 2))
    else:
        index = list({0, 1, 2} - {rem_i})
        ret = max(dp(i + 1, u+util[i][index[0]], index[0]), dp(i + 1, u+util[i][index[1]], index[1]))
    memo[i] = ret
    return ret

print(dp(0, 0, 3))

Submission status: Runtime Error

Solution code:
n = int(input())
joy = []

for i in range(n):
    cur = [int(i) for i in input().split()]
    joy.append(cur)

dp = [[0 for i in range(n+5)] for j in range(3)]
import sys
sys.setrecursionlimit(100000)
def brute(curday, prvmove):
    if curday>=n:
        return 0
    if dp[prvmove][curday]!=0:
        return dp[prvmove][curday]
    ans = 0
    
    for i in range(3):
        if prvmove == i:
            continue
        ans = max(ans, brute(curday+1, i) + joy[curday][i])

    #print('with', daysleft)
    #print('returning', ans)
    dp[prvmove][curday] = ans
    return ans

ans = 0

for i in range(3):
    ans = max(ans, brute(0, i))
print(ans)

Submission status: Runtime Error

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

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

for idx, val in enumerate(lis[1:], start=1):
	a = val[0] + max([dp[idx-1][i] for i in range(3) if 0 != i ])
	b = val[1] + max([dp[idx-1][i] for i in range(3) if 1 != i ])
	c = val[2] + max([dp[idx-1][i] for i in range(3) if 2 != i ])
	dp[idx] = [a,b,c]
print(max(dp[-1][0]))

Submission status: Runtime Error

Solution code:
n = int(input())

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

# n日目に act を行うときの最大値
def rec(n, act):
    if n == 0:
        return data[n][act]

    if act == 0:
        available = [1, 2]
    elif act == 1:
        available = [0, 2]
    else:
        available = [0, 1]
    return data[n][act] + max(rec(n - 1, available[0]), rec(n - 1, available[1]))

mx = max(rec(n - 1, 0), rec(n - 1, 1), rec(n - 1, 2))

print(mx)

Submission status: Runtime Error

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

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

for i in range(1,N):

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


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

Submission status: Runtime Error

Solution code:
n = int(input())
table = [0]*n
activities = [[]*3 for x in range(3)]
temp = []

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

table[0] = max(activities[0])

x = activities[0].index((table[0]))
for i in range(1, n):
    temp = activities[i][:x] + activities[i][x+1:]
    x = activities[i].index(max(temp))
    table[i] = table[i-1] + max(temp)

print(table[n-1])

Submission status: Runtime Error

Solution code:
N = int(input())
scores = []

for i in range(N):
    scores.append(tuple(map(int, input().split())))

mem = [[None]*3 for i in range(N)]

def getScore(day, act):
    if mem[day][act] is not None:
        return mem[day][act]
    score = scores[day][act]
    if day < N-1:
        score += max(getScore(day+1, (act+1)%3), getScore(day+1, (act+2)%3))
    mem[day][act] = score
    return score

print(max(getScore(0, act) for act in range(3)))

Submission status: Runtime Error

Solution code:
import math
import sys
from operator import itemgetter

# N, K = map(int, input().split())
# h = list(map(int, input().split()))
# S = [input() for _ in range(H)]
# X = [[0 for j in range(100)] for i in range(200)]


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

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


### solve
hpns = [[0 for j in range(101)] for i in range(3)]

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

Submission status: Runtime Error

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

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

def find(i, j):
	global happ
	if i==N-1:
		if j==0:
			return max(arr[i][1], arr[i][2])
		elif j==1:
			return max(arr[i][0], arr[i][2])
		else:
			return max(arr[i][0], arr[i][1])
	if dp[i][j] != -1:
		happ += dp[i][j]
		return dp[i][j]
	if j==0:
		dp[i][j] = max(find(i+1, 1), find(i+1, 2)) + arr[i][j]
	elif j==1:
		dp[i][j] = max(find(i+1, 0), find(i+1, 2)) + arr[i][j]
	else:
		dp[i][j] = max(find(i+1, 0), find(i+1, 1)) + arr[i][j]
	happ += dp[i][j]
	return dp[i][j]
happ += max(find(0, 0), find(0, 1), find(0, 2))
print(happ)

Submission status: Runtime Error

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

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

def find(i, j):
	global happ
	if i==N-1:
		if j==0:
			return max(arr[i][1], arr[i][2])
		elif j==1:
			return max(arr[i][0], arr[i][2])
		else:
			return max(arr[i][0], arr[i][1])
	if dp[i][j] != -1:
		happ += dp[i][j]
		return dp[i][j]
	if j==0:
		dp[i][j] = max(find(i+1, 1), find(i+1, 2)) + arr[i][j]
	elif j==1:
		dp[i][j] = max(find(i+1, 0), find(i+1, 2)) + arr[i][j]
	else:
		dp[i][j] = max(find(i+1, 0), find(i+1, 1)) + arr[i][j]
	happ += dp[i][j]
	return dp[i][j]
print(happ)
happ += max(find(0, 0), find(0, 1), find(0, 2))
print(happ)

Submission status: Runtime Error

Solution code:
N = int(raw_input())
act = [map(int, raw_input().split()) for _ in range(N)]
memo = [[-1]*3 for _ in range(N)]

def happy(n, i):
  if memo[n][i] >= 0:
    return memo[n][i]
  if n == 0:
    h = act[n][i]
  else:
    h = -1
    for j in range(3):
      if j == i:
        continue
      c = happy(n-1,j)+act[n][i]
      h = h if h > c else c
  memo[n][i] = h
  return h
print max(happy(N-1,0),happy(N-1,1),happy(N-1,2))

Submission status: Runtime Error

Solution code:
import sys
import numpy as np

N = int(sys.stdin.readline().strip())

a_list = [None for _ in range(N)]
b_list = [None for _ in range(N)]
c_list = [None for _ in range(N)]

for i in range(N):
    a, b, c = list(map(int, sys.stdin.readline().strip().split(" ")))
    a_list[i] = a
    b_list[i] = b
    c_list[i] = c
    
tmp = [a_list, b_list, c_list]
happy = [[0 for _ in range(3)] for __ in range(N)]

happy[0][0] = a_list[0]
happy[0][1] = b_list[0]
happy[0][2] = c_list[0]

for i in range(1, N):
    for j in range(3):
        #今日なにするか
        max_h = 0
        for k in range(3):
            #昨日なにしてたか
            if k == j:
                continue
            max_h = max(max_h, happy[i-1][k] + tmp[i][j])
        happy[i][j] = max_h

#print(happy)
print(max(happy[N-1]))
    

"""

happy = [0 for _ in range(N)]
select = [None for _ in range(N)]

ind = np.argmax([a_list[0], b_list[0], c_list[0]])
select[0] = [0, 1, 2][ind]
happy[0] = [a_list[0], b_list[0], c_list[0]][ind]

if N == 1:
    print(happy[0])
    sys.exit()

ind = np.argmax([a_list[0] + b_list[1], a_list[0] + c_list[1], b_list[0] + a_list[1], b_list[0] + c_list[1], c_list[0] + a_list[1], c_list[0] + b_list[1]])
happy[1] = [a_list[0] + b_list[1], a_list[0] + c_list[1], b_list[0] + a_list[1], b_list[0] + c_list[1], c_list[0] + a_list[1], c_list[0] + b_list[1]][ind]
select[1] = [1, 2, 0, 2, 0, 1][ind]

if N == 2:
    print(happy[1])
    sys.exit()

for i in range(2, N):
    max_h = 0
    for j in range(3):
        for k in range(3):
            if k == j:
                continue
            if j == select[i-2]:
                continue
            if max_h < tmp[k][i] + tmp[j][i-1] + happy[i-2]:
                max_h = tmp[k][i] + tmp[j][i-1] + happy[i-2]
                select[i] = k
            #print(tmp[k][i-1], tmp[j][i-2])
    happy[i] = max_h

print(happy[N-1])
"""

Submission status: Runtime Error

Solution code:
import sys
import numpy as np

N = int(sys.stdin.readline().strip())

a_list = [None for _ in range(N)]
b_list = [None for _ in range(N)]
c_list = [None for _ in range(N)]

for i in range(N):
    a, b, c = list(map(int, sys.stdin.readline().strip().split(" ")))
    a_list[i] = a
    b_list[i] = b
    c_list[i] = c

tmp = [a_list, b_list, c_list]
happy = [0 for _ in range(N)]
select = [None for _ in range(N)]

ind = np.argmax([a_list[0], b_list[0], c_list[0]])
select[0] = [0, 1, 2][ind]
happy[0] = [a_list[0], b_list[0], c_list[0]][ind]

ind = np.argmax([a_list[0] + b_list[1], a_list[0] + c_list[1], b_list[0] + a_list[1], b_list[0] + c_list[1], c_list[0] + a_list[1], c_list[0] + b_list[1]])
happy[1] = [a_list[0] + b_list[1], a_list[0] + c_list[1], b_list[0] + a_list[1], b_list[0] + c_list[1], c_list[0] + a_list[1], c_list[0] + b_list[1]][ind]
select[1] = [1, 2, 0, 2, 0, 1][ind]

for i in range(2, N):
    max_h = 0
    for j in range(3):
        for k in range(3):
            if k == j:
                continue
            if j == select[i-2]:
                continue
            if max_h < tmp[k][i] + tmp[j][i-1] + happy[i-2]:
                max_h = tmp[k][i] + tmp[j][i-1] + happy[i-2]
                select[i] = k
            #print(tmp[k][i-1], tmp[j][i-2])
    happy[i] = max_h

print(happy[N-1])

Submission status: Runtime Error

Solution code:
import sys
import numpy as np

N = int(sys.stdin.readline().strip())

a_list = [None for _ in range(N)]
b_list = [None for _ in range(N)]
c_list = [None for _ in range(N)]

for i in range(N):
    a, b, c = list(map(int, sys.stdin.readline().strip().split(" ")))
    a_list[i] = a
    b_list[i] = b
    c_list[i] = c

tmp = [a_list, b_list, c_list]
happy = [0 for _ in range(N)]
select = [None for _ in range(N)]
ind = np.argmax([a_list[0], b_list[0], c_list[0]])
select = [0, 1, 2][ind]
happy[0] = [a_list[0], b_list[0], c_list[0]][ind]

ind = np.argmax([a_list[0] + b_list[1], a_list[0] + c_list[1], b_list[0] + a_list[1], b_list[0] + c_list[1], c_list[0] + a_list[1], c_list[0] + b_list[1]])
happy[1] = [a_list[0] + b_list[1], a_list[0] + c_list[1], b_list[0] + a_list[1], b_list[0] + c_list[1], c_list[0] + a_list[1], c_list[0] + b_list[1]][ind]
select = [1, 2, 0, 2, 0, 1][ind]

for i in range(2, N):
    max_h = 0
    for j in range(3):
        for k in range(3):
            if k == j:
                continue
            max_h = max(max_h, tmp[k][i] + tmp[j][i-1] + happy[i-2])
            #print(tmp[k][i-1], tmp[j][i-2])
    happy[i] = max_h

print(happy[N-1])

Submission status: Runtime Error

Solution code:
import copy

class Item:
	def __init__(self):
		self.weight = 0
		self.value = 0

	def __repr__(self):
		return "(" + str(self.weight) + ", " + str(self.value) + ")"

items = []


def main():
	N, W = [int(x) for x in input().split()]
	global items
	
	for _ in range(0, N):
		x = Item()
		x.weight, x.value = [int(y) for y in input().split()]
		items.append(x)


	dp = [[]] * (N + 1)
	for _ in range(0, N + 1):
		dp[_] = [0] * (W + 1)
	

	dp[0][0] = 0
	for i in range(1, N + 1):
		for j in range(0, W + 1):
			if items[i - 1].weight > j:
				dp[i][j] = dp[i - 1][j]
			else:
				dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - items[i - 1].weight] + items[i - 1].value)

	print(dp[N][W])

if __name__ == "__main__":
	main()

Submission status: Runtime Error

Solution code:
a = int(input())

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

c = [[0,0,0] in i for range(a+1)]

for i in range(a):
	for j in range(3):
		for k in range(3):
			if j == k:
				continue
			else:
				c[i+1][k] = max(c[i+1][k],c[i][k] + b[i][j])

print(max(c[-1]))

Submission status: Runtime Error

Solution code:
N = int(input())
L = [list(map(int,input().split())) for i in range(N)]
NOW = [0]*3
NEXT = [None]*3
for i in range(N) :
    NEXT[0] = max(NOW[1],NOW[2]) + L[0]
    NEXT[1] = max(NOW[2],NOW[0]) + L[1]
    NEXT[2] = max(NOW[0],NOW[1]) + L[2]    
    NOW = NEXT.copy()
print(max(NOW))

Submission status: Runtime Error

Solution code:
N = int(input())
L = [[map(int,input().split())] for i in range(N)]

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

Submission status: Runtime Error

Solution code:
n = int(input())
x = [list(map(int,input().split())) for _ in range(n)]
a = 0
b = 0
c = 0
for i in range(n):
  pa = a
  pb = b
  pc = c
  x = x[i]
  a = max(pb,pc)+x[0]
  b = max(pc,pa)+x[1]
  c = max(pa,pb)+x[2]
  
print(max(a,b,c))

Submission status: Runtime Error

Solution code:
N = int(input())
Happiness = [sorted([int(i) for i in input().split()]) for j in range(N)]

ans1 = ans2 = 0
for move, choice1, choice2 in zip(Happiness, cycle([1, 2]), cycle([2, 1])):
    ans1 += move[choice1]
    ans2 += move[choice2]

print(max(ans1, ans2))

Submission status: Runtime Error

Solution code:
mport sys
def 解():
    input = sys.stdin.readline
    N = int(input())
    dp = [int(_) for  _ in input().split()]
    for _ in range(N-1):
        a,b,c = [int(_) for  _ in input().split()]
        dp = [max(dp[1]+a,dp[2]+a),max(dp[0]+b,dp[2]+b),max(dp[0]+c,dp[1]+c)]
    print(max(dp))
解()

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
#!/mnt/c/Users/moiki/bash/env/bin/python
N = int(input())
ilist = [ tuple(zip(tuple(map(int, input().split())), ("a","b","c"))) for i in range(N)]

# print(ilist)
suml = [(0,"z")]
for i in range(1,N+1):
    l = (ilist[i-1])
    l = sorted(l, reverse=True)
    # print("list: ", l)
    if suml[i-1][1] != l[0][1]:
        # suml[i] = suml[i-1] + l[0][0]
        if (i+1)<=N and sorted(ilist[i+1], reverse=True)[0][1] == l[0][1]:
            # print("\tinside")
            l_n = sorted(ilist[i+1], reverse=True)# [0][1]
            if (l[0][0] + l_n[1][0]) < (l[1][0] + l_n[0][0]):

                suml.append(( suml[i-1][0]+l[1][0], l[1][1]  ))
            else:
                suml.append(( suml[i-1][0]+l[0][0], l[0][1]  ))
        else:
            suml.append(( suml[i-1][0]+l[0][0], l[0][1]  ))

        # // l[0][1] == sorted(ilist[i+1], reverse=True)[0][1]  \
        #  and l[0][0] + sorted(ilist[i+1], reverse=True)[0][1]
    else:
        # suml[i] = suml[i-1] + l[1][0]
        suml.append(( suml[i-1][0]+l[1][0], l[1][1]  ))

# print(suml)
print(suml[-1][0])