题目描述
Problem Description
Due to the large-scale infiltration of foreign spies, national security is in a state of high crisis. If spy A holds incriminating evidence against spy B, it is said that A can expose B. Some spies take bribes; as long as they are given a certain amount of dollars, they are willing to hand over all the information they hold. Therefore, if we can bribe some spies, we may be able to control every member of the spy network. Because once we arrest a spy, all the information he holds will be ours, which may lead to the arrest of new spies and the acquisition of new information.
Our counter-espionage agency has provided a document that includes all known bribable spies and the specific amounts they are willing to accept. We also know which spies hold information on which other spies. Suppose there are a total of $n$ spies ($n$ does not exceed 3000), each identified by an integer from 1 to 3000.
Please determine whether it is possible to control all the spies based on this document. If it is possible, calculate the minimum amount of money we need to pay. Otherwise, output a spy that cannot be controlled.
Input Format
The first line contains a single integer $n$.
The second line contains an integer $p$, indicating the number of spies willing to be bribed, where $1 \le p \le n$.
The next $p$ lines each contain two integers: the first is the identifier of a spy willing to be bribed, and the second is the amount of money required to bribe him. This amount does not exceed 20000.
The next line contains a single integer $r$, where $1 \le r \le 8000$. The following $r$ lines each contain two positive integers, representing pairs $(A, B)$, where spy A holds evidence against spy B.
Output Format
If it is possible to control all the spies, output YES on the first line and the minimum amount of bribe money on the second line. Otherwise, output NO on the first line and the smallest identifier of a spy that cannot be controlled on the second line.
Sample Input and Output
Sample Input #1
3
2
1 10
2 100
2
1 3
2 3
Sample Output #1
YES
110
Sample Input #2
4
2
1 100
4 200
2
1 2
3 4
Sample Output #2
NO
3
输入格式
见题面 Input 部分
输出格式
见题面 Output 部分