Problem 02C. Counting Inversions ≡

Author:Pavel Mesenev   Time limit:3 sec
Input file:Standard input   Memory limit:256 Mb
Output file:Standard output  

Statement

You are given an array of n integers a0, a1, …, an − 1. An inversion is a pair of indices i < j with ai > aj — a pair that appears out of order.

Count the inversions. A sorted array has 0; a strictly decreasing array has binomn2; the count equals the number of adjacent swaps bubble sort would make.

Input format

The first line contains one integer n. The second line contains n space-separated integers a0… an − 1.

Output format

Print one integer — the number of inversions.

Constraints

1 ≤ n ≤ 2 ⋅ 105

 − 109 ≤ ai ≤ 109

The answer fits into a signed 64-bit integer.

Sample tests

No. Standard input Standard output
1
8
5 2 6 1 3 4 8 7
9
2
5
1 2 3 4 5
0
3
4
4 3 2 1
6

0.035s 0.009s 18