CF1637F.Towers

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a tree with nn vertices numbered from 11 to nn . The height of the ii -th vertex is hih_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 ee costs ee coins, where e>0e > 0 .

It is considered that a vertex xx gets a signal if for some pair of towers at the vertices uu and vv ( uvu \neq v , but it is allowed that x=ux = u or x=vx = v ) with efficiencies eue_u and eve_v , respectively, it is satisfied that min(eu,ev)hx\min(e_u, e_v) \geq h_x and xx lies on the path between uu and vv .

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 nn ( 2n2000002 \le n \le 200\,000 ) — the number of vertices in the tree.

The second line contains nn integers hih_i ( 1hi1091 \le h_i \le 10^9 ) — the heights of the vertices.

Each of the next n1n - 1 lines contain a pair of numbers vi,uiv_i, u_i ( 1vi,uin1 \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 22 at vertices 11 and 33 .

In the second test case it's optimal to install a tower with efficiency 11 at vertex 11 and two towers with efficiencies 33 at vertices 22 and 55 .

In the third test case it's optimal to install two towers with efficiencies 66 at vertices 11 and 22 .

首页