#P2811. Protect the school

Protect the school

Description

The school has nn checkpoints. Because the guards do not want to think too hard, they decide to build mm passages between these nn checkpoints. Due to lax administration and militarized management, these roads are one-way; going in the reverse direction will result in punishment.

The guards are short-handed (too many game quests), so they will select some checkpoints to stand guard. Since the guards have special skills, a guard can instantly traverse along any path reachable from the checkpoint where he is stationed (i.e., teleport to any vertex reachable via directed edges). Each checkpoint has a value representing its difficulty.

To protect the school, please help them ensure that if an incident occurs at any checkpoint, at least one guard can instantly reach it. For convenience and easier management, tell them the minimum possible total difficulty under the constraint of using the fewest number of guards.

Input Format

The first line contains an integer nn, the number of checkpoints.

The second line contains nn integers, the difficulties of the checkpoints.

The third line contains an integer mm, the number of roads.

Each of the next mm lines contains two integers u,vu, v, indicating a one-way passage from uu to vv.

Output Format

Output two integers.

The first integer is the minimum total difficulty. The second integer is the number of valid selections that achieve both the minimum number of guards and that minimum total difficulty.

5
31619 26195 18669 1198 178
4
2 4
3 5
1 2
4 1
20045 1

Hint

1n1041 \le n \le 10^4, 1m5×1041 \le m \le 5 \times 10^4, and the answer fits in the int range.

Translated by ChatGPT 5