Logo
(追記) (追記ここまで)

34272번 - Ornaments on a Tree 다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
4 초 2048 MB20151487.500%

문제

You're helping your friend decorate their Christmas tree! Funnily enough, the places to put your ornaments on their Christmas tree can be represented by a (graph-theoretic) tree with nodes labeled 1ドル$ to $N,ドル with node 1ドル$ being the root of the tree and other nodes numbered arbitrarily. You have an infinite supply of ornaments of every non-negative integer weight (including 0ドル$), and you must place exactly one ornament on each node of the tree.

However, your friend has some restrictions on how they want their tree decorated. First, they have strong opinions about which ornament must go on some of the tree nodes; you are only allowed to choose decorations on the other nodes. Second, each region of their tree can support only so much weight: if the sum of the weights of the ornaments on a node and all of its immediate children exceeds a constant $K,ドル the whole tree will come crashing down!

Your friend wants to know the largest possible total weight of ornaments on their tree, given the above restrictions. Can you help them find out?

입력

The first line of input has two space-separated integers $N$ and $K$ (1ドル \leq N \leq 2 \cdot 10^5, 0 \leq K \leq 10^9$), the number of nodes in the tree and the weight constant, respectively.

The next line contains $N$ space-separated integers. The $i^\text{th}$ integer (starting at $i=1$) is either $-1$ or a non-negative integer. If it is $-1,ドル you are free to choose any ornament for node $i$. If it is a non-negative integer $w_i$ (0ドル \leq w_i \leq 10^9$), your friend insists you place an ornament with weight $w_i$ on node $i$.

The next $N-1$ lines each contain two space-separated integers $a$ and $b$ (1ドル \leq a, b \leq N$), indicating that nodes $a$ and $b$ are connected by an edge. The input graph is guaranteed to be a tree: there is a unique path of edges between every pair of nodes in the graph.

출력

If it is impossible to place ornaments on the tree in a way that satisfies all of the constraints described above, print $-1$. Otherwise, print the maximum possible total weight of the ornaments on the tree, subject to the constraints.

제한

예제 입력 1

5 10
-1 2 3 -1 -1
1 2
1 3
2 4
2 5

예제 출력 1

18

예제 입력 2

1 5
-1

예제 출력 2

5

예제 입력 3

2 5
5 5
1 2

예제 출력 3

-1

힌트

출처

ICPC > Regionals > North America > North America Championship > North America Championship 2025 H번

  • 문제를 만든 사람: Omkar Bhalerao
(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /