CF1061E.Politics
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
There are n cities in the country.
Two candidates are fighting for the post of the President. The elections are set in the future, and both candidates have already planned how they are going to connect the cities with roads. Both plans will connect all cities using n−1 roads only. That is, each plan can be viewed as a tree. Both of the candidates had also specified their choice of the capital among n cities ( x for the first candidate and y for the second candidate), which may or may not be same.
Each city has a potential of building a port (one city can have at most one port). Building a port in i -th city brings ai amount of money. However, each candidate has his specific demands. The demands are of the form:
- k x , which means that the candidate wants to build exactly x ports in the subtree of the k -th city of his tree (the tree is rooted at the capital of his choice).
Find out the maximum revenue that can be gained while fulfilling all demands of both candidates, or print -1 if it is not possible to do.
It is additionally guaranteed, that each candidate has specified the port demands for the capital of his choice.
输入格式
The first line contains integers n , x and y ( 1≤n≤500 , 1≤x,y≤n ) — the number of cities, the capital of the first candidate and the capital of the second candidate respectively.
Next line contains integers a1,a2,…,an ( 1≤ai≤100000 ) — the revenue gained if the port is constructed in the corresponding city.
Each of the next n−1 lines contains integers ui and vi ( 1≤ui,vi≤n , ui=vi ), denoting edges between cities in the tree of the first candidate.
Each of the next n−1 lines contains integers ui′ and vi′ ( 1≤ui′,vi′≤n , ui′=vi′ ), denoting edges between cities in the tree of the second candidate.
Next line contains an integer q1 ( 1≤q1≤n ), denoting the number of demands of the first candidate.
Each of the next q1 lines contains two integers k and x ( 1≤k≤n , 1≤x≤n ) — the city number and the number of ports in its subtree.
Next line contains an integer q2 ( 1≤q2≤n ), denoting the number of demands of the second candidate.
Each of the next q2 lines contain two integers k and x ( 1≤k≤n , 1≤x≤n ) — the city number and the number of ports in its subtree.
It is guaranteed, that given edges correspond to valid trees, each candidate has given demand about each city at most once and that each candidate has specified the port demands for the capital of his choice. That is, the city x is always given in demands of the first candidate and city y is always given in the demands of the second candidate.
输出格式
Print exactly one integer — the maximum possible revenue that can be gained, while satisfying demands of both candidates, or -1 if it is not possible to satisfy all of the demands.
输入输出样例
输入#1
4 1 2 1 2 3 4 1 2 1 3 3 4 1 2 2 3 1 4 2 1 3 4 1 1 2 3
输出#1
9
输入#2
5 1 1 3 99 99 100 2 1 2 1 3 3 4 3 5 1 3 1 2 2 4 2 5 2 1 2 3 1 2 1 2 2 1
输出#2
198
输入#3
4 1 2 1 2 3 4 1 2 1 3 3 4 2 1 2 4 4 3 1 1 4 2 4 1 2 4
输出#3
-1
说明/提示
In the first example, it is optimal to build ports in cities 2 , 3 and 4 , which fulfills all demands of both candidates and gives revenue equal to 2+3+4=9 .
In the second example, it is optimal to build ports in cities 2 and 3 , which fulfills all demands of both candidates and gives revenue equal to 99+99=198 .
In the third example, it is not possible to build ports in such way, that all demands of both candidates are specified, hence the answer is -1.