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 Ruby (judge verdict: Accepted)

require 'set'

class UnionFind
	def initialize
		@obj_to_node = {}
	end

	def add(obj)
		@obj_to_node[obj] ||= Node.new(obj)
	end

	def size(x = nil)
		x ? @obj_to_node[x].root.size : @obj_to_node.size
	end

	class Node
		def initialize(obj)
			@obj = obj
			@parent = self
			@size = 1
			@rank = 0
		end

		def root
			return self if @parent == self
			@parent = @parent.root
		end

		attr_accessor :parent, :size, :rank
	end

	def union(x, y)
		u = (@obj_to_node[x] ||= Node.new(x)).root
		v = (@obj_to_node[y] ||= Node.new(y)).root
		return if u == v
		if u.rank > v.rank
			v.parent = u
			u.size += v.size
		elsif v.rank > u.rank
			u.parent = v
			v.size += u.size
		else
			u.parent = v
			v.rank += 1
			v.size += u.size
		end
	end

	def same?(x, y)
		return false unless u = @obj_to_node[x]
		return false unless v = @obj_to_node[y]
		u.root == v.root
	end

    def groups
        @obj_to_node.keys.group_by {|x| @obj_to_node[x].root}.values
    end
end

n = gets.to_i
contribute = [0] + gets.split.map(&:to_i)
uf = UnionFind.new
(1..n).each {|i| uf.add(i)}

g = {}
g_inv = {}
(1..n).map {|i| g[i] = Set.new; g_inv[i] = Set.new}

m = gets.to_i
neighbour = (0..n).map { Set.new }
m.times do
    u, v = gets.split.map(&:to_i)
    g[v] << u if contribute[u] <= contribute[v]
    g[u] << v if contribute[u] >= contribute[v]
    g_inv[v] << u if contribute[u] >= contribute[v]
    g_inv[u] << v if contribute[u] <= contribute[v]
    neighbour[u] << v
    neighbour[v] << u
    uf.union(u, v) if contribute[u] == contribute[v]

end

(1..n).each do |c|
    neighbour[c].sort_by{|v| contribute[v]}.each_cons(2) do |u, v|
        if contribute[u] == contribute[v]
            g[v] << u
            g[u] << v
            g_inv[v] << u
            g_inv[u] << v
            uf.union(u, v)
        else
            g[v] << u
            g_inv[u] << v
        end
    end
end

scc_size = Hash.new(1)

#g.strongly_connected_components.each do |scc|
uf.groups.each do |scc|
    next if scc.size == 1
    rep = scc[0]
    scc_size[rep] = scc.size

    scc[1..-1].each do |u|
        g[rep] += g[u]
        g[rep].delete(rep)
        g_inv[rep] += g_inv[u]
        g_inv[rep].delete(rep)

        g_inv[u].each do |v|
            g[v].delete(u)
            g[v] << rep if v != rep
        end

        g[u].each do |v|
            g_inv[v].delete(u)
            g_inv[v] << rep if v != rep
        end

        g.delete(u)
        g_inv.delete(u)
    end
end

salary = {}
out_deg = {}
queue = []
g.each do |u, s|
    queue << u if s.empty?
    out_deg[u] = s.size
end

until queue.empty?
    u = queue.shift
    salary[u] = (g[u].map {|v| salary[v]}.max || 0) + 1
    g_inv[u].each do |v|
        out_deg[v] -= 1
        queue << v if out_deg[v] == 0
    end
end

p g.keys.map {|v| salary[v] * scc_size[v]}.inject(:+)