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

#FT0000123. Trung bình của các đoạn con (Mean of Array)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Trung bình của các đoạn con (Mean of Array)

Mean of Array

Source: SPOJ

Version: Phuoc Hung OJ Extended

Problem Statement

Given NN positive integers and a positive integer KK, count all non-empty contiguous subarrays whose arithmetic mean is not less than KK (that is, ≥K\ge K).

Each pair of start/end positions identifies a distinct subarray, even when its contents match another range. The condition can be evaluated with integers by comparing its sum with KK times its length, or by subtracting KK from each element.

Input

  • First line: positive integers N,KN,K.
  • Second line: NN positive integers of the array.

Output

Print the number of non-empty contiguous subarrays whose arithmetic mean is at least KK.

Subtasks

  • Subtask 1 (20 points): 1≤N≤401\le N\le 40; 1≤K≤231−11\le K\le2^{31}-1; 1≤ai≤231−11\le a_i\le2^{31}-1; all data values are positive 32-bit integers.
  • Subtask 2 (30 points): 1≤N≤38001\le N\le 3800; 1≤K≤231−11\le K\le2^{31}-1; 1≤ai≤231−11\le a_i\le2^{31}-1; all data values are positive 32-bit integers.
  • Subtask 3 (50 points): 1≤N≤2000001\le N\le 200000; 1≤K≤231−11\le K\le2^{31}-1; 1≤ai≤231−11\le a_i\le2^{31}-1; all data values are positive 32-bit integers.

Examples

Example 1

Input

5 4
5 2 4 5 1

Output

5

Explanation

  • [1,1]: 5/1 >= 4 → ✓
  • [1,2]: 7/2 >= 4 → ✗
  • [1,3]: 11/3 >= 4 → ✗
  • [1,4]: 16/4 >= 4 → ✓
  • [1,5]: 17/5 >= 4 → ✗
  • [2,2]: 2/1 >= 4 → ✗
  • [2,3]: 6/2 >= 4 → ✗
  • [2,4]: 11/3 >= 4 → ✗
  • [2,5]: 12/4 >= 4 → ✗
  • [3,3]: 4/1 >= 4 → ✓
  • [3,4]: 9/2 >= 4 → ✓
  • [3,5]: 10/3 >= 4 → ✗
  • [4,4]: 5/1 >= 4 → ✓
  • [4,5]: 6/2 >= 4 → ✗
  • [5,5]: 1/1 >= 4 → ✗

5 valid subarrays in total.

Example 2

Input

4 3
1 5 2 4

Output

7

Explanation

  • [1,1]: 1/1 >= 3 → ✗
  • [1,2]: 6/2 >= 3 → ✓
  • [1,3]: 8/3 >= 3 → ✗
  • [1,4]: 12/4 >= 3 → ✓
  • [2,2]: 5/1 >= 3 → ✓
  • [2,3]: 7/2 >= 3 → ✓
  • [2,4]: 11/3 >= 3 → ✓
  • [3,3]: 2/1 >= 3 → ✗
  • [3,4]: 6/2 >= 3 → ✓
  • [4,4]: 4/1 >= 3 → ✓

7 valid subarrays in total.