Black Company

Time limit5sMemory limit512 MB

Summary
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 NN employees as low as possible (employees are numbered from 11 to NN). 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 ii's contribution degree c_ic\_i is greater than employee jj's contribution degree c_jc\_j, employee ii must receive a higher salary than employee jj. 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 ii and jj are close to each other, c_i<c_j⇔p_i<p_jc\_i < c\_j \Leftrightarrow p\_i < p\_j must hold, where p_ip\_i is employee ii's salary.
  • If employee ii is close to employees jj and kk, c_j<c_k⇔p_j<p_kc\_j < c\_k \Leftrightarrow p\_j < p\_k 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 NN (1≤N≤100,0001 \le N \le 100{,}000), which indicates the number of employees. The second line contains NN integers c_ic\_i (1≤c_i≤100,0001 \leq c\_i \leq 100{,}000) representing the contribution degree of employee ii.

The third line contains an integer MM (0≤M≤200,0000 \leq M \leq 200{,}000), which specifies the number of relationships. Each of the following MM lines contains two integers a_ia\_i and b_ib\_i (a_i≠b_ia\_i \neq b\_i, 1≤a_i,b_i≤N1 \leq a\_i, b\_i \leq N). This means that employees a_ia\_i and b_ib\_i 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.

Examples5

  1. Example 1

    Input
    3
    1 3 3
    2
    1 2
    1 3
    
    Expected output
    5
    
  2. Example 2

    Input
    3
    1 2 3
    2
    1 2
    1 3
    
    Expected output
    6
    
  3. Example 3

    Input
    4
    1 1 2 2
    2
    1 2
    3 4
    
    Expected output
    4
    
  4. Example 4

    Input
    5
    1 2 5 5 1
    6
    1 2
    4 1
    2 3
    5 2
    4 3
    4 5 
    
    Expected output
    10
    
  5. Example 5

    Input
    6
    4 3 2 1 5 3
    7
    4 2
    1 5
    2 6
    6 5
    4 1
    1 6
    6 3
    
    Expected output
    13