PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FT0000059. Đếm cặp nghịch thế (Inversion Pairs)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Đếm cặp nghịch thế (Inversion Pairs)

Inversion Pairs

Source: Luogu

Version: Phuoc Hung OJ Extended

Problem Statement

Tom and Jerry compete to count pairs of numbers according to a special rule. Given a sequence of nn positive integers a1,a2,…,ana_1,a_2,\ldots,a_n, positions are numbered from left to right starting at 11.

An inversion is a pair of positions (i,j)(i,j) satisfying both 1≤i<j≤n1\le i<j\le n and ai>aja_i>a_j. Equal values do not form an inversion even when they occur at different positions.

Determine the total number of such position pairs, counting each pair exactly once.

Input

The first line contains the integer nn, the sequence length.

The second line contains exactly nn positive integers a1,a2,…,ana_1,a_2,\ldots,a_n in positional order.

Output

Print one integer: the inversion count of the sequence.

Subtasks

  • Subtask 1 (25 points): 1≤n≤25001\le n\le 2500; 1≤ai≤10000000001\le a_i\le 1000000000 for every element.

  • Subtask 2 (25 points): 1≤n≤400001\le n\le 40000; 1≤ai≤10000000001\le a_i\le 1000000000 for every element.

  • Subtask 3 (50 points): 1≤n≤5000001\le n\le 500000; 1≤ai≤10000000001\le a_i\le 1000000000 for every element.

Examples

Input

6
5 4 2 6 3 1

Output

11

Explanation

For [5,4,2,6,3,1][5,4,2,6,3,1], count later values smaller than each value:

  • The first value 55 exceeds 4,2,3,14,2,3,1: 44 pairs.
  • The second value 44 exceeds 2,3,12,3,1: 33 pairs.
  • The third value 22 exceeds 11: 11 pair.
  • The fourth value 66 exceeds 3,13,1: 22 pairs.
  • The fifth value 33 exceeds 11: 11 pair.
  • The last value contributes 00.

Thus the answer is 4+3+1+2+1+0=114+3+1+2+1+0=11.