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

#PS0000181. Đếm đoạn có tổng không vượt quá K (Count Subarrays With Sum at Most K)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Đếm đoạn có tổng không vượt quá K (Count Subarrays With Sum at Most K)

Count Subarrays With Sum at Most K

Source: Phước Hưng OJ

Version: Phuoc Hung OJ Extended

Problem Statement

Given nn nonnegative integers a1,…,ana_1,\ldots,a_n and a nonnegative integer KK, count index pairs (l,r)(l,r) with 1≤l≤r≤n1\le l\le r\le n and ∑i=lrai≤K\sum_{i=l}^{r}a_i\le K. Count only nonempty contiguous intervals, distinguishing intervals by their positions.

Input

The first line contains n,Kn,K. The second contains nn integers a1,…,ana_1,\ldots,a_n.

Output

Print the number of valid intervals.

Subtasks

  • Subtask 1 (20 points): 1≤n≤5001\le n\le 500; 0≤ai≤1090\le a_i\le10^9; 0≤K≤10150\le K\le10^{15}.
  • Subtask 2 (30 points): 1≤n≤2000001\le n\le 200000; 0≤ai≤1090\le a_i\le10^9; 0≤K≤10150\le K\le10^{15}; K=0K=0.
  • Subtask 3 (50 points): 1≤n≤2000001\le n\le 200000; 0≤ai≤1090\le a_i\le10^9; 0≤K≤10150\le K\le10^{15}.

Example 1

Input

3 2
1 0 2

Output

5

Explanation

The valid intervals are [1,1], [2,2], [3,3], [1,2], [2,3]. The full interval sums to 3.

Example 2

Input

4 0
0 0 1 0

Output

4

Explanation

The first two zeros yield three intervals, and the last zero yields one.