Submission #64557828


Source Code Expand

Copy
inf = 1 << 60
n = int(input())
c = list(map(int, input().split()))
x = list(map(int, input().split()))
C = c + c
ans=inf
for s in range(n):
goal = C[s : s + n]
dp = [[inf] * n for _ in range(n)]
for i in range(n):
dp[i][i] = 1 + x[goal[i] - 1]
for i in range(2, n + 1):
for l in range(0, n - i + 1):
r = l + i - 1
dp[l][r] = min(dp[l][r],dp[l][r - 1] + 1 + x[goal[r] - 1])
for k in range(l, r):
if goal[k] == goal[r]:
c = dp[l][k]+r-k
if k + 1 < r:
c += dp[k + 1][r - 1]
if c < dp[l][r]:
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
inf = 1 << 60
n = int(input())
c = list(map(int, input().split()))
x = list(map(int, input().split()))
C = c + c
ans=inf
for s in range(n):
    goal = C[s : s + n]
    dp = [[inf] * n for _ in range(n)]
    for i in range(n):
        dp[i][i] = 1 + x[goal[i] - 1]
    for i in range(2, n + 1):
        for l in range(0, n - i + 1):
            r = l + i - 1
            dp[l][r] = min(dp[l][r],dp[l][r - 1] + 1 + x[goal[r] - 1])
            for k in range(l, r):
                if goal[k] == goal[r]:
                    c = dp[l][k]+r-k
                    if k + 1 < r:
                        c += dp[k + 1][r - 1]
                    if c < dp[l][r]:
                        dp[l][r] = c
    ans=min(dp[0][-1],ans)
print(ans)

Submission Info

Submission Time
Task F - Happy Birthday! 3
User juten
Language Python (PyPy 3.10-v7.3.12)
Score 0
Code Size 753 Byte
Status TLE
Exec Time 3318 ms
Memory 143224 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 550
Status
AC × 3
AC × 14
TLE × 31
Set Name Test Cases
Sample sample00.txt, sample01.txt, sample02.txt
All sample00.txt, sample01.txt, sample02.txt, testcase00.txt, testcase01.txt, testcase02.txt, testcase03.txt, testcase04.txt, testcase05.txt, testcase06.txt, testcase07.txt, testcase08.txt, testcase09.txt, testcase10.txt, testcase11.txt, testcase12.txt, testcase13.txt, testcase14.txt, testcase15.txt, testcase16.txt, testcase17.txt, testcase18.txt, testcase19.txt, testcase20.txt, testcase21.txt, testcase22.txt, testcase23.txt, testcase24.txt, testcase25.txt, testcase26.txt, testcase27.txt, testcase28.txt, testcase29.txt, testcase30.txt, testcase31.txt, testcase32.txt, testcase33.txt, testcase34.txt, testcase35.txt, testcase36.txt, testcase37.txt, testcase38.txt, testcase39.txt, testcase40.txt, testcase41.txt
Case Name Status Exec Time Memory
sample00.txt AC 56 ms 76472 KB
sample01.txt AC 56 ms 76708 KB
sample02.txt AC 57 ms 76508 KB
testcase00.txt TLE 3314 ms 87780 KB
testcase01.txt TLE 3315 ms 87808 KB
testcase02.txt TLE 3315 ms 88172 KB
testcase03.txt TLE 3315 ms 88240 KB
testcase04.txt TLE 3315 ms 88528 KB
testcase05.txt TLE 3315 ms 88240 KB
testcase06.txt TLE 3315 ms 89316 KB
testcase07.txt TLE 3315 ms 88156 KB
testcase08.txt TLE 3315 ms 88804 KB
testcase09.txt TLE 3314 ms 88112 KB
testcase10.txt TLE 3315 ms 88300 KB
testcase11.txt TLE 3315 ms 87904 KB
testcase12.txt AC 359 ms 83532 KB
testcase13.txt TLE 3315 ms 87600 KB
testcase14.txt TLE 3315 ms 89996 KB
testcase15.txt TLE 3315 ms 87596 KB
testcase16.txt TLE 3315 ms 92144 KB
testcase17.txt TLE 3317 ms 87264 KB
testcase18.txt AC 492 ms 83568 KB
testcase19.txt TLE 3315 ms 87956 KB
testcase20.txt AC 205 ms 83632 KB
testcase21.txt TLE 3315 ms 87732 KB
testcase22.txt TLE 3315 ms 91840 KB
testcase23.txt TLE 3315 ms 88576 KB
testcase24.txt AC 2038 ms 90644 KB
testcase25.txt TLE 3315 ms 87636 KB
testcase26.txt TLE 3315 ms 90812 KB
testcase27.txt TLE 3315 ms 87616 KB
testcase28.txt AC 280 ms 83920 KB
testcase29.txt TLE 3315 ms 88272 KB
testcase30.txt AC 152 ms 83648 KB
testcase31.txt TLE 3317 ms 140716 KB
testcase32.txt AC 2421 ms 114728 KB
testcase33.txt TLE 3318 ms 90824 KB
testcase34.txt AC 88 ms 82816 KB
testcase35.txt TLE 3315 ms 89828 KB
testcase36.txt AC 1041 ms 92316 KB
testcase37.txt TLE 3318 ms 143224 KB
testcase38.txt AC 2197 ms 111772 KB
testcase39.txt TLE 3315 ms 90156 KB
testcase40.txt AC 704 ms 88672 KB
testcase41.txt TLE 3315 ms 88408 KB


2025-04-07 (Mon)
15:45:20 +09:00