問題一覧 > 通常問題

No.1215 City Annihilation Beam

レベル : / 実行時間制限 : 1ケース 6.000秒 / メモリ制限 : 512 MB / 標準ジャッジ問題
タグ : (AC するまで非表示) / 解いたユーザー数 8
作問者 : Thistle / テスター : autumn-eel
お気に入りにしたユーザー ProblemId : 4896 / 自分の提出
問題文最終更新日: 2020-08-30 14:14:38
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Back to the original
技術室奥プログラミングコンテスト#5 Day2 (順位表) の他の問題:

Problem Statement

There is a country with a tree structure, and the $N$ cities that make up the country are numbered $1, 2, \dots , N$.
The cities are connected by bidirectional highways, and the $i$-th highway connects city $A_i$ and city $B_i$.

This country has $K$ temples; the $i$-th temple is located in city $C_i$ and its value is $D_i$.

The god worshipped by this country is more pleased the larger the value $X$ defined by the following procedure is.

  • Let $S$ be the sum of the values of the temples.
  • Viewing the country as a tree rooted at city $1$, let city $T$ be the LCA of all cities that have a temple.
  • $X$ is $S$ $+$ (the minimum number of roads that must be traversed on the way from city $T$ to city $1$).
  • However, if no temples exist, $X$ is $-10^{10^{10}}$.

One day, Paken-kun, who lives in this country, resolved to make $X$ as large as possible for the sake of the god.
Fortunately, he can perform the following operation at most $1$ time:

  • Choose integers $l$ , $r$ ($1$ $\leq$ $l$ $\leq$ $r$ $\leq$ $K$) and annihilate with a beam all temples whose number (note that this is not the city number) is between $l$ and $r$, inclusive.


Since it was difficult for Paken-kun to figure out the most efficient way to fire the beam, he asked you to find the maximum $X$ achievable by the possible operations.

However, you felt pangs of conscience about annihilating temples, so you decided to compute $X$ for each of all $\frac{K(K+1)}{2}+1$ ways of operating, and then tell Paken-kun the median of these values.
Here, the median refers to the $\left\lfloor \frac{\left(\frac{K(K+1)}{2}+1\right)+1}{2}\right\rfloor$-th smallest value among the values of $X$ achievable by all the ways of operating.

Input

$N\ K$
$C_1\ C_2\ \dots\ C_K$
$D_1\ D_2\ \dots\ D_K$
$A_1\ B_1$
$A_2\ B_2$
$\dots$
$A_{N-1}\ B_{N-1}$

  • $2\leq K\leq N\leq 10^5$
  • $1\leq C_i\leq N (1\leq i\leq K)$
  • $C_i\neq C_j (1\leq i < j\leq K)$
  • $-10^9\leq D_i\leq 10^9 (1\leq i\leq K)$
  • $1\leq A_i,B_i\leq N (1\leq i\leq N-1)$
  • All inputs are integers.
  • The graph given in the input is a tree.

Output

Find the median of $X$. Print a newline at the end.

Sample

Sample 1
Input
3 2
2 3
1 1
1 2
2 3
Output
2

The costs for all $4$ ways are $-10^{10^{10}}, 2, 3, 3$, and the median is $2$.

Sample 2
Input
7 3
4 5 7
1 10 100
1 2
2 3
3 4
3 5
1 6
6 7
Output
101

The costs for all $7$ ways are $-10^{10^{10}}, 4, 13, 101, 102, 110, 111$, and the median is $101$.

提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。