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

#FT0000124. Các cặp tập phim dễ nhầm lẫn (Tufurama)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Các cặp tập phim dễ nhầm lẫn (Tufurama)

Tufurama

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

A TV series has nn seasons numbered 11 to nn. Season ii has aia_i episodes numbered 11 to aia_i.

An unordered pair of distinct seasons (x,y)(x,y), with 1≤x<y≤n1\le x<y\le n, is confusing if both season xx, episode yy, and season yy, episode xx exist. Equivalently, ax≥ya_x\ge y and ay≥xa_y\ge x must hold simultaneously.

Count all such pairs, each exactly once.

Input

  • First line: nn (seasons).
  • Second line: nn positive integers a1,…,ana_1,\ldots,a_n.

Output

Print the number of season pairs (x,y)(x,y) with x<yx<y for which both corresponding episodes exist.

Subtasks

  • Subtask 1 (20 points): 1≤n≤401\le n\le 40; 1≤ai≤1091\le a_i\le10^9.
  • Subtask 2 (30 points): 1≤n≤38001\le n\le 3800; 1≤ai≤1091\le a_i\le10^9.
  • Subtask 3 (50 points): 1≤n≤2000001\le n\le 200000; 1≤ai≤1091\le a_i\le10^9.

Examples

Example 1

Input

3
8 12 7

Output

3

Explanation

Valid season pairs: (1,2), (1,3), (2,3). Each pair meets both episode-existence conditions. 3 pairs.

Example 2

Input

3
3 2 1

Output

2

Explanation

Valid season pairs: (1,2), (1,3). Each pair meets both episode-existence conditions. 2 pairs.