Prefix Sum Addicts
Contest: Codeforces Global Round 22
Difficulty rating: 1200
Tags: constructive algorithms, greedy, math, sortings

Suppose $$$a_1, a_2, \dots, a_n$$$ is a sorted integer sequence of length $$$n$$$ such that $$$a_1 \leq a_2 \leq \dots \leq a_n$$$.

For every $$$1 \leq i \leq n$$$, the prefix sum $$$s_i$$$ of the first $$$i$$$ terms $$$a_1, a_2, \dots, a_i$$$ is defined by $$$$$$ s_i = \sum_{k=1}^i a_k = a_1 + a_2 + \dots + a_i. $$$$$$

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

Formally, given $$$k$$$ integers $$$s_{n-k+1}, \dots, s_{n-1}, s_{n}$$$, the task is to check whether there is a sequence $$$a_1, a_2, \dots, a_n$$$ such that

1. $$$a_1 \leq a_2 \leq \dots \leq a_n$$$, and
2. $$$s_i = a_1 + a_2 + \dots + a_i$$$ for all $$$n-k+1 \leq i \leq n$$$.

Input format:
Each test contains multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \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 $$$n$$$ ($$$1 \leq n \leq 10^5$$$) and $$$k$$$ ($$$1 \leq k \leq n$$$), indicating the length of the sequence $$$a$$$ and the number of terms of prefix sums, respectively.

The second line of each test case contains $$$k$$$ integers $$$s_{n-k+1}, \dots, s_{n-1}, s_{n}$$$ ($$$-10^9 \leq s_i \leq 10^9$$$ for every $$$n-k+1 \leq i \leq n$$$).

It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^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).

Constraints:
Time limit: 2.0 s
Memory limit: 512.0 MB

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

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

In the third test case, the prefix sums define the only sequence $$$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.

Example input:
4
5 5
1 2 3 4 5
7 4
-6 -5 -3 0
3 3
2 3 4
3 2
3 4

Example output:
Yes
Yes
No
No

Editorial:
If $$$k = 1$$$, it is always possible, so the answer is "YES".
In the following, we assume that $$$k \geq 2$$$. Here, we are given $$$s_{n-k+1}, \dots, s_n$$$. We can resume $$$a_{n-k+2}, \dots, a_{n}$$$ by letting $$$a_{i} = s_{i} - s_{i-1}$$$ for every $$$n-k+2 \leq i \leq n$$$.
If the known elements of $$$a_i$$$ cannot form a sorted array, i.e., it does not hold that $$$a_{n-k+2} \leq \dots \leq a_n$$$, the answer is "NO".
Note that the sum of the first $$$n-k+1$$$ elements of $$$a_i$$$ should satisfy that $$$s_{n-k+1} = a_1 + \dots + a_{n-k+1} \leq (n-k+1) a_{n-k+2}$$$. If this does not hold, the answer is "NO".
Having checked that both $$$a_{n-k+2} \leq \dots \leq a_n$$$ and $$$s_{n-k+1} \leq (n-k+1) a_{n-k+2}$$$ hold, we claim that the answer is "YES". A possible solution could be $$$$$$ a_i = \begin{cases} \left\lfloor\dfrac{s_{n-k+1}}{n-k+1}\right\rfloor, & 1 \leq i \leq s_{n-k+1} \bmod (n-k+1), \\ \left\lceil\dfrac{s_{n-k+1}}{n-k+1}\right\rceil, & s_{n-k+1} \bmod (n-k+1) < i \leq n-k+1. \end{cases} $$$$$$