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

#TP0000077. MEX so với trung vị (MEX vs MED)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

MEX so với trung vị (MEX vs MED)

MEX vs MED

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

Given a permutation of 0,1,…,n−10,1,\ldots,n-1, count nonempty contiguous segments whose mex is strictly greater than the lower median (the ⌊(∣S∣+1)/2⌋\lfloor(|S|+1)/2\rfloor-th sorted element).

Input

Line 1: nn. Line 2: permutation pp. Exactly one case, no TT.

Output

Print the 64-bit count of valid segments.

Subtasks

  • Subtask 1 (20 points): Exactly one dataset; 1≤n≤101\le n\le10; the sequence is a permutation of 0,…,n−10,\ldots,n-1.
  • Subtask 2 (30 points): Exactly one dataset; 1≤n≤20001\le n\le2000; the sequence is a permutation of 0,…,n−10,\ldots,n-1.
  • Subtask 3 (50 points): Exactly one dataset; 1≤n≤2000001\le n\le200000; the sequence is a permutation of 0,…,n−10,\ldots,n-1.

Example

Input

3
1 0 2

Output

4

Explanation

Four subsegments have mex greater than the lower median.