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

The Number of Inversions

For a given sequence $A = \{a_0, a_1, ... a_{n-1}\}$, the number of pairs $(i, j)$ where $a_i > a_j$ and $i < j$, is called the number of inversions. The number of inversions is equal to the number of swaps of Bubble Sort defined in the following program:

bubbleSort(A)
cnt = 0 // the number of inversions
for i = 0 to A.length-1
for j = A.length-1 downto i+1
if A[j] < A[j-1]
swap(A[j], A[j-1])
cnt++

return cnt

For the given sequence $A$, print the number of inversions of $A$. Note that you should not use the above program, which brings Time Limit Exceeded.

Input

In the first line, an integer $n$, the number of elements in $A$, is given. In the second line, the elements $a_i$ ($i = 0, 1, .. n-1$) are given separated by space characters.

output

Print the number of inversions in a line.

Constraints

$ 1 \leq n \leq 200,000$

$ 0 \leq a_i \leq 10^9$

$a_i$ are all different

Sample Input 1

5
3 5 2 1 4

Sample Output 1

6

Sample Input 2

3
3 1 2

Sample Output 2

2

Solution in Ruby (judge verdict: Accepted)

n = gets.to_i
s = gets.split.map(&:to_i)

$cnt = 0
$INF = 10**9

def marge(s, left, mid, right)
  l = s[left..(mid-1)].push($INF) #左側の配列, $INFは番兵
  r = s[mid..(right-1)].push($INF) #右側の配列, $INFは番兵

  i, j = [0, 0]
  left.upto(right-1) do |k|
    if l[i] < r[j]
      s[k] = l[i]
      i += 1
    else
      s[k] = r[j]
      j += 1
      $cnt += mid + j - k - 1
      #↑ゴチャゴチャやった結果、これが反転数
      #要素がどれくらい動いたか、添字の差を取ることで反転数が出る。
    end
  end
end

def marge_sort(s, left, right)
  #left:部分配列の先頭, right:部分配列の末尾+1
  #要素数が1以上のときマージソート
  if right - left > 1
    mid = (left + right)/2
    #配列の左側をソート
    marge_sort(s, left, mid)
    #配列の右側をソート
    marge_sort(s, mid, right)
    #左の配列と右の配列をマージする
    marge(s, left, mid, right)
  end
end

marge_sort(s, 0, n)

#puts s.join(" ")
puts $cnt