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

#CC0000013. Đếm cặp nghịch thế (INVCNT - Inversion Count)

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ế (INVCNT - Inversion Count)

INVCNT - Inversion Count

Source: SPOJ

Version: Phuoc Hung OJ Extended

Problem Statement

You are given nn pairwise distinct positive integers. An inversion is a pair of positions i<ji<j for which ai>aja_i>a_j. Count all inversions, including those formed by non-adjacent elements.

Input

The first line contains the integer nn. The next line contains nn positive integers a1,a2,…,ana_1,a_2,\ldots,a_n in their original order.

Output

Print one integer: the total number of inversions.

Subtasks

  • Subtask 1 (20 points): 1≤n≤121\le n\le 12; 1≤ai≤1071\le a_i\le 10^7 for every ii; all aia_i are pairwise distinct.
  • Subtask 2 (30 points): 1≤n≤12001\le n\le 1200; 1≤ai≤1071\le a_i\le 10^7 for every ii; all aia_i are pairwise distinct.
  • Subtask 3 (50 points): 1≤n≤2000001\le n\le 200000; 1≤ai≤1071\le a_i\le 10^7 for every ii; all aia_i are pairwise distinct.

Examples

Input

5
2 3 8 6 1

Output

5

Explanation

The five inversions are the value pairs (2,1)(2,1), (3,1)(3,1), (8,6)(8,6), (8,1)(8,1), and (6,1)(6,1), at position pairs (1,5)(1,5), (2,5)(2,5), (3,4)(3,4), (3,5)(3,5), and (4,5)(4,5). No other pair has its first element greater than its second, so the answer is 55.