結果

問題 No.1154 シュークリームゲーム(Hard)
ユーザー Kiri8128Kiri8128
提出日時 2020-07-01 01:22:09
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 41 ms / 2,000 ms
コード長 1,370 bytes
コンパイル時間 99 ms
コンパイル使用メモリ 10,916 KB
実行使用メモリ 12,884 KB
最終ジャッジ日時 2023-10-11 17:07:11
合計ジャッジ時間 3,508 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
8,608 KB
testcase_01 AC 21 ms
8,700 KB
testcase_02 AC 19 ms
8,568 KB
testcase_03 AC 19 ms
8,612 KB
testcase_04 AC 19 ms
8,620 KB
testcase_05 AC 19 ms
8,628 KB
testcase_06 AC 19 ms
8,564 KB
testcase_07 AC 19 ms
8,704 KB
testcase_08 AC 20 ms
8,684 KB
testcase_09 AC 19 ms
8,728 KB
testcase_10 AC 19 ms
8,688 KB
testcase_11 AC 19 ms
8,724 KB
testcase_12 AC 20 ms
8,632 KB
testcase_13 AC 20 ms
8,720 KB
testcase_14 AC 19 ms
8,740 KB
testcase_15 AC 19 ms
8,716 KB
testcase_16 AC 19 ms
8,640 KB
testcase_17 AC 26 ms
8,944 KB
testcase_18 AC 27 ms
8,940 KB
testcase_19 AC 25 ms
8,968 KB
testcase_20 AC 26 ms
9,128 KB
testcase_21 AC 25 ms
8,952 KB
testcase_22 AC 26 ms
8,948 KB
testcase_23 AC 26 ms
9,048 KB
testcase_24 AC 25 ms
8,948 KB
testcase_25 AC 25 ms
9,064 KB
testcase_26 AC 25 ms
8,944 KB
testcase_27 AC 25 ms
9,036 KB
testcase_28 AC 25 ms
9,116 KB
testcase_29 AC 25 ms
9,056 KB
testcase_30 AC 25 ms
9,040 KB
testcase_31 AC 25 ms
8,972 KB
testcase_32 AC 25 ms
8,968 KB
testcase_33 AC 26 ms
8,984 KB
testcase_34 AC 25 ms
9,052 KB
testcase_35 AC 26 ms
8,944 KB
testcase_36 AC 25 ms
9,128 KB
testcase_37 AC 25 ms
9,012 KB
testcase_38 AC 19 ms
8,604 KB
testcase_39 AC 19 ms
8,692 KB
testcase_40 AC 41 ms
12,884 KB
testcase_41 AC 25 ms
8,956 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
N = int(input())
A = [int(a) for a in input().split()]
E = [[] for _ in range(N)]
for _ in range(N - 1):
    a, b = map(int, input().split())
    E[a-1].append(b-1)
    E[b-1].append(a-1)

X = [[] for i in range(N)]
P = [-1] * N
Q = deque([0])
R = []
while Q:
    i = deque.popleft(Q)
    R.append(i)
    for a in E[i]:
        if a != P[i]:
            P[a] = i
            E[a].remove(i)
            deque.append(Q, a)

C = [[] for _ in range(N)]
    
def calclist(p, L):
    L = sorted(L)
    while L and L[-1] > p:
        p -= L.pop()
        if L:
            if L[-1] > - (1 << 50):
                p += L.pop()
            else:
                while L:
                    p += L.pop() + ((1 << 50) if p < - (1 << 50) else 0)
        else:
            p -= 1 << 50
            return [p]
    return [p] + L

def summ(L):
    p = 0
    L = sorted(L)
    while L:
        if L[-1] > - (1 << 50):
            p += L.pop()
            if L:
                if L[-1] > - (1 << 50):
                    p -= L.pop()
                else:
                    p -= sum([a + (1 << 50) for a in L])
                    L = []
        else:
            p += sum([a + (1 << 50) for a in L])
            L = []
    return p

for i in R[::-1]:
    t = calclist(A[i], C[i])
    if P[i] >= 0:
        C[P[i]] += t
    else:
        print(summ(t))
0