We need to parse problem: Given non-decreasing a of length n, each nonnegative, a_n > 0. We need to represent a as sum of m non-decreasing arrays b_i (each length n, nonnegative integers, non-decreasing) with the additional constraint that each b_i has at most k distinct values (i.e., the number of distinct integers appearing in that array ≤ k). We want minimal m. If impossible, output -1.
Observations: The b_i arrays are independent but must sum to a. They are all non-decreasing sequences of non-negative integers.
We need to find minimal number of such sequences, each with limited distinct values (bounded by k).
We can think of each b_i as a piecewise constant? Actually non-decreasing and at most k distinct values implies that each b_i is a step function with at most k steps, i.e., it can change value at most k-1 times. Since non-decreasing, it can stay same and then increase to a new value. So each b_i is composed of at most k plateaus (constant segments). But the array length is n, so it's basically a sequence with at most k distinct numbers, sorted non-decreasing.
Goal: Represent a as sum of such sequences.
Important: Since a is non-decreasing, we need b_i non-decreasing individually, their sum will be non-decreasing automatically.
We want minimal m. Clearly, if we could use one array, condition is: can we have a single non-decreasing array b with at most k distinct values? That would require that a itself has at most k distinct values. Because if we use m=1, b = a, then distinct values in b equals distinct values in a must be ≤ k. So if distinct(a) ≤ k, then m=1 is possible. But wait: Is there any other possibility with m=1 where b is not exactly a? Actually if m=1, then we must have b = a, because sum is just b. So condition for m=1 is exactly distinct(a) ≤ k.
If distinct(a) > k, then we need at least 2? But maybe we can't always do it with any m? The problem asks to report -1 if impossible. When could it be impossible? Since we can always use many arrays each maybe 0? But each b_i must be non-decreasing, non-negative. Summing to a. Could there be a scenario where it's impossible? Possibly if a_n > 0 and we try to represent with arrays that each have at most k distinct values, maybe there is some constraint on the "slope"? Let's explore.
We need to partition the "increase" of a into multiple sequences. Each sequence can have at most k distinct numbers. Since each b_i is non-decreasing, we can think of each b_i as having some values at positions. The total distinct values per array limited.
We need to find minimum m such that we can decompose a into m monotone non-decreasing arrays each with at most k distinct values.
This is reminiscent of "splitting a monotone sequence into a few sequences with bounded number of distinct values" which might be related to "chromatic number" or "interval covering".
Alternatively, think in terms of the differences: For a non-decreasing array a, define increments: Let d_j = a_j - a_{j-1} (with a_0=0). Then a_j = sum_{t=1 to j} d_t. Each d_t ≥ 0. The sum of all b_i at each position must equal a_j. If we think about each b_i, they are non-decreasing, so they also have increments delta_i,j = b_i,j - b_i,j-1 (with b_i,0=0). Then sum_i delta_i,j = d_j. The constraint that b_i has at most k distinct values is equivalent to that the number of indices where delta_i,j > 0 (i.e., where b_i increases) is at most k-1? Not exactly: b_i can increase by more than 1 at a single step, but the distinct values count is number of distinct b_i,j values. Since it's non-decreasing, the number of distinct values equals the number of times it changes value plus one (if the first value >0 maybe?) Actually if b_i,1 = some value, and later it increases to a new value, that's a new distinct value. So if it changes value at positions j (where b_i,j > b_i,j-1), each change introduces a new distinct value. But it could also have same value after increasing? Actually once it increases, it cannot go down, so distinct values are the set of values taken. If it increases in steps, each step could be to a value that hasn't been seen before. But could it increase to a value that has been seen before? Since it's non-decreasing, if it increases, the new value must be greater than previous, so cannot repeat a lower value. So distinct values are exactly the set of values that appear, which correspond to the values at the start of each plateau. The number of distinct values is equal to the number of plateaus, i.e., the number of maximal intervals where b_i is constant. Since the array is monotone, it can be described by breakpoints where value changes. So number of distinct values = number of times value changes + 1 (if we consider first constant segment). That is at most k => number of changes ≤ k-1.
Thus each b_i can have at most k-1 increases (positions where b_i,j > b_i,j-1) if we consider distinct values count includes initial value. But careful: b_i could start at 0, which is a distinct value; if it stays 0 whole time, that's 1 distinct value. If it starts positive, still counts as distinct. So the constraint is that the number of indices j where b_i,j > b_i,j-1 is at most k-1. However, what about increments that are >0 but not increasing distinct value? For instance, if b_i,j = b_i,j-1 + some positive amount, that would increase the value, so it's a change. But could b_i increase and still not create a new distinct value? No, because the value becomes larger than before, so it's a new distinct value unless it was already that value before? Since it's increasing strictly, it's a new value (unless there were duplicates later? But if it increased to a value that later appears? Actually after increasing, the value becomes new; but later it might stay constant at that value, but the distinct count includes that value. So yes, any positive increment creates a new distinct value at that position (if the new value wasn't present earlier). But could it have the same value as a previous plateau? Not possible because monotonic: values only increase, so once you leave a value, you never come back to it, and you never encounter it again because you are always increasing or staying same. So each increase yields a strictly larger value, which is distinct from all previous values. Therefore each increase corresponds to a new distinct value. So number of increases = number of distinct values - 1 (if final value is also distinct). So number of increases ≤ k-1.
But there's nuance: if b_i starts with value >0, that's the first distinct value, but it's not an increase from previous (since we consider b_i,0 = 0, maybe we treat it as increase if b_i,1 > 0? Usually we consider b_i,1 as first element; there's no previous. However, the distinct values count includes the initial value. So the number of increases (i.e., indices j ≥2 where b_i,j > b_i,j-1) plus possibly the initial element being >0? Actually the number of distinct values equals 1 + number of indices j where b_i,j > b_i,j-1, because the first value (b_i,1) is a distinct value, and each time we increase we get a new distinct value. That holds regardless of whether b_i,1 > 0 or =0. So yes, increases (j=2..n) count as new distinct values. So total distinct values = 1 + (# of increases). So #increases ≤ k-1.
Thus each b_i is limited in how many times it can increase.
Now we need to decompose the total increments d_j among the m sequences. For each position j (1..n), we have d_j = sum_i delta_i,j, where delta_i,j = b_i,j - b_i,j-1 (with b_i,0=0). delta_i,j ≥ 0. And for each i, the number of indices j where delta_i,j > 0 is ≤ k-1.
But note that delta_i,j could be >0 at position j, meaning that b_i increases at j. Since b_i is non-decreasing, we can allow multiple increments at same position? Actually at a given j, delta_i,j is the increase from previous position to j. So yes, at each position j, each b_i can have some increase (maybe 0). The constraint is on the number of positions where delta_i,j > 0 (i.e., where b_i actually jumps) over j=1..n.
Also note that b_i,1 = delta_i,1 (since b_i,0=0). So the first element itself is an increase from 0 to b_i,1. So that counts as an increase if b_i,1 > 0. So we treat j=1 also as a possible increase position? In our definition, delta_i,1 = b_i,1 - 0 = b_i,1, so if b_i,1 > 0, that is a positive delta. So total number of positive deltas for i across j=1..n must be ≤ k-1? Wait, distinct values count = 1 + number of j ≥2 where b_i,j > b_i,j-1, but does b_i,1 > 0 count as a new distinct value? Yes, it is a distinct value. However, the count of distinct values includes the value at j=1 regardless of whether it's zero or positive. If b_i,1 = 0, distinct values includes 0, but that doesn't count as an increase because it's from 0? Actually if b_i,1 = 0, then the distinct values set includes 0. But the number of increases from previous element (which we treat as 0) does not count? In our earlier formula: distinct values = 1 + (# of j where b_i,j > b_i,j-1) for j ≥2? Let's test: Suppose b_i = [0,0,5,5]. Here, distinct values: {0,5} => count 2. Increases: at j=3, b_i,3=5 > b_i,2=0 => one increase (j=3). At j=2, b_i,2=0 = b_i,1=0, no increase. So #increases =1, distinct = 2 = 1+1. That works. But consider b_i = [5,5,5]. Distinct values: {5} => count 1. Increases: j=1? b_i,1=5 > 0? But we didn't define b_i,0 = 0? If we consider b_i,0 = 0, then at j=1, delta_i,1 =5 >0, that is an increase from 0 to 5. That would count as an increase, but our formula distinct = 1 + (# of j where b_i,j > b_i,j-1 for j≥2) fails because it would give 1+0=1, but we have one increase at j=1. However, note that distinct values count includes the first value 5. So we could define #increases = number of indices j (from 1 to n) where b_i,j > b_i,j-1, with b_i,0 = 0. Then distinct values = (1 if b_i,1 > 0? Hmm, need to handle zero). Actually careful: The distinct values set includes 0 only if some b_i,j = 0. If b_i,1 = 5, then 0 is not necessarily a value in the array. So distinct values are exactly the set of values taken by b_i,j for j=1..n. That set does not necessarily contain 0. So the count of distinct values is not simply 1 + number of positive deltas from 0? Let's define: Let r_i be the number of distinct values in b_i. Since b_i is non-decreasing, the sequence of values is something like v1 ≤ v2 ≤ ... ≤ vn. Distinct values appear as v_{t_1} < v_{t_2} < ... < v_{t_r}. where t_1 =1. So r is the number of times the value changes plus one (including first). The changes occur at indices where b_i,j > b_i,j-1. Those indices are among j=2..n. So the number of changes = # {j: 2 ≤ j ≤ n, b_i,j > b_i,j-1}. Then distinct values = 1 + that number. That's valid because the first value is always a distinct value, regardless of whether it's equal to 0 or not. The fact that b_i,0=0 is not part of the array, so it's irrelevant. So increases from 0 at j=1 do not correspond to a change within the array; they just set the first value. So if b_i,1 > 0, that's a distinct value, but that doesn't count as a change from previous element within the array. So we should not count j=1 as an increase for the purpose of distinct values count. Therefore, number of distinct values = 1 + (# of indices j from 2 to n where b_i,j > b_i,j-1). So the constraint is that this number ≤ k, i.e., #changes ≤ k-1.
Thus each b_i can have at most (k-1) positions (among indices 2..n) where it strictly increases.
But what about the first value? It can be any non-negative, but if it's positive, it's a distinct value. However, that doesn't impose a separate "increase" count. So we can set b_i,1 arbitrarily, as long as the total distinct values count is ≤ k. That means we could have up to k distinct values, which could include 0 or not. So effectively, b_i can have at most k plateaus.
Now, how does this relate to the increments d_j? For each b_i, the values are determined by the initial value and the increments at change points. But the increments at positions j are exactly the amounts by which b_i increases at that position. However, note that if b_i jumps by a positive amount at some j, that j is a change point (if j≥2) or if j=1, it's just the starting value. But we can treat b_i,1 as the "increment" at j=1 from 0. But that increment at j=1 also contributes to the distinct values count? Actually b_i,1 is a distinct value, but it doesn't count toward the number of changes (since changes counted from j=2 onward). However, we still have a degree of freedom: b_i,1 can be arbitrarily large, but it's still just one distinct value (the first plateau). So the number of distinct values is 1 + (# of changes). So the total number of positions where we can have positive delta_i,j for j≥2 is limited to at most k-1. For j=1, we can have positive delta_i,1 without affecting that limit (since it's not a change within the array, but it's still a distinct value). But wait, if we set b_i,1 > 0, that uses up one distinct value (the value at position 1). The limit is on total distinct values, not specifically on changes. So b_i,1 being positive uses one of the k slots. So we need to consider that we have at most k distinct values overall. That means the total number of positions where we have "new" distinct values introduced is at most k. The first position (j=1) introduces the first distinct value (if b_i,1 is some value). Then each change (j where b_i,j > b_i,j-1) introduces a new distinct value. So the total number of distinct values = (# of indices j where b_i,j is the first occurrence of that value). That is essentially the number of "segments". So it's easier to think of b_i as a sequence that can change value at most k-1 times (i.e., at most k segments). That includes the first segment starting at j=1.
Thus the constraint can be restated: b_i can be represented by at most k constant runs (consecutive intervals covering 1..n) with non-decreasing values (since each run's value must be ≤ next run's value). And within each run, the value is constant. So we partition the indices 1..n into at most k intervals (contiguous, because runs are contiguous due to non-decreasing? Actually if it's non-decreasing, the values can stay constant for a while then increase. The intervals where it's constant are contiguous and cover all indices. So yes, it's a partition into contiguous blocks where values are constant and strictly increase between blocks. So b_i can be described by a sequence of breakpoints 1 = p0 < p1 < p2 < ... < p_{r} = n+1? Actually if we have r runs, r ≤ k. The runs are intervals [l_1, r_1], [l_2, r_2], ..., [l_r, r_r] where l_1=1, r_r = n, and l_{t+1}=r_t+1 (or maybe overlapping? They should be contiguous). And values v_1 < v_2 < ... < v_r (strictly increasing because if equal they would be same run). Actually they could be equal? If equal, they would merge into one run. So we can assume v_t < v_{t+1}. So b_i,j = v_t for j in that run.
So each b_i contributes to the sum a_j as sum over i of v_{i,t} for the run covering j.
We need to find minimum number of such run-limited sequences that sum to a.
Alternative viewpoint: We are given a non-decreasing target sequence a. We need to cover it with m "layers" each of which is a non-decreasing step function with at most k steps (i.e., at most k distinct values). Equivalent to representing a as a sum of m such step functions. Since a is non-decreasing, we can think of its increments d_j. Each layer can allocate its increments at specific positions.
We need to find the minimal m for which a decomposition exists.
First, necessary conditions: Since each b_i is non-decreasing, their sum is non-decreasing (ok). The main limitation is that each b_i cannot have too many changes.
Observation: For any j, the sum of the b_i up to j must be a_j. Think about the cumulative sums.
Maybe we can formulate as a covering problem on the "breakpoints" needed to represent a.
Consider the number of distinct values in a, say D. If D ≤ k, m=1 works. If D > k, we need at least ceil(D/k) maybe? Because each b_i can contribute at most k distinct values, but the sum of such arrays could have many distinct values because of combinations. However, note that the sum of step functions can produce a more complex function. For instance, if we have two step functions each with ≤k distinct values, the sum can have up to something like O(k^2) distinct values (if the breakpoints align differently). So maybe we can achieve a large D with small m. The example: a with 11 elements and k=3, answer m=2. In that example, a has many distinct values? Let's count: a = 0,1,2,2,3,3,3,4,4,4,4. Distinct values: 0,1,2,3,4 => 5 distinct values. k=3, so D=5 > 3, but m=2 works. ceil(5/3)=2, so that fits. Another example: n=5, k=3, a=1,2,3,4,5 => distinct values: 1,2,3,4,5 => 5 distinct values. ceil(5/3)=2, answer is 2. Example with 9 elements: 2,2,3,5,7,11,13,13,17. Distinct values: 2,3,5,7,11,13,17 => 7 distinct values, k=4, ceil(7/4)=2, answer 2. Example with 10 7: a has distinct: 0,1,2,3,4,5,6? Actually a: 0,1,1,2,3,3,4,5,5,6 => distinct: 0,1,2,3,4,5,6 => 7 distinct values, k=7, so m=1 works (since distinct ≤ 7). So in these examples, answer seems to be ceil(D/k). But is it always possible to achieve m = ceil(D/k)? Or maybe sometimes we need more? What about the first test case: n=4, k=1, a=0,0,0,1. D=2 (0 and 1). ceil(2/1)=2, but answer is -1. So m=2 would be suggested but it's impossible. So there are additional constraints when k=1? Let's examine: k=1 means each b_i can have at most 1 distinct value, i.e., it must be constant across all indices (since non-decreasing and only one distinct value means it's constant). So each b_i is a constant array (all same value). The sum of constant arrays is also constant? Actually if we have m constant arrays, each constant (say c_i), then the sum at each position is sum_i c_i, which is constant across j. So the total sum a must be constant. But in first test case, a is not constant (0,0,0,1). So impossible. So -1. So that's a special case where k=1 imposes that each b_i is constant, so sum must be constant. So we need to consider that when k=1, m must be such that a is constant, else impossible. Actually if a is constant, then we can take m=1 with that constant. So answer 1. But if a is not constant, no m works because regardless of m, sum of constant sequences is constant. So -1. So that's a necessary condition: if k=1, a must be constant; otherwise impossible.
Now for k>1, maybe we can always achieve m = ceil(D/k) ? But need to verify. Let's test with small cases manually.
Consider k=2. Then each b_i can have at most 2 distinct values. That means each b_i is of the form: first some constant value v for a prefix, then possibly a higher constant value w for the suffix (or entire array constant if no increase). So each b_i is a two-step function: up to some index, it's one value; after that (maybe from some index to end) it's a higher value (could be same? but then distinct values would be 1). So essentially each b_i can have at most one increase point (since 2 distinct values implies at most one increase). So we want to sum such two-step functions to get a non-decreasing a.
Now, what is the minimal m? This reminiscent of representing a monotone sequence as sum of m "step" functions each with at most one step. This is like we need to decompose the increments of a into at most m "jumps" because each b_i can have one jump. But each b_i can also contribute to the baseline constant value. Actually each b_i is determined by an initial value c_i (for all indices) and optionally a jump at some position p_i to a higher value d_i (d_i > c_i) applied to indices >= p_i. So the sum over i yields a function that is piecewise constant plus accumulated jumps. The total number of jumps across all b_i is at most m (since each b_i can have at most one jump). But also each b_i contributes a constant offset across all positions (the initial value). So the sum a_j = S + sum_{i: p_i ≤ j} (d_i - c_i), where S = sum_i c_i. So a is a non-decreasing step function whose increases occur at positions where some b_i jumps. The total increase from a_{j-1} to a_j is sum_{i: p_i = j} (d_i - c_i). So each jump position can have multiple jumps from different b_i, contributing to the increment.
Thus, with k=2, the problem reduces to: we need to represent the increments d_j (the increase at each position) as the sum of contributions from at most m jumps (since each b_i can contribute at most one jump at some position). But careful: each b_i can contribute a jump of some positive amount at exactly one position (or none if it's constant). But also each b_i contributes a baseline constant c_i. So the total S = sum c_i can be any non-negative integer, and then we have m possible jump positions with positive amounts (each assigned to a b_i's increase). The total increments across positions must equal the sum of those jump amounts. However, the jumps can be assigned to any positions; they don't have to be distinct? Actually multiple b_i can jump at the same position j. That would cause a larger increment at that j. So we can combine jumps. So essentially we need to cover the positive increments d_j (for j where a_j > a_{j-1}) using at most m "jump units" each assigned to a position. Because each b_i can contribute its jump amount entirely at one position. So if we have m b_i, the total number of jumps (i.e., b_i that actually increase) is at most m. But each jump can be of arbitrary positive integer magnitude. So the condition for existence with m is that the number of positions where d_j > 0 is at most m? Not exactly, because we can assign multiple jumps to same position, but we cannot split a single jump across multiple positions. So if d_j is positive, we need to allocate some positive integer amount to that position from the jumps. Each jump contributes its amount to exactly one position. So we need to partition the total increments into at most m jumps (each jump assigned to some position). However, there's also the possibility that some b_i have no jump (constant), they just contribute to S.
Thus, with k=2, we can always achieve m equal to the number of positive increments (i.e., number of distinct values minus 1? Actually if a is strictly increasing at many positions, each d_j may be >0). But we can combine them: if we have m jumps, we can assign multiple d_j amounts to a single jump? Wait, we need to satisfy d_j exactly. If we have a jump of amount Δ assigned to position j, it contributes Δ to d_j. If we need to produce d_j that is, say, 5, we could have one jump of 5 at j, or two jumps: one of 2 and one of 3 at same j, etc. So we can use multiple jumps to contribute to same d_j. So the total number of jumps used can be as low as 1 if we assign all increments to a single jump at the first positive position? But then a would have only one increase: from S to S+total increase at that one position, making a have only two distinct values. But if a has many distinct values (many steps), we need to match the pattern of increases at different positions. Because after the jump at position j, the value stays constant (since no further jumps) unless there are later jumps from other b_i. Actually sum of step functions each with one jump can produce a sequence that increases at multiple positions: if we have two b_i, one jumps at position 2, another jumps at position 5, then the total sum will have increases at positions 2 and 5. So the positions of increases in a correspond to the union of jump positions across b_i. So to have increases at positions j1, j2, ..., jt (where d_j >0), we need to assign at least one jump to each such position? Actually consider: if we have only one b_i, it jumps at most once, so total sum will have at most one increase (two distinct values). So to have t increase positions, we need at least t b_i? Not necessarily: if we have two b_i, one jumps at j1, the other jumps at j2, that gives two increase positions. But could we have two increase positions with only one b_i? No, because a single step function has only one jump. So the number of increase positions in a (i.e., number of indices where a_j > a_{j-1}) must be ≤ total number of jumps across all b_i. Since each b_i can contribute at most one jump (when k=2), the total jumps ≤ m. So we need m ≥ number of positive increments (let's call p = count of j where a_j > a_{j-1}). Additionally, we need to handle the magnitudes: the sum of increments at each position must equal sum of jump amounts assigned to that position. Since jump amounts can be arbitrary, we can always allocate: For each position j with positive increment d_j, assign exactly one b_i to jump at that j with amount d_j, and set all other b_i constant (maybe zero). That would use exactly p b_i, with each having one jump. So m = p works, provided p ≤? But is there any other constraint? Also we need b_i to be non-negative, and the constant part S must be non-negative. If we assign each b_i to jump at its position, we also need to ensure that the constant part (the baseline) for each b_i is non-negative and that the b_i values are non-decreasing, which they are. Also we need the sum of constants across all b_i to equal the initial value a_1? Let's see: a_1 = sum_i b_i,1. For b_i that jump at position p_i ≥ 2, b_i,1 is its constant c_i. For those that jump at position 1? Jump at position 1 would mean b_i,1 > 0 from the start. But if we define jumps as happening at position j, meaning that for indices < j, b_i = c_i; for indices ≥ j, b_i = c_i + Δ_i. That's a typical step function. However, note that if we allow a jump at j=1, that would mean b_i,1 = c_i + Δ_i? Actually the model: b_i is constant on [1, n] but if we want a jump at position 1, it means the value at index 1 is already the higher value; but then there is no lower constant before that. That can be seen as having initial value equal to the jump amount, with no lower constant. That's still a step function with one distinct value (if we treat the jump at position 1 as effectively no lower plateau). But if we want two distinct values, we need a point where value increases; that increase could be at j=1 meaning that the first value is higher than the "imagined" value before index 1 (which is 0). But within the array, that's still just one constant if we consider b_i,1 = v, and b_i,j = v for all j? Actually if the jump is at position 1, then the value for all indices is the same (the higher value). That would be a constant array, which has 1 distinct value, not 2. So to have 2 distinct values, there must be some index where the value changes from a lower to a higher value. That change must occur at some j ≥ 2, because to have two distinct values, we need at least one index where the value is different from the first index. If the change occurs at j=2, then b_i,1 = c, b_i,2..n = c+Δ. So the first value is lower; then it jumps at position 2. So jumps always happen at positions ≥2 to get two distinct values. If we want only one distinct value, we can have constant array, which corresponds to either no jump or jump at position >n (i.e., never). So for k=2, each b_i either is constant (0 jumps) or has exactly one jump at some j≥2.
Now consider the sum: a_1 = sum_i b_i,1. For those b_i that are constant (no jump) or have a jump later, b_i,1 = c_i. For those that have a jump at j≥2, b_i,1 = c_i (the lower value). So all b_i,1 are the lower constants. Then the sum at position j (≥2) includes contributions from those b_i that have jumped at or before j. So a_j = sum_i c_i + sum_{i: jump position p_i ≤ j} Δ_i.
Let S = sum_i c_i. Then a_1 = S. So S must equal a_1. That's a constraint. For each j≥2, a_j = S + sum_{i: p_i ≤ j} Δ_i. So the increments d_j = a_j - a_{j-1} = sum_{i: p_i = j} Δ_i.
Thus, to represent a, we need to assign each positive increment d_j to be the sum of Δ_i for jumps exactly at j. Also S = a_1 must equal sum of c_i.
We can choose c_i arbitrarily non-negative. So we can set S = a_1 by adjusting c_i accordingly.
Now, if we use m b_i, we can have at most m jumps (since each b_i can have at most one jump). So we need to cover the positive increments d_j (j≥2) with at most m jumps, meaning we need to partition the set of positions with d_j>0 into at most m groups? Actually each jump contributes to exactly one d_j. So the number of jumps must be at least the number of positions with d_j>0 if we require each such position to have at least one jump? Not necessarily, because a single jump can contribute to a d_j, but if there are multiple positions with positive d_j, we need jumps at each of those positions, because a jump at position j contributes only to d_j. So to have d_{j1}>0 and d_{j2}>0 with j1 ≠ j2, we must have at least one jump at j1 and at least one jump at j2. Because there is no other way to create an increase at j2 if no jump is scheduled at j2. Could we schedule a jump at j1 that also affects later positions? Yes, a jump at j1 changes the value for all indices ≥ j1, so it contributes to all later increments? Let's examine: Suppose we have one jump at position p. Then the sum function is S for j < p, and S+Δ for j ≥ p. So the increments: d_p = Δ, and for j>p, d_j = 0. So only one position gets a positive increment. So indeed, to have positive increments at multiple positions, we need jumps at each of those positions. Therefore, the number of positions j where d_j > 0 (i.e., the number of times a increases) must be ≤ total number of jumps across all b_i.
Given that each b_i can have at most one jump (for k=2), total jumps ≤ m. So necessary condition: m ≥ number of positive increments (let's denote t). And we can achieve m = t by using t b_i each with a jump at a distinct position j where d_j>0, with Δ_i = d_j, and setting c_i = 0 for all? But then S = sum c_i = 0, which would force a_1 = 0. But a_1 may be >0. However, we can also set some c_i to non-zero to achieve S = a_1. For instance, we could add an extra b_i that is constant (no jump) with c = a_1, and then use t jumps with c_i = 0? But then the jumps would be from b_i with c_i=0, and those b_i would be zero before the jump and Δ after, which is fine. However, the total number of b_i would become t+1 if we need a_1 > 0. But maybe we can incorporate the constant part into one of the jumping b_i: instead of setting c_i=0 for a jumping b_i, we can set its c_i to some value, and then the jump amount Δ_i = d_j. Then S = sum c_i = a_1. So we could have t b_i, each with a jump at a distinct j, and choose c_i such that they sum to a_1. Since c_i can be any non-negative, we can always distribute a_1 among the t b_i (e.g., set one of them to a_1 and others 0). But careful: For a b_i that has a jump, its c_i must be ≤ the value after jump? Actually since it's non-decreasing, we need c_i ≤ c_i+Δ_i, which holds if Δ_i ≥ 0. So no problem. So we can indeed have exactly t b_i: assign each positive increment d_j to a distinct b_i that jumps at j with Δ_i = d_j, and set c_i such that sum c_i = a_1 (e.g., set c_1 = a_1, others 0). However, if we set c_i = 0 for some b_i, that's fine; its values: before jump (positions < j) are 0, after jump are d_j. That's non-decreasing. And if we set c_1 = a_1 for the b_i that jumps at some j1, then before its jump, it is a_1; after jump, it becomes a_1 + d_{j1}. That's fine. The other b_i with c=0 and jumps at other positions will add to the sum accordingly. So total S = a_1. So m = t works, provided t is finite. But wait, what about b_i that have jumps at positions j, but if we set c_i = 0 for i>1, then before their jump, they contribute 0, after jump they contribute d_j. This yields a_j = a_1 + sum_{i: p_i ≤ j} d_{p_i}? Actually sum of increments from jumps up to j equals sum of d_j for those positions? Because we set Δ_i = d_{p_i} exactly. Then total sum at position j is S + sum_{p_i ≤ j} d_{p_i}. But note that sum_{p_i ≤ j} d_{p_i} should equal a_j - a_1 if the d's are exactly the increments at those positions and we have a jump for each positive d. That yields a_j = a_1 + sum_{2 ≤ i ≤ j} d_i? Actually if we have jumps exactly at each position where d_i > 0, and we set Δ_i = d_i, then sum_{p_i ≤ j} Δ_i = sum_{i=2}^{j} d_i (since only positions with positive d contribute, but if d_i=0, there is no jump, so sum_{p_i ≤ j} Δ_i = sum_{i=2}^{j} d_i because d_i=0 adds nothing). So indeed a_j = a_1 + sum_{i=2}^{j} d_i = a_j. Works. So m = number of positions j (≥2) with d_j > 0 is sufficient. That number is exactly the number of times a increases (i.e., number of distinct values minus 1 if a_1 > 0? Let's see: distinct values D. If a_1=0 and a has values 0,1,2,3,... then each increase corresponds to new distinct value, so t = D-1. But if a has repeated values, increases may be less than D-1? Actually distinct values count can be less than number of increases if there are plateaus? Example: a = 0,0,1,1,2: distinct values = 3 (0,1,2). Increases: at j=3 (0->1) and j=5 (1->2) => t=2 = D-1. If a = 2,2,2,5,5: distinct=2 (2,5), increases: at j=4 (2->5) => t=1 = D-1. So generally, if a_1 is the smallest distinct value, then each new distinct value appears at the first index where it occurs, which corresponds to an increase. So t = number of indices j where a_j > a_{j-1}, which equals number of times the value changes, which is exactly D-1 (since the first distinct value appears at j=1). So t = D-1. Indeed, distinct values D = 1 + t. So m ≥ D-1. With k=2, we can achieve m = D-1. But is that minimal? Could we do with fewer? Suppose D=3, we need at least 2 increases? Could we have m=1? That would give at most 2 distinct values (since one b_i can have at most 2 distinct values, but sum of constant? Actually with m=1, b_1 can have at most 2 distinct values, so a would have at most 2 distinct values. So if D>2, m=1 impossible. So m must be at least ceil((D-1)/1)? Wait, each b_i can contribute at most one increase (since k=2). So total increases capacity = m (since each b_i can have at most one increase). To achieve D-1 increases, we need m ≥ D-1. So minimal m = D-1. So for k=2, answer is D-1? But wait, the example: n=5, k=3 gave D=5, answer 2, not 4. For k=2, let's test a hypothetical: n=3, k=2, a=1,2,3 (distinct D=3). Then D-1=2, so answer would be 2. Can we do with m=2? Yes as described. Could we do with m=1? No because b_1 can have at most 2 distinct values, so a would have at most 2 distinct. So minimal m=2. That seems plausible. But is there any scenario where D-1 is not achievable because of non-negativity or something? Probably achievable as described. So for k=2, answer = D-1 (provided D>1). But wait, check a_1 could be zero? That doesn't matter.
Now consider k=3. Each b_i can have at most 3 distinct values, i.e., at most 2 increases (since distinct values = 1 + number of increases). So each b_i can contribute up to 2 increase points (jumps). So total number of increases we can cover with m arrays is at most 2m (since each b_i can have up to 2 increases). But there may be additional constraints: the positions of increases for a given b_i must be in increasing order, but that's natural. Also the values must be non-decreasing, which is fine.
Thus, to achieve D-1 total increases, we need 2m ≥ D-1, i.e., m ≥ ceil((D-1)/2). That's a lower bound. But is it sufficient? We need to show that we can always achieve m = ceil((D-1)/2) with k=3. Let's test the given example: a with D=5, k=3 => ceil(4/2)=2, matches answer. Another example: D=7, k=4 => each b_i can have up to 4 distinct values => up to 3 increases. So lower bound m ≥ ceil((D-1)/3). For D=7, ceil(6/3)=2, matches answer. For first test case, k=1 => each b_i has at most 1 distinct => 0 increases, so need m infinite unless D=1, else impossible. That matches.
So pattern suggests answer is ceil((D-1)/(k-1)) for k>1, provided other constraints? But we need to verify for k=2: (D-1)/(1) = D-1, ceil is D-1. Good.
But what about the case where a_1 > 0? That doesn't affect D-1 count. Also what about a with repeated values? D-1 is number of distinct values minus one.
Now, is there any scenario where this simple formula fails? Let's test more cases, especially where k is large relative to D, the formula gives m=1 if D ≤ k, which matches condition distinct(a) ≤ k, because then ceil((D-1)/(k-1)) might be 1 when D ≤ k? Let's compute: For D=3, k=3: (3-1)/(3-1)=2/2=1 => ceil=1, works. For D=3, k=4: (2)/(3)=0.666 => ceil=1, works. For D=1, k arbitrary: (0)/(k-1)=0 => ceil=0? But m can be 1? Actually if D=1 (all elements equal), we can take m=1 (b_1 = a). The formula ceil((0)/(k-1)) would be 0, but m must be at least 1 because we need to represent a as sum of at least one array. So we need to handle D=1 separately: then m=1 works (since distinct ≤ k). So minimal m is 1. So formula should be max(1, ceil((D-1)/(k-1))) maybe? But when D=1, (D-1)=0, ceil(0)=0, but m=1 is needed. However, could m=0? No, we need at least one array. So answer should be at least 1. So we can define m = 1 if D ≤ k, else ceil((D-1)/(k-1)). But note that if D ≤ k, ceil((D-1)/(k-1)) might be 1 as well? Let's test D=2, k=2: (1)/(1)=1 => 1, matches. D=2, k=3: (1)/(2)=0.5 ceil=1, matches. D=3, k=3: (2)/(2)=1, matches. So actually for D ≤ k, (D-1)/(k-1) ≤ (k-1)/(k-1) = 1, so ceil ≤ 1. It could be 0 only when D=1, because then numerator 0. So special case D=1 gives 0, but we need 1. So answer = 1 for D=1. So overall answer = 1 if D ≤ k, else ceil((D-1)/(k-1)).
But we need to check if there are any other constraints that could make it impossible even when this m is satisfied, aside from k=1 case? For k>1, does there always exist a decomposition with m = ceil((D-1)/(k-1))? Let's try to construct such a decomposition.
General problem: We need to represent a as sum of m arrays, each with at most k distinct values. Equivalent to covering the increases (breakpoints) of a across positions. The distinct values correspond to values v_1 < v_2 < ... < v_D. Let the positions where a first attains v_s be p_s (with p_1=1). Actually we can define the set of indices where a changes value: let J = {j | a_j > a_{j-1}} with a_0 = -inf? We'll use j from 1 to n, but easier: let the distinct values be u_1 < u_2 < ... < u_D, and let the last index of u_s be r_s (or the segment). But we can think of the increments: at each change, the increase is δ_s = u_{s+1} - u_s (for s=1..D-1). However, the increase may happen over multiple positions? Actually if a is non-decreasing, when it jumps from u_s to u_{s+1}, it could happen in one step (i.e., at some index j, a_j = u_s, a_{j+1} = u_{s+1}) or it could increase gradually over several steps where intermediate values are not part of distinct set? But since distinct values are only u_s, the intermediate values would be additional distinct values. So if a increases from u_s to u_{s+1} in one step, the increase amount is δ_s. If it increases gradually, that would introduce intermediate distinct values, increasing D. So given D is number of distinct values, the increases happen exactly at D-1 moments (positions) where the value jumps to the next distinct value. Actually it's possible that a increases by δ_s in multiple small increments, but those small increments would create additional distinct values, contradicting D being the count of distinct values. For example, a = 0,2,2: distinct values are 0 and 2, D=2, but the increase from 0 to 2 happens at j=2, in one step (increase by 2). That's fine. If we had a = 0,1,2, distinct values = 3, D=3, there are two increases: at j=2 (+1) and j=3 (+1). So each increase corresponds to a step to a new distinct value. So indeed, D-1 equals the number of indices j where a_j > a_{j-1}, because each time we get a new distinct value, there must be at least one increase; and if there are multiple increases that lead to the same value? Could we have a = 0,1,1,2? Distinct = 3 (0,1,2). Increases: at j=2 (+1), at j=4 (+1). So two increases. So yes, D-1 equals the count of positive increments, because each new distinct value appears for the first time at some index, and that index is where an increase occurs; also each increase introduces a new distinct value (since the value after increase is higher than any previous). So they are in one-to-one correspondence: each increase introduces a new distinct value. So D-1 = number of positive increments.
Thus we need to distribute these D-1 increases among m arrays, each array can have at most k-1 increases. So a necessary condition is m*(k-1) ≥ D-1. That is the lower bound. Is it also sufficient? We need to construct arrays b_i such that the total sum is a, with each b_i having at most k distinct values (i.e., at most k-1 increases). Additionally, we need to handle the actual values (the magnitudes of increases) and the baseline.
Given the increases are at specific positions j with amounts δ_j = a_j - a_{j-1} (for j=2..n; also maybe j=1? But a_1 is baseline; we treat a_1 as baseline). So we have positions p_1, p_2, ..., p_{D-1} (the indices where increase happens) and corresponding amounts δ_{p_t} > 0.
We need to assign each increase to some array i and assign it as a jump of that array at that exact position. Because as argued earlier, to get an increase at a particular position j, at least one array must have a jump at j. However, could an increase at j be realized by multiple jumps at earlier positions? Let's examine: If an array jumps at position p < j, its value increases for all later indices, contributing to the sum's value at j, but the increment from a_{j-1} to a_j is the difference in sums between j and j-1. If an array jumped earlier, its value is the same at j-1 and j (since after the jump, it stays constant until maybe another jump). So earlier jumps do not contribute to d_j. Only jumps that occur exactly at j contribute to d_j. So indeed, to achieve a positive d_j, we need at least one array to have a jump at j. So each increase position must be assigned to at least one jump from some array.
But we could have multiple arrays jumping at the same position, summing to the total δ_j. So essentially we have a multiset of jumps at positions, with total amount at each position equal to δ_j. The number of jumps (counting multiplicity) used at position j can be more than one. However, each jump belongs to a specific array, and each array can have jumps at multiple positions (up to k-1). So we need to pack the D-1 positions into at most m arrays, each covering at most k-1 positions (the positions where that array jumps). Additionally, we need to allocate the amounts: each jump has a positive amount; the sum of amounts at position j across arrays assigned to that position must equal δ_j.
If we can assign each position to exactly one array (i.e., each δ_j assigned as a single jump in some array), then the number of jumps used equals D-1. But we might need to split δ_j into multiple jumps if an array already has jumps at other positions, but we can always assign each δ_j to a distinct array? However, we have limited number of arrays m. If m is less than D-1, we need some arrays to take multiple jumps. That's allowed as long as each array takes at most k-1 jumps. So we need to partition the set of D-1 positions into m groups, each of size at most k-1, and then assign each group to an array. That array will have jumps at those positions, with amounts equal to the δ_j for those positions (or could split a δ_j among multiple arrays? But that would increase number of jumps, not reduce). To minimize number of arrays, we want to use as many jumps per array as possible, up to k-1. So the minimal m is ceil((D-1)/(k-1)), provided we can assign each δ_j entirely to one array and handle the baseline.
But is there any constraint that prevents assigning arbitrary combinations of positions to a single array? For a single array with multiple jumps, the values must be non-decreasing. The jumps occur at increasing positions. The array's value before any jumps is some constant c. At each jump position, it increases by some positive amount. The resulting array values are piecewise constant with increasing levels. That's always possible as long as the jumps are at increasing positions and the increments are positive. So we can take any subset of positions (sorted) and assign positive increments to them. So yes.
Now the baseline: For each array i, we have a constant starting value c_i (value for indices before its first jump, or entire array if no jumps). The sum of c_i over i must equal a_1. Since we can choose c_i arbitrarily (non-negative), we can always set them to satisfy sum c_i = a_1. For example, set c_1 = a_1, and all others c_i = 0. But need to ensure that if an array has jumps, its c_i can be zero or any non-negative. That's fine. So baseline is not an issue.
But we also need to ensure that the final sum matches a exactly at each position. If we assign each δ_j to a jump in some array, and we set baseline sum = a_1, then the cumulative sum of jumps up to position j equals sum of δ at positions ≤ j, which equals a_j - a_1. Since each δ_j is contributed exactly once by a jump at that position, the total from jumps up to j is exactly sum_{p ≤ j} δ_p = a_j - a_1. So works.
Thus, the construction is straightforward: Let m = ceil((D-1)/(k-1)). Group the D-1 increase positions into m groups, each of size at most k-1. For each group, create an array b_i that has jumps exactly at the positions in that group, with increments equal to the δ_j for those positions. Set c_i = 0 for all i except maybe one where we set c_1 = a_1 to cover baseline. But careful: if we set c_i=0 for all, then sum of c_i = 0, not a_1. So we need to allocate a_1 among the m arrays. We can set the first array's c_1 = a_1, and others 0. However, if the first array also has jumps, its initial value is a_1, then it jumps by amounts at its positions. That's fine. The other arrays start at 0 and jump as per their positions. Then sum at any position j: a_1 + (sum of jumps from array 1 at positions ≤ j) + (sum of jumps from other arrays at positions ≤ j) = a_1 + sum_{jumps in all arrays at ≤ j} δ = a_1 + sum_{t: p_t ≤ j} δ_{p_t} = a_j. Perfect.
We need to ensure that the jumps are assigned to arrays such that each array gets at most k-1 jumps. That's possible by grouping. So m = ceil((D-1)/(k-1)) works.
But wait: what about the requirement that each b_i is non-decreasing and has at most k distinct values? In our construction, each b_i has as many distinct values as 1 + (number of jumps). If it has r jumps, distinct values = r+1. Since r ≤ k-1, distinct ≤ k. So okay.
Now, is there any scenario where this construction fails because the values in b_i might become negative? No, all are non-negative: c_i ≥ 0, increments δ_j > 0 (since δ_j = a_j - a_{j-1} > 0 at increase positions; for positions where a does not increase, δ_j = 0 and we don't assign jumps there). So values are non-negative.
Thus, for any k ≥ 2, the minimal m is max(1, ceil((D-1)/(k-1))). But need to check boundary: if D=1, then D-1=0, ceil(0/(k-1))=0, but m must be at least 1. So answer = 1. And if D ≤ k, then (D-1)/(k-1) ≤ 1, so ceil ≤ 1, answer =1. So overall: if k=1, special case: if D=1 (i.e., all elements equal) then m=1 works, else impossible (-1). But note: For k=1, D cannot exceed 1 because if a has more than 1 distinct value, it's impossible regardless of m. So answer -1 when k=1 and a is not constant. Also if a is constant, answer 1. So the formula for k=1 would be: if D=1 then 1 else -1. This aligns with max(1, ceil((D-1)/(0))) undefined.
Now, test on provided examples:
- n=4,k=1,a=0,0,0,1 => D=2 (0,1). k=1 -> -1 (output matches)
- n=3,k=1? Actually second test: 3 1, a=3 3 3 => D=1 -> answer 1 (matches)
- n=11,k=3,a as given => distinct values: 0,1,2,3,4 => D=5. ceil((5-1)/(3-1)) = ceil(4/2)=2 -> matches.
- n=5,k=3,a=1,2,3,4,5 => D=5 -> ceil(4/2)=2 -> matches.
- n=9,k=4,a=2,2,3,5,7,11,13,13,17 => distinct: 2,3,5,7,11,13,17 => D=7. ceil((7-1)/(4-1)) = ceil(6/3)=2 -> matches.
- n=10,k=7,a distinct: 0,1,2,3,4,5,6 => D=7, k=7 -> ceil(6/6)=1 -> matches.
So all examples match.
Now we need to ensure no hidden pitfalls. Let's think about edge cases:
-
a_1 could be >0 but all other values same? Then D maybe 2? Example: a=[5,5,5,5] => D=1? Actually if all equal, D=1. So m=1. Good.
-
a=[0,5,5,5] => distinct: 0,5 => D=2. For k=2: ceil((1)/(1))=1? Actually (2-1)/(2-1)=1 -> m=1. Can we represent with one array? b_1 must have at most 2 distinct values. b_1 is non-decreasing. Can we have b_1 such that sum = a? Since m=1, b_1 = a. Does a have ≤2 distinct values? Yes, 0 and 5. So okay. So m=1 works. Good.
-
a=[0,1,2,2,3] distinct: 0,1,2,3 => D=4. k=2 => ceil(3/1)=3. So m=3. Let's try to construct: increases at positions where a increases: at j=2 (+1), j=3 (+1) (since from 1 to 2), j=5 (+1) (from 2 to 3). So three increases. With k=2, each array can have at most 1 increase. So we need at least 3 arrays. So m=3. Is it possible with 3? Yes, assign each increase to a separate array with baseline a_1=0. But then a_1=0, we can set c_i=0 for all? Then sum c_i=0, matches a_1. So three arrays: one jumps at j=2 by 1, one at j=3 by 1, one at j=5 by 1. Check: at j=1: all 0 => sum=0 correct. j=2: first array contributes 1, others 0 => sum=1. j=3: first array still 1, second jumps at 3 adds 1 => total 2. j=4: same as j=3 => total 2. j=5: third jumps at 5 adds 1 => total 3. Works. So m=3 minimal. Could we do with 2 arrays? Each can have up to 1 increase, so total increase capacity 2, but we need 3 increases, so impossible. So formula holds.
-
Now consider k=3, a with D=5 => need ceil(4/2)=2. Works.
-
Consider k=3, D=6 => ceil(5/2)=3. So m=3. Is it always possible to pack 5 increases into 2 arrays? max increases per array =2, so total capacity 4, insufficient. So need 3.
Now, check if there is any constraint about the values of increments: could an increment be so large that it cannot be assigned to a single array? But we can assign any positive integer as jump amount, so fine.
Also, what about the requirement that b_i are non-decreasing arrays of non-negative integers. Our construction yields integer arrays since a are integers and increments are integer, so b_i are integer.
Thus, the answer seems to be straightforward: compute number of distinct values in a, call D. If k==1:
- if D == 1: answer 1
- else: answer -1
Else:
- if D == 1: answer 1
- else: answer = ceil((D-1)/(k-1)). Since D-1 and k-1 integers, we can compute (D-1 + k-2) // (k-1). But careful: if D-1 == 0, this gives 0, but we want 1. So we can handle D=1 separately or compute max(1, (D-2)//(k-1)+1? Actually simpler: if D <= k: answer = 1; else answer = (D-1 + k-2) // (k-1). Because when D > k, D-1 > k-1, so this will be at least 2. And for D == k, D-1 = k-1, (k-1 + k-2) // (k-1) = (2k-3)//(k-1). For k>=2, (2k-3)//(k-1) equals 2? Let's test: k=2: (1+0)//1=1? Actually D=k=2: D-1=1, k-2=0, (1+0)//1=1, but we want answer 1 because D≤k? Actually D=2, k=2 => D ≤ k, so answer should be 1. So (D-1 + k-2)//(k-1) for D=2,k=2 yields (1+0)//1=1, okay. For k=3, D=3: D-1=2, k-2=1, (2+1)//2 = 3//2=1? Actually 3//2=1, but we want 1. Works? 1 is correct. For k=3, D=3 -> answer 1. For k=4, D=4: D-1=3, k-2=2, (3+2)//3=5//3=1, correct. So maybe (D-1 + k-2)//(k-1) yields 1 for D≤k? Let's test D=5,k=4: D>k, D-1=4, k-2=2, (4+2)//3=6//3=2, which is ceil(4/3)=2, correct. D=5,k=5: D≤k, D-1=4, k-2=3, (4+3)//4=7//4=1? Wait 7//4=1, but we want 1, correct. So the formula (D-1 + k-2) // (k-1) seems to give correct for all D≥1, k≥2? Check D=1, k=2: D-1=0, k-2=0, 0//1=0, but we need 1. So need to handle D=1 separately. So answer = 1 if D=1 else (D-1 + k-2)//(k-1) for k>1. But also check D=2,k=2: 1+0//1=1, ok. D=2,k=3: 1+1//2=2//2=1, ok. So yes, D=1 is only special.
But also need to check if k=1 is separate.
Thus algorithm: For each test case:
- Read n, k.
- Read array a of length n.
- Compute number of distinct values D: iterate and count different numbers.
- If k == 1:
- If D == 1: print 1.
- Else: print -1.
- Else (k >= 2):
- If D == 1: print 1.
- Else: print (D - 2) // (k - 1) + 1? Actually (D-1 + k-2) // (k-1) is same as ceil((D-1)/(k-1)). Let's compute m = (D - 2) // (k - 1) + 1? Let's derive: ceil((D-1)/(k-1)) = floor((D-2)/(k-1)) + 1? Actually ceil(x/y) = (x + y - 1)//y. So with x = D-1, y = k-1 => m = (D-1 + (k-1) - 1) // (k-1) = (D-1 + k-2) // (k-1). So that's the expression.
But we must also consider if there is any additional constraint from the fact that a is non-negative and non-decreasing and a_n>0. That's fine.
Now, is there any scenario where even though m*(k-1) >= D-1, it's still impossible? Let's think about the case where k=2 we already reasoned it's possible. For k>2, we need to ensure that we can assign the jumps to arrays respecting that each array's jumps are in increasing order (which they will be if we assign groups sorted). Also we need to ensure that the increments are positive integers, and we can split baseline arbitrarily. No further constraints.
But wait: There is also the condition that each b_i is non-decreasing array of non-negative integers. In our construction, we set c_1 = a_1, and other c_i = 0. But consider an array with c_i=0 and jumps at positions. That's fine. However, we need to ensure that after jumps, the values are non-negative (they are). Also need to ensure that the arrays are non-decreasing: they are step functions with increases at jump positions, so yes.
One more nuance: The number of distinct elements in b_i is at most k. In our construction, if an array has r jumps, its distinct values count = r+1 (including the starting constant). If r = k-1, then distinct = k, okay. If r < k-1, distinct < k, okay. So constraint satisfied.
But what if a_1 = 0? Then baseline sum of c_i must be 0. In our construction, we set c_1 = a_1 = 0, and others 0, so fine.
Now, what about the possibility that we need to assign jumps to arrays but also the baseline a_1 might be >0 and we might need to have an array with no jumps to carry the baseline? In our construction, we set c_1 = a_1, and that array may also have jumps. But if that array also has jumps, its starting value is a_1. That's fine. If a_1 > 0 and we assign jumps to that array, its distinct values may increase by the number of jumps. That's okay as long as total distinct ≤ k. But what if the number of jumps we assign to that array is already k-1? Then distinct would be k, still okay. So no problem.
Now, is there any scenario where we cannot group the increase positions arbitrarily because the increments need to be integers and we might need to split a large increment among multiple arrays if one array cannot accommodate a large increment? But we don't need to split; we assign the whole increment to one array. So each increment is assigned wholly to one array. That's fine regardless of its size.
Thus, the construction works for any k ≥ 2.
But wait, what about the requirement "non-decreasing arrays of non-negative integers"? Our arrays b_i are non-decreasing because they are step functions with positive jumps. Yes.
Thus the answer is simply based on distinct count.
But let's double-check the reasoning that each increase position must correspond to at least one jump. We argued that if no array jumps at position j, then the total sum does not change from j-1 to j, so a_j = a_{j-1}. Therefore, if a_j > a_{j-1}, there must be at least one array that jumps at j. That is correct.
But could there be a scenario where an array jumps at j by a negative amount? No, arrays must be non-decreasing, so jumps are non-negative; positive if increase.
Thus, the number of jumps across all arrays is at least the number of positions with a_j > a_{j-1}. Each array can have at most (k-1) jumps (since distinct values ≤ k). So m*(k-1) ≥ number_of_increases = D-1. So lower bound.
Now, is there any possibility to have fewer jumps than increases by using jumps that are not exactly at the increase position but somehow cause increase over two positions? Let's examine more carefully: Suppose we have two arrays. Array A jumps at position 2 by amount 2. Array B jumps at position 3 by amount -? Not allowed. Or maybe have an array that increases gradually by having multiple equal increments? But that would be multiple jumps, which count as multiple increases. However, could we have an array that increases at position j by a fraction, but since integers, maybe increase by 1 at position 2 and also at position 3, resulting total increase of 2 from a_1 to a_3, but a_2 might increase by 1 and a_3 by 1. That yields two increases in a, so we still need two increase positions. So no.
What if an array jumps at position 2 by amount 2, and another array has a negative jump? Not allowed.
Thus, the mapping is strict.
Therefore, the lower bound is tight and we have construction meeting it.
Now, we should confirm with some random tests via mental or small brute force? Let's try to see if there is any tricky case: a = [0,2,2] with k=2: D=2, m=1 works? Since distinct ≤2, yes. Our formula gives 1. Construction: b1 = a. Works.
a = [0,0,2,2] D=2, m=1 works.
a = [0,1,1,2] D=3, k=2 => m=2. Construction: increases at j=2 (+1), j=4 (+1). Use two arrays: one jumps at 2 by 1, one jumps at 4 by 1, baseline a1=0 -> set both c=0. Works.
Now consider a = [0,1,2,3] D=4, k=2 => m=3. Increases at j=2,3,4 each +1. Need 3 arrays. Works.
Now consider k=3: a = [0,1,2,3,4] D=5 => m= ceil(4/2)=2. Construction: Need to pack 4 increases into 2 arrays each with at most 2 increases. So one array gets 2 increases, the other 2 increases. We need to assign increments to positions. For example, array1 jumps at j=2 (+1) and j=4 (+1); array2 jumps at j=3 (+1) and j=5 (+1). Baseline a1=0, set c's=0. Then check: j=1 sum=0; j=2 sum=1; j=3: array1 still 1, array2 jumps at 3 adds 1 => sum=2; j=4: array1 jumps +1 => total 3; j=5: array2 jumps +1 => total 4. Works.
If we try to pack 4 increases into 2 arrays with each having exactly 2, fine. What about distribution of increments: all are 1, easy. If increments vary, still fine.
Now consider a = [0,5,5,5] D=2, k=2 => m=1, works as a itself.
Now consider a with D=6, k=3 => m= ceil(5/2)=3. So 3 arrays. Could we possibly do with 2 arrays? They could have at most 2 increases each, total capacity 4, insufficient. So 3.
Now consider k large, e.g., k >= D, then m=1.
Thus the answer seems consistent.
But we should also consider the condition that a_n > 0, ensures D≥1 (since there is at least one positive value, but a could be all zeros? Actually a_n > 0, so a_n > 0, so at least one positive, so D≥1, and if all values are same positive, D=1. So D≥1 always.
Now, there is a nuance: The problem statement says "non-decreasing arrays of non-negative integers b_i". It doesn't explicitly say that the b_i must be integer? It says non-negative integers, so yes integer. So our integer construction works.
One more nuance: Could we have a situation where we need more than ceil((D-1)/(k-1)) because of the requirement that each b_i has at most k different elements, but also the b_i themselves must be non-decreasing and the sum must exactly equal a. Perhaps there is an additional constraint about the values of b_i being non-negative and also the sum of their initial values must be a_1. That we can always set.
But wait, is there any restriction that the b_i arrays themselves must be non-decreasing and each have at most k distinct values, but also the sum across i must be exactly a. We assigned jumps to arrays. However, if we assign jumps to arrays, the arrays will have values that are sums of c_i and the jumps applied. But we need to ensure that each b_i is non-decreasing: yes. Also we need to ensure that the values in b_i are integers: yes.
Now, consider the scenario where k=2, D-1 = 1, m=1. That's fine. If D-1 = 0, m=1 (since D=1). So no issue.
Now, what about the first test case: k=1, D=2 -> -1. That's correct because with k=1, each b_i is constant. Sum of constants is constant, cannot produce increase. So -1.
Now, what about k=1, D=1 -> 1, as example 2.
Thus the solution is simple: compute distinct count D, then answer as described.
But wait, the problem constraints: n up to 100, t up to 100. So we can compute D by scanning.
But is there any scenario where the answer could be larger than ceil((D-1)/(k-1)) due to some other hidden condition? Let's think about the fact that each b_i must be non-decreasing and have at most k distinct values, but also the values themselves must be integers. Could there be a scenario where the increments δ_j are so large that we cannot assign them to arrays without causing some array to have more than k distinct values even if number of jumps is ≤ k-1? Actually distinct values count depends only on number of jumps, not on the magnitude of jumps. So fine.
What about the possibility that an array with many jumps might need to have increasing values, but the increments must sum to certain amounts. Could there be a conflict where the assigned increments at positions within an array must be in non-decreasing order? Actually the values of the array are c_i plus cumulative sum of its jumps. Since jumps are positive, the values are strictly increasing at jump points, so they are non-decreasing. No other ordering constraints.
Thus, the answer is indeed determined solely by D and k.
But let's test with a more complex example: a = [0, 10, 10, 20] D=3 (0,10,20). Increases: at j=2 (+10), j=4 (+10). k=2 => m=2. Construction: array1: c=0, jump at 2 by 10; array2: c=0, jump at 4 by 10. Works. k=3 => m=1? Since D=3 ≤3, m=1 works (a itself has 3 distinct values, which is ≤3). So answer 1.
Now test a case where D-1 not divisible by k-1: e.g., D=7, k=3 => m= ceil(6/2)=3. 2 arrays would give max 4 increases, insufficient. So 3.
Now check if there might be any scenario where the number of distinct values D is not equal to the number of increases + 1 due to zero values? Actually if a_1=0 and there are zeros later, distinct includes 0, and increases count correspond to new distinct values. So D-1 = number of positive increments. So correct.
Edge: a = [0,0,1,1,2,2] D=3, increases at j=3 (+1) and j=5 (+1) => D-1=2. So formula: for k=2, m=2; for k=3, m=1? Since D=3 ≤3, m=1 works (a itself). Indeed a has distinct values 0,1,2 (3), so k>=3 allows m=1.
Now, what about a case where a_1 > 0 but there are zeros later? But a is non-decreasing, so if a_1 > 0, all later are ≥ a_1 > 0, so zeros cannot appear. So a_1 is the minimum. So fine.
Now, what about k=0? Not in constraints (k≥1). So fine.
Thus solution appears trivial.
But let's re-read problem statement carefully: "The number of different elements in the array b_i is at most k for all 1 ≤ i ≤ m." That means each b_i individually has at most k distinct values. Yes.
We derived that minimal m is ceil((D-1)/(k-1)) for k>1, and special case k=1.
Now, is there any possibility that we can use m smaller than ceil((D-1)/(k-1)) by allowing some arrays to have more than k distinct values? No, constraint prohibits that.
Thus answer stands.
But wait, there might be another nuance: The b_i arrays themselves must be non-decreasing, but also they must be arrays of non-negative integers. In our construction, when we set c_1 = a_1, and other c_i = 0, and assign jumps, we need to ensure that the arrays b_i are indeed non-decreasing. They are. However, consider array1 with c_1 = a_1 > 0 and jumps at later positions. That's fine. But what if we assign jumps to array1 at positions that are before the first jump of another array? That doesn't matter.
Now, one more thought: Could we reduce m by using arrays that have exactly k distinct values, but also have some jumps that are zero? No, jumps are positive only if we need increase.
Thus solution is straightforward.
But I recall a similar Codeforces problem: "Decrease the Sum of Digits" maybe? Actually there is a problem "1462E2" no. This looks like a problem from Codeforces Round #... I think it's from problem "1490D" no. It might be from "1526C1" no. Actually I recall a problem "1462C" no. Let me search memory: There's a problem "1343C" no. However, I think it's from Codeforces Round #1799? Might be "1730B"? Not sure. But I've seen a solution that uses distinct count. So I'm confident.
Nevertheless, before finalizing, let's test with some edge cases we can think of:
Case: n=1, k=1, a=[5] (since a_n>0). D=1. k=1: answer 1. Our handling: k==1, D==1 -> 1. Good.
Case: n=1, k=2, a=[5]: D=1 -> answer 1.
Case: n=2, k=1, a=[0,1] D=2 -> -1. correct.
Case: n=2, k=1, a=[1,1] D=1 -> 1.
Case: n=2, k=2, a=[0,1] D=2 -> ceil(1/1)=1? Actually D=2 <= k=2, so answer 1. Since a has 2 distinct values, which is ≤2, so m=1 works. Yes.
Case: n=2, k=2, a=[0,2] D=2 -> m=1 works.
Case: n=100, k=100, a arbitrary -> D ≤ 100? Actually a_n ≤ 100, so D at most 101? Wait, a_i ≤ 100, but they are non-negative integers, so distinct values at most 101 (0..100). But k can be up to n (100). However, D could be up to 101 maybe, but n=100, so a has at most 100 elements, distinct values ≤ 100. Actually maximum distinct possible is 100 if all values distinct. So D ≤ n. Since k ≤ n, D can be > k. So formula applies.
Now test borderline: n=100, k=2, a is strictly increasing from 0 to 99 (100 distinct values? Actually 0..99 is 100 distinct, n=100). Then D=100, k=2 => m = ceil(99/1)=99. Is that plausible? Each array can have at most 1 increase, so we need at least 99 arrays to produce 99 increases. With 99 arrays, we can assign each increase to a distinct array, baseline a_1=0, so c_i=0, each array jumps at its assigned position with amount 1. Works. Could we do with 98? Total increases 99, but capacity 98*1=98 <99, impossible. So m=99 minimal. So answer 99.
Now what about n=100, k=3, a strictly increasing 0..99 => D=100 => m = ceil(99/2)=50 (since 99/2=49.5 -> 50). So with 50 arrays each can have up to 2 increases, total capacity 100, enough for 99 increases. So m=50 works. Could we do with 49? capacity 49*2=98 <99, impossible. So 50.
Thus formula seems plausible.
But is there any scenario where the number of increases is not exactly D-1? As argued, yes it is. Because each time a_j > a_{j-1}, the value a_j is greater than all previous values, so it's a new distinct value. Conversely, each new distinct value appears first at some index j where a_j > a_{j-1} (since if it's the first occurrence, it must be > previous because previous max was previous distinct). More formally, for a non-decreasing sequence, the set of distinct values is {a_1, a_{i_2}, ..., a_{i_D}} where i_1=1 and for t>1, i_t is the first index where a_j > a_{i_{t-1}}. At each such i_t, we have a_{i_t} > a_{i_t -1} (since otherwise it would have appeared earlier). So indeed D-1 equals count of indices j with a_j > a_{j-1}. Good.
Thus implementation: For each test case, read n, k, array a. Compute D = number of distinct elements. If k == 1:
if D == 1: print(1)
else: print(-1)
Else:
if D == 1: print(1)
else:
m = (D - 2) // (k - 1) + 1? Let's compute properly.
Use m = (D-1 + k-2) // (k-1). Since D>=2, this gives at least 1.
Print m.
Check for D=2, k=2: (1+0)//1=1.
D=2, k=3: (1+1)//2=2//2=1.
D=2, k=100: (1+98)//99=99//99=1.
For D=3, k=2: (2+0)//1=2. Good.
Now check D=3, k=3: (2+1)//2=3//2=1. Good.
Now D=1, we already handle separately.
Now ensure integer division works.
Now, we must also consider that k can be equal to n (max 100). That's fine.
Thus solution is O(n) per test.
But before finalizing, let's think if there is any scenario where the answer is -1 for k>1. Could it be impossible even if the distinct count condition is satisfied? For k>1, the only potential impossibility is when k=1. But consider k=2, D=2, a=[1,2]? That's possible with one array. What about a=[1,3]? Also possible. So always possible. For k>1, we can always construct as described. But is there any constraint that b_i arrays must have size n and non-decreasing, and the sum must equal a. With our construction, we use at most m arrays. Could there be a case where a_1 is large and we assign all jumps to arrays with c_i=0 except one with c_1 = a_1, but that array may have many jumps (up to k-1). That's fine. Could there be a case where a_1 = 0 and we assign all c_i = 0, but then some array might have a jump at position 1? We don't assign jumps at position 1 because increases only at j>=2 (since a_1 is baseline). Actually could there be an increase at position 1? We define a_0=0? But there's no a_0. The first element a_1 is given. There's no requirement that the sum of b_i,1 equals a_1, which we set by c_i. So we don't need jumps at position 1. In our construction, all jumps are at positions where a increases, which are ≥2. So that's fine.
What about the case where a_1 > 0 but we set c_i=0 for all except one? That one has c = a_1. Its first value is a_1. That array could also have jumps at positions, but those jumps will increase its value further. So its values are a_1 + cumulative jumps. That's non-negative.
One more nuance: The arrays b_i must be non-decreasing. In our construction, each array's values are monotone increasing when jumps occur. However, if we set c_i=0 for some array and assign it jumps, its values start at 0 and then increase at its jump positions, so it's non-decreasing. Good.
Thus, no impossibility for k>1.
But wait, the problem statement includes "non-negative integers" for b_i. So zero allowed.
Thus, answer is as above.
But I recall that in the example first test case, they printed -1 for k=1, a not constant. That's consistent.
Now, double-check the third test case: a = 0 1 2 2 3 3 3 4 4 4 4. D=5, k=3 => m=2. Our construction would group the 4 increases into two groups of 2 each. Increases positions? Let's compute increments: a: index1=0; index2=1 (+1); index3=2 (+1); index4=2 (+0); index5=3 (+1); index6=3 (+0); index7=3 (+0); index8=4 (+1); index9=4 (+0); index10=4 (+0); index11=4 (+0). So positive increments at j=2,3,5,8. So four increases. Group them into two arrays each with two jumps: e.g., array1 jumps at 2 and 5; array2 jumps at 3 and 8. Each jump amount =1. Baseline a1=0, set c_i=0. Then sums:
j1=0
j2: array1 gives1, array2 0 =>1
j3: array1 still1, array2 jumps at3 gives1 =>2
j4: still2
j5: array1 jumps at5 gives +1 => total3
j6:3
j7:3
j8: array2 jumps at8 gives +1 => total4
j9..11:4. That matches a (since a has 0,1,2,2,3,3,3,4,4,4,4). Yes.
So works.
Now, final check: Could there be any scenario where the number of distinct values D is not equal to the number of increases +1 because the first value might be 0 but there is also 0 later? That's fine; distinct count includes 0 once, and increases count includes each time we see a new value, which first 0 is at j=1, but there is no increase at j=1. So D=1 + number of increases. So okay.
Thus answer is simply based on D.
Now, we should consider if there's any trick when k=0? Not allowed.
Therefore, solution implementation is straightforward.
But let's read problem constraints: a_i ≤ 100. So D ≤ 101. So small.
Now, we must output for each test case.
Let's write pseudo-code:
for each test case:
read n, k
read array a[0..n-1]
compute distinct count:
D = 1
for i from 1 to n-1:
if a[i] != a[i-1]: D++
if k == 1:
if D == 1: print(1)
else: print(-1)
else:
if D == 1: print(1)
else:
m = (D - 1 + k - 2) // (k - 1)
print(m)
But we need to be careful: when D==1, the expression (D-1 + k-2)//(k-1) gives (0 + k-2)//(k-1). For k=2, that's 0//1 =0; for k=3, 1//2=0; So it yields 0. But we want 1. So we must handle D==1 separately.
Alternatively, we could compute m = max(1, (D-1 + k-2)//(k-1)). But when D==1, (D-1)=0, (0 + k-2)//(k-1) is 0 for k>=2? Let's test: k=2: (0+0)//1=0; max(1,0)=1. k=3: (0+1)//2=0; max=1. So max works. But we also need to handle k=1 separately. So overall:
if k == 1:
if D == 1: print(1)
else: print(-1)
else:
m = (D - 1 + k - 2) // (k - 1)
if m < 1: m = 1
print(m)
But careful: For D=1, (D-1 + k-2) // (k-1) is less than 1 for all k>=2? Let's test k=2: (0+0)//1 =0. So m=0, then set to 1. That's correct.
But for D=2, k=2: (1+0)//1=1 -> m=1, fine. For D=2, k=3: (1+1)//2=1 -> m=1, fine. So the max(1, ...) will work.
So we can compute:
if k == 1:
print(1 if D == 1 else -1)
else:
m = (D - 1 + k - 2) // (k - 1)
if m < 1: m = 1
print(m)
But what about D=0? Not possible since a_n>0 ensures at least one positive, but a could be all zeros? a_n>0 so not all zeros. So D≥1.
Now, is there any scenario where the formula yields a m that is too low because we also need to consider that each b_i must have at most k distinct values, but our construction might cause some b_i to have more than k distinct values if we assign more than k-1 jumps. But we limit each group size to at most k-1. So as long as m = ceil((D-1)/(k-1)), we can partition D-1 jumps into m groups each of size ≤ k-1. That's possible because ceil ensures total groups needed. So we can always partition a set of size D-1 into groups of size at most k-1 using m groups: the minimal m is indeed ceil((D-1)/(k-1)). And we can assign each group to one array. So construction works.
Now, final answer: implement accordingly.
But before finalizing, let's double-check with the example where k=1, D=1: answer 1. Our code will output 1. Good.
Now, is there any possibility that when k=1 and D=1, but a has constant positive value, we can use m=1. Yes.
Thus solution is correct.
However, I recall a Codeforces problem "1730A - Planar Reflections"? No. Actually I recall a problem "Minimum number of arrays" where answer is ceil((distinct-1)/(k-1)). I think it's from Codeforces Round #1791 (Div. 2) problem D? Not sure.
Anyway, answer seems simple.
But wait: there is a nuance: The arrays b_i must be non-decreasing. In our construction, we set b_i values as step functions. However, we need to ensure that the values are non-negative integers. Our c_i and increments are integers, so fine.
Now, think about an extreme case: n=100, k=2, a is like [0,0,0,...,0,1] (only last element is 1). Then D=2, increases at j=n (since a_{n-1}=0, a_n=1). So m=1 works? Since D=2, k=2, so m=1. Indeed b1 = a itself has 2 distinct values (0 and 1), which is ≤2, so m=1. That matches formula. But note: a has many zeros then a 1. b1 = a is non-decreasing, okay.
Now, what about a = [0,0,0,2]? D=2, k=2, m=1, b1 = a, distinct values 0 and 2, okay.
Now, what about a = [1,1,2]? D=2 (1 and 2), k=2, m=1. b1 = a works.
All good.
Thus solution is implemented.
But hold on: The problem statement says "non-decreasing array of non-negative integers a_1,...,a_n". It also says a_n > 0. So a_1 could be 0 or positive.
Now, let's test the case: k=2, D=2, a=[5,6]. Distinct=2, m=1 works. b1 = [5,6] has two distinct values, okay.
Now, what about k=2, D=3, a=[5,6,7] distinct=3, m=2. Construction: need two arrays. Let's try to build: increases at j=2 (+1), j=3 (+1). Use two arrays: array1 jumps at j=2 with Δ=1, array2 jumps at j=3 with Δ=1, baseline a1=5. So we need sum of c_i =5. We could set array1 c=5, array2 c=0. Then array1: at j=1:5, j=2:6, j=3:6; array2: j=1:0, j=2:0, j=3:1. Sum: j1:5, j2:6, j3:7. Works. array1 has distinct values 5 and 6 (2), array2 has 0 and 1 (2). So okay.
Thus fine.
Now, what about a case where the increments are large and we need to split them? But we don't need to split; we assign whole increments to one array. So okay.
Thus solution stands.
But I'd like to consider if there is any scenario where we could use fewer arrays by allowing some arrays to have jumps that are not exactly at the increase positions, but by combining jumps from multiple arrays we could create an increase at a position without any array jumping exactly at that position? Let's examine more formally.
We have m arrays b_i. For each j from 2 to n, define d_j = a_j - a_{j-1} = sum_i (b_{i,j} - b_{i,j-1}). Since each b_i is non-decreasing, each term δ_{i,j} = b_{i,j} - b_{i,j-1} ≥ 0. Then d_j = sum_i δ_{i,j}. If d_j > 0, then at least one δ_{i,j} > 0. That means at least one array i has b_{i,j} > b_{i,j-1}, i.e., a jump at position j. So indeed each positive d_j requires at least one jump at j. So number of positions with positive d_j is ≤ total number of jumps across all arrays. Each array i can have at most (k-1) jumps (since distinct values ≤ k). So total jumps ≤ m*(k-1). Thus m ≥ number of positive d_j / (k-1) rounded up.
Thus lower bound is necessary and we have shown it's sufficient by constructing each jump exactly corresponds to a positive d_j and grouping jumps per array. So it's tight.
Thus answer = ceil(t / (k-1)) where t = number of positive d_j = D-1. So the formula.
Now, one more nuance: When k=1, k-1=0, the division is undefined. So we handle separately.
Now, is there any possibility that we could use m smaller than ceil(t/(k-1)) by having some arrays with jumps that are not exactly at each positive d_j but somehow produce the same cumulative effect? For instance, suppose we have two positive d_j at positions 2 and 3. Could we have a single array that jumps at position 2 by amount d_2 + d_3, and another array that does something to offset the extra increase at position 3? But if we have a single array jumping at 2 by d_2+d_3, then at position 2 the sum increases by d_2+d_3, which is too large. To correct, we could have another array that has a negative jump at position 3? But negative jumps are not allowed (non-decreasing). Could we have an array that jumps at position 2 by d_2, and also at position 3 by d_3, that's two jumps, requiring capacity 2. If k=2, capacity per array is 1, so can't. Could we have an array that jumps at position 2 by d_2, and another array that jumps at position 2 by d_3? That would give increase at position 2 of d_2+d_3, but we need d_2 at 2 and d_3 at 3. To get increase at 3, we need some jump at 3. So no.
What about using a combination of jumps at earlier positions and constant values? For example, suppose we want d_2=1, d_3=1. Could we have one array that jumps at position 2 by 1, and another array that is constant 0? That gives increase only at 2. To get increase at 3, we need a jump at 3. So need at least two jumps.
Thus, the lower bound is strict.
Now, consider k=2, we need at least t arrays. Could we ever use fewer than t arrays by having some array with no jumps but with a large constant that changes? No, constant doesn't create increase.
Thus, solved.
Now, implementation details:
- Input t
- Loop test cases:
- read n, k
- read list a of n ints
- compute distinct count:
distinct = 1
for i in range(1,n):
if a[i] != a[i-1]: distinct += 1
- if k == 1:
if distinct == 1: print(1)
else: print(-1)
- else:
if distinct == 1: print(1)
else:
needed = (distinct - 1 + k - 2) // (k - 1)
print(needed) # will be at least 1 because distinct>=2, k>=2 => needed>=1
But I want to test for distinct=2, k=2: (1+0)//1=1 -> print 1. Good.
Now, what about distinct=2, k=100: (1+98)//99=99//99=1, print 1. Good.
Now, distinct=3, k=2: (2+0)//1=2, print 2.
Now, distinct=3, k=3: (2+1)//2=3//2=1, print 1 (since distinct ≤ k). That's okay.
Now, distinct=4, k=3: (3+1)//2=4//2=2, print 2.
Now, distinct=4, k=2: (3+0)//1=3, print 3.
All good.
Now, I'll code.
But wait: The problem statement says "Find the minimum possible value of m, or report that there is no possible m." So we output -1 when impossible.
Thus final solution.
Before finalizing, I will think about any possible oversight: The arrays b_i must be non-decreasing, but also they have to be of size n. In our construction, we used step functions that are constant on intervals. That's fine.
But what about the requirement "non-decreasing arrays of non-negative integers"? That's satisfied.
Now, one more thing: The number of distinct elements in b_i is at most k. In our construction, for arrays that have no jumps (constant), they have 1 distinct element ≤ k. For arrays with r jumps, they have r+1 distinct elements, and we ensure r ≤ k-1, so ≤ k.
Now, could there be a scenario where we need to split an increment across multiple arrays because we need to keep each array's distinct values count ≤ k but also the total number of arrays is limited? But we are minimizing m, so we want to pack as many jumps as possible per array, up to k-1. That is optimal.
Thus answer is correct.
Now, let's consider if there is any additional constraint that b_i must be non-decreasing and also the sum must equal a exactly. In our construction, we sum to a exactly.
Thus we are done.
Now, I will produce final solution in Python.
But one more nuance: The input t up to 100, n up to 100, so trivial.
Now, double-check the note: In first test case, they output -1, which matches.
Second test case: 3 1, a=3 3 3, D=1, k=1 -> output 1, matches.
Third: D=5, k=3 -> output 2, matches.
Fourth: D=5, k=3 -> 2, matches.
Fifth: D=7, k=4 -> 2, matches.
Sixth: D=7, k=7 -> 1, matches.
All good.
Thus final answer.
However, I recall a similar problem on Codeforces: "1462C" no. Actually it's problem "1730B" maybe not. I think it's from Codeforces Round #1799 (Div. 2) problem "C. Minimum Extraction"? Not sure. But the solution is indeed simple.
But wait, there is a nuance: The number of different elements in b_i is at most k. But "different elements" means distinct values. However, if an array has 0 as a value and later 0 again, it's still one distinct. So fine.
Now, one more thought: Could we reduce m by using arrays that are not simple step functions but have more complex patterns that still have limited distinct values? For instance, an array could have values like 0,1,0? That would not be non-decreasing. So not allowed. So only non-decreasing, so step functions.
Thus, I'm confident.
Now, implement.
But I'll also test some random cases by mental.
Case: n=5, k=3, a=[0,0,1,1,2] D=3, k=3 => m=1. Indeed a has distinct values 0,1,2 (3) so m=1 works.
Case: n=5, k=3, a=[0,1,1,2,3] D=4, m= ceil(3/2)=2. Let's construct: increases at j=2 (+1), j=4 (+1), j=5 (+1). Actually a:0,1,1,2,3. So d: j2:+1, j4:+1, j5:+1. So three increases. Need two arrays: one with 2 jumps, one with 1 jump. Group: array1 jumps at j2 and j5, array2 jumps at j4. Baseline a1=0. Then sums: j1=0; j2: array1 +1 =>1; j3: still1; j4: array2 +1 =>2; j5: array1 +1 =>3. Works.
Case: n=5, k=2, a=[0,1,2,2,3] D=4, m= ceil(3/1)=3. Increases at j2:+1, j3:+1, j5:+1. So three arrays each with one jump.
Works.
Thus fine.
Now, final code:
def solve():
import sys
input = sys.stdin.readline
t = int(input().strip())
for _ in range(t):
n,k = map(int, input().split())
a = list(map(int, input().split()))
distinct = 1
for i in range(1,n):
if a[i] != a[i-1]:
distinct += 1
if k == 1:
if distinct == 1:
print(1)
else:
print(-1)
else:
if distinct == 1:
print(1)
else:
# m = ceil((distinct-1)/(k-1))
m = (distinct - 1 + k - 2) // (k - 1)
# but ensure at least 1 (though distinct>=2 => m>=1)
if m < 1:
m = 1
print(m)
Edge: For distinct=2, k=2 -> m=1 as computed.
Now, double-check integer division: In Python, // is floor division. For positive numbers, (x + y -1)//y gives ceil(x/y). Here x = distinct-1, y = k-1. So m = (distinct-1 + k-2) // (k-1). That's correct.
Now, confirm with distinct=2, k=2: (1+0)//1 = 1.
distinct=2, k=3: (1+1)//2 = 2//2=1.
distinct=3, k=2: (2+0)//1=2.
distinct=3, k=3: (2+1)//2=3//2=1.
distinct=3, k=4: (2+2)//3=4//3=1.
All good.
Now, final answer.
But there is a potential twist: The problem might require that the b_i arrays are non-decreasing, but also that each b_i is an array of non-negative integers. In our construction, we used c_i possibly zero. That's fine.
Now, I'll produce final answer in ChatGPT with explanation.
However, I recall a Codeforces problem "1506C" no. Wait, there is a problem "1741C Minimize the Thickness"? Not.
Anyway, I'll write solution.
But the problem statement originally had multiple test cases. We'll output each answer.
Now, I'll also consider if there is any other impossibility condition for k>1: maybe if k=2 and a_1 > 0 and we have D-1 = 0? That is D=1, works. If D>1, always possible.
Thus final.
But wait, is it always possible to partition D-1 jumps into m groups each of size ≤ k-1 when m = ceil((D-1)/(k-1))? Yes, because we can take groups of size at most k-1, e.g., fill first m-1 groups with k-1 each and last with remainder (which will be ≤ k-1). Since total items = D-1, and m = ceil((D-1)/(k-1)), we have (m-1)(k-1) < D-1 ≤ m(k-1). So we can assign.
Thus construction works.
Thus answer is final.
Now, I'll provide final solution.
But I want to think about if the array a can have repeated values and we need to ensure that after assigning jumps, the sum matches exactly even for positions where a does not increase. In our construction, if no jumps occur at a position, the sum stays the same as previous, which matches a if a also stays same. So fine.
Thus all good.
Now, implement.
One more edge: If k=1, D must be 1, else impossible. So we output -1 for k=1 and D>1.
Now, just to be thorough, consider the scenario: k=1, D=1 but a might have zeros? a_n>0, so if D=1, all elements equal some positive constant. Then constant array b1 = a works, because distinct=1 ≤1. So m=1. Good.
Now, time to code.
Will use Python.
Complexity: O(t*n).
Now produce final answer. ```python
import sys
def solve() -> None:
it = iter(sys.stdin.read().strip().split())
t = int(next(it))
out_lines = []
for _ in range(t):
n = int(next(it))
k = int(next(it))
a = [int(next(it)) for _ in range(n)]
# count distinct values in a
distinct = 1
for i in range(1, n):
if a[i] != a[i-1]:
distinct += 1
if k == 1:
if distinct == 1:
out_lines.append("1")
else:
out_lines.append("-1")
else:
if distinct == 1:
out_lines.append("1")
else:
# m = ceil((distinct-1) / (k-1))
m = (distinct - 1 + k - 2) // (k - 1)
if m < 1: # should not happen for distinct>=2, k>=2
m = 1
out_lines.append(str(m))
sys.stdout.write("\n".join(out_lines))
if name == "main":
solve()