CF BUDDY
← Problems·

1738B · Prefix Sum Addicts

1200 · constructive algorithms, greedy, math

Problem: Suppose a1,a2,,ana_1, a_2, \dots, a_n is a sorted integer sequence of length nn such that a1a2ana_1 \leq a_2 \leq \dots \leq a_n.

For every 1in1 \leq i \leq n, the prefix sum sis_i of the first ii terms a1,a2,,aia_1, a_2, \dots, a_i is defined by si=k=1iak=a1+a2++ai.s_i = \sum_{k=1}^i a_k = a_1 + a_2 + \dots + a_i.

Now you are given the last kk terms of the prefix sums, which are snk+1,,sn1,sns_{n-k+1}, \dots, s_{n-1}, s_{n}. Your task is to determine whether this is possible.

Formally, given kk integers snk+1,,sn1,sns_{n-k+1}, \dots, s_{n-1}, s_{n}, the task is to check whether there is a sequence a1,a2,,ana_1, a_2, \dots, a_n such that

  1. a1a2ana_1 \leq a_2 \leq \dots \leq a_n, and
  2. si=a1+a2++ais_i = a_1 + a_2 + \dots + a_i for all nk+1inn-k+1 \leq i \leq n.

Input Format: Each test contains multiple test cases. The first line contains an integer tt (1t1051 \leq t \leq 10^5) — the number of test cases. The following lines contain the description of each test case.

The first line of each test case contains two integers nn (1n1051 \leq n \leq 10^5) and kk (1kn1 \leq k \leq n), indicating the length of the sequence aa and the number of terms of prefix sums, respectively.

The second line of each test case contains kk integers snk+1,,sn1,sns_{n-k+1}, \dots, s_{n-1}, s_{n} (109si109-10^9 \leq s_i \leq 10^9 for every nk+1inn-k+1 \leq i \leq n).

It is guaranteed that the sum of nn over all test cases does not exceed 10510^5.

Output Format: For each test case, output "YES" (without quotes) if it is possible and "NO" (without quotes) otherwise.

You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).

Note: In the first test case, we have the only sequence a=[1,1,1,1,1]a = [1, 1, 1, 1, 1].

In the second test case, we can choose, for example, a=[3,2,1,0,1,2,3]a = [-3, -2, -1, 0, 1, 2, 3].

In the third test case, the prefix sums define the only sequence a=[2,1,1]a = [2, 1, 1], but it is not sorted.

In the fourth test case, it can be shown that there is no sequence with the given prefix sums.

Sample Cases

Case 1

Input

4
5 5
1 2 3 4 5
7 4
-6 -5 -3 0
3 3
2 3 4
3 2
3 4

Output

Yes
Yes
No
No

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback