A14961 | Towers
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
You are given a tree with $n$ vertices numbered from $1$ to $n$ . The height of the $i$ -th vertex is $h_i$ . You can place any number of towers into vertices, for each tower you can choose which vertex to put it in, as well as choose its efficiency. Setting up a tower with efficiency $e$ costs $e$ coins, where $e > 0$ .
It is considered that a vertex $x$ gets a signal if for some pair of towers at the vertices $u$ and $v$ ( $u \neq v$ , but it is allowed that $x = u$ or $x = v$ ) with efficiencies $e_u$ and $e_v$ , respectively, it is satisfied that $\min(e_u, e_v) \geq h_x$ and $x$ lies on the path between $u$ and $v$ .
Find the minimum number of coins required to set up towers so that you can get a signal at all vertices.
It is considered that a vertex $x$ gets a signal if for some pair of towers at the vertices $u$ and $v$ ( $u \neq v$ , but it is allowed that $x = u$ or $x = v$ ) with efficiencies $e_u$ and $e_v$ , respectively, it is satisfied that $\min(e_u, e_v) \geq h_x$ and $x$ lies on the path between $u$ and $v$ .
Find the minimum number of coins required to set up towers so that you can get a signal at all vertices.
输入格式
The first line contains an integer $n$ ( $2 \le n \le 200\,000$ ) — the number of vertices in the tree.
The second line contains $n$ integers $h_i$ ( $1 \le h_i \le 10^9$ ) — the heights of the vertices.
Each of the next $n - 1$ lines contain a pair of numbers $v_i, u_i$ ( $1 \le v_i, u_i \le n$ ) — an edge of the tree. It is guaranteed that the given edges form a tree.
The second line contains $n$ integers $h_i$ ( $1 \le h_i \le 10^9$ ) — the heights of the vertices.
Each of the next $n - 1$ lines contain a pair of numbers $v_i, u_i$ ( $1 \le v_i, u_i \le n$ ) — an edge of the tree. It is guaranteed that the given edges form a tree.
输出格式
Print one integer — the minimum required number of coins.
输入输出样例
输入 #1
3 1 2 1 1 2 2 3
输出 #1
4
输入 #2
5 1 3 3 1 3 1 3 5 4 4 3 2 3
输出 #2
7
输入 #3
2 6 1 1 2
输出 #3
12
In the first test case it's optimal to install two towers with efficiencies $2$ at vertices $1$ and $3$ .
In the second test case it's optimal to install a tower with efficiency $1$ at vertex $1$ and two towers with efficiencies $3$ at vertices $2$ and $5$ .
In the third test case it's optimal to install two towers with efficiencies $6$ at vertices $1$ and $2$ .
In the second test case it's optimal to install a tower with efficiency $1$ at vertex $1$ and two towers with efficiencies $3$ at vertices $2$ and $5$ .
In the third test case it's optimal to install two towers with efficiencies $6$ at vertices $1$ and $2$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted