CF1394D.Boboniu and Jianghu
普及/提高-
通过率:0%
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 n mountains. He uses n−1 roads to connect all n mountains. Every pair of mountains is connected via roads.
For the i -th mountain, Boboniu estimated the tiredness of doing Kungfu on the top of it as ti . He also estimated the height of each mountain as hi .
A path is a sequence of mountains M such that for each i ( 1≤i<∣M∣ ), there exists a road between Mi and Mi+1 . Boboniu would regard the path as a challenge if for each i ( 1≤i<∣M∣ ), hMi≤hMi+1 .
Boboniu wants to divide all n−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 M is the sum of tiredness of all mountains in it, i.e. ∑i=1∣M∣tMi .
He asked you to find the minimum total tiredness. As a reward for your work, you'll become a guardian in his Jianghu.
输入格式
The first line contains a single integer n ( 2≤n≤2⋅105 ), denoting the number of the mountains.
The second line contains n integers t1,t2,…,tn ( 1≤ti≤106 ), denoting the tiredness for Boboniu to do Kungfu on each mountain.
The third line contains n integers h1,h2,…,hn ( 1≤hi≤106 ), denoting the height of each mountain.
Each of the following n−1 lines contains two integers ui , vi ( 1≤ui,vi≤n,ui=vi ), denoting the ends of the road. It's guaranteed that all mountains are connected via roads.
输出格式
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 1 : 3→1→2
- Challenge 2 : 5→2→4
The total tiredness of Boboniu is (30+40+10)+(20+10+50)=160 . It can be shown that this is the minimum total tiredness.