#Q97. 「一本通 3.5 练习 3」间谍网络

「一本通 3.5 练习 3」间谍网络

Description

Due to the massive infiltration of foreign spies, national security is in a state of high crisis. If spy AA possesses incriminating evidence about spy BB, then AA can expose BB. Some spies are willing to accept bribes and will hand over all the information they possess in exchange for a certain amount of dollars. Therefore, if we can bribe some spies, we may be able to control every member of the spy network. Once we arrest a spy, all the information they possess will become ours, potentially allowing us to arrest new spies and gather new intelligence.

Our counterintelligence agency has provided a dossier that includes all known bribable spies and the specific amounts they are willing to accept. Additionally, we know which spies possess information about which other spies. Assume there are a total of nn spies, each identified by an integer from 11 to nn.

Based on this dossier, determine whether it is possible to control all the spies. If possible, calculate the minimum amount of money required. Otherwise, output the smallest identifier of a spy that cannot be controlled.

Input Format

The first line contains a single integer nn. The second line contains an integer pp, representing the number of spies willing to be bribed.

The next pp lines each contain two integers: the first is the identifier of a bribable spy, and the second is the amount they will accept.

Following this is a line with a single integer rr. Then, rr lines follow, each containing two positive integers representing a pair (A,B)(A, B), indicating that spy AA has evidence against spy BB.

Output Format

If it is possible to control all spies, the first line should output YES, and the second line should output the minimum bribe amount required. Otherwise, output NO on the first line, and on the second line, output the smallest identifier of a spy that cannot be controlled.

Sample 1

2
1
2 512
2
1 2
2 1

YES
512

Constraints and Hints

1n3000,1pn,1r80001 \le n \le 3000, 1 \le p \le n, 1 \le r \le 8000. The bribe amount for each spy is a non-negative integer not exceeding 2000020000.