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 C (judge verdict: Wrong Answer)

#include<stdio.h>
#include<stdlib.h>
#include<limits.h>


int size;
int *S;
int *L;
int *R;
int cnt;

void merge(int left,int mid,int right){
  int i;
  int r,l;
  for(i=left;i<mid;i++) L[i-left]=S[i];
  L[mid-left]=INT_MAX;
  for(i=mid;i<right;i++) R[i-mid]=S[i];
  R[right-mid]=INT_MAX;
  l=r=0;
  for(i=left;i<right;i++){
    if(L[l]<R[r]){
      S[i]=L[l];
      l++;
    }
    else{
      cnt+=mid-left-l;
      S[i]=R[r];
      r++;
    }
  }
}


void m_sort(int left,int right){
  if(right <= left+1) return;
  int mid;
  mid=(left +right)/2;
  m_sort(left,mid);
  m_sort(mid,right);
  merge(left,mid,right);
}






int main(){
  scanf("%d",&size);
  S=malloc(size*sizeof(int));
  L=malloc(size*sizeof(int));
  R=malloc(size*sizeof(int));
  int i;
  for(i=0;i<size;i++){
    scanf("%d",&S[i]);
    S[i]--;
  }
  cnt=0;
  m_sort(0,size);
  printf("%d\n",cnt);
  return 0;
}