Black Company
Time limit5sMemory limit512 MB
Assign positive salaries minimizing their sum so that every edge and every two edges sharing an endpoint order their endpoints consistently with contribution degrees.
- Level
Hard9 of 10
- Topics
- Graph, Topological sort, Greedy, Number theory
- Solved
- No attempts yet
Problem
JAG Company is a sweatshop (a sweatshop is called "burakku kigyo" in Japanese), and you are the CEO of the company.
You are planning to set the salaries of employees as low as possible (employees are numbered from to ). Each employee's salary must be a positive integer greater than zero. You must also pay attention to each employee's contribution degree. If employee 's contribution degree is greater than employee 's contribution degree , employee must receive a higher salary than employee . If this condition is not satisfied, employees may complain about their salaries.
However, this does not have to hold for every pair of employees, because each employee can only know the contribution degrees and salaries of the employees close to them. Therefore, employees do not complain about their salaries as long as the following two conditions hold.
- If employees and are close to each other, must hold, where is employee 's salary.
- If employee is close to employees and , must hold.
Write a program that computes the minimum possible sum of all employees' salaries such that no employee complains about their salary.
Input
Each input is formatted as follows:
$N$
$c_1$ ... $c_N$
$M$
$a_1$ $b_1$
...
$a_M$ $b_M$
The first line contains an integer (), which indicates the number of employees. The second line contains integers () representing the contribution degree of employee .
The third line contains an integer (), which specifies the number of relationships. Each of the following lines contains two integers and (, ). This means that employees and are close to each other. There is at most one relationship between each pair of employees.
Output
Print the minimum sum of all employees' salaries on one line.