CF1394D.Boboniu and Jianghu

省选/NOI-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Since Boboniu finished building his Jianghu, he has been doing Kungfu on these mountains every day.

Boboniu designs a map for his nn mountains. He uses n−1n-1 roads to connect all nn mountains. Every pair of mountains is connected via roads.

For the ii-th mountain, Boboniu estimated the tiredness of doing Kungfu on the top of it as tit_i. He also estimated the height of each mountain as hih_i.

A path is a sequence of mountains MM such that for each ii (1≤i<∣M∣1 \le i \lt |M|), there exists a road between MiM_i and Mi+1M_{i+1}. Boboniu would regard the path as a challenge if for each ii (1≤i<∣M∣1\le i \lt |M|), hMi≤hMi+1h_{M_i}\le h_{M_{i+1}}.

Boboniu wants to divide all n−1n-1 roads into several challenges. Note that each road must appear in exactly one challenge, but a mountain may appear in several challenges.

Boboniu wants to minimize the total tiredness to do all the challenges. The tiredness of a challenge MM is the sum of tiredness of all mountains in it, i.e. ∑i=1∣M∣tMi\sum_{i=1}^{|M|}t_{M_i}.

He asked you to find the minimum total tiredness. As a reward for your work, you'll become a guardian in his Jianghu.

自从鲍伯牛建好他的江湖后,他每天都在这些山上练习功夫。

鲍伯牛为他的 nn 座山设计了一张地图。他用 n−1n-1 条道路将全部 nn 座山连通,使得任意两座山之间都可通过道路相互到达。

对于第 ii 座山,鲍伯牛估算了在其山顶练功的疲劳值 tit_i;同时,他也估算了每座山的高度 hih_i。

一条路径是一个山的序列 MM,满足:对每个 ii(1≤i<∣M∣1 \le i < |M|),MiM_i 与 Mi+1M_{i+1} 之间存在一条道路。若对每个 ii(1≤i<∣M∣1 \le i < |M|)均有 hMi≤hMi+1h_{M_i} \le h_{M_{i+1}},则鲍伯牛将该路径视为一项挑战。

鲍伯牛希望将全部 n−1n-1 条道路划分成若干项挑战。注意:每条道路必须且仅能出现在一项挑战中,但一座山可以出现在多项挑战中。

鲍伯牛希望最小化完成所有挑战所需的总疲劳值。一项挑战 MM 的疲劳值定义为其中所有山的疲劳值之和,即 ∑i=1∣M∣tMi\sum_{i=1}^{|M|}t_{M_i}。

他请你求出最小总疲劳值。作为对你工作的嘉奖,你将成为他江湖中的一名守护者。

输入格式

The first line contains a single integer nn (2≤n≤2⋅1052 \le n \le 2 \cdot 10^5), denoting the number of the mountains.

The second line contains nn integers t1,t2,…,tnt_1, t_2, \ldots, t_n (1≤ti≤1061 \le t_i \le 10^6), denoting the tiredness for Boboniu to do Kungfu on each mountain.

The third line contains nn integers h1,h2,…,hnh_1, h_2, \ldots, h_n (1≤hi≤1061 \le h_i \le 10^6), denoting the height of each mountain.

Each of the following n−1n - 1 lines contains two integers uiu_i, viv_i (1≤ui,vi≤n,ui≠vi1 \le u_i, v_i \le n, u_i \neq v_i), denoting the ends of the road. It's guaranteed that all mountains are connected via roads.

第一行包含一个整数 nn(2≤n≤2⋅1052 \le n \le 2 \cdot 10^5),表示山的数量。

第二行包含 nn 个整数 t1,t2,…,tnt_1, t_2, \ldots, t_n(1≤ti≤1061 \le t_i \le 10^6),表示 Boboniu 在每座山上练习功夫所消耗的疲劳值。

第三行包含 nn 个整数 h1,h2,…,hnh_1, h_2, \ldots, h_n(1≤hi≤1061 \le h_i \le 10^6),表示每座山的高度。

接下来的 n−1n - 1 行中,每行包含两个整数 uiu_i、viv_i(1≤ui,vi≤n1 \le u_i, v_i \le n,且 ui≠viu_i \neq v_i),表示一条道路的两个端点。保证所有山通过道路相互连通。

输出格式

Print one integer: the smallest sum of tiredness of all challenges.

输出一个整数:所有挑战的疲劳度总和的最小值。

输入输出样例

  • 输入#1

    5
    40 10 30 50 20
    2 3 2 3 1
    1 2
    1 3
    2 4
    2 5

    输出#1

    160
  • 输入#2

    5
    1000000 1 1 1 1
    1000000 1 1 1 1
    1 2
    1 3
    1 4
    1 5

    输出#2

    4000004
  • 输入#3

    10
    510916 760492 684704 32545 484888 933975 116895 77095 127679 989957
    402815 705067 705067 705067 623759 103335 749243 138306 138306 844737
    1 2
    3 2
    4 3
    1 5
    6 4
    6 7
    8 7
    8 9
    9 10

    输出#3

    6390572

说明/提示

For the first example:

In the picture, the lighter a point is, the higher the mountain it represents. One of the best divisions is:

  • Challenge 11: 3→1→23 \to 1 \to 2
  • Challenge 22: 5→2→45 \to 2 \to 4

The total tiredness of Boboniu is (30+40+10)+(20+10+50)=160(30 + 40 + 10) + (20 + 10 + 50) = 160. It can be shown that this is the minimum total tiredness.

对于第一个样例:

图中,一个点越亮,代表其对应的山越高。其中一种最优划分方式为:

  • 挑战 11:3→1→23 \to 1 \to 2
  • 挑战 22:5→2→45 \to 2 \to 4

鲍伯牛的总疲劳值为 (30+40+10)+(20+10+50)=160(30 + 40 + 10) + (20 + 10 + 50) = 160。可以证明这是最小的总疲劳值。

输入解题思路,AI测评打分。不知道怎么写?

首页