結果

問題 No.1221 木 *= 3
ユーザー marroncastlemarroncastle
提出日時 2020-09-13 04:09:57
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 463 ms / 2,000 ms
コード長 643 bytes
コンパイル時間 495 ms
コンパイル使用メモリ 10,872 KB
実行使用メモリ 52,300 KB
最終ジャッジ日時 2023-08-30 22:13:10
合計ジャッジ時間 8,373 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
7,868 KB
testcase_01 AC 15 ms
7,852 KB
testcase_02 AC 15 ms
7,940 KB
testcase_03 AC 15 ms
7,868 KB
testcase_04 AC 15 ms
7,812 KB
testcase_05 AC 14 ms
7,812 KB
testcase_06 AC 15 ms
7,948 KB
testcase_07 AC 458 ms
52,300 KB
testcase_08 AC 450 ms
51,340 KB
testcase_09 AC 463 ms
51,972 KB
testcase_10 AC 461 ms
51,572 KB
testcase_11 AC 463 ms
51,656 KB
testcase_12 AC 385 ms
33,580 KB
testcase_13 AC 374 ms
33,424 KB
testcase_14 AC 391 ms
33,500 KB
testcase_15 AC 391 ms
33,492 KB
testcase_16 AC 384 ms
33,464 KB
testcase_17 AC 414 ms
33,096 KB
testcase_18 AC 410 ms
32,884 KB
testcase_19 AC 416 ms
33,020 KB
testcase_20 AC 415 ms
32,948 KB
testcase_21 AC 417 ms
32,956 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(10**6)
input = sys.stdin.readline

N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
edge = [[] for _ in range(N)]
for i in range(N-1):
  a,b = map(int, input().split())
  edge[a-1].append(b-1)
  edge[b-1].append(a-1)

def dfs(v, p):
  if p>=0 and len(edge[v])==1:
    offs, ons = max(A[v],0), max(A[v],B[v]+B[p])
    return offs, ons 
  offs, ons = A[v],0
  for u in edge[v]:
    if u!=p:
      off, on = dfs(u, v)
      offs += off
      ons += on
  if p==-1:
    return offs, ons
  return max(offs,ons),max(offs,ons+B[v]+B[p])

off, on = dfs(0, -1)
print(max(off,on))
0