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

#CC0000041. Đếm đoạn con có trung vị bằng K (Count Subarrays With Median K)

HỌC THEO LỘ TRÌNH

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

2 lộ trình

Đếm đoạn con có trung vị bằng K (Count Subarrays With Median K)

Count Subarrays With Median K

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

Array aa is a permutation of 1..n1..n. Count contiguous subarrays with median equal to kk. The median is the middle element of the sorted subarray; for even length, use the left middle.

Input

The first line contains n,kn,k. The second line contains a permutation aa of 1..n1..n.

Output

Print the number of subarrays whose median equals kk.

Subtasks

  • Subtask 1 (20 points): 1≤n≤151\le n\le 15; aa là hoán vị của 1,2,…,n1,2,\ldots,n; 1≤k≤n1\le k\le n.
  • Subtask 2 (30 points): 1≤n≤4201\le n\le 420; aa là hoán vị của 1,2,…,n1,2,\ldots,n; 1≤k≤n1\le k\le n.
  • Subtask 3 (50 points): 1≤n≤1000001\le n\le 100000; aa là hoán vị của 1,2,…,n1,2,\ldots,n; 1≤k≤n1\le k\le n.

Examples

Input

5 4
3 2 1 4 5

Output

3

Explanation

Valid subarrays are [4][4], [4,5][4,5] and [1,4,5][1,4,5]. The median of [4,5][4,5] is 4 because the left middle is used.