CF BUDDY
← Problems·

1710A · Color the Picture

1500 · constructive algorithms, greedy, math

Problem: A picture can be represented as an n×mn\times m grid (nn rows and mm columns) so that each of the nmn \cdot m cells is colored with one color. You have kk pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most aia_i cells with the ii-th pigment.

A picture is considered beautiful if each cell has at least 33 toroidal neighbors with the same color as itself.

Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers 1x1,x2n1 \leq x_1,x_2 \leq n and 1y1,y2m1 \leq y_1,y_2 \leq m, the cell in the x1x_1-th row and y1y_1-th column is a toroidal neighbor of the cell in the x2x_2-th row and y2y_2-th column if one of following two conditions holds:

  • x1x2±1(modn)x_1-x_2 \equiv \pm1 \pmod{n} and y1=y2y_1=y_2, or
  • y1y2±1(modm)y_1-y_2 \equiv \pm1 \pmod{m} and x1=x2x_1=x_2.

Notice that each cell has exactly 44 toroidal neighbors. For example, if n=3n=3 and m=4m=4, the toroidal neighbors of the cell (1,2)(1, 2) (the cell on the first row and second column) are: (3,2)(3, 2), (2,2)(2, 2), (1,3)(1, 3), (1,1)(1, 1). They are shown in gray on the image below:

The gray cells show toroidal neighbors of (1,2)(1, 2).

Is it possible to color all cells with the pigments provided and create a beautiful picture?

Input Format: Each test contains multiple test cases. The first line contains the number of test cases tt (1t1041 \leq t \leq 10^4). The description of the test cases follows.

The first line of each test case contains three integers nn, mm, and kk (3n,m1093 \leq n,m \leq 10^9, 1k1051 \leq k \leq 10^5) — the number of rows and columns of the picture and the number of pigments.

The next line contains kk integers a1,a2,,aka_1,a_2,\dots, a_k (1ai1091 \leq a_i \leq 10^9) — aia_i is the maximum number of cells that can be colored with the ii-th pigment.

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

Output Format: For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes).

Note: In the first test case, one possible solution is as follows:

In the third test case, we can color all cells with pigment 11.

Sample Cases

Case 1

Input

6
4 6 3
12 9 8
3 3 2
8 8
3 3 2
9 5
4 5 2
10 11
5 4 2
9 11
10 10 3
11 45 14

Output

Yes
No
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