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

#FT0000060. Đếm nghịch thế của dãy phân biệt (Inversion Count)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Đếm nghịch thế của dãy phân biệt (Inversion Count)

Inversion Count

Source: SPOJ

Version: Phuoc Hung OJ Extended

Problem Statement

You are given a sequence of nn pairwise distinct positive integers, indexed from 11 to nn.

An inversion is a pair (i,j)(i,j) for which position ii precedes jj but ai>aja_i>a_j, that is, i<ji<j and ai>aja_i>a_j.

Calculate the total number of inversions. The answer may exceed the range of a signed 32-bit integer. The relative order in the given sequence matters and must be preserved when counting.

Input

The first line contains nn.

Exactly nn integers a1,…,ana_1,\ldots,a_n follow in positional order; whitespace or line breaks may separate them.

Output

Print one integer: the number of inversions.

Subtasks

  • Subtask 1 (20 points): 1≤n≤601\le n\le 60; 1≤ai≤100000001\le a_i\le 10000000 for every element; all values are pairwise distinct.

  • Subtask 2 (30 points): 1≤n≤30001\le n\le 3000; 1≤ai≤100000001\le a_i\le 10000000 for every element; all values are pairwise distinct.

  • Subtask 3 (50 points): 1≤n≤2000001\le n\le 200000; 1≤ai≤100000001\le a_i\le 10000000 for every element; all values are pairwise distinct.

Examples

Input

5
2
3
8
6
1

Output

5

Explanation

The sequence is [2,3,8,6,1][2,3,8,6,1]. The inversion position pairs are (1,5)(1,5) for (2,1)(2,1), (2,5)(2,5) for (3,1)(3,1), (3,4)(3,4) for (8,6)(8,6), (3,5)(3,5) for (8,1)(8,1), and (4,5)(4,5) for (6,1)(6,1).

These are exactly the five qualifying pairs, so the output is 5.