题库练习 Least Prefix Sum
← 上一题 下一题 →

A15570 | Least Prefix Sum

时间限制1s
内存限制256MB
通过 / 提交0/0

题目描述

Baltic, a famous chess player who is also a mathematician, has an array $a_1,a_2, \ldots, a_n$ , and he can perform the following operation several (possibly $0$ ) times:

- Choose some index $i$ ( $1 \leq i \leq n$ );
- multiply $a_i$ with $-1$ , that is, set $a_i := -a_i$ .

Baltic's favorite number is $m$ , and he wants $a_1 + a_2 + \cdots + a_m$ to be the smallest of all non-empty prefix sums. More formally, for each $k = 1,2,\ldots, n$ it should hold that $$$$a_1 + a_2 + \cdots + a_k \geq a_1 + a_2 + \cdots + a_m. $$ </p><p>Please note that multiple smallest prefix sums may exist and that it is only required that $a\_1 + a\_2 + \\cdots + a\_m$ is one of them.</p><p>Help Baltic find the minimum number of operations required to make $a\_1 + a\_2 + \\cdots + a\_m$$$ the least of all prefix sums. It can be shown that a valid sequence of operations always exists.

输入格式

Each test contains multiple test cases. The first line contains the number of test cases $t$ ( $1 \leq t \leq 10\,000$ ). The description of the test cases follows.

The first line of each test case contains two integers $n$ and $m$ ( $1 \leq m \leq n \leq 2\cdot 10^5$ ) — the size of Baltic's array and his favorite number.

The second line contains $n$ integers $a_1,a_2, \ldots, a_n$ ( $-10^9 \leq a_i \leq 10^9$ ) — the array.

It is guaranteed that the sum of $n$ over all test cases does not exceed $2\cdot 10^5$ .

输出格式

For each test case, print a single integer — the minimum number of required operations.

输入输出样例

输入 #1
6
4 3
-1 -2 -3 -4
4 3
1 2 3 4
1 1
1
5 5
-2 3 -5 1 -20
5 2
-2 3 -5 -5 -20
10 4
345875723 -48 384678321 -375635768 -35867853 -35863586 -358683842 -81725678 38576 -357865873
输出 #1
1
1
0
0
3
4
C++ 编辑器
输入
输出