We are given n piles of pebbles with sizes a_i. We have k colors. Need to assign colors to each pebble such that for each color c, across piles, the counts of that color differ by at most 1. That is, for each color, the number of pebbles of that color in any pile is either floor(x_c) or ceil(x_c) for some integer? Actually condition: For each color c, let b_{i,c} = count of color c in pile i. Then for all i,j, |b_{i,c} - b_{j,c}| ≤ 1. So essentially for each color c, the counts across piles form a multiset where the max and min differ by at most 1. So for each c, the distribution is "almost uniform" across piles.
We can think: For each color c, we need to assign some number of occurrences total T_c = sum_i b_{i,c}. Then b_{i,c} must be either q_c or q_c+1 for some integer q_c (≥0) where q_c = floor(T_c / n) and the remainder r_c piles get q_c+1, the rest get q_c. So the condition is that we can partition pebbles by colors such that for each pile i, the total number of pebbles assigned to pile i equals a_i, and for each color c, the distribution across piles is as described.
We need to find any assignment or determine impossibility.
Observations: Since for each color c, the counts across piles differ at most 1, the total number of pebbles of color c is at most n * (bmax_c) and at least n * (bmin_c). But we don't know bmax,bmin.
We can treat the problem as: We need to assign to each pile a multiset of colors, such that the color counts across piles are "balanced". This is reminiscent of scheduling tasks or distributing items into bins with nearly equal numbers.
Given constraints n,k ≤ 100 and a_i ≤ 100, we could construct a solution via greedy or flow.
This is similar to constructing a matrix B (n rows, k columns) where each row sum is a_i, and each column values differ at most 1. Additionally each entry is nonnegative integer.
We need to decide if such a matrix exists and produce one.
This is a classic problem: Given row sums and column sums? But we don't have column sums; they are determined by condition. The condition essentially imposes that for each column c, the entries in that column are almost equal. Equivalent to: There exist integers q_c and r_c (0 ≤ r_c < n) such that in column c, exactly r_c rows have value q_c+1, and the remaining n - r_c rows have value q_c. Then total sum of column c is T_c = (n - r_c)q_c + r_c(q_c+1) = n*q_c + r_c.
But T_c is unknown. However, the sum over c of T_c = total pebbles S = sum_i a_i. So S = sum_c (nq_c + r_c) = nsum_c q_c + sum_c r_c.
Where q_c ≥ 0 integers, and r_c ∈ [0, n-1] integers (though r_c can be n? Actually if r_c = n then all rows have q_c+1, and we could set q_c' = q_c+1 and r_c'=0; so we can assume r_c < n). So 0 ≤ r_c ≤ n-1.
We also have for each row i: sum_c b_{i,c} = a_i. So each row sum must be satisfied.
We need to find q_c and r_c and assignments of which rows get the +1 for each column.
Let’s define for each column c, we need to decide a "base" value q_c that appears in (n - r_c) rows, and q_c+1 appears in r_c rows. The total sum of column c is n*q_c + r_c.
We can think of constructing a matrix where each column is "balanced". One approach: Fill the matrix gradually: For each column, we can decide a pattern of which rows get an extra. Since we can choose any pattern, we can try to assign the "extra" counts across rows to meet row sums.
Alternatively, we can treat as a flow: We have n rows with required sums a_i. We have k colors (columns). We need to assign numbers to cells such that each cell b_{i,c} is either floor(T_c/n) or ceil(T_c/n) (which depend on T_c). This is a kind of integer matrix existence.
Given small constraints, we could attempt to find a constructive algorithm. Maybe it's always possible if certain conditions hold? Let's test examples.
Example1: n=4,k=4, a=[1,2,3,4]. Total S=10. We need to assign colors. Solution given:
pile1: [1]
pile2: [1,4]
pile3: [1,2,4]
pile4: [1,2,3,4]
Let's compute column counts:
Color1: pile1=1, pile2=1, pile3=1, pile4=1 -> all 1 -> difference 0.
Color2: pile1=0, pile2=0, pile3=1, pile4=1 -> max-min=1? Actually pile1 has 0, pile4 has1 => diff1. So balanced.
Color3: pile1=0, pile2=0, pile3=0, pile4=1 -> diff1? Actually max=1 min=0, okay.
Color4: pile1=0, pile2=1, pile3=1, pile4=1 -> min=0, max=1 diff1.
So yes.
Example2: n=5,k=2, a=[3,2,4,1,3] total=13. Try to see if possible? They say NO.
Example3: n=5,k=4, a=[3,2,4,3,5] total=17. Output YES and given assignment:
Row1: 1 2 3
Row2: 1 3
Row3: 1 2 3 4
Row4: 1 3 4
Row5: 1 1 2 3 4
Compute columns:
Color1: counts: row1:1, row2:1, row3:1, row4:1, row5:2 -> max=2, min=1 diff=1 allowed.
Color2: row1:1, row2:0, row3:1, row4:0, row5:1 -> max=1, min=0 diff=1.
Color3: row1:1, row2:1, row3:1, row4:1, row5:1 -> all 1 diff=0.
Color4: row1:0, row2:0, row3:1, row4:1, row5:1 -> max=1, min=0 diff=1.
So yes.
So how to determine possibility? Let's derive necessary conditions.
For each column c, values b_{i,c} differ by at most 1. So for each column, the set of values is either all equal or some have one extra. So for each column, we can think of it as: there exists an integer d_c such that b_{i,c} ∈ {d_c, d_c+1} for all i. d_c can be 0.
If we let for each column, let d_c = floor(T_c / n) and r_c = T_c mod n, then exactly r_c rows have d_c+1 and n-r_c have d_c.
Now, each row i has sum a_i. So sum over c of b_{i,c} = a_i. So we need to assign for each column, a set of rows that get the +1. That is, we need to choose for each column c a subset S_c of rows of size r_c (the ones that get d_c+1) such that for each row i, the sum over columns of (d_c + I(i ∈ S_c)) = a_i. Since d_c is the base for column c.
So we have: a_i = sum_c d_c + sum_c I(i ∈ S_c). Let D = sum_c d_c. Then a_i = D + t_i, where t_i = number of columns where row i is in S_c (i.e., gets the extra). Since D is constant across rows? Wait, D is sum of d_c, which is constant across rows. So t_i = a_i - D. Thus, for the assignment to exist, all a_i - D must be nonnegative integers (since t_i counts columns where row i gets the extra; also t_i cannot exceed k, the number of columns, because it's count of extras in that row). Also, D must be integer. Also, we must have sum_i t_i = sum_c r_c (since total number of extra assignments = sum_c |S_c| = sum_c r_c). And sum_i t_i = sum_i (a_i - D) = S - n*D.
But we also have sum_c r_c = sum_c (T_c - nd_c) = S - nD. So consistent.
So the necessary and sufficient condition reduces to: There exists integer D and nonnegative integers d_c, r_c (0 ≤ r_c < n) such that:
-
For each column c: d_c ≥ 0, r_c ∈ [0, n-1] (or 0 to n, but we can reduce as said). Actually r_c can be from 0 to n-1 inclusive. (If r_c = n, we could increase d_c by 1 and set r_c=0, so without loss we assume 0 ≤ r_c < n.)
-
sum_c (n*d_c + r_c) = S (total pebbles).
-
For each row i, define t_i = a_i - D, where D = sum_c d_c. Then we require t_i nonnegative integers and also 0 ≤ t_i ≤ k? Actually t_i is the number of columns where row i gets extra. That number cannot exceed k. But also it's automatically ≤ k if we have k columns; but it's okay because t_i = a_i - D, and if D is large, t_i might be negative. So we need a_i ≥ D for all i. Also t_i ≤ k? Actually there is no explicit upper bound because row i can have at most k extras? Since there are k columns, the extra count for a row can be at most k (each column gives at most 1 extra). So we need t_i ≤ k. That gives D ≥ a_i - k. So D must be between max(0, max_i a_i - k) and min_i a_i? Actually from a_i ≥ D, we need D ≤ min_i a_i. Also from t_i = a_i - D ≤ k, we need D ≥ a_i - k for all i, i.e., D ≥ max_i (a_i - k). So D must be in [max(0, max_i(a_i - k)), min_i a_i]. But D is sum_c d_c, where d_c are nonnegative integers, and we have k columns. So D can range from 0 to something.
-
Additionally, we must be able to assign the extras to rows such that each column c gets exactly r_c rows with extra, and each row i gets exactly t_i extras. This is a bipartite assignment problem: we have columns with capacities r_c (how many rows they need to give extra to), and rows with demands t_i (how many extras they need to receive). We need to choose a 0-1 matrix X (n rows, k cols) where sum_i X_{i,c} = r_c, sum_c X_{i,c} = t_i. This is a classic feasibility of bipartite graph: it exists if and only if sum_i t_i = sum_c r_c (already) and for any subset of rows, the sum of t_i ≤ sum_c min(r_c, |subset|)? Actually it's a transportation problem. But more directly, such a binary matrix exists if and only if the t_i's and r_c's are nonnegative integers and the sum condition holds, plus a condition like Hall's condition for regular bipartite? Actually it's a simple existence of a matrix with given row and column sums. For binary matrices, the Gale-Ryser theorem gives conditions: There exists a binary matrix with row sums vector t (length n) and column sums vector r (length k) if and only if sum t = sum r and for all subsets of rows, the sum of t_i for those rows is ≤ sum over columns of min(r_c, size_of_subset). But here the matrix is not necessarily 0-1? It is exactly 0-1 because extras are 1 per column per row? Actually X_{i,c} ∈ {0,1} indicating whether row i gets the extra in column c. So it's a binary matrix.
But maybe there is a simpler condition: Since we can freely choose d_c and r_c as we like, maybe we can set d_c and r_c in a convenient way to ensure such X exists trivially (e.g., by ordering rows and columns). Actually, we can try to construct a solution without needing to check deep conditions by a greedy algorithm that directly fills pebbles.
Let's think: The condition |b_{i,c} - b_{j,c}| ≤ 1 means the distribution of each color across piles is almost uniform. This is similar to constructing a matrix where each column is "balanced". There is known necessary and sufficient condition: For any subset of piles, the total pebbles in those piles must be between some bounds related to colors. However, maybe we can find a simpler characterization.
Another approach: Since each color appears in each pile either floor(T_c/n) or ceil(T_c/n). The sum over piles of b_{i,c} is T_c. The difference constraint implies that for each color, the count in a pile is either ⌊T_c/n⌋ or ⌈T_c/n⌉. So essentially, we can treat the whole problem as a kind of "round robin" assignment.
We can also think of constructing color assignments directly: For each pebble, assign a color such that across piles, each color appears as uniformly as possible. This is like a scheduling: we have total S pebbles, we need to assign them to colors, then distribute each color's pebbles evenly across piles.
We could try to fill the pebbles in order: iterate over piles, assign colors in a round-robin fashion across piles? But that would try to balance colors per pile? Not exactly.
Alternatively, consider constructing a sequence of all pebbles across piles, but we need per pile structure. However, we could view the whole table as a matrix of pebble positions. For each pebble, we assign a color. The condition is about counts per pile per color. This is similar to: For each color, we assign pebbles to piles, such that the number of pebbles of that color in each pile is either floor or ceil of total/ n.
This is reminiscent of the concept of "balanced incomplete block designs"? Not exactly.
Given small constraints, we can solve with network flow: We have n piles (rows) and k colors (columns). We need to decide b_{i,c} (nonnegative integers) such that:
- sum_i b_{i,c} = T_c (unknown)
- For each c, the values b_{i,c} differ at most 1.
- sum_c b_{i,c} = a_i.
We can treat the condition on b_{i,c} as: For each column c, all entries are either p_c or p_c+1 for some p_c≥0. So we can treat p_c as integer, and a binary variable x_{i,c} indicating if it's p_c+1 (x=1) or p_c (x=0). Then b_{i,c}=p_c + x_{i,c}. Then we have:
For each row i: sum_c p_c + sum_c x_{i,c} = a_i => sum_c x_{i,c} = a_i - sum_c p_c = t_i.
For each column c: sum_i x_{i,c} = r_c (where r_c is number of rows with extra, which can be from 0 to n). Also, total sum over c of (n p_c + r_c) = S.
We need to find nonnegative integer p_c and binary x_{i,c} meeting row sums t_i and column sums r_c, with t_i = a_i - P where P = sum_c p_c.
But also note that p_c and r_c are not independent; for each c, r_c can be any number from 0 to n, but the total T_c = n p_c + r_c must be nonnegative. Also p_c is nonnegative.
We need to find P such that t_i = a_i - P are nonnegative and ≤ k (since row i can get at most k extras), and also sum_i t_i = S - nP = sum_c r_c. And we need to be able to assign binary matrix with row sums t_i and column sums r_c (where r_c are unknown but sum to S - nP). This suggests that we can choose P and then determine r_c and the assignment.
Better: Let's try to see if there's a constructive method that always works when a solution exists, perhaps based on greedy coloring of pebbles in some order.
Another perspective: Since the condition is symmetric across piles for each color, we can think of distributing each color's pebbles as evenly as possible across piles. That suggests an algorithm: For each color c, we decide how many pebbles it will have total, T_c, then assign its pebbles to piles by giving ⌈T_c/n⌉ to some piles and ⌊T_c/n⌋ to others.
The total pebbles sum to S. So we need to pick T_c for c=1..k such that sum T_c = S, and then for each pile i, the sum over c of either floor or ceil of T_c/n (depending on which piles get the extra) equals a_i.
But if we choose T_c arbitrarily, we still have to decide which piles get the extra for each color, and that assignment must satisfy row sums a_i.
Alternatively, we can first decide the values of p_c = floor(T_c/n) and r_c = T_c mod n. Then we need to assign extras as before.
Thus the problem reduces to existence of nonnegative integers p_c, r_c (0 ≤ r_c < n) and a binary matrix X with row sums t_i and column sums r_c, where t_i = a_i - sum_c p_c, and sum_i t_i = sum_c r_c = S - n*sum_c p_c.
Simplify: Let P = sum_c p_c. Then t_i = a_i - P must be between 0 and k (since each row can get at most k extras). Also, sum_i t_i = S - nP.
Given that, we need to find p_c (nonnegative integers summing to P) and r_c (0 ≤ r_c < n) summing to S - nP, and also there exists a binary matrix X with row sums t_i and column sums r_c.
Is it always possible to choose p_c and r_c to satisfy these, given that we have freedom? Possibly we can set p_c = 0 for all colors? Then P = 0, t_i = a_i. But then t_i must be ≤ k because each row can have at most k extras (since there are only k columns). If any a_i > k, then that's impossible. In Example1: a_i are 1,2,3,4, k=4. Here a_i=4 > k=4? Actually a_4=4 equals k=4, but a_3=3 ≤4, a_2=2 ≤4, a_1=1 ≤4. So t_i = a_i are all ≤4, okay. Then we need r_c such that sum r_c = S - 0 = 10. Also r_c < n = 4. So we need to split 10 into k=4 numbers each between 0 and 3 (since r_c < n). The maximum sum for k=4 each max 3 is 12, min 0. 10 is possible: e.g., r = [3,3,2,2] sum 10. Then we need a binary matrix X (n=4 rows, k=4 columns) with row sums t_i = a_i = [1,2,3,4] and column sums r = [3,3,2,2]. Is that possible? Possibly yes. Indeed example uses p_c=0? Let's check: In the assignment, for color1, each pile has 1 pebble? Actually color1 appears in all piles: pile1 has 1, pile2 has 1, pile3 has 1, pile4 has 1. So for color1, b_{i,1}=1 for all i, so p_1=1? Wait if p_c is the base (floor), then for color1, all rows have 1, so floor(T_1/n) = 1, r_1 = 0? Because all equal 1, so p_1=1, r_1=0. Similarly, color2: values [0,0,1,1]. That is p_2=0? floor(2/4)=0, r_2=2? Actually total T_2=2, so floor=0, ceil=1, r_2=2 rows get 1. So p_2=0, r_2=2. Color3: [0,0,0,1] => T_3=1 => p_3=0, r_3=1. Color4: [0,1,1,1] => T_4=3 => p_4=0? floor(3/4)=0, r_4=3. So p_c = [1,0,0,0] sum P=1. Then t_i = a_i - P: a=[1,2,3,4], P=1 => t_i=[0,1,2,3]. Indeed, t_i counts extras: row1 extras: 0 (since only color1 base 1, but row1 got color1 base 1, plus no extras because color1 base already accounted; extras from colors 2,3,4: row1 got none, so t1=0). Row2: extras: color4 gave extra (since got 1, base 0) and maybe? Actually row2 has colors1(base) and 4(extra) => t2=1; row3: colors1(base),2(extra),4(extra) => t3=2; row4: colors1(base),2(extra),3(extra),4(extra) => t4=3. So t_i matches. And r_c = [0,2,1,3] sum=6 = S - nP = 10 - 4*1 = 6. So indeed.
Thus p_c need not be zero. So we need to find a feasible P and distribution of p_c and r_c and binary matrix.
Constraints small: n,k <= 100, a_i <=100. So we can try to search over possible P? Because P = sum p_c. Since p_c are nonnegative, P can range from 0 up to floor(S/n) perhaps? Actually sum_c p_c = P, and we have S = nP + sum_c r_c, with sum_c r_c < kn? Actually r_c < n, so sum_c r_c ≤ k(n-1). So nP ≤ S ≤ nP + k(n-1). Thus P = floor((S - something)/n). But P can be from 0 up to floor(S/n). However, also need t_i = a_i - P between 0 and k inclusive (since t_i ≤ k). So P must satisfy a_i - k ≤ P ≤ a_i for all i, i.e., max_i (a_i - k) ≤ P ≤ min_i a_i. Since a_i ≤ 100, k ≤ 100, this range is at most 100. So we can iterate over all possible integer P in that range. For each P, check if we can find p_c and r_c and binary matrix.
But we also need to find p_c (nonnegative integers) that sum to P. There are many possibilities. However, the existence of binary matrix might only depend on the vector t_i (which is determined by P) and the r_c's, but we have freedom to choose r_c's as long as sum is R = S - nP, and each r_c < n. Also we need to pick p_c such that r_c can be associated with columns, but p_c don't affect the binary matrix except through r_c? Actually the binary matrix only uses the extras. The base values p_c are added to each row regardless (they are part of each row's sum). So the row sum from bases is P (since each row gets sum_c p_c = P). So row i's total is P + t_i, and we need that to equal a_i, so t_i = a_i - P. So that's fixed given P. Then we need to assign extras: For each column c, we need to assign exactly r_c rows to get an extra. The column sums of extras must be r_c. The existence of binary matrix with given row sums t_i (each between 0 and k) and column sums r_c (each between 0 and n-1, sum = R) is the only remaining condition.
Thus, given P, we have t_i = a_i - P (must be nonnegative ≤ k). Also we have R = S - nP must be nonnegative and integer. Also R must be ≤ k*(n-1) because each r_c ≤ n-1, so R ≤ k*(n-1). And also R must be ≥ 0. Also t_i sum to R automatically (since sum_i t_i = sum_i (a_i - P) = S - nP = R). So the row sums and total column sum match.
Now we need to know if there exists a binary matrix with row sums t_i (length n) and column sums r_c (length k) such that each r_c < n. Since we are free to choose the r_c distribution (subject to sum R and each ≤ n-1). So we can also choose the column sums to facilitate existence. So the problem reduces to: Given t_i (nonnegative integers, sum R, each ≤ k), can we find a sequence r_c (nonnegative integers, each ≤ n-1, sum R) such that there exists a binary matrix with those row and column sums? And we need to also output the matrix.
If we can always construct such a binary matrix for any t_i (with t_i ≤ k and sum R) by appropriate choice of r_c, then the condition reduces to existence of P such that t_i meet constraints and R = S - nP is between 0 and k*(n-1). But is it always possible to choose r_c to realize a given t_i? Not always. For instance, consider extreme case: n=2, k=2, a = [2,0]? But a_i must be positive (since piles have at least 1 pebble). But let's say n=2, k=1, a=[2,0] not allowed because a_i positive? Actually constraints say ai >= 1, so all positive. So t_i will be nonnegative. But still there might be constraints: For a binary matrix with given row sums and column sums, a necessary and sufficient condition is the Gale-Ryser theorem. Given we can choose column sums, maybe we can always adjust to satisfy the theorem for any t_i? Let's explore.
We have t_i values sum to R. We need to pick k numbers r_c between 0 and n-1 summing to R. Moreover, we want existence of binary matrix with row sums t and column sums r. The binary matrix existence condition: For any subset of rows, the sum of t_i over that subset must be ≤ sum over columns of min(r_c, |subset|). Also, conversely, for any subset of columns, the sum of r_c over that subset must be ≤ sum over rows of min(t_i, |subset|). But we can choose r_c to satisfy these.
One simple way: Set all r_c equal to either floor(R/k) or ceil(R/k) so that they differ by at most 1, ensuring each ≤ n-1? But we need to ensure each r_c < n. Since R ≤ k*(n-1), it's possible to have all r