There is a row of n cells. Initially, all cells are empty (don't contain any color). Cells are numbered from 1 to n.
You are given an integer m and m integers l_1, l_2, …, l_m (1 ≤ l_i ≤ n)
Dreamoon will perform m operations.
In i-th operation, Dreamoon will choose a number p_i from range [1, n-l_i+1] (inclusive) and will paint all cells from p_i to p_i+l_i-1 (inclusive) in i-th color. Note that cells may be colored more one than once, in this case, cell will have the color from the latest operation.
Dreamoon hopes that after these m operations, all colors will appear at least once and all cells will be colored. Please help Dreamoon to choose p_i in each operation to satisfy all constraints.
Input
The first line contains two integers n,m (1 ≤ m ≤ n ≤ 100 000).
The second line contains m integers l_1, l_2, …, l_m (1 ≤ l_i ≤ n).
Output
If it's impossible to perform m operations to satisfy all constraints, print "'-1" (without quotes).
Otherwise, print m integers p_1, p_2, …, p_m (1 ≤ p_i ≤ n - l_i + 1), after these m operations, all colors should appear at least once and all cells should be colored.
If there are several possible solutions, you can print any.
Examples
Input
5 3
3 2 2
Output
2 4 1
Input
10 1
1
Output
-1
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We are given a row of n cells, initially empty. We have m operations, each with a given length l_i, and we choose a starting position p_i (in [1, n - l_i + 1]). The operation paints the interval [p_i, p_i + l_i - 1] with color i (where i is operation index). If a cell is painted multiple times, it retains the color of the latest operation. After all operations, we need two conditions:
All cells are colored (i.e., each cell has been painted at least once).
Each color appears at least once (i.e., for each i, there is at least one cell that finally has color i).
We need to output feasible p_i, or -1 if impossible.
Constraints: n,m up to 100k, l_i up to n.
Observations: This is a typical problem about interval covering and ensuring each color appears. It's reminiscent of constructing a sequence of intervals such that the final coverage is complete and each color has at least one cell that is not overwritten by later intervals. Since later operations override earlier ones, the final color of a cell is the color of the last operation covering it.
We need to choose positions for intervals so that the final coloring:
Covers all cells (so union of all intervals must be whole [1,n], but careful: later intervals can cover gaps? Actually, if a cell is never painted, it remains empty; but condition is all cells colored, so every cell must be painted at least once. The union of intervals must be [1,n]. However, note that if an earlier interval covers a cell and later intervals also cover it, that's fine as long as at least one operation covers each cell. So we need union of all intervals = [1,n].
Additionally, each color i must appear at least once in final coloring. That means there exists at least one cell that is covered by operation i and not covered by any later operation (j > i). Because later operations would override the color.
So for each i, there must be some cell in its interval that is not covered by any later interval. In other words, intervals should be placed such that each interval i contributes at least one "exclusive" cell that is not overlapped by any later interval.
Given that intervals are applied in order 1 to m, later intervals can overlap earlier ones and wipe out their colors.
This is reminiscent of constructing a sequence of intervals where we need a "staircase" pattern: later intervals should not completely cover earlier intervals. More formally, we can think backwards: The last operation (color m) will always appear on all cells it paints because no later operation overrides it. So we must ensure that the last interval covers at least one cell (obviously it will if it's placed somewhere) and also we need overall coverage. Similarly, for each earlier color, we need some part not covered by later colors.
We can approach by planning the positions in reverse order, ensuring each earlier interval leaves some uncovered part that will remain its color. Alternatively, think in terms of segment covering with "uncovered" cells that are not yet assigned to any later color; we need to assign to each earlier interval an exclusive cell.
One typical solution: sort of greedy: The sum of lengths might be insufficient to cover all cells if placed poorly, but we can choose positions to fill gaps. However, the condition that each color appears at least once imposes a lower bound on total length? Actually, we need each color i to have at least one exclusive cell, so the sum of lengths must be at least n + (m-1) ??? Not exactly. Because overlapping can cause some cells to be shared, but each exclusive cell must be distinct? Wait, does exclusive cell for different colors have to be distinct? For color i, we need a cell that finally has color i. Those cells are distinct for different colors because each cell has exactly one final color. So the set of cells that are final color i are disjoint across i. So the union of all final colors = all cells (since each cell has some color). Therefore, we have a partition of n cells into m colors (some colors may have multiple cells, but each cell assigned to exactly one color). So the final coloring is a partition of the n cells into m non-empty subsets, each subset corresponding to the cells that are last painted by that color.
Now, the intervals: For each i, the cells that finally have color i must be a subset of the interval [p_i, p_i + l_i - 1] (since they were painted by i and not overridden). Also, they must be contiguous? Not necessarily; the final region for a color could be multiple segments? Actually, because later intervals can overwrite parts, the final color i region consists of those parts of its interval that are not covered by any later interval. Since later intervals are intervals, the uncovered part of interval i will be a union of subintervals (possibly several). But the condition only requires at least one cell; it's fine if it's multiple, but we need at least one. However, the final coloring partition must be a proper partition of the line into segments? Because each cell gets a color from the last interval covering it. If we have intervals placed arbitrarily, the final coloring will be such that colors appear in contiguous blocks? Let's examine: The process: start with no color. Apply interval 1: paints a contiguous block with color 1. Then interval 2: paints its block, overriding any overlapping cells with color 2. Then interval 3, etc. The final coloring is determined by the last interval covering each cell. Since intervals are contiguous, the final coloring's color changes only at boundaries of intervals. More formally, the function that assigns to each cell the index of the last interval covering it is piecewise constant with changes at endpoints of intervals. Actually, consider we have sorted interval start and end positions. The final color for a cell is the maximum i such that the cell lies within interval i. Because later intervals have larger index. So the final color of each cell is the maximum index among intervals covering that cell. So the coloring is determined by taking intervals sorted by index and for each cell, take max i covering it. That's like a sweep where we maintain the highest index covering each point. The coloring regions are contiguous because the function f(x) = max{ i : x in interval i } can change only at interval endpoints. So indeed the final colors form contiguous segments (maybe multiple segments per color if later intervals are nested? Let's test: intervals: [1,5] color1, [2,3] color2, [4,5] color3. Then f: cell1 ->1 (only 1), cell2->2 (2>1), cell3->2, cell4->3 (3>1? Actually 3 covers 4,5; also 1 covers 4,5, so max=3), cell5->3. So colors: 1,2,2,3,3 -> contiguous segments: [1,1]=1, [2,3]=2, [4,5]=3. So contiguous. In general, because intervals are closed and we take maximum index, the function is piecewise constant with breakpoints at interval starts and ends. However, if intervals are disjoint or partially overlapping, it's still contiguous segments. So the final coloring partitions the line into contiguous segments, each with a single color, and segments correspond to the last covering interval for that region. Moreover, the segment for color i will be a subset of its interval, and it will be exactly the portion of its interval not covered by any interval with index > i. And that portion is a union of intervals, but due to the max-index property, it will be a contiguous segment? Actually, not necessarily contiguous: Consider intervals: 1: [1,10]; 2: [2,4]; 3: [6,8]; then final: cell1:1; cells2-4:2? Actually max covering cell2: both 1 and 2, max=2; cell3:2; cell4:2; cell5: only 1? 2 covers 2-4, 3 covers 6-8. So cell5: only 1 =>1; cell6: 1 and 3 =>3; cell7:3; cell8:3; cell9-10:1. So final colors: 1,2,2,2,1,3,3,3,1,1. So color 1 appears in three separate segments. So a color can appear in multiple disjoint segments. So the final partition is not necessarily one segment per color; colors can be fragmented. So the exclusive cells for a color can be multiple separated segments.
Nevertheless, each color i must have at least one cell, meaning that its interval must contain some point not covered by any later interval. That condition is: for each i, the union of intervals of indices > i must not completely cover interval i. Or equivalently, there is at least one point in [p_i, p_i+l_i-1] that is not in any [p_j, p_j+l_j-1] for j>i.
Thus the problem reduces to: assign positions p_i such that:
The union of all intervals covers [1,n].
For each i from 1 to m, interval i is not fully covered by the union of intervals with larger index.
We can also think about constructing from last to first: Start with the requirement that after all operations, all cells colored. The last operation m: its final color region is exactly its interval (since no later overrides). So we need that its interval does not have to cover everything, but the union of all intervals must be full. However, from a backward perspective, we can consider that we need to assign each cell a "last" interval that covers it. That is equivalent to: we need to choose for each cell an interval index (the last covering it). The constraints: for each interval i, the set of cells assigned to it must be a subset of its interval (and non-empty for each i). Also, the assignment must be such that if a cell is assigned to interval i, then no interval j>i covers that cell (since i is last). But if we just assign each cell to some interval, we can then try to place intervals to satisfy that assignment? However intervals are continuous, so the assignment must be consistent with interval placements.
Another viewpoint: This is similar to constructing a schedule where intervals are placed, and we need the final coverage to be a "coloring" such that each interval's color appears. I recall a known problem: "Dreamoon and Coloring" or similar? Might be from Codeforces: "Dreamoon Likes Coloring" (CF round). Actually I recall a Codeforces problem: "Dreamoon Likes Coloring" (maybe 1330C?). It had exactly this description: n,m, l_i, output p_i. The solution is to check feasibility and construct p_i greedily. Let's recall: In CF problem "Dreamoon Likes Coloring" (Round #631, Div2 C?), the constraints are similar. The typical solution: Check if sum of lengths >= n, and also for each i, l_i <= n-i+1? Actually recall: In that problem, we need to ensure each color appears at least once, which means that the starting positions must be such that later intervals don't completely cover earlier ones. A typical necessary condition is that for each i, l_i <= n - i + 1? Not exactly. Let's think: If we want each color to have an exclusive cell, consider the worst-case scenario: we might try to place intervals as far right as possible? There was a known approach: Place intervals such that they form a "staircase": p_i = i? But with lengths, we need to adjust.
Let's derive conditions.
Let’s denote final coverage: After all operations, each cell gets the color of the highest index covering it. So for color i to appear, there must exist a cell x such that i is the maximum index covering x. Equivalent: There exists x such that x is in interval i and for all j>i, x not in interval j.
So we need to ensure that for each i, interval i is not completely covered by the union of intervals j>i.
A necessary condition: The total length of intervals from i to m (i.e., sum_{j=i}^m l_j) must be at most something? Actually, if we want interval i not to be fully covered by later intervals, then the later intervals cannot cover all of its length. Since later intervals can be placed arbitrarily, they might cover it entirely if the sum of lengths of later intervals is at least l_i, but they might also be placed elsewhere. So it's not a strict sum condition; we can place later intervals so that they avoid covering some part of interval i. However, there is also the need to cover all cells from 1 to n. So later intervals must cover all cells that are not covered by earlier intervals? Actually, the union of all intervals must cover [1,n]. That imposes that the intervals must be placed such that the uncovered gaps are filled.
Another necessary condition: Since each color must appear at least once, we need at least m cells each assigned to distinct colors. So n must be at least m. The problem statement says m ≤ n, so that's given.
Also, the sum of lengths must be at least n, because even if we perfectly overlap, the union cannot exceed sum of lengths, but to cover n cells, we need union = n, so sum of lengths must be at least n. Indeed, the union cannot be larger than the sum of lengths (if disjoint) but can be smaller due to overlaps. To achieve full coverage, we need sum of lengths >= n, but also we need to be able to arrange overlaps such that no gaps remain. However, is sum >= n sufficient for coverage? Not always, because if intervals are too short and forced to be placed within a range due to starting constraints, they might not cover extremes. But we can choose positions freely. Usually, with sum >= n, we can cover all cells if we can arrange them contiguously without gaps; but we also have the constraint that each interval i must start at ≤ n - l_i +1. That's always possible.
But we also have the exclusive appearance condition.
I recall the CF problem: In "Dreamoon Likes Coloring", the solution uses the condition sum l_i >= n and also for each i, we need something like i + l_i - 1 <= n? Actually, we need to ensure that we can place intervals such that each color appears. The known solution: compute suffix sums of lengths, then set p_i = max(i, n - suffix_sum_i + 1) maybe. Let me recall.
Search memory: There is a Codeforces problem "1330C - Dreamoon Likes Coloring". Yes, that's Div2 C. Let me recall the solution: We have n, m, and lengths l_i. Need to output positions. The solution: If sum l_i < n, impossible. Also, we need to ensure that for each i, l_i <= n - i + 1? Actually there is another condition: Because we need each color to appear, we need to ensure that the intervals are not too long such that they "cover" all later cells leaving no room for earlier colors. In the CF solution, they compute an array p_i: p_i = max(i, n - (sum_{j=i}^{m} l_j) + 1). And then check that for all i, p_i <= n - l_i + 1 and p_i >= 1. Actually, they set p_i as the maximum between i and n - suffix_sum_i + 1. Then they check if p_i + l_i - 1 <= n? Actually that's always satisfied if p_i <= n - l_i + 1. They then check that p_i <= n - l_i + 1 (automatically if the max condition ensures not exceeding). They also check that p_i >= i? Not necessarily, but they have max(i, ...). Let me reconstruct reasoning.
I recall from editorial: The idea is to place intervals as far to the left as possible initially (like p_i = i), but then we need to cover the whole segment up to n, so we shift some intervals to the right to fill the gap. The condition for each color to appear is that the starting position of interval i must be at least i, because otherwise the interval would start before i and possibly cover cell i? Actually, to ensure that color i appears, we need some cell that is not covered by later intervals. If we place intervals such that they are all "non-overlapping" in a certain pattern, we can guarantee each has a unique cell. A typical safe construction: Place intervals such that the rightmost endpoint of interval i is strictly less than the starting point of interval i+1? But that would give disjoint intervals, then each color appears on its own interval, and union covers sum lengths. But sum lengths may exceed n or be less. But we need exactly coverage of all cells without gaps. If sum l_i = n, we could place them back-to-back without gaps: p_1=1, p_2=1+l_1, p_3=p_2+l_2, ... This yields disjoint intervals covering [1,n]. Then each color appears because intervals are disjoint, no overlaps, so each color's cells are exactly its interval. However, we need to ensure that each p_i is within [1, n-l_i+1]. That placement works if the cumulative sum does not exceed n. For disjoint placement, we need p_i = 1 + sum_{j< i} l_j. But we must also ensure that p_i ≤ n - l_i + 1, which is equivalent to sum_{j< i} l_j + l_i ≤ n, i.e., sum_{j≤i} l_j ≤ n. This holds for all i if total sum = n. So if total sum = n, we can simply place them consecutively from left: p_i = 1 + sum_{j=1}^{i-1} l_j. This yields full coverage and each color appears (since intervals are disjoint). So that's a valid solution when sum = n.
But if sum > n, we have to overlap some intervals to reduce total coverage to exactly n (since union can't exceed n but we need union exactly n; overlapping reduces union length). However, overlapping must be done carefully to still have each color appear: if we overlap, some colors might get completely covered by later ones. So we need to ensure each interval has some exclusive part.
We can think in terms of "exclusive minimum length" needed: For color i to appear, there must be at least one cell that is covered by i but not by any later interval. That means that the total length of interval i that is not covered by later intervals > 0. Equivalently, the union of later intervals does not cover the whole interval i. One way to guarantee this is to ensure that the rightmost endpoint of interval i is greater than the leftmost start of later intervals? That doesn't guarantee exclusive part.
Better to think backwards: Starting from the last interval, it will be fully visible. So we can let it cover some suffix of the line. Then the second last interval must cover some cell to the left of that suffix that is not covered by the last interval. So we can place intervals from right to left, each one leaving some uncovered portion to its left for earlier intervals.
This suggests a greedy from right: Place intervals so that they cover a suffix of the yet-uncovered region, while ensuring each interval extends left enough to provide an exclusive cell. More concretely, we want to cover the whole line [1,n] with intervals placed in order 1..m, but we can think of constructing positions from m down to 1. Let’s denote remaining uncovered cells after placing intervals from m down to i+1. Initially, after placing intervals m, m-1, ..., i+1, we have some uncovered segment [1, R] perhaps. Then we need to place interval i such that it covers some part of this uncovered region, and also it must have at least one cell that remains uncovered by later intervals (i.e., that lies in the uncovered region before placing later intervals? Actually careful: When placing backwards, later intervals (higher index) are placed after i in the forward order, so when placing from right to left, we have already placed intervals i+1..m. Those later intervals cover certain cells. For color i to appear, its interval must include at least one cell that is not covered by any of intervals i+1..m. So if we ensure that when we place interval i, it covers at least one cell that is currently uncovered (i.e., not covered by later intervals), then that cell will retain color i because later intervals won't cover it (since it's uncovered by them). So that's a clean condition: While constructing backwards, we maintain the set of cells that are already covered by later intervals (i.e., cells that will get colors > i). Then we choose p_i so that its interval covers some part of the yet-uncovered cells, and also perhaps extends into covered region, but we must ensure that at least one cell of its interval falls into the uncovered region. Moreover, we also need that after placing all intervals, the whole [1,n] is covered. So eventually, after placing interval 1, the uncovered region should become empty.
Thus the backward construction: We want to cover the whole line by intervals placed from m down to 1, each new interval (with lower index) must cover at least one new cell that was previously uncovered, to ensure its color appears; and we want to minimize overlaps to also satisfy that total coverage becomes full.
This is reminiscent of covering from right: Suppose we want to cover [1,n] with intervals of given lengths, with the constraint that when we add interval i (going backwards), it must extend the covered segment to the left (or fill gaps). Typically, we might place intervals so that they cover a suffix, and each new interval extends the covered suffix leftwards by at least 1 (its exclusive part) but possibly more due to its length.
Let's formalize: Let's define after processing intervals from m down to i+1, the covered region is some set C_{i+1} (cells that will be colored by colors > i). Since later intervals are contiguous? Not necessarily: Later intervals might be placed anywhere, so the covered region might be a union of intervals. However, we can aim for a simpler structure: we can enforce that the covered region after placing intervals from m down to i+1 is a suffix of the line, i.e., [L, n] for some L. Is that always possible while achieving optimal? Probably yes, because we can choose to place intervals from the rightmost side, each covering a suffix, and overlapping. If we place intervals from right to left, each interval i can be placed such that its right endpoint is as far right as possible (maybe n) or not? Let's examine.
In many solutions, they propose to start with the last interval: place it at the rightmost possible? Actually, to maximize coverage, we often align intervals to the right. The standard approach from CF editorial: First check if sum l_i >= n. Then compute suffix sums S_i = sum_{j=i}^m l_j. Then set p_i = max(i, n - S_i + 1). And then check if p_i + l_i - 1 <= n (which is equivalent to p_i <= n - l_i + 1). If any p_i > n - l_i + 1, then it's impossible. Else output p_i.
Let's test this with sample: n=5,m=3, l=[3,2,2]. sum=7 >=5. Compute suffix sums: S_3 = 2, S_2=4, S_1=7. Then p_3 = max(3, n - S_3 +1) = max(3, 5-2+1=4) =4. p_2 = max(2, 5-4+1=2) =2. p_1 = max(1, 5-7+1=-1) but max(1, -1) =1. So p = [1,2,4]. But sample output is [2,4,1]? Actually sample output: 2 4 1. Our computed is 1 2 4. Are both valid? Let's test: intervals: color1: [1,3]; color2: [2,3]; color3: [4,5]. Final: cells 1: only color1 ->1; cells 2,3: max color? color1 covers 2,3; color2 covers 2,3; color3 covers none. So max is 2? Actually indices: 1,2,3. cell2: covered by 1 and 2, max=2; cell3: same. So final: cell1=1, cell2=2, cell3=2, cell4=3, cell5=3. All colors appear (1,2,3) and all cells colored. So [1,2,4] works. The sample output is different but also works. So the formula gives a valid answer. However, does it always guarantee p_i <= n - l_i +1? Let's check: For p_1: 1 <= 5-3+1=3 OK. p_2:2 <= 5-2+1=4 OK. p_3:4 <= 5-2+1=4 OK.
Now check another case: n=3,m=2,l=[2,2]. sum=4 >=3. suffix: S2=2, S1=4. p2 = max(2, 3-2+1=2) =2. p1 = max(1, 3-4+1=0) -> max(1,0)=1. p1+ l1 -1 =1+2-1=2 <=3. So p=[1,2]. Intervals: [1,2] and [2,3]. Final: cell1:1; cell2: max(1,2)=2; cell3:2. Colors: 1 appears? cell1 is color1, yes. Both appear. All cells colored. That's valid.
What about n=3,m=3,l=[1,1,1]. sum=3 >=3. suffix: S3=1, S2=2, S1=3. p3 = max(3, 3-1+1=3) =3. p2 = max(2, 3-2+1=2)=2. p1 = max(1, 3-3+1=1)=1. So p=[1,2,3]. Intervals: [1,1],[2,2],[3,3]. All colors appear, all cells colored. Good.
Now consider n=5,m=3,l=[4,1,1]. sum=6>=5. suffix: S3=1, S2=2, S1=6. p3= max(3,5-1+1=5)=5. p2= max(2,5-2+1=4)=4. p1= max(1,5-6+1=0)=1. Check p_i + l_i -1: p3+l3-1=5+1-1=5 <=5 OK; p2+l2-1=4+1-1=4<=5 OK; p1+l1-1=1+4-1=4<=5 OK. So intervals: [1,4], [4,4], [5,5]. Final: cell1-3: only color1 =>1; cell4: covered by color1 and color2, max=2; cell5: color3. Colors: 1 appears (cells1-3), 2 appears (cell4), 3 appears (cell5). All colored. Valid.
Now consider a tricky case: n=5,m=3,l=[4,4,1]. sum=9>=5. suffix: S3=1, S2=5, S1=9. p3= max(3,5-1+1=5)=5; p2= max(2,5-5+1=1) => max(2,1)=2? Wait compute: n - S2 + 1 =5-5+1=1. So p2= max(2,1)=2; p1= max(1,5-9+1=-3)=1. So p=[1,2,5]. Check p2: 2+4-1=5 <=5 OK; p1:1+4-1=4 <=5 OK. Intervals: [1,4], [2,5], [5,5]. Final: cell1: only color1 ->1; cell2: covered by 1 and 2, max=2; cell3: same ->2; cell4: covered by 1 and 2 ->2; cell5: covered by 2 and 3 -> max=3. So colors: 1 appears (cell1), 2 appears (cells2-4), 3 appears (cell5). All colored. Good.
Now consider case that might be impossible: sum < n obviously impossible. Also, if for some i, l_i > n - i + 1? Actually, recall the CF problem had an additional condition: For each i, l_i must be at most n - i + 1. But is that necessary? Let's test: n=3,m=2,l=[3,1]. sum=4>=3. suffix: S2=1, S1=4. p2 = max(2,3-1+1=3)=3; p1 = max(1,3-4+1=0)=1. Intervals: [1,3], [3,3]. p2=3 is valid (since n-l2+1=3-1+1=3). So intervals: [1,3] (color1), [3,3] (color2). Final: cell1,2: color1; cell3: max(1,2)=2. So colors: 1 appears, 2 appears. All cells colored. So l1=3, i=1, n-i+1=3, so l1=3 OK. That's fine.
Now test n=4,m=3,l=[3,2,1]. sum=6>=4. suffix: S3=1, S2=3, S1=6. p3 = max(3,4-1+1=4)=4; p2 = max(2,4-3+1=2)=2; p1 = max(1,4-6+1=-1)=1. Intervals: [1,3], [2,3], [4,4]. Final: cell1:1; cell2:2; cell3:2; cell4:3. Colors: all present. Good.
Now test a case where maybe the formula fails due to p_i > n - l_i + 1. Consider n=5,m=3,l=[5,5,1]. sum=11>=5. suffix: S3=1, S2=6, S1=11. p3 = max(3,5-1+1=5)=5; p2 = max(2,5-6+1=0)=2; p1 = max(1,5-11+1=-5)=1. p2+l2-1=2+5-1=6 >5, invalid because p2 must be ≤ n - l2 + 1 = 5-5+1=1, but p2=2 >1. So the formula gives p2=2 which is not allowed. So according to this, it's impossible? But is it truly impossible? Let's think: n=5,m=3, l1=5, l2=5, l3=1. We need to place three intervals: lengths 5,5,1. We need all colors appear. Since the last interval (color3) length 1, it will occupy exactly one cell. The first two intervals both cover the whole line if placed appropriately. But to have color2 appear, interval2 must have at least one cell not covered by interval3. Since interval3 is just one cell, that's easy: interval2 can cover the whole line except perhaps the cell of interval3? But interval3's cell will be overwritten by interval3, so color2 will not appear on that cell. So color2 needs a cell that is not covered by interval3. So interval3 must not cover the entire interval2; i.e., interval2 must have at least one cell not covered by interval3. Since interval3 length 1, that's fine. However, interval2 itself must also not be completely covered by interval1? Wait, color1 needs a cell not covered by any later interval (i.e., intervals2 and3). So interval1 must have at least one cell that is not covered by interval2 or interval3. Since interval2 length 5, it can cover the whole line if placed appropriately. If interval2 covers the whole line, then interval1 will have no exclusive cell, because interval2 (later) covers everything. So to allow color1 to appear, interval2 must leave some cell uncovered. But interval2 itself must be placed within [1, n-l2+1]=[1,1] because l2=5 => only possible start p2=1. So interval2 is forced to be [1,5] (covers entire row). Then interval2 covers all cells. Then any cell covered by interval1 (which also must be placed with start between 1 and 1 => [1,5]) will be overwritten by interval2. So color1 would have no cell (since later interval2 overrides all). That would violate condition. Could we place interval2 not covering entire row? But l2=5 and n=5, so any start p2 must be 1 (since n-l2+1=1). So interval2 is forced to be [1,5]. Thus color1 cannot appear. So indeed impossible. So the condition p2 > n-l2+1 indicates impossibility. So our formula detected it. Good.
Now consider n=5,m=3,l=[4,4,4]. sum=12>=5. suffix: S3=4, S2=8, S1=12. p3 = max(3,5-4+1=2)=3? Actually n - S3 +1 =5-4+1=2; max(3,2)=3 => p3=3. Check if p3 <= n - l3 +1 =5-4+1=2? p3=3 >2, invalid. So impossible. Is it actually impossible? lengths: all 4. n=5. Each interval length 4, so possible start positions: 1 or 2. Last interval (color3) must start at 1 or 2. Then interval2 length 4, start 1 or 2. Interval1 also start 1 or 2. We need all colors appear. Since intervals are length 4, each covers almost all cells. But can we arrange such that each has an exclusive cell? Let's attempt: color3 must have some cell not covered by any later (none), so it's fine as long as placed. But color2 needs a cell not covered by color3. So we need interval2 to have a cell not covered by interval3. Since intervals are length 4, if we place interval3 at start=1, it covers [1,4]; interval2 could start at 2 covering [2,5]. Then cells: interval2 exclusive? It covers cell5 which is not covered by interval3 (since interval3 covers 1-4), so color2 can have exclusive cell5. Color1 needs a cell not covered by interval2 or interval3. interval2 covers 2-5, interval3 covers 1-4. Their union is [1,5] (all cells). Is there any cell not covered by either? Cell? 1 covered by interval3, 2-4 covered by both, 5 covered by interval2. So all cells covered by later intervals. So color1 would have no exclusive cell. Could we shift differently? Options: p3=1 or 2. p2=1 or 2. p1=1 or 2. Let's enumerate: If p3=1 -> covers 1-4. p2=1 -> covers 1-4. Then union of later intervals (2 and 3) = 1-4. p1 can be 1 or 2. If p1=1 -> covers 1-4, all cells covered by later -> no exclusive. If p1=2 -> covers 2-5, but then cell5 is not covered by later? Later intervals: p2=1 covers 1-4; p3=1 covers 1-4. So cell5 is not covered by later intervals, so interval1 would have exclusive cell5, but wait: interval1 covers 2-5. cell5 is not covered by later, so color1 would appear on cell5. However, does color2 appear? interval2 covers 1-4, but later interval3 covers 1-4 as well, so interval2's cells are all covered by interval3? Actually interval2's cells are 1-4. Interval3 also covers 1-4, and since interval3 is later, all those cells become color3. So color2 would have no exclusive cell. So fails.
If p3=2 -> covers 2-5. p2=2 -> covers 2-5. Then later union =2-5. p1 can be 1 or 2. If p1=1 -> covers 1-4. Then cell1 is not covered by later (since later cover 2-5), so color1 appears on cell1. But color2? interval2 covers 2-5, all covered by later interval3 (2-5), so color2 has no exclusive cell. If p1=2 -> covers 2-5, all covered by later, so color1 no exclusive. So any assignment leads to at least one of the earlier two colors lacking exclusive cell. What about p3=1, p2=2? Then later: interval3 covers 1-4, interval2 covers 2-5, union=1-5. color2's interval =2-5. Are any cells not covered by later (i.e., interval3 only? later intervals for color2 are intervals >2: i.e., interval3 only). So color2 needs a cell not in interval3. Interval2 has cells 2-5; interval3 covers 1-4. So cell5 is not in interval3, so color2 can be exclusive on cell5. Good. Then color1: later intervals are 2 and 3, union=1-5. interval1 must have a cell not in that union? That's impossible because union covers all cells. So color1 fails. If we set p1=1? interval1 covers 1-4. All cells in 1-4 are covered by later? interval2 covers 2-5 includes 2-4, interval3 covers 1-4 includes 1-4, so yes all covered. If p1=2 covers 2-5, all covered as well. So no exclusive for color1. So impossible. So indeed condition is correct.
Thus the construction p_i = max(i, n - S_i + 1) seems to be the standard solution, with an additional check that p_i <= n - l_i + 1 for all i (which may fail indicating impossibility). Also we need sum l_i >= n.
Now we need to confirm that this condition (p_i <= n - l_i + 1) is equivalent to the necessary condition that for each i, l_i <= n - i + 1? Let's derive: p_i = max(i, n - S_i + 1). The condition p_i <= n - l_i + 1. Since p_i is at least i, we must have i <= n - l_i + 1, i.e., l_i <= n - i + 1. That's a necessary condition. Also, from the max with n - S_i + 1, we need n - S_i + 1 <= n - l_i + 1 => - S_i + 1 <= - l_i + 1 => -S_i <= -l_i => l_i <= S_i, which is true (since S_i includes l_i). Actually need to check both components: p_i could be either i or n - S_i + 1, whichever larger. For p_i to be feasible, we need both possibilities to be ≤ n - l_i + 1. So we need i ≤ n - l_i + 1 and n - S_i + 1 ≤ n - l_i + 1. The second simplifies to -S_i + 1 ≤ -l_i + 1 => -S_i ≤ -l_i => S_i ≥ l_i, which holds. But careful: If n - S_i + 1 is larger, then p_i = n - S_i + 1, and we need that ≤ n - l_i + 1 => n - S_i + 1 ≤ n - l_i + 1 => -S_i ≤ -l_i => l_i ≤ S_i, which is true. However, also we need n - S_i + 1 ≥ 1? Actually p_i must be ≥1. n - S_i + 1 could be negative if S_i > n+1? But S_i could be larger than n? Since sum l_i may be > n. But n - S_i + 1 might be negative. Then max(i, negative) = i, so p_i=i. So the expression automatically ensures p_i ≥ i. So the only actual feasibility condition is that when p_i = i, we need i ≤ n - l_i + 1, i.e., l_i ≤ n - i + 1. And when p_i = n - S_i + 1, we need that value to be ≤ n - l_i + 1. But that is automatically true if n - S_i + 1 ≤ n - l_i + 1 which reduces to S_i ≥ l_i, true. However, we also need p_i ≥ 1. n - S_i + 1 may be ≤0. But if it's ≤0, we take i which is ≥1. So p_i always ≥1. So the only possible violation is when p_i > n - l_i + 1. But given p_i = max(i, n - S_i + 1), we need to check that both i and n - S_i + 1 are ≤ n - l_i + 1. Actually we need the maximum to be ≤ n - l_i + 1, which implies both are ≤ that bound, because if the max is ≤ bound, then each individual ≤ bound. So we need i ≤ n - l_i + 1 and n - S_i + 1 ≤ n - l_i + 1. As argued, the second is l_i ≤ S_i (always true). So the only necessary condition is i ≤ n - l_i + 1, i.e., l_i ≤ n - i + 1 for all i. Also we need sum l_i ≥ n. But is that sufficient for the construction to always satisfy p_i ≤ n - l_i + 1? Let's test: if l_i ≤ n - i + 1 holds, does it guarantee p_i ≤ n - l_i + 1? p_i = max(i, n - S_i + 1). We need to check both: i ≤ n - l_i + 1 is exactly l_i ≤ n - i + 1, so okay. For the other: n - S_i + 1 ≤ n - l_i + 1 ⇒ S_i ≥ l_i, true. So seems always satisfied. But wait: the example earlier where we found p2 > n - l2 +1 (n=5, l=[5,5,1]) gave violation: for i=2, l2=5, n-i+1=5-2+1=4, so l2=5 > 4, thus condition l_i ≤ n - i + 1 fails. So indeed the violation came from that. So the necessary and sufficient condition might be: sum l_i ≥ n and for all i, l_i ≤ n - i + 1. In CF editorial, they mention these two conditions: total length at least n, and for each i, l_i ≤ n - i + 1. But is that second condition always necessary? Let's reason: For color i to have an exclusive cell, there must be at least one cell that is covered by i and not by any later intervals. Consider the set of cells that can potentially be exclusive for i. Since later intervals are placed after i, they can be anywhere. However, a necessary condition can be derived: The union of all intervals (including i) must cover [1,n]. But perhaps there is a more direct necessity: The total length of intervals after i (i+1..m) cannot cover everything from i to n? Not sure.
Let's try to see if a case with sum >= n and each l_i ≤ n - i + 1 could still be impossible? According to the construction, if these hold, then p_i computed as above will be valid, so it's sufficient. So the condition is sufficient.
But is the condition "l_i ≤ n - i + 1" necessary? Consider i=1: l_1 ≤ n? That's always true since l_i ≤ n. So for i=1, condition is l1 ≤ n, which holds. For i=2: l2 ≤ n-1. Could there be a case where l2 > n-1 but still possible? Example: n=5,m=2,l=[2,5]. sum=7≥5. l2=5 > n-1=4. Is it possible? We need two intervals: length2 and length5. The second interval (color2) length5 must start at 1 (only possibility). So it covers all cells [1,5]. The first interval length2, can start anywhere 1..4. Its interval will be covered by interval2 (since interval2 covers all). So color1 would have no exclusive cell. So impossible. So condition necessary.
What about i=3 with n=5,m=3,l=[1,1,4]? l3=4, n - 3 +1 =3, so 4>3. sum=6≥5. Is it impossible? Check: l3=4, start must be 1 or 2. Color3 (last) is fine. Color2: length1, can be placed. Color1: length1. Need each color appears. Could we arrange? Let's try: p3 must be 1 or 2. If p3=1 -> covers 1-4. Then to have color2 appear, interval2 must have a cell not covered by interval3. Since interval3 covers 1-4, the only cell not covered is 5. So interval2 must cover 5, so p2=5? But l2=1, p2 can be up to 5? Actually n-l2+1=5, so p2=5 is allowed. Then interval2 covers cell5. Color2 exclusive? Later interval3 does not cover cell5, so color2 appears on cell5. Good. Now color1 needs a cell not covered by intervals2 or 3. Interval2 covers 5, interval3 covers 1-4. Their union is 1-5, all cells. So there is no cell not covered by later intervals. Could we place interval2 elsewhere? If p3=1, interval2 could be placed at say 2 covering 2, but that's covered by interval3; then color2 would be overwritten. So no exclusive. If p3=2 -> covers 2-5. Then interval3 covers 2-5. The uncovered cell is 1. So interval2 must cover 1 to be exclusive (p2=1). Then color2 exclusive on 1. Then later intervals union = [1] from interval2? Actually interval2 covers 1, interval3 covers 2-5. So union = 1-5. Color1 needs a cell not in union, impossible. So indeed impossible. So condition necessary.
Now consider a borderline: n=5,m=3,l=[2,3,3]. Check l2=3, n-i+1=5-2+1=4, so 3≤4 ok; l3=3, 5-3+1=3 ok. sum=8≥5. Construction: suffix S3=3, S2=6, S1=8. p3=max(3,5-3+1=3)=3; p2=max(2,5-6+1=0)=2; p1=max(1,5-8+1=-2)=1. Check p3+l3-1=3+3-1=5 ≤5 ok. p2:2+3-1=4 ok. p1:1+2-1=2 ok. Intervals: [1,2], [2,4], [3,5]. Final: cell1:1; cell2: max(1,2)=2; cell3: max(1,2,3)=3; cell4: max(2,3)=3? Wait cell4 covered by interval2 (2-4) and interval3 (3-5) => max=3; cell5:3. Colors: 1 appears (cell1), 2 appears (cell2), 3 appears (cells3-5). Good.
Now let's test a case where l_i ≤ n-i+1 holds but sum < n: e.g., n=5,m=3,l=[1,1,1]. sum=3<5, impossible because union cannot cover all cells. So sum>=n necessary.
Thus necessary and sufficient conditions: sum_{i=1}^m l_i >= n and for all i, l_i <= n - i + 1.
Now we need to output positions. The construction p_i = max(i, n - suffix_sum_i + 1) yields valid positions if the conditions hold. But is it always that this construction satisfies p_i <= n - l_i + 1 given l_i <= n - i + 1? As argued, if l_i ≤ n-i+1, then i ≤ n - l_i + 1, so the i component is okay. Also n - S_i + 1 ≤ n - l_i + 1 (since S_i >= l_i). So both ≤ bound, so max ≤ bound. But wait, what if n - S_i + 1 is less than 1? That's fine, the max is i. So it's okay. So p_i is always valid.
We should also ensure that the final coverage covers all cells. Let's verify that the construction indeed yields union covering [1,n]. We'll analyze: The p_i are constructed such that intervals are placed in a "staircase" manner: each interval starts at least at i (so they are shifted to the right as i increases). Also, the construction ensures that the intervals collectively cover from 1 to n without gaps. Typically, the reasoning: For i from m down to 1, we want the interval i to cover the point n - (sum_{j=i+1}^m l_j) maybe. Actually, the idea is to cover the line by aligning intervals to the right: The last interval should cover the suffix of length l_m, but its start is p_m = max(m, n - l_m + 1). Since m ≤ n (as m ≤ n) and n - l_m + 1 is often the rightmost start? Wait, we want the intervals to cover from left to right. The construction sets p_i so that the rightmost endpoint of interval i (i.e., p_i + l_i -1) is at least something.
From CF editorial: They compute p_i as the maximum between i and n - (suffix sum of lengths from i to m) + 1. This ensures that the intervals, when placed, will cover the whole [1,n] and each interval has an exclusive part at its left (i.e., the point i is covered only by intervals up to i? Actually, they also ensure that for each i, p_i ≥ i, so that the point i is covered by interval i? Possibly to guarantee that interval i covers cell i, ensuring it has a cell that might be exclusive? But not exactly; but the condition p_i ≥ i ensures that interval i does not start before i, so that earlier intervals cannot cover too far left.
Let's prove that the construction works:
Define suffix sums: suff[i] = sum_{j=i}^m l_j.
Set p_i = max(i, n - suff[i] + 1).
Properties:
p_i ≥ i.
p_i ≤ n - l_i + 1 (as argued if conditions hold).
Now consider coverage: For each i, define right_i = p_i + l_i - 1. Then we have:
Right_i = max(i, n - suff[i] + 1) + l_i - 1.
We want to show that the union of intervals covers [1,n].
Observe that for i=1, we have p_1 = max(1, n - suff[1] + 1). Since suff[1] = total sum >= n, n - suff[1] + 1 <= 1. So p_1 = 1. So interval1 starts at 1.
Also, for consecutive intervals, we can see that p_{i+1} ≤ p_i + l_i? Actually we need no gaps: we need that the leftmost uncovered point after placing intervals from m down to i is covered by interval i. The construction essentially ensures that the intervals are placed in such a way that the "coverage from the right" is contiguous.
Another way: Define the "required start" from the right: We want after placing intervals i+1..m, the covered suffix is from some point to n. Specifically, after placing intervals from m down to i+1, the minimum point that is covered (the leftmost point of the union) should be something like n - suff[i+1] + 1? Let's calculate.
Suppose we place intervals from m down to i+1 in the constructed positions. What is the leftmost point covered by these intervals? Since each interval j (j>i) has p_j set to max(j, n - suff[j] + 1). Typically, for later intervals, the start p_j might be as left as possible to cover the needed region. Actually, a simpler argument: The construction ensures that for each i, p_i + l_i - 1 >= n - suff[i+1] (maybe?). Let's derive:
Given p_i = max(i, n - suff[i] + 1). Consider two cases:
Case 1: n - suff[i] + 1 ≥ i => then p_i = n - suff[i] + 1. Then p_i + l_i - 1 = n - suff[i] + l_i = n - (suff[i] - l_i) = n - suff[i+1] (since suff[i] = l_i + suff[i+1]). So right_i = n - suff[i+1]. So the interval i ends exactly at n - suff[i+1].
Case 2: n - suff[i] + 1 < i => then p_i = i. Then we have n - suff[i] + 1 ≤ i - 1. This implies suff[i] ≥ n - i + 2. Since suff[i] = l_i + suff[i+1], we might have something. Then right_i = i + l_i - 1. But note that i + l_i - 1 ≤? Since l_i ≤ n - i + 1, we have i + l_i - 1 ≤ n. Also, we want to ensure that there is no gap between interval i and the coverage of later intervals. The later intervals (i+1..m) collectively cover a suffix starting at some L. Since we used the construction, it's likely that L = n - suff[i+1] + 1? Actually, we can prove by induction that after placing intervals i..m, the union covers from min(p_i, something) to n, and specifically, the leftmost point covered is ≤ something. But maybe easier: The construction is known to work.
Let's test the coverage property with an example where some p_i = i and others = n - suff[i] + 1.
Take n=10,m=4,l=[5,5,5,5]. sum=20≥10. Check l_i ≤ n - i +1: l1=5 ≤10; l2=5 ≤9; l3=5 ≤8; l4=5 ≤7 OK. Compute suffix: suff4=5, suff3=10, suff2=15, suff1=20. p4 = max(4,10-5+1=6) =>6. p3 = max(3,10-10+1=1) => max(3,1)=3. p2 = max(2,10-15+1=-4) =>2. p1 = max(1,10-20+1=-9) =>1. Check p4=6, p4+l4-1=10; p3=3, right3=3+5-1=7; p2=2, right2=2+5-1=6; p1=1, right1=1+5-1=5. Intervals: [1,5],[2,6],[3,7],[6,10]. Union: [1,7] U [6,10] = [1,10]. All covered. Now check exclusive cells: color4 (last) [6,10] appears. color3 [3,7]: cells 3,4,5,6,7. Later interval4 covers 6-10, so cells 3,4,5,7? Actually cell7 is covered by interval4? interval4 covers 6-10, includes 7, so cell7 is overwritten. Cells 3,4,5 are not covered by interval4 (since interval4 starts at6), but check interval? Actually interval3 also overlaps interval2? Later intervals for color3 are 4 only? Wait later intervals indices >3: only 4. So cells 3,4,5 are not in interval4, so they remain color3. So color3 appears. color2 [2,6]: later intervals 3 and 4. Their union covers [3,7] U [6,10] = [3,10]. So interval2 covers [2,6]; cells in [2,6] that are not in [3,10] is just cell2 (since cell2 is not in [3,10]), so color2 appears on cell2. color1 [1,5]: later intervals union = [2,6] U [3,7] U [6,10] = [2,10] essentially. So cell1 is exclusive. So all good.
Now test a case where some p_i = i but suff[i] is huge: n=6,m=3,l=[3,3,3]. sum=9≥6. Conditions: l1=3≤6; l2=3≤5; l3=3≤4 OK. suffix: suff3=3, suff2=6, suff1=9. p3 = max(3,6-3+1=4) =>4; p2 = max(2,6-6+1=1) =>2; p1 = max(1,6-9+1=-2)=>1. Intervals: [1,3],[2,4],[4,6]? Wait p3=4, l3=3 => [4,6]; p2=2 => [2,4]; p1=1=> [1,3]. Union: [1,3]U[2,4]U[4,6]=[1,6] covered. Check exclusives: color3: [4,6] all exclusive (no later). color2: [2,4]; later interval3 covers [4,6]; so cells 2,3 are not covered by later (since 4 is covered), so color2 appears on 2 or3. color1: [1,3]; later intervals union = [2,4] U [4,6] = [2,6]; cell1 is exclusive. Works.
Now test a case where n=100000,m=... works.
Thus the solution is:
If total sum of lengths < n: print -1.
Else, compute suffix sums.
For i from 1 to m, compute p_i = max(i, n - suffix[i] + 1).
Then check if for any i, p_i > n - l_i + 1. If so, print -1.
Else, output p_i.
But note: The condition p_i > n - l_i + 1 might be redundant if we already ensure l_i ≤ n - i + 1? Actually, if sum >= n and l_i ≤ n - i + 1 for all i, then p_i computed will satisfy p_i ≤ n - l_i + 1 automatically. However, what about the case where sum >= n but some l_i > n - i + 1? Then p_i may exceed the bound, and we correctly output -1. But is there any case where sum >= n and l_i ≤ n - i + 1 for all i, yet p_i > n - l_i + 1? Let's test: Suppose n=5, m=3, l=[3,2,1]? Check l2=2 <=4, l3=1 <=3. sum=6>=5. p3 = max(3,5-1+1=5) =5, p3+l3-1=5, n-l3+1=5, p3=5 OK. p2 = max(2,5-3+1=3) =3, p2+l2-1=4, n-l2+1=4, p2=3 <=4 OK. p1 = max(1,5-6+1=0)=1, OK. So fine.
What about n=5, m=3, l=[3,3,1]? l2=3 <=4? 3≤4 yes; l3=1≤3 yes; sum=7>=5. suffix: S3=1, S2=4, S1=7. p3 = max(3,5-1+1=5)=5; p2 = max(2,5-4+1=2)=2; p1=1. p2=2, n-l2+1=5-3+1=3, so 2≤3 ok. Good.
What about n=5, m=3, l=[2,4,1]? l2=4 <=4 ok; sum=7>=5. suffix: S3=1, S2=5, S1=7. p3= max(3,5-1+1=5)=5; p2= max(2,5-5+1=1)=2; p1=1. p2=2, n-l2+1=5-4+1=2 => OK (2≤2). Works.
Thus algorithm: compute total sum, if sum < n -> -1. else compute suffix sums array (size m+2 maybe). Then compute p_i = max(i, n - suffix[i] + 1). Then check for each i if p_i > n - l_i + 1; if any -> -1. else output p_i.
But wait: In some cases, the p_i might be > n - l_i + 1 even if l_i <= n - i + 1? Let's try to find a counterexample. We need to test mathematically: p_i = max(i, n - S_i + 1). Bound = n - l_i + 1. We need to ensure max(i, n - S_i + 1) ≤ n - l_i + 1. Since we already know i ≤ n - l_i + 1 (by l_i ≤ n - i + 1), the only worry is when n - S_i + 1 > i, and then we need n - S_i + 1 ≤ n - l_i + 1 => S_i ≥ l_i, which holds. So it's fine. So p_i never exceeds the bound. So we might skip the explicit check, but it's safe to include.
However, there is nuance: the condition l_i ≤ n - i + 1 might not be explicitly checked; but if sum >= n and we compute p_i as above, p_i will automatically be within bounds if and only if for all i, i ≤ n - l_i + 1? Actually, if for some i, l_i > n - i + 1, then i > n - l_i + 1. In that case, p_i will be at least i (since max(i,...) ) and i > n - l_i + 1, so p_i > bound. So the check p_i > n - l_i + 1 will catch it. So we don't need separate condition; we can just compute p_i and check bound. But is there a scenario where l_i <= n - i + 1 holds but p_i > n - l_i + 1 due to the other term? As argued, no because n - S_i + 1 ≤ n - l_i + 1 always holds (since S_i >= l_i). So p_i will be ≤ bound. So the check p_i > bound is equivalent to i > n - l_i + 1, i.e., l_i > n - i + 1. So it's simpler to just compute p_i and then verify bound; if any violation, output -1.
But is sum >= n sufficient to guarantee that p_i computed as above yields coverage of all cells? I think yes under the bound condition. But what about a case where sum >= n but some p_i computed is > n - l_i + 1? That we output -1. So final algorithm: compute total sum, if sum < n -> -1. Compute suffix sums, compute p_i, then check if for all i, p_i + l_i - 1 ≤ n (i.e., p_i ≤ n - l_i + 1). If any fails, print -1. Else output p_i.
Now test edge cases:
m = n, each l_i = 1. Then sum = n >= n. suffix: for i from n down to 1, S_i = n - i + 1. p_i = max(i, n - (n-i+1) + 1) = max(i, i) = i. So p_i = i. Check p_i ≤ n - 1 + 1 = n? For l_i=1, n - l_i + 1 = n, i ≤ n holds. Output: positions 1..n. Intervals: each covers a single distinct cell. All colors appear, all cells colored.
m = 1, n arbitrary. Then sum = l1. Need sum >= n. Also p1 = max(1, n - l1 + 1) = max(1, n - l1 + 1). Since l1 >= n, n - l1 + 1 ≤ 1, so p1=1. Check p1 ≤ n - l1 + 1? That would be 1 ≤ n - l1 + 1 => l1 ≤ n, but l1>=n, so equality l1=n gives 1≤1; if l1>n? But l1 ≤ n by problem constraints? Actually l_i ≤ n, but can be equal to n. So if l1 = n, p1=1, bound=1, ok. If l1 > n? not allowed. So for m=1, we need l1 = n to cover all cells (since union must be n). Actually sum >= n, but if l1 = n, union is n; if l1 > n impossible because l1 ≤ n. So sum = n is required. So if l1 = n, output p1=1; if l1 < n, sum < n -> -1.
m = n, but some l_i > 1? For instance n=3,m=3,l=[2,1,1]. sum=4>=3. Check p_i: suffix S3=1, S2=2, S1=4. p3=max(3,3-1+1=3)=3; p2=max(2,3-2+1=2)=2; p1=max(1,3-4+1=0)=1. Check bounds: p3+l3-1=3+1-1=3 ok; p2:2+1-1=2 ok; p1:1+2-1=2 ≤3 ok. So output 1,2,3. Intervals: [1,2],[2,2],[3,3]. Final: cell1:1; cell2: max(1,2)=2; cell3:3. All colors appear.
Now consider n=4,m=3,l=[3,2,1]. sum=6>=4. p3 = max(3,4-1+1=4) =4; p2 = max(2,4-3+1=2)=2; p1= max(1,4-6+1=-1)=1. Check p2+l2-1=2+2-1=3 ok; p1+3-1=3 ok. Intervals: [1,3],[2,3],[4,4]. Final: cell1:1; cell2: max(1,2)=2; cell3: max(1,2,? Actually interval3 covers 4 only, so cell3 is covered by 1 and2, max=2; cell4:3. Colors: 1,2,3 appear. Good.
Now consider a case with sum >= n but l_i condition fails, e.g., n=5,m=3,l=[3,3,3] we already saw fails bound? Actually l2=3, n-i+1=5-2+1=4 ok; l3=3,5-3+1=3 ok; l1=3 ≤5. So conditions hold. sum=9>=5. p3 = max(3,5-3+1=3) =3; p2 = max(2,5-6+1=0)=2; p1=1. Check p3+l3-1=3+3-1=5 <=5; p2:2+3-1=4 <=5; p1:1+3-1=3 <=5. So it's valid? But earlier we thought [3,3,3] might be impossible? Wait we previously tested n=5,m=3,l=[4,4,4] was impossible because l_i > n-i+1 for i=2,3? Actually for l=[3,3,3] with n=5: i=3, l3=3, n-i+1=3, so OK. i=2, l2=3, n-i+1=4, OK. So it should be possible. Let's try to construct manually: lengths 3,3,3. Could we? p1=1, p2=2, p3=3 as per construction yields intervals: [1,3],[2,4],[3,5]. Union = [1,5]. Check exclusive: color3 [3,5] all fine. color2 [2,4]: later interval3 covers [3,5]; so cell2 is exclusive for color2. color1 [1,3]: later intervals union = [2,4] U [3,5] = [2,5]; cell1 exclusive. So works. Good.
So the bound condition l_i ≤ n - i + 1 is indeed necessary and sufficient with sum >= n.
Thus solution is straightforward.
Now we need to implement:
Input n,m, array l[1..m].
Compute total = sum(l). If total < n, output -1.
Compute suffix array: suff[i] = sum_{j=i}^{m} l_j. Could compute in O(m) by reverse loop.
Then for i from 1 to m: p_i = max(i, n - suff[i] + 1). Also, if p_i > n - l_i + 1, we can break and output -1, but as argued if total >= n, this check might be redundant but we'll do it anyway.
But note: n - suff[i] + 1 might be negative, so max(i, negative) = i. That's fine.
After computing all p_i, we can optionally verify coverage, but the construction guarantees it.
Edge case: The check p_i > n - l_i + 1 may be triggered if total >= n but l_i > n - i + 1. So we output -1.
Now also consider: Could there be a scenario where total >= n and l_i <= n - i + 1 for all i, yet the construction yields p_i that are not strictly increasing? Not needed.
Now implementation details: Use 64-bit for sums since n up to 100k, l_i up to n, sum up to 1e5*1e5 = 1e10, which fits in 64-bit (max ~1e10, 64-bit holds up to 9e18). Use long long in C++.
Now let's test some more random cases with brute force small n to ensure correctness. Since we can't run code here, we'll reason.
But before finalizing, let's double-check the logic behind the construction: Why does p_i = max(i, n - S_i + 1) work? The idea is to place intervals as far left as possible while ensuring that the "leftmost uncovered point" after placing intervals from i+1..m is covered by interval i. The leftmost uncovered point after intervals i+1..m should be L = n - S_{i+1} + 1 (since the suffix lengths total S_{i+1} can cover at most a suffix of length S_{i+1} from the right, so the leftmost point they can cover if placed optimally is n - S_{i+1} + 1; but if they are placed not optimally, they might leave gaps. So to ensure coverage, we force that interval i covers up to that point: we set its right endpoint to at least n - S_{i+1} (or its start such that its right endpoint reaches that). Actually, if we want to cover the whole line, we need that the intervals, when placed, leave no gaps. One way: after placing intervals from m down to 1, the leftmost point covered should be 1. This can be achieved if we set p_i such that p_i <= something. The construction ensures that the right endpoint of interval i is at least n - S_{i+1}. Let's compute right_i = p_i + l_i - 1. If p_i = n - S_i + 1, then right_i = n - S_i + 1 + l_i - 1 = n - (S_i - l_i) = n - S_{i+1}. So in that case, interval i ends exactly at n - S_{i+1}. Then the intervals i+1..m, which have total length S_{i+1}, can be placed (by the same construction) to cover the suffix starting at n - S_{i+1} + 1? Actually, the next interval i+1 will have its right endpoint at n - S_{i+2} maybe, etc. Ultimately, interval m will cover the far right. So this ensures a contiguous coverage from some point to n. If p_i = i (because n - S_i + 1 is smaller), then we have p_i = i, and we need to ensure that the coverage still connects. In that case, we have S_i >= n - i + 1? Actually, if n - S_i + 1 < i, then S_i > n - i + 1. That means the total length from i to m is more than the number of cells from i to n. So even if we start at i, we will cover beyond necessary. The right_i = i + l_i - 1. Since l_i might be large, right_i could be as high as n. But we need to ensure that the intervals i+1..m fill any gap between right_i+1 and n? Actually, if S_i is large, there will be overlaps, but we need to ensure that the union covers all cells up to n. If p_i = i, the interval i covers from i to i+l_i-1. Since S_i is large, the remaining intervals will cover the rest. But we must guarantee that no gap appears between the leftmost point covered by intervals i..m and 1. This is achieved by also having p_1 = 1 always, because for i=1, n - S_1 + 1 <= 1 since S_1 >= n, so p_1 = 1. So the leftmost start is 1.
Now, does the construction guarantee that for each i, the interval i has an exclusive cell? Not explicitly, but the condition p_i >= i ensures that the point i is covered by interval i (since p_i <= i? Actually p_i >= i, so the start is at least i, so the interval might not cover i if p_i > i. For example, if p_i = i+something, then i might not be covered. In the construction, if p_i = n - S_i + 1, which may be > i, then interval i starts after i, so i might not be covered by i. So the exclusive cell for color i might not be i. But still, the construction ensures an exclusive cell because interval i extends left enough that its leftmost point is not covered by later intervals? Let's analyze: The exclusive cell for i could be the leftmost point of its interval, provided that point is not covered by any later interval. Since later intervals are placed with starts at least i+1? Actually p_{i+1} >= i+1, so later intervals start at i+1 or later. If p_i > i, then its leftmost point is p_i, which is >= i+1? Actually p_i could be > i, say 5 for i=3. Later intervals start at least 4, so they might also cover that point if they start at 4 or 5. But we need to ensure p_i is not covered by any later interval. The construction likely ensures that p_i is strictly less than the start of the next interval? Not necessarily; intervals can overlap. However, the exclusive cell for color i is guaranteed because the interval i will cover the point n - S_{i+1} (its right endpoint in the case p_i = n - S_i + 1) and that point might be the boundary. Actually, in the typical proof, the exclusive part is the segment [p_i, p_{i+1})? Let's derive.
We want to ensure that there exists some x in [p_i, p_i + l_i -1] such that for all j>i, x not in [p_j, p_j+l_j-1]. One way is to ensure that p_i < p_{i+1} and that the intervals are arranged such that they are "almost disjoint" but with some overlaps. Not trivial but known.
I trust the CF editorial that this construction works given the conditions.
Now let's confirm with an edge case: n=5, m=3, l=[3,3,3] we already did; works.
Another: n=5, m=3, l=[5,1,1] (l1=5, l2=1, l3=1). Check conditions: l2=1 <= 4; l3=1 <=3; sum=7>=5. suffix: S3=1, S2=2, S1=7. p3 = max(3,5-1+1=5) =5; p2 = max(2,5-2+1=4) =4; p1 = max(1,5-7+1=-1)=1. Check bounds: p2=4, n-l2+1=5, ok; p3=5, n-l3+1=5, ok. Intervals: [1,5], [4,4], [5,5]. Final: cell1-3: color1 (since later intervals cover only 4,5); cell4: covered by color1 and color2 -> max=2; cell5: covered by color1,2,3? Actually color1 covers 5, color2 covers 4 only, color3 covers 5, so max=3. So colors: 1 appears on 1-3; 2 appears on 4; 3 appears on 5. Good.
What about n=5,m=3,l=[4,2,2]? sum=8>=5. l2=2 <=4; l3=2 <=3. suffix: S3=2, S2=4, S1=8. p3 = max(3,5-2+1=4)=4; p2 = max(2,5-4+1=2)=2; p1=1. Intervals: [1,4],[2,3],[4,5]. Final: cell1:1; cell2: max(1,2)=2; cell3: max(1,2)=2? Actually interval3 covers 4-5, so cell3 only covered by 1 and2, max=2; cell4: covered by 1,2? interval2 covers 2-3, so cell4 only by 1 and3? Wait interval3 covers 4-5, so cell4 is covered by 1 and 3 => max=3; cell5:3. Colors: 1 appears cell1; 2 appears cell2 or3; 3 appears cell4. Good.
All good.
Now we need to handle output format: print m integers.
Implementation steps:
Read n, m.
Read list l of size m.
Compute total = sum(l). If total < n: print -1 and return.
Compute suffix sums: suff[m+1] = 0; for i from m down to 1: suff[i] = suff[i+1] + l[i].
For i from 1 to m:
p_i = max(i, n - suff[i] + 1)
if p_i > n - l[i] + 1:
print -1; return.
Output p_i separated by spaces.
But wait: Could there be a case where total >= n but p_i > n - l_i + 1 for some i even though l_i ≤ n - i + 1? As argued no. But let's test a borderline: n=100, m=50, l_i maybe large. Suppose i=50, l_50 = n - i + 1 = 100-50+1=51. So l_50=51. Then n - l_50 + 1 = 100-51+1=50. i=50. So p_50 = max(50, n - S_50 + 1). Need to check that n - S_50 + 1 ≤ 50. S_50 = sum_{j=50}^{50} l_j + maybe more? Actually if i=50 and there are more intervals after? But i=50, m=50 so it's the last. Then S_50 = l_50 = 51. n - S_50 + 1 = 100-51+1=50. So p_50 = max(50,50)=50. So p_50 = 50, which equals bound 50. OK.
If there are more intervals after, say i=49, l_49 = n - i + 1 = 100-49+1=52. Then bound = n - l_49 +1 = 100-52+1=49. i=49. p_49 = max(49, n - S_49 + 1). S_49 = l_49 + S_50. S_50 at least l_50 which might be something. To have p_49 exceed bound, we would need n - S_49 + 1 > 49. But S_49 is at least l_49 =52, so n - S_49 + 1 ≤ 100-52+1=49. So it's ≤49. So p_49 = max(49, ≤49) =49, which ≤ bound 49. So fine.
Thus condition holds.
Now confirm with maximum constraints: n=100000, m=100000, all l_i = 1. Works.
Now one more nuance: The problem states "all colors will appear at least once and all cells will be colored." The construction ensures that. But we should also verify that the intervals as placed indeed cover all cells. We argued yes, but let's prove quickly: By construction, for i from 1 to m, we have p_i + l_i - 1 >= n - suff[i+1] (since if p_i = n - suff[i] +1 then equality; if p_i = i, then p_i + l_i -1 >= i + l_i -1. But we also have that suff[i] >= n - i +1? Not necessarily. But we can prove by induction that after processing intervals from i to m, they cover [min(p_i, something), n]. More concretely, we can show that for each i, the union of intervals i..m covers [p_i, n] (or maybe [i, n]?) But p_i could be > i. However, we can show that the leftmost point covered by intervals i..m is ≤ i? Actually, we know p_1 = 1, and the intervals are placed such that there are no gaps. The typical CF solution also checks that p_i <= p_{i+1} maybe? Let's examine: p_i = max(i, n - suff[i] + 1). Compare p_i and p_{i+1}. Is p_i <= p_{i+1}? Possibly not always monotonic? Let's test: n=10,m=4,l=[5,5,5,5] gave p=[1,2,3,6] which is non-decreasing: 1,2,3,6. For previous examples, it was non-decreasing. Is it always? p_i = max(i, n - suff[i] + 1). Since suff[i] decreases as i increases (removing l_i), n - suff[i] + 1 increases as i increases (since suff[i] gets smaller). So n - suff[i] + 1 is non-decreasing as i increases. Also i is increasing. So the maximum of two non-decreasing sequences is non-decreasing. Thus p_i is non-decreasing. Good. So intervals are placed in non-decreasing start positions. That means they are sorted left to right. So they are like a "staircase". Additionally, we need to ensure that there is no gap between consecutive intervals. Since p_i is non-decreasing, the left endpoint of interval i is p_i, and the right endpoint is p_i + l_i -1. For coverage to be continuous, we need that p_{i+1} <= p_i + l_i (i.e., the next interval starts at or before the previous interval ends +1). Otherwise there would be a gap between the end of interval i and start of interval i+1. Since p_i are computed via suffix sums, they satisfy this property? Let's check: For i where p_i = n - suff[i] + 1, then p_i + l_i -1 = n - suff[i+1]. And p_{i+1} is at least n - suff[i+1] + 1? Actually p_{i+1} = max(i+1, n - suff[i+1] + 1). The second term is exactly n - suff[i+1] + 1. So p_{i+1} >= n - suff[i+1] + 1, which is one more than the right endpoint of interval i (if p_i = n - suff[i] + 1). That would create a gap of at least 1: interval i ends at n - suff[i+1], interval i+1 starts at n - suff[i+1] + 1 at minimum. But if p_{i+1} = i+1, maybe i+1 <= n - suff[i+1]? Could be less, but since p_{i+1} is max, if n - suff[i+1] + 1 > i+1, then p_{i+1} = n - suff[i+1] + 1, which would start exactly one after the end of interval i, causing a gap. However, is it possible that there is no coverage at cell n - suff[i+1] + 1? Interval i+1 would cover it because it starts at that point. So the cell n - suff[i+1] is covered by interval i, and cell n - suff[i+1] + 1 is covered by interval i+1, so coverage is continuous. But is there a gap if interval i ends at n - suff[i+1] and interval i+1 starts at n - suff[i+1] + 1? That's consecutive cells, so no gap: the integer cells are discrete; if interval i covers up to x, and interval i+1 starts at x+1, then cell x+1 is covered, and there is no integer cell between them. So continuity holds as long as p_{i+1} <= p_i + l_i (i.e., start of next ≤ end of current +1). Actually for continuous coverage of integers, we need that there is no integer k such that k is not covered. If interval i covers [a,b] and interval i+1 covers [c,d], we need that either c ≤ b+1 (so that the intervals touch or overlap) to cover all integers from a to d without gap. In our case, if b = x and c = x+1, then all integers covered: a..x and x+1..d. So condition is c ≤ b+1. In the scenario where p_i = n - suff[i] + 1, b = n - suff[i+1]. c = p_{i+1} which is at least n - suff[i+1] + 1. So c >= b+1. But could c be > b+1? If p_{i+1} > n - suff[i+1] + 1, then c > b+1, causing a gap. However, p_{i+1} = max(i+1, n - suff[i+1] + 1). If i+1 > n - suff[i+1] + 1, then p_{i+1}=i+1. But then we have i+1 > b+1? b = n - suff[i+1]. So i+1 > n - suff[i+1] + 1 => i > n - suff[i+1]. That would mean i >= n - suff[i+1] + 1? Actually, if i+1 > n - suff[i+1] + 1, then i > n - suff[i+1]. But note that suff[i+1] is the total length of intervals i+1..m. Since total sum >= n, we might have suff[i+1] >= n - i? Possibly. Let's examine: If p_{i+1} = i+1 because i+1 > n - suff[i+1] + 1, then n - suff[i+1] + 1 < i+1 => suff[i+1] > n - i. That means the remaining length is more than the number of cells from i+1 to n? That could happen. In that case, p_i might be = i (since n - suff[i] + 1 < i, as suff[i] = l_i + suff[i+1] > l_i + n - i). Let's test: n=10,m=4,l=[5,5,5,5] we had p1=1, p2=2, p3=3, p4=6. For i=3, we have p3=3 (i) not the suffix expression. Let's check continuity: interval3: [3,7] (since l3=5, start 3 => end 7). interval4: [6,10] start 6 end 10. Here b=7, c=6, c <= b, so overlap, no gap. That's fine. For i=2: interval2 [2,6], interval3 starts at 3, c=3 <= b=6+1? b=6, c=3 <=7, so fine. Actually we need to check the worst case: when p_i = i and p_{i+1} = i+1? That would be start exactly at next integer, and since interval i length at least 1, its end at least i, so i+1 may be covered? Actually if p_i = i and l_i >=2, then end >= i+1, so covers i+1. So overlapping possibly. In general, with non-decreasing starts and lengths, it's likely no gaps. Let's prove: The construction ensures that the union covers all cells. In editorial, they argue that after placing intervals with these starts, the rightmost uncovered point before placing interval i is exactly p_i (or something). Actually, they define ans[i] = max(i, n - sum_{j=i}^m l_j + 1). Then they argue that ans[i] + l_i - 1 >= ans[i+1]? Not exactly. But they show that ans[i] <= ans[i+1] (non-decreasing) and that the intervals cover the entire segment because ans[1] = 1 and ans[m] + l_m - 1 = n (since ans[m] = max(m, n - l_m + 1). If l_m is such that n - l_m + 1 >= m, then ans[m] = n - l_m + 1, so ans[m] + l_m -1 = n. If n - l_m + 1 < m, then ans[m] = m, but then m + l_m -1 must be <= n? But if n - l_m + 1 < m, then m > n - l_m + 1 => m + l_m -1 > n. But l_m <= n - m +1? Actually condition l_m <= n - m +1 ensures that m + l_m -1 <= n. So ans[m] = m, and end = m + l_m -1 <= n. But does it reach n? Not necessarily, but later intervals? Wait, m is last, so its interval must cover the rightmost part. If it doesn't reach n, then cells after its end would be uncovered, but earlier intervals might cover them? However, earlier intervals have start >= their index, so they are to the left. The rightmost cell covered by union is max over ends. Since intervals are placed non-decreasing, the rightmost end is from the last interval m. So we need its end to be n. Does the construction guarantee that? Let's check: p_m = max(m, n - l_m + 1). If n - l_m + 1 >= m, then p_m = n - l_m + 1, so end = n. If n - l_m + 1 < m, then p_m = m. Then end = m + l_m - 1. But since l_m <= n - m +1, we have m + l_m - 1 <= n. It could be strictly less than n. Example: n=10, m=5, l_m=1. Then n - l_m + 1 = 10-1+1=10, which is >=5, so p_m=10? Actually max(5,10)=10, end=10. So that covers n. What about n=5,m=5,l5=1: n -1+1=5, max(5,5)=5, end=5 covers n. So if l_m is small, p_m becomes n - l_m +1 which is large. So p_m often equals n - l_m +1 if that is at least m. But if m is close to n and l_m is large, then n - l_m +1 might be less than m, so p_m = m. Then end = m + l_m -1. Since l_m could be up to n - m +1, end = m + (n - m +1) -1 = n. So indeed end = n. Because l_m <= n - m +1, so m + l_m -1 <= n, but is it forced to be exactly n? Not necessarily; could be less if l_m is smaller. Example: n=10,m=8,l8=1 (allowed because l_i ≤ n, but also l8=1 <= n - 8 +1 =3, okay). Then n - l_m +1 = 10-1+1=10, which is >=8, so p_m = 10, end=10. So it reaches n. If l_m = 2, n - 2 +1 =9 >=8, p_m=9, end=10. So reaches n. If l_m = 3, n-3+1=8, p_m= max(8,8)=8, end=10. So reaches n. If l_m = 1 but m=9, n=10: then l9=1 ≤ 2, n -1+1=10, max(9,10)=10, end=10. If m=9, l9=2: n-2+1=9, max(9,9)=9, end=10. So indeed the last interval always ends at n. Because either p_m = n - l_m +1 -> end = n, or p_m = m but then l_m must be at least n - m +1 to reach n? Wait, if p_m = m, end = m + l_m - 1. For this to be n, we need l_m = n - m + 1. But is it forced that l_m = n - m + 1? Not necessarily; l_m could be smaller, but then end < n. But would that be allowed? Let's test: n=10, m=6, l6=2. Check condition: l6 <= n - 6 +1 =5, so 2 ≤5 ok. total sum? Suppose other lengths sum to 9, total=11≥10. Compute suffix: S6=2. p6 = max(6, 10-2+1=9) =>9 (since 9>6). So p6=9, end=10. So p6 not m. So the case where p_m = m happens only when n - l_m +1 < m, i.e., m > n - l_m + 1 => m + l_m > n + 1? Rearranged: l_m > n - m + 1? But we have condition l_m ≤ n - m + 1. So the only way to have m > n - l_m + 1 is if l_m > n - m + 1, which violates the necessary condition. So under the condition l_m ≤ n - m + 1, we have n - l_m + 1 ≥ n - (n - m + 1) + 1 = m. So n - l_m + 1 ≥ m. Thus p_m = max(m, n - l_m + 1) = n - l_m + 1 (since that is at least m). Therefore p_m is always n - l_m + 1 under the valid condition. Wait, check: n - l_m + 1 ≥ m? Since l_m ≤ n - m + 1 => -l_m ≥ - (n - m + 1) => n - l_m + 1 ≥ n - (n - m + 1) + 1 = m. Yes. So p_m = n - l_m + 1 (because it's the max and it's at least m). Thus the last interval always starts at n - l_m + 1, and thus ends at n. So the last interval covers the suffix of length l_m exactly to n. Good.
Similarly, we can prove by induction that the union covers all cells.
Thus the algorithm is correct.
Now, confirm with the CF editorial: The solution indeed is:
if sum l_i < n: print -1 and return.
for i from 1 to m:
if i + l_i - 1 > n: // i.e., l_i > n - i + 1
print -1 and return.
Then compute suffix sums and ans[i] = max(i, n - suffix[i] + 1) and output.
But they may also include a check that ans[i] + l_i - 1 <= n, which is automatically satisfied if the previous condition holds.
In many implementations, they first check the condition l_i <= n - i + 1 for each i. If any fails, print -1. Then they compute ans.
But we can combine by checking after computing p_i.
Case 4: n=5,m=5,l=[1,1,1,1,1] sum=5, all l_i=1. p_i = max(i, n - suffix[i] + 1). suffix[i] = number of remaining 1's = (5 - i + 1). So n - suffix[i] + 1 = 5 - (5-i+1) +1 = i. So p_i = i. output 1 2 3 4 5. Works.
Case 8: n=5,m=2,l=[5,1] sum=6>=5. Check l2 <=4? 1≤4 OK. suffix: S2=1, S1=6. p2 = max(2,5-1+1=5)=5; p1=1. Check p2=5 ≤5-1+1=5 OK. Intervals: [1,5],[5,5]. Final: cell1-4:1; cell5:2? Actually cell5 covered by both, last is 2, so color1 appears on 1-4, color2 on 5. Works.
Case 10: n=5,m=2,l=[5,3] sum=8>=5. Check l2 <=4? 3≤4 OK. suffix: S2=3, S1=8. p2 = max(2,5-3+1=3)=3; p1=1. Check p2=3 ≤5-3+1=3 OK. Intervals: [1,5],[3,5]. Final: cell1-2:1; cell3-5:2. Colors: 1 appears,2 appears. Works.
Case 11: n=5,m=2,l=[5,4] sum=9>=5. Check l2 <=4? 4≤4 OK. suffix: S2=4, S1=9. p2 = max(2,5-4+1=2)=2; p1=1. Check p2=2 ≤5-4+1=2 OK. Intervals: [1,5],[2,5]. Final: cell1:1; cell2-5:2. Colors: 1 appears,2 appears. Works.
Case 12: n=5,m=2,l=[5,5] sum=10>=5. Check l2 <=4? 5>4 fails. So should be impossible. Indeed earlier we argued impossible. So output -1.
Thus algorithm consistent.
Now, regarding output: print m integers separated by spaces.
Edge case: The check p_i > n - l_i + 1 could be done while computing, but we may compute p_i first and then verify. Since we need to compute suffix sums anyway, we can do a single pass after computing p_i to validate.
Now, one more nuance: In the problem, they require that after all operations, all cells are colored. The construction yields full coverage as argued. However, is there any scenario where even though p_i are within bounds and sum >= n, the coverage might still miss some cells because of gaps? Let's attempt to construct a counterexample manually.
What about n=6,m=3,l=[3,3,1]? sum=7>=6. l2=3 ≤5, l3=1 ≤4. suffix: S3=1, S2=4, S1=7. p3 = max(3,6-1+1=6)=6; p2 = max(2,6-4+1=3)=3; p1=1. Intervals: [1,3],[3,5],[6,6]. Union: [1,3] U [3,5] U [6,6] = [1,5] U [6,6] = [1,6]? Wait [1,5] covers 1-5, and [6,6] covers 6. So full coverage.
What about n=6,m=3,l=[2,2,3]? sum=7>=6. l2=2 ≤5, l3=3 ≤4. suffix: S3=3, S2=5, S1=7. p3 = max(3,6-3+1=4)=4; p2 = max(2,6-5+1=2)=2; p1=1. Intervals: [1,2],[2,3],[4,6]. Union: [1,2] U [2,3] = [1,3]; plus [4,6] => [1,6].
What about n=6,m=4,l=[2,2,2,1]? sum=7>=6. Check l3=2 ≤6-3+1=4 OK; l4=1 ≤6-4+1=3 OK. suffix: S4=1, S3=3, S2=5, S1=7. p4 = max(4,6-1+1=6)=6; p3 = max(3,6-3+1=4)=4; p2 = max(2,6-5+1=2)=2; p1=1. Intervals: [1,2],[2,3],[4,5],[6,6]. Union: [1,3] and [4,5] and [6,6] => missing cell? Actually we have [1,2] and [2,3] cover 1-3; [4,5] covers 4-5; [6,6] covers 6. So full.
What about a scenario where p_i are not contiguous: e.g., if p_i = i and p_{i+1} = i+2, and l_i =1, then gap at i+1. But can that happen? For p_{i+1} to be i+2, we need n - suff[i+1] +1 = i+2 (or i+1 is less, but max gives i+2). That would mean n - suff[i+1] +1 >= i+2, and also i+1 < that, so p_{i+1}= n - suff[i+1] +1 = i+2. Then p_i = max(i, n - suff[i] +1). Since suff[i] = l_i + suff[i+1], n - suff[i] +1 = n - (l_i + suff[i+1]) +1 = (n - suff[i+1] +1) - l_i = (i+2) - l_i. If l_i >=2, then n - suff[i] +1 <= i. So p_i = i. So intervals: i: [i, i+l_i-1]; i+1: starts at i+2. To avoid gap, we need i+l_i-1 >= i+1 => l_i >=2. So if l_i=2, then i covers i and i+1; i+1 starts at i+2, no gap. If l_i=1, then i covers only i, and i+1 starts at i+2, leaving i+1 uncovered. So is it possible that l_i=1 and p_{i+1}=i+2? Let's test with numbers. Suppose n= something. Let's try n=5,m=3,l=[1,1,3]? sum=5. Check conditions: l2=1 ≤4, l3=3 ≤3? i=3: n-i+1=3, l3=3 ok. suffix: S3=3, S2=4, S1=5. p3 = max(3,5-3+1=3)=3; p2 = max(2,5-4+1=2)=2; p1 = max(1,5-5+1=1)=1. So p2=2, not i+2. So not that.
Try to get p_{i+1}=i+2. Need n - suff[i+1] +1 = i+2 and > i+1. That means suff[i+1] = n - (i+1). Since suff[i+1] is sum of lengths from i+1 to m. That sum equals n - (i+1). That is less than the number of cells remaining? Possibly. For i+1 = 2, suff[2] = n-2. For n=5, suff[2]=3. Could l = [?,?,?] such that suff[2]=3. Example: m=3, l1=?, l2=1, l3=2 gives suff2=3. Let's compute: n=5,m=3,l=[? ,1,2] total sum must be >=5, so l1 could be 2, total=5. Check conditions: l2=1 ≤4 ok, l3=2 ≤3 ok. suffix: S3=2, S2=3, S1=5. p3 = max(3,5-2+1=4)=4; p2 = max(2,5-3+1=3)=3; p1 = max(1,5-5+1=1)=1. So p2=3 which is i+1 for i=2? Actually i=2, p2=3 = i+1, not i+2. So not.
To get p2 = 3 for i=1? Actually i=1, p2 = 3 would be i+2 if i=1, p2=3. That would require n - suff[2] +1 = 3 and > 2? For n=5, suff[2] = 3 => 5-3+1=3, okay. So p2=3. Then p1 = max(1, 5 - (l1+3) +1). l1? total sum = l1+3 >=5 => l1 >=2. If l1=2, suff1=5, n -5+1=1, p1=1. So intervals: [1,2] (l1=2), [3,?] l2=1? Wait l2 is part of suff[2]=3 includes l2 and l3. For suff[2]=3, possible l2=1, l3=2 as before. Then p2=3, l2=1 -> interval2 [3,3]; p3=4 (as computed) interval3 [4,5]? Actually p3=4, l3=2 -> [4,5]. So intervals: [1,2], [3,3], [4,5]. Union covers 1-5, no gap. l1=2 so covers cell2, so gap between [1,2] and [3,3] is none. So fine.
Now what if l1=1? Then total sum = 1+? must be >=5, so suff[2] must be >=4. But then p2 would be n - suff[2] +1 <= 5-4+1=2, so p2 <=2, not 3. So can't have p2=3 with l1=1.
Thus gap scenario where l_i=1 and p_{i+1}=i+2 seems not possible because if l_i=1, then suff[i] = 1 + suff[i+1]. For p_{i+1} to be i+2, we need suff[i+1] = n - (i+1). Then suff[i] = 1 + n - (i+1) = n - i. Then n - suff[i] +1 = n - (n - i) +1 = i+1. So p_i = max(i, i+1) = i+1. So p_i would be i+1, not i. So interval i would start at i+1, covering only i+1 (since l_i=1). Then cell i might be uncovered? But then p_{i-1}? Let's not dig deeper; the construction likely ensures no gaps because of the condition sum >= n and the way p_i is defined. We can trust.
Thus the solution stands.
Now, we need to implement in a language. Since the user didn't specify language, but we can provide solution in Python (since it's easy). However constraints up to 100k, Python is fine.
Implementation plan in Python:
def solve():
import sys
input_data = sys.stdin.read().split()
it = iter(input_data)
n = int(next(it))
m = int(next(it))
l = [0] + [int(next(it)) for _ in range(m)] # 1-indexed
total = sum(l[1:])
if total < n:
print(-1)
return
# compute suffix sums from m down to 1
suff = [0](m+2) # suff[i] = sum from i to m
for i in range(m, 0, -1):
suff[i] = suff[i+1] + l[i]
p = [0](m+1)
possible = True
for i in range(1, m+1):
p[i] = max(i, n - suff[i] + 1)
if p[i] > n - l[i] + 1:
possible = False
break
if not possible:
print(-1)
else:
print(' '.join(str(p[i]) for i in range(1, m+1)))
Edge: Could there be integer overflow? In Python it's fine.
Now test with sample:
Input:
5 3
3 2 2
total=7>=5. suff: i=3:2, i=2:4, i=1:7. p: i=1: max(1,5-7+1=-1) =1; i=2: max(2,5-4+1=2)=2; i=3: max(3,5-2+1=4)=4. Check bounds: all okay. Output: 1 2 4. But sample output is 2 4 1. Both valid. So okay.
Now test with m=1, n=5, l=[5]: total=5, suff[1]=5, p1 = max(1,5-5+1=1)=1, bound=5-5+1=1 OK, output 1.
Test with m=1, n=5, l=[4]: total=4<5 => -1.
Thus solution correct.
Now, but is there any case where total >= n, p_i <= bound for all i, yet the union does not cover all cells? Let's attempt to construct manually: n=6, m=3, l=[3,2,2]? total=7>=6. Check conditions: l2=2 ≤5, l3=2 ≤4 OK. suffix: S3=2, S2=4, S1=7. p3 = max(3,6-2+1=5)=5; p2 = max(2,6-4+1=3)=3; p1 = max(1,6-7+1=0)=1. Intervals: [1,3],[3,4],[5,6]. Union: [1,4] and [5,6] covers 1-6. Good.
What about n=6,m=3,l=[4,1,2]? total=7>=6. l2=1≤5, l3=2≤4 OK. suffix: S3=2, S2=3, S1=7. p3= max(3,6-2+1=5)=5; p2= max(2,6-3+1=4)=4; p1=1. Intervals: [1,4],[4,4],[5,6]. Union covers 1-6. Good.
What about n=6,m=4,l=[2,2,1,2]? total=7>=6. l2=2≤5, l3=1≤4, l4=2≤3? l4=2 <= 6-4+1=3 OK. suffix: S4=2, S3=3, S2=5, S1=7. p4 = max(4,6-2+1=5)=5; p3 = max(3,6-3+1=4)=4; p2 = max(2,6-5+1=2)=2; p1=1. Intervals: [1,2],[2,3],[4,4],[5,6]. Union: [1,3] and [4,4] and [5,6] = [1,6]. Good.
All good.
Thus solution is correct.
Now, we should also note that the problem statement originally might have had output " '-1" (with a quote) but it's a typo; it's "-1". So we output -1.
Now finalize.
But wait: I recall a nuance in the CF problem: The condition also required that p_i is chosen from [1, n-l_i+1], which we respect. And the answer must have all colors appear and all cells colored. The construction ensures that.
One more check: For i where p_i = n - suff[i] + 1, the start might be > n - l_i + 1? Actually we already argued it's not possible if conditions hold. So fine.
Now, for completeness, we might want to add an early check: for i from 1 to m, if i + l_i - 1 > n: impossible. But that's same as l_i > n - i + 1. So we could do that before computing p_i. However, the later check after computing p_i will also catch it; but computing p_i may involve large suffix sums, which is fine.
Now implement.
Potential pitfalls: The suffix sum may be large, but we use Python int.
Now I'll produce final answer with explanation and code.
But the user might not have requested code, just the solution? The problem statement says "Please help Dreamoon to choose p_i", and output -1 if impossible. The user is likely expecting a solution algorithm, but the typical answer on Codeforces would be code. Since this is a text-based reasoning, we can provide the solution description and code.
Thus I'll output a Python solution.
Now final answer. ```python
import sys
def solve() -> None:
data = sys.stdin.read().split()
if not data:
return
it = iter(data)
n = int(next(it))
m = int(next(it))
l = [0] + [int(next(it)) for _ in range(m)] # 1‑based
code
total = sum(l[1:])
if total < n:
print(-1)
return
# suffix sums: suff[i] = l[i] + l[i+1] + ... + l[m]
suff = [0] * (m + 2)
for i in range(m, 0, -1):
suff[i] = suff[i+1] + l[i]
p = [0] * (m + 1)
for i in range(1, m + 1):
p[i] = max(i, n - suff[i] + 1)
if p[i] > n - l[i] + 1:
print(-1)
return
print(' '.join(str(p[i]) for i in range(1, m + 1)))