提出 #68015518


ソースコード 拡げる

import pypyjit
pypyjit.set_param('max_unroll_recursion=-1')
import sys
sys.setrecursionlimit(2 * 10**7)
input = sys.stdin.readline
n, k = map(int, input().split())
a = list(map(int, input().split()))
g = [[] for _ in range(n)]
for _ in range(n - 1):
    u, v = map(int, input().split())
    g[u - 1].append(v - 1)
    g[v - 1].append(u - 1)
INF = 10**18
def f(to, fr):
    d0 = [-INF] * (k + 1)
    d1 = [-INF] * (k + 1)
    d2 = [-INF] * (k + 1)
    d0[0] = 0
    for x in g[to]:
        if x == fr:
            continue
        new_d0 = [-INF] * (k + 1)
        new_d1 = [-INF] * (k + 1)
        new_d2 = [-INF] * (k + 1)
        di0, di1 = f(x, to)
        for i in range(k + 1):
            for j in range(k + 1):
                if i + j <= k:
                    new_d0[i + j] = max(new_d0[i + j], d0[i] + di0[j])
                    new_d1[i + j] = max(new_d1[i + j], d0[i] + di1[j] + a[to])
                    new_d1[i + j] = max(new_d1[i + j], d1[i] + di0[j])
                    new_d2[i + j] = max(new_d2[i + j], d2[i] + di0[j])
                    # new_d1[i + j] = max(new_d1[i + j], d1[i] + di1[j])
                    new_d2[i + j] = max(new_d2[i + j], d2[i] + di1[j])
                if 0 <= i + j - 1 <= k:
                    new_d2[i + j - 1] = max(new_d2[i + j - 1], d1[i] + di1[j])
        d0, d1, d2 = new_d0, new_d1, new_d2
    for i in range(1, k + 1):
        d1[i] = max(d1[i], d0[i - 1] + a[to])
    for i in range(k + 1):
        d0[i] = max(d0[i], d1[i], d2[i])
    return d0, d1
d0, d1 = f(0, -1)
print(max(d0))

提出情報

提出日時
問題 F - Paint Tree 2
ユーザ igeee
言語 Python (PyPy 3.10-v7.3.12)
得点 525
コード長 1585 Byte
結果 AC
実行時間 1672 ms
メモリ 581176 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 525 / 525
結果
AC × 3
AC × 25
セット名 テストケース
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_handmade_00.txt, 01_handmade_01.txt, 02_random_00.txt, 02_random_01.txt, 02_random_02.txt, 02_random_03.txt, 02_random_04.txt, 02_random_05.txt, 02_random_06.txt, 02_random_07.txt, 02_random_08.txt, 02_random_09.txt, 02_random_10.txt, 02_random_11.txt, 02_random_12.txt, 02_random_13.txt, 02_random_14.txt, 02_random_15.txt, 02_random_16.txt, 02_random_17.txt, 02_random_18.txt, 02_random_19.txt
ケース名 結果 実行時間 メモリ
00_sample_00.txt AC 59 ms 76388 KiB
00_sample_01.txt AC 59 ms 76060 KiB
00_sample_02.txt AC 60 ms 76504 KiB
01_handmade_00.txt AC 60 ms 76124 KiB
01_handmade_01.txt AC 59 ms 76312 KiB
02_random_00.txt AC 187 ms 88104 KiB
02_random_01.txt AC 382 ms 112564 KiB
02_random_02.txt AC 222 ms 91456 KiB
02_random_03.txt AC 178 ms 92396 KiB
02_random_04.txt AC 446 ms 113088 KiB
02_random_05.txt AC 387 ms 119212 KiB
02_random_06.txt AC 394 ms 119104 KiB
02_random_07.txt AC 512 ms 119056 KiB
02_random_08.txt AC 518 ms 119008 KiB
02_random_09.txt AC 503 ms 119332 KiB
02_random_10.txt AC 369 ms 119196 KiB
02_random_11.txt AC 383 ms 119332 KiB
02_random_12.txt AC 1468 ms 490256 KiB
02_random_13.txt AC 1672 ms 581176 KiB
02_random_14.txt AC 1434 ms 460688 KiB
02_random_15.txt AC 1447 ms 445472 KiB
02_random_16.txt AC 1536 ms 481656 KiB
02_random_17.txt AC 1575 ms 477796 KiB
02_random_18.txt AC 414 ms 119204 KiB
02_random_19.txt AC 508 ms 119352 KiB