Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Problem J:
Black Company

JAG Company is a sweatshop (sweatshop is called "burakku kigyo" in Japanese), and you are
the CEO for the company.

You are planning to determine $N$ employees' salary as low as possible (employees are numbered
from 1 to $N$). Each employee's salary amount must be a positive integer greater than zero. At
that time, you should pay attention to the employees' contribution degree. If the employee $i$'s
contribution degree $c_i$ is greater than the employee $j$'s contribution degree $c_j$ , the employee i
must get higher salary than the employee $j$'s salary. If the condition is not satisfied, employees
may complain about their salary amount.

However, it is not necessarily satisfied in all pairs of the employees, because each employee can
only know his/her close employees' contribution degree and salary amount. Therefore, as long as
the following two conditions are satisfied, employees do not complain to you about their salary
amount.

If the employees $i$ and $j$ are close to each other, $c_i < c_j \Leftrightarrow p_i < p_j$ must be satisfied, where
$p_i$ is the employee $i$'s salary amount.

If the employee $i$ is close to the employees $j$ and $k$, $c_j < c_k \Leftrightarrow p_j < p_k$ must be satisfied.
Write a program that computes the minimum sum of all employees' salary amount 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 $N$ ($1 \leq N \leq 100,000$), which indicates the number of employees.
The second line contains $N$ integers $c_i$ ($1 \leq c_i \leq 100,000$) representing the contribution degree
of employee $i$.

The third line contains an integer $M$ ($0 \leq M \leq 200,000$), which specifies the number of relationships. Each of the following $M$ lines contains two integers $a_i$ and $b_i$ ($a_i \ne b_i, 1 \leq a_i, b_i \leq N$).
It represents that the employees $a_i$ and $b_i$ are close to each other. There is no more than one
relationship between each pair of the employees.

Output

Print the minimum sum of all employees' salary amounts in a line.

Sample Input

3
1 3 3
2
1 2
1 3

Output for the Sample Input

5

Sample Input

3
1 2 3
2
1 2
1 3

Output for the Sample Input

6

Sample Input

4
1 1 2 2
2
1 2
3 4

Output for the Sample Input

4

Sample Input

5
1 2 5 5 1
6
1 2
4 1
2 3
5 2
4 3
4 5

Output for the Sample Input

10

Sample Input

6
4 3 2 1 5 3
7
4 2
1 5
2 6
6 5
4 1
1 6
6 3

Output for the Sample Input

13

Solution in C++11 (judge verdict: Wrong Answer)

#include<iostream>
#include<vector>
#include<algorithm>
#include<numeric>
#include<queue>
#include<set>
using namespace std;
typedef long long int lli;
typedef pair<lli, lli> pii;

vector<set<lli> > compression(vector<set<lli> > g, vector<pii> &c, vector<set<int> > &same){
  int n = c.size();
  vector<set<lli> > res(n);
  vector<int> min_ele(n);
  iota(min_ele.begin(), min_ele.end(), -n);
  for (int i = 0; i < n; i++) {
    if(min_ele[i] >= 0)continue;
    min_ele[i] = i;
    same[i].insert(i);
    queue<int> que;
    que.push(i);
    while(not que.empty()){
      int v = que.front();
      que.pop();
      for (int to: g[v]) {
        if(min_ele[to] >= 0 or c[to].first != c[i].first)continue;
        same[i].insert(to);
        min_ele[to] = i;
        que.push(to);
      }
    }
  }

  for (int i = 0; i < n; i++) {
    if(same[i].size() == 0)continue;
    for (int j: same[i]) {
      for (int to: g[j]) {
        if(c[i].first > c[to].first)continue;
        if(same[i].find(to) != same[i].end())continue;
        res[i].insert(min_ele[to]);
        res[min_ele[to]].insert(i);
      }
    }
  }
  return res;
}

int main(){
  int n, m;
  std::cin >> n;
  vector<pii> c(n);
  vector<lli> salary(n, 1), id(n);
  for (int i = 0; i < n; i++){
    std::cin >> c[i].first;
    c[i].second = i;
  }
  std::cin >> m;
  vector<set<lli> > g(n);
  for (int i = 0; i < m; i++) {
    int to, from;
    std::cin >> to >> from;
    to--, from--;
    g[from].insert(to);
    g[to].insert(from);
  }
  vector<set<int> > same(n);
  // for (int i = 0; i < g.size(); i++) {
  //   std::cout << "i:" << i << " ";
  //   for (int j: g[i]) {
  //     std::cout << j << " ";
  //   }
  //   std::cout << std::endl;
  // }
  // for (int i = 0; i < n; i++) {
  //   std::cout << "i:" << i << " ";
  //   for (int j: same[i]) { 
  //     std::cout << j << " ";
  //   }
  //   std::cout << std::endl;
  // }
  // std::cout << std::endl;
  vector<set<lli> > gg = g;
  for (int i = 0; i < n; i++) {
    if(gg[i].size() == 0)continue;
    vector<pii> vertex;
    for (int to: gg[i]) vertex.push_back(c[to]);
    sort(vertex.begin(), vertex.end());
    for (int j = 0; j < vertex.size() - 1; j++) {
      int neighbor = vertex[j].second;
      g[neighbor].insert(vertex[j + 1].second);
      g[vertex[j + 1].second].insert(neighbor);
    }
  }

  g = compression(g, c, same);
  sort(c.begin(), c.end());
  for (int i = 0; i < n; i++) id[c[i].second] = i;
  // std::cout << "after" << std::endl;
  // for (int i = 0; i < g.size(); i++) {
  //   std::cout << "i:" << i << " ";
  //   for (int j: g[i]) {
  //     std::cout << j << " ";
  //   }
  //   std::cout << std::endl;
  // }
  // for (int i = 0; i < n; i++) {
  //   std::cout << "i:" << i << " ";
  //   for (int j: same[i]) { 
  //     std::cout << j << " ";
  //   }
  //   std::cout << std::endl;
  // }

  for (int i = 0; i < n; i++) {
    int v = c[i].second;
    if(same[v].size() == 0)continue;
    for (int to: g[v]) {
      if(c[i].first >= c[id[to]].first)continue;
      salary[to] = salary[v] + (c[i].first < c[id[to]].first);
    }
  }
  lli result = 0;
  for(int i = 0; i < n; i++) {
    // if(same[i].size() > 0)std::cout << salary[i] << " ";
    // else std::cout << 0 << " ";
    result += salary[i]*same[i].size();
  }
  std::cout << result << std::endl;
  return 0;
}