Codeforces Round 982 (Div. 2)
D1
The Endspeaker (Easy Version)
Codeforces
1700
原题
← CD2 →
翻译中…

This is the easy version of this problem. The only difference is that you only need to output the minimum total cost of operations in this version. You must solve both versions to be able to hack.

翻译中…

You're given an array $a$ of length $n$, and an array $b$ of length $m$ ($b_i \gt b_{i+1}$ for all $1 \le i \lt m$). Initially, the value of $k$ is $1$. Your aim is to make the array $a$ empty by performing one of these two operations repeatedly:

翻译中…
  • Type $1$ — If the value of $k$ is less than $m$ and the array $a$ is not empty, you can increase the value of $k$ by $1$. This does not incur any cost.
  • Type $2$ — You remove a non-empty prefix of array $a$, such that its sum does not exceed $b_k$. This incurs a cost of $m - k$.
翻译中…

You need to minimize the total cost of the operations to make array $a$ empty. If it's impossible to do this through any sequence of operations, output $-1$. Otherwise, output the minimum total cost of the operations.

翻译中…

Input

翻译中…

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 1000$). The description of the test cases follows.

翻译中…

The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 3 \cdot 10^5$, $\boldsymbol{1 \le n \cdot m \le 3 \cdot 10^5}$).

翻译中…

The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).

翻译中…

The third line of each test case contains $m$ integers $b_1, b_2, \ldots, b_m$ ($1 \le b_i \le 10^9$).

翻译中…

It is also guaranteed that $b_i \gt b_{i+1}$ for all $1 \le i \lt m$.

翻译中…

It is guaranteed that the sum of $\boldsymbol{n \cdot m}$ over all test cases does not exceed $3 \cdot 10^5$.

翻译中…

Output

翻译中…

For each test case, if it's possible to make $a$ empty, then output the minimum total cost of the operations.

翻译中…

If there is no possible sequence of operations which makes $a$ empty, then output a single integer $-1$.

翻译中…

Note

翻译中…

In the first test case, one optimal sequence of operations which yields a total cost of $1$ is as follows:

翻译中…
  • Perform an operation of type $2$. Choose the prefix to be $[9]$. This incurs a cost of $1$.
  • Perform an operation of type $1$. The value of $k$ is now $2$. This incurs no cost.
  • Perform an operation of type $2$. Choose the prefix to be $[3, 4]$. This incurs a cost of $0$.
  • Perform an operation of type $2$. Choose the prefix to be $[3]$. This incurs a cost of $0$.
  • The array $a$ is now empty, and the total cost of all operations is $1$.
翻译中…

In the second test case, it's impossible to remove any prefix of the array since $a_1 \gt b_1$, so array $a$ cannot be made empty by any sequence of operations.

翻译中…

样例

Input #1
5
4 2
9 3 4 3
11 7
1 2
20
19 18
10 2
2 5 2 1 10 3 2 9 9 6
17 9
10 11
2 2 2 2 2 2 2 2 2 2
20 18 16 14 12 10 8 6 4 2 1
1 6
10
32 16 8 4 2 1
Output #1
1
-1
2
10
4
InputCopy OutputCopy
题目信息
题号
2027D1
时间限制
2 s
内存限制
256 MB
标签
二分
动态规划
图论
贪心
模拟
双指针

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。