提出 #67891641
ソースコード 拡げる
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 |
| ユーザ |
sounansya |
| 言語 |
Python (PyPy 3.10-v7.3.12) |
| 得点 |
525 |
| コード長 |
1587 Byte |
| 結果 |
AC |
| 実行時間 |
1680 ms |
| メモリ |
580940 KiB |
ジャッジ結果
| セット名 |
Sample |
All |
| 得点 / 配点 |
0 / 0 |
525 / 525 |
| 結果 |
|
|
| セット名 |
テストケース |
| 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 |
56 ms |
76424 KiB |
| 00_sample_01.txt |
AC |
60 ms |
76196 KiB |
| 00_sample_02.txt |
AC |
58 ms |
76380 KiB |
| 01_handmade_00.txt |
AC |
55 ms |
76064 KiB |
| 01_handmade_01.txt |
AC |
55 ms |
76452 KiB |
| 02_random_00.txt |
AC |
178 ms |
87896 KiB |
| 02_random_01.txt |
AC |
311 ms |
112156 KiB |
| 02_random_02.txt |
AC |
212 ms |
91496 KiB |
| 02_random_03.txt |
AC |
154 ms |
92324 KiB |
| 02_random_04.txt |
AC |
398 ms |
113004 KiB |
| 02_random_05.txt |
AC |
291 ms |
118900 KiB |
| 02_random_06.txt |
AC |
297 ms |
118844 KiB |
| 02_random_07.txt |
AC |
453 ms |
118812 KiB |
| 02_random_08.txt |
AC |
477 ms |
119004 KiB |
| 02_random_09.txt |
AC |
457 ms |
118968 KiB |
| 02_random_10.txt |
AC |
355 ms |
118836 KiB |
| 02_random_11.txt |
AC |
342 ms |
118848 KiB |
| 02_random_12.txt |
AC |
1401 ms |
490532 KiB |
| 02_random_13.txt |
AC |
1680 ms |
580940 KiB |
| 02_random_14.txt |
AC |
1387 ms |
453732 KiB |
| 02_random_15.txt |
AC |
1391 ms |
438752 KiB |
| 02_random_16.txt |
AC |
1508 ms |
475844 KiB |
| 02_random_17.txt |
AC |
1519 ms |
474076 KiB |
| 02_random_18.txt |
AC |
420 ms |
119032 KiB |
| 02_random_19.txt |
AC |
437 ms |
119016 KiB |