cho.sh
Notes
Loading...

Biomagnification

Time limit

5s

Memory limit

128 MB

Problem

In one region, N species live together. When predator-prey relationships connect several species, they form a food chain. A species that synthesizes organic matter directly from solar energy is a producer, and a species that obtains energy by eating other species is a consumer.

The ecosystem used to be stable, but recently the producers became contaminated with heavy metals. Humans are the final consumers in this ecosystem, and humans are species N. People want to know whether humans can survive if the contamination continues to concentrate along the food chain.

Each species i follows these rules.

  • To survive, it must obtain at least C[i] kcal from other species. A producer uses solar energy, so its C[i] is 0.
  • If the heavy metal accumulated in species i exceeds H[i] ppm, species i becomes extinct, and no other species can eat it.
  • When eating, a species chooses a diet that satisfies its required calories while minimizing the heavy metal accumulated in its body.

When species i is eaten, it always provides E[i] kcal. Eating other species does not increase species i's own E[i]. However, heavy metal is not discharged: the amount accumulated in the prey is added directly to the predator.

Assume every species can eat only species smaller than itself. Therefore, two species cannot eat each other, and the food relationships cannot contain a cycle. There are enough individuals of every species, so a predator may eat multiple individuals of the same prey species to meet its calorie requirement.

At the moment, only producers have been contaminated. Every producer that has not gone extinct has accumulated exactly H[i] heavy metal, the maximum it can tolerate. If biomagnification continues from this state, determine whether humans go extinct; if they do not, compute the minimum amount of heavy metal accumulated in the human body.

Input

The first line contains the number of species N. (1 <= N <= 1,000)

The next N lines each contain three integers C[i], H[i], and E[i]. C[i] (0 <= C[i] <= 10,000) is the number of calories species i must consume, H[i] (1 <= H[i] <= 10,000) is the maximum heavy metal amount species i can tolerate, and E[i] (1 <= E[i] <= 10,000) is the number of calories gained when another species eats species i.

The next line contains the number of predator-prey relationships M. (1 <= M <= 10,000)

Each of the next M lines contains two distinct integers a b. This means species a is eaten by species b.

Output

On the first line, print yes if species N, the human, can survive, and print no otherwise.

If the human can survive, print on the second line the minimum amount of heavy metal accumulated in the human body.