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

#CC0000046. Đếm cặp nghịch thế nhân đôi (Reverse Pairs)

HỌC THEO LỘ TRÌNH

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

2 lộ trình

Đếm cặp nghịch thế nhân đôi (Reverse Pairs)

Reverse Pairs

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

Given an integer array aa, count index pairs (i,j)(i,j) with i<ji<j and ai>2aja_i>2a_j. The comparison is strict; negatives and 32-bit boundary values are allowed.

Input

The first line is nn. The second line contains nn integers aia_i.

Output

Print the number of qualifying pairs.

Subtasks

  • Subtask 1 (20 points): 1≤n≤151\le n\le 15; −231≤ai≤231−1-2^{31}\le a_i\le2^{31}-1.
  • Subtask 2 (30 points): 1≤n≤4201\le n\le 420; −231≤ai≤231−1-2^{31}\le a_i\le2^{31}-1.
  • Subtask 3 (50 points): 1≤n≤500001\le n\le 50000; −231≤ai≤231−1-2^{31}\le a_i\le2^{31}-1.

Examples

Input

5
1 3 2 3 1

Output

2

Explanation

The two valid pairs are positions (2,5) and (4,5), using 1-based indexing.