We are given n up to 1e5. For each column i (1-indexed), a boomerang is thrown from bottom of column i, going upward. When it hits a target (grid cell with a cross), it bounces: it makes a 90-degree turn to the right (so from up to right, from right to down, from down to left, from left to up? Actually the description: "When the boomerang hits any target, it will bounce off, make a 90 degree turn to the right and fly off in a straight line in its new direction." The initial direction is upward. Upon hitting a target, it turns right relative to its current direction. So from up, turning right gives direction to the right. Then from right, turning right gives down. From down, turning right gives left. From left, turning right gives up. So it's like a "mirror" that always turns right 90 degrees, not left. So path is deterministic given target locations. The boomerang continues until it leaves the grid. It counts number of targets it hits (including the first hit) before leaving. It is known that a_i ≤ 3.
We need to construct a set of target positions (r,c) such that each row and each column has at most 2 targets (the given constraints) and such that for each column i, the number of hits along that boomerang's path is exactly a_i. Output any valid configuration or -1 if none.
Observation: The boomerang's path is a piecewise linear path moving up, right, down, left etc depending on bounces. This resembles a path that bounces at right-turn mirrors. This is similar to a light ray with mirrors that always turn right (like a corner reflector). The path can be described as: starting at (row = n+1? actually from bottom of column i, below grid, moving up). So starting position: just below row n, column i, direction up.
When it moves upward, it will first encounter any target in that column at some row r (if any). Among targets in column i, the boomerang hits the lowest one? Actually since it starts below the grid and goes up, the first target encountered is the one with the highest row? Wait: bottom of column means below row n, so it enters the grid at row n (the bottom row) and moves upward. So it will pass rows n, n-1, ..., 1. So the first target hit is the one with the smallest row index? Actually row numbers increase downward: row 1 top, row n bottom. So moving upward means decreasing row number. So it will first encounter a target that is at the highest row (closest to bottom) among those in column i. Because it starts at row n (the bottom row) and moves up. If there is a target at row n, it will be hit immediately. Otherwise, it continues upward. So the first hit is at the target with the maximum row in that column (the lowest one visually). So the first hit occurs at (r1, i) where r1 is the maximum row number among targets in column i, if any.
After hitting, it turns right, so direction becomes right. Then it moves horizontally to the right, increasing column. It will hit the next target in that row, but careful: after turning right, it moves to the right from the hit cell. So it goes east. It will travel along row r1, from column i to the right. It may encounter another target in the same row at some column c > i. Since it moves right, it will hit the nearest target to the right in that row (i.e., the one with smallest column > i). So second hit occurs at (r1, c2) where c2 is the next target column in that row to the right of i.
After hitting, it turns right again, so direction becomes down. Now it moves downward along column c2, from row r1 down. It will hit the next target in that column below r1: since direction is down, it will hit the target with largest row > r1? Actually row numbers increase downward, so moving down means increasing row number. So the next target in column c2 that is below row r1 is the one with the smallest row greater than r1? Actually row numbers larger => lower. So if moving down, we go to rows r1+1, r1+2,... So the nearest target below is the one with the smallest row > r1 (i.e., the highest row among those below? Actually row numbers increase downward, so the nearest is the one with minimal row number greater than r1? Wait: row numbers: top=1, bottom=n. So if we are at row r1 and go down, we go to r1+1, r1+2,... The next target encountered is the one with the smallest row index among those > r1? But row index is numeric: r1+1 is smallest numeric difference. So yes, the nearest below is the target with minimal row > r1, i.e., the topmost among those below. So it's the one with smallest row > r1? Actually careful: If there is a target at row r1+1, that is the immediate next cell. That is the smallest row index greater than r1? Because row index numbers get larger as you go down, so r1+1 is the immediate next row downward. So the nearest target below is the one with the smallest row greater than r1 among targets in that column. That corresponds to the target that is closest above in the vertical direction? No, we want the first target encountered when moving downwards. That is the target with the minimum row > r1 (the one with the smallest row index among those greater than r1). But because row index increases downwards, the one with smallest index greater than r1 is actually the highest position (closest to top?) Wait, think physically: Starting at (r1, c2). If we move down, we go to rows with larger numbers. The next cell is (r1+1, c2). If there is a target there, it is hit. So we want the target with minimal row > r1, i.e., the one with the least row number above? Actually no, r1+1 is the smallest row number greater than r1. So yes. That target is just one step down. So it's the one that is immediately below. So it's the nearest in terms of distance. So it's the one with the smallest row difference. So it is the highest among the below? Actually if there are multiple targets in column below r1, the one with smallest row > r1 is the one that is closest to r1, i.e., the one that is the uppermost among those below (since row numbers increase downward, the smallest row > r1 is the one that is the least down, i.e., the highest). So it's the topmost target among those below r1. So it's the nearest.
After that hit, it turns right, direction becomes left? Actually from down, turning right gives left? Let's check: directions cycle: up -> right -> down -> left -> up. So from down, turning right gives left. So after second hit (first bounce), direction became right. After third hit (second bounce), direction becomes down. After fourth hit (third bounce), direction becomes left. After fifth hit (fourth bounce), direction becomes up again, etc. So the boomerang can bounce up to a_i times.
Important: a_i <= 3, so each column's boomerang hits at most 3 targets. So path can have at most 3 bounces? Actually hits targets count includes the first hit. So a_i=0: no hit; 1: hits one target then leaves; 2: hits two; 3: hits three.
Thus for each column, the path can be described by the sequence of cells hit: (r1,i), (r1,c2), (r3,c2) possibly, (r3,c4) etc. But since a_i <=3, the maximum sequence length is 3, so patterns:
-
a_i=0: no target in column i, or the boomerang goes through without hitting any target? Actually if there is a target in column i, the boomerang will hit it because it goes upward through entire column. So for a_i=0, there must be no target in column i. So column i has 0 targets.
-
a_i=1: The boomerang hits exactly one target then leaves. This can happen if after hitting that target, the new direction leads out of grid without hitting another target. For instance, hit (r,i). Then turn right -> go right. If in that row r, there is no target to the right (i.e., no target in row r with column > i), then the boomerang goes all the way to the right edge and leaves. So a_i=1 means column i has at least one target (the one hit first). But it could also be that the boomerang hits first target and then maybe hits a second target? That would be a_i>=2. So for a_i=1, we require that after hitting the first target, moving right, there is no other target in that row to the right. So the row of the first hit must have no target in columns > i (or maybe there is a target but the boomerang leaves before hitting it? No, if there is a target to the right, it will hit it because it moves straight right, so it will encounter the first target in that direction. So to avoid second hit, no target to the right in that row. Also, what about the possibility that after hitting, it goes right and then maybe goes out, but could it later hit another target after turning? No, because it only turns upon hitting a target. So if no target encountered, it leaves.
Thus a_i=1 implies: there exists a target in column i at some row r, and in row r there is no target with column > i.
But also note: It could be that the boomerang hits a target in column i, then turns right, then later hits a target in that row, but then maybe that second hit is such that after turning right again (downwards), it leaves without hitting a third target. That would be a_i=2. Or it could hit a third target downwards if exists, giving a_i=3.
- a_i=2: The boomerang hits two targets: first in column i (r1,i), then a second in the same row r1 to the right (r1,c2) (must be the nearest to the right). After second hit, it turns right (so direction becomes down). Then it moves down. For it to not hit a third target, there must be no target below in column c2 (i.e., no target in column c2 with row > r1). So a_i=2 implies: column i has a target at row r1; row r1 has another target at some column c2 > i (and there is no target between i and c2 in that row, but that's automatically if we choose the target as the nearest). Also, column c2 has a target at row r1? Actually the second hit is at (r1,c2), so there must be a target at (r1,c2). And then for a_i=2, there must be no target in column c2 below row r1 (i.e., with row > r1). However, there might be a target at row r1? Actually the second hit is at (r1,c2) itself, we already placed a target there. There could also be a target above row r1 in column c2? That wouldn't be encountered because we are moving down from row r1, so only targets below matter. So no target below.
Also note: Could a_i=2 happen with a different path pattern? For instance, what if the first hit occurs, then after turning right, the boomerang maybe goes out of grid without hitting a second target? That's a_i=1. So yes. Could a_i=2 happen if after first hit, boomerang goes right and then hits a target, then after turning down, it leaves, that's the pattern. Could there be scenario where first hit, then after turning right, it goes out left? No.
Also what about the possibility of a bounce turning left? No.
Thus a_i=2 implies a pair of targets (r,i) and (r,c) with c>i, and column c has no target below row r (strictly greater row). Also note that column c may have other targets above row r? If there is a target above (row < r) in column c, that would not affect the path because the boomerang is moving down from row r. So it's fine as long as it doesn't have a target below. However, column c may have at most 2 targets overall. So we need to consider constraints later.
- a_i=3: Hits three targets: first at (r1,i), second at (r1,c2), third at (r3,c2) with r3 > r1 (since after second hit, direction down, so third hit is below in column c2). After third hit, turns right again, so direction becomes left. Then it moves left. For it to not hit a fourth target (since a_i <=3, we need it to leave after three hits; we are told a_i ≤ 3, so we require exactly 3 hits, so after third hit, moving left should not encounter any target in row r3 with column < c2. Also note that after third hit, direction left, it might later hit a target and that would be fourth, which is not allowed. So a_i=3 requires: column i has target at r1; row r1 has target at c2>i (nearest to the right); column c2 has target at r3>r1 (nearest below); and row r3 has no target to the left of c2 (i.e., no target in row r3 with column < c2) to avoid fourth hit. Also, it's possible that after third hit, moving left, it might hit a target at some column < c2, but that would be a fourth hit, which would make a_i>=4, not allowed. So must have no target in row r3 with column < c2 (strictly less). Also, note that there could be a target at (r3, something) with column > c2? That wouldn't be encountered because moving left only goes decreasing columns. So not relevant.
Thus the patterns are essentially chains: For a_i >=1, we have a starting target in column i; then if a_i>=2, we need a target to the right in the same row; if a_i=3, we need a target below that second target. The constraints are about absence of other targets that would cause extra hits along the path.
But we also have constraints per row and column: at most 2 targets each. Also we can place multiple targets, but each row and column limited to 2.
We need to assign for each column i a path pattern or decide that column i has no target. But columns can interact: A target can serve as part of the path for multiple columns? Possibly yes, because a target placed at some (r,c) can be hit by boomerangs from different columns if their paths intersect it. But we need to ensure that for each column i, the number of hits a_i is exactly as given. So we must design the set of targets such that for each column i, when the boomerang is thrown from that column, it hits exactly a_i targets, and the order is as described. Since a_i ≤ 3, each column's path is relatively short. So the problem is essentially constructing a directed graph-like structure where each column i may be associated with a chain of up to three targets.
However, the path from a column i is deterministic based on all targets. So if we place targets, each column will produce some number of hits that might not correspond exactly to a simple chain if there are interfering targets. For instance, if we set up a chain for column i as above, but there is another target in column i above the first hit? Actually the first hit is the lowest target in column i because the boomerang comes from bottom. So if there are multiple targets in column i, the one with the highest row (largest row number) will be the first encountered. So the presence of another target above it might not affect the first hit? Actually if there is a target above (smaller row), the boomerang will hit the lowest one first. After hitting that target and turning right, it moves right, so it might not hit the above target because it's now moving horizontally. However, could the boomerang later after bounces come back to column i and hit the above target? Possibly if the path goes around. But given the right-turn behavior and limited bounces, it seems unlikely to come back to same column after a few bounces? Actually after 3 hits, direction becomes left; it might cross column i again but could hit a target in that row if any. But if a_i=3, the third hit is at (r3,c2), then direction left; it could then go left across row r3, and if it reaches column i (which is less than c2) it might hit a target at (r3,i) if exists, which would be a fourth hit. So we must ensure that doesn't happen. So indeed placements must be carefully coordinated.
Thus it's a constraint satisfaction problem. However, given n up to 1e5, we need a constructive algorithm, likely greedy.
Let's think of the interpretation in terms of matchings or pairing. The description reminiscent of "permutation with at most two per row/col" and the boomerang path essentially forms a path that goes right, down, left, up,... but limited to at most 3 hits means we only consider up to two turns.
We can think of each column's a_i as requiring a certain number of targets in its "path". But perhaps we can model the problem as a kind of 2-SAT or flow? However constraints are high, but a_i ≤ 3 suggests limited interactions.
Alternate approach: We can think of building a configuration where each target is part of some chain. Consider chains of length 1,2,3. For a_i=1, we need a "singleton" target in column i such that its row has no target to its right. For a_i=2, we need a pair of targets: (r,i) and (r,c) with c>i, and column c has no target below (r,c). For a_i=3, we need a triple: (r1,i), (r1,c2), (r3,c2) with r3>r1, and row r3 has no target left of c2.
Also, note that a target can be used as first hit for its column, but also potentially as second or third hit for other columns? For instance, consider two columns i and j with i<j. Could the target (r,i) be the first hit for column i, and also be the second hit for some column k? For that to happen, the column k's boomerang would first hit its own first target at some row, then go right and hit (r,i). That would require that the row of its first hit be the same as r, and that (r,i) is to the right of that first hit? Actually if the first hit is at (r, k) with k< i, then turning right, it goes right and might hit (r,i) if there is no target between k and i in that row. So (r,i) could serve as second hit for column k. That would mean that for column k, a_k >=2 and its second hit is at (r,i). Then column k's path would then go down from (r,i), and so column k's third hit might be at some (r',i) if exists. So (r,i) could be part of multiple chains. Similarly, a target might serve as first hit for its column and also as third hit for some other column? Possibly if a column's boomerang goes down to that target? That would require that target be in a column that is the second column of some other path. For example, column k has a_i=3: first hit at (r1,k), second at (r1,c), third at (r3,c). The third hit is in column c, row r3. So that target (r3,c) is the third hit for column k. Could it also be first hit for its own column c? Yes, if column c's boomerang from bottom hits that target first (since it's the lowest target in column c? But need to check if it is the lowest in column c). If there is another target lower in column c, that would be hit first. So it could be the first hit if it's the lowest target in column c. So we need to coordinate.
Thus we have a graph where each column i can have a "path" that may involve one, two, or three targets. These targets can be shared across columns, but the total constraints per row and column (max 2 targets) limit sharing.
Given the constraints, we might aim to construct a configuration where each column's a_i is achieved independently, with minimal interference, maybe by using distinct rows for each chain. Since n up to 1e5, we can assign rows as needed.
But we also must respect per-row max 2 targets and per-col max 2 targets. Columns already have possibly up to 2 targets? Wait each column can have at most 2 targets, because the problem statement says each row and each column has at most 2 targets. So any column can have at most 2 targets. This is a critical constraint.
Now consider the implications for the patterns:
- For a_i=1: column i must have at least 1 target (the first hit). But column i could have 2 targets; but if there are two targets, the lower one (largest row) will be the first hit. The other target above could interfere? If column i has a second target above, then after the first hit, the boomerang turns right and goes horizontally, it doesn't go upward, so the above target would not be hit in that path. However, could the boomerang later, after more bounces, come back to column i and hit that above target? Possibly if the path goes left and then up, but with a_i=1, the boomerang leaves after first hit because no further targets? Actually a_i=1 means it hits exactly one target then leaves. So we must ensure that after the first hit, the boomerang does not hit any other target before leaving. So we need to ensure that there is no target in that row to the right, and also after turning right, moving right, it doesn't hit any target; then it leaves. But if there is another target in column i above, it won't be encountered because direction is right, then after that maybe later it could come back? After hitting first target, it goes right; if it exits the grid at right edge, it's done. So no. So column i can have additional target(s) above as long as they don't cause extra hits. But they could cause extra hits for other columns? Possibly other columns might hit them as part of their path. So column i can have up to 2 targets overall, but we must consider that for its own a_i, if there is a second target in column i, it must not be hit by its own boomerang. That's fine because the boomerang after first hit doesn't go up. However, what about the case when column i has two targets: one lower (first hit) and one higher. For the boomerang from column i, it will hit the lower target first. But after that, could it ever hit the higher target? After turning right, it goes horizontally; then later, if it bounces again (if it hits another target), it could go down, left, up, etc. But for a_i=1, we want it to exit after the first hit. So we must ensure that after first hit, it doesn't hit any target. That means no target to the right in that row. That is sufficient. Even if there is a higher target in the same column, it won't be hit because direction changes to right. So it's fine.
But wait: Could the boomerang after first hit, go right, then maybe bounce off another target and later go up and hit the higher target? For that to happen, it would need to hit a second target, which would already make a_i >=2. So as long as we avoid any second hit, it won't bounce further. So a_i=1 is safe if there is no target to the right of the first hit in its row.
Thus a column can have up to 2 targets, but the presence of a second target above does not affect its own a_i as long as it doesn't cause a second hit. However, the second target might affect other columns.
Similarly, for a_i=2: column i must have at least one target (the first). But also there is a second target in the same row to the right. The column of that second target (c) must have at least that target. Could column c have another target? Possibly, but we must ensure that after the second hit, moving down does not hit any target below (in column c) to keep a_i=2. So column c must have no target below the row of that second hit. But it could have a target above that row (row < r1). That wouldn't affect because moving down from row r1 only goes to larger rows. So column c could have a second target above, as long as it doesn't cause a_i to become 3. However, if column c has a target above, could that be hit earlier? No, because the boomerang enters column c at row r1 and moves downward; it won't go upward. So it's safe.
Also note: column i may have a second target above; but that might affect the path? After first hit at lower target (r1,i), boomerang turns right; it will not go back to column i. So the second target above in column i is not hit. So it's fine. But careful: If column i has two targets, the lower one is the first hit. For a_i=2, we need to ensure that after first hit, we hit a second target to the right. If there is also a target above, it's fine.
But what about the row r1? It can have up to 2 targets. In a_i=2 pattern, row r1 will contain at least two targets: one at column i, one at column c. So row r1 has at least 2 targets. If row r1 already has 2 from other assignments, then it's full.
For a_i=3: pattern involves three targets: (r1,i), (r1,c2), (r3,c2) with r3>r1. This uses two rows: r1 and r3. Column i gets one target (r1,i) (could have another?), column c2 gets two targets: (r1,c2) and (r3,c2) (so column c2 must have at most 2 targets; this uses both slots potentially). Row r1 has two targets: (r1,i) and (r1,c2). Row r3 has one target (r3,c2) (maybe could have another). Also, we need that row r3 has no target to the left of c2; i.e., for columns < c2 in row r3, there must be no target. That's because otherwise a fourth hit could occur. So row r3 must have no target in columns < c2. But it could have a target in columns > c2, because after third hit, direction is left, so it would only go left. So that's fine.
Also, after the third hit, the boomerang turns left and moves left; if there is a target in row r3 at column < c2, it would be hit, causing a_i=4, which is not allowed (a_i <=3 but exactly 3). So we must avoid that.
Now, also note that column i could have a second target (above or below) but careful: if column i has a second target above the first, that would not be hit. But if column i has a second target below? Actually below means larger row; but the first hit is at r1, which is the lowest target? Since it's the first hit, it must be the lowest target in column i. So any other target in column i must have row < r1 (above). So they are above, so safe. So column i can have up to 2 targets, with the lower being r1, the other above.
Similarly, column c2 has two targets: (r1,c2) and (r3,c2). Since r3 > r1, (r3,c2) is lower. So the lower target is at r3, and the upper at r1. For the boomerang from column i, after second hit at (r1,c2), it goes down and hits (r3,c2) because that is the nearest target below. So that works. However, note that column c2's lower target is r3. Could that cause issues for other columns? For instance, if some other column's boomerang has c2 as its first column? Let's think.
Thus the construction problem reduces to assigning for each column i a pattern that satisfies a_i, while respecting per-row and per-col target limits and also the "no extra hit" constraints that come from the patterns (i.e., for a_i=1, the row of its target must have no target to the right; for a_i=2, the column of the second target must have no target below that row; for a_i=3, the row of the third target must have no target to the left). Moreover, targets placed for one column might inadvertently create extra hits for other columns. For example, if we have a column j with a_j=2, and its second target is at (r, c). But suppose there is also a target below (r,c) in column c (for some other reason). Then for column j, the boomerang after hitting (r,c) would go down and hit that lower target, making a_j at least 3, which would be wrong if a_j=2. So we must ensure that for every column that uses a pattern, the required absence conditions hold globally, not just initially. This introduces interdependencies.
Thus it seems tricky to manage globally.
Maybe there's a known solution: This problem is from Codeforces (likely "Boomerang" or something). I recall a problem "Boomerang" or "Zookeeper and The Infinity Zoo"? Not sure.
Alternatively, we might approach by modeling as a matching problem. Since each row can have at most 2 targets, and each column at most 2, we can think of constructing a graph where targets are edges between rows and columns. Actually each target is a point (r,c). So we can view it as a bipartite graph with left nodes = rows, right nodes = columns, edges = targets. The constraints: degree of any row <=2, degree of any column <=2. So the bipartite graph is a collection of paths and cycles (since max degree 2 on both sides). Indeed, a bipartite graph where each vertex has degree ≤2 is a disjoint union of paths and even cycles.
Now, what does the boomerang path correspond to? Starting from a column i, we go up until we hit the first target (edge) incident to column i. Since we start from bottom, the first target is the one with the largest row (lowest). If column i has degree 0, a_i=0. If degree >=1, the boomerang will hit the target with the largest row. After hitting, it turns right, so it moves along the row of that target. Now along that row, we need to consider its incident edges (targets). Since the row can have up to 2 targets, after hitting the target at (r,i), we are at column i on row r. Turning right, we move to the right along row r. Which target will we hit next? The row may have another target at some column to the right of i (or left). Since we are moving right, we will hit the nearest target to the right. If the row has only the one target (degree 1), then there is no other target, so boomerang continues right and exits -> a_i=1. If the row has two targets, then the other target could be to the left or to the right. If it is to the right, then we will hit it -> a_i at least 2. If it is to the left, then moving right we would not hit it (since we go right), so we would exit -> a_i=1. So for a_i=2 or 3, we need the row to have the second target to the right.
Now after hitting that second target (at (r,c)), we turn right again (so direction becomes down). Now we are at column c, row r. In column c, we consider its incident edges (targets). Column c has possibly two targets. Since we are moving down (increasing row), we will hit the nearest target below row r, if any. That is the target with the smallest row > r among those in column c. If there is none, we exit -> a_i=2. If there is one, we hit it -> a_i at least 3.
After hitting that third target (at (r',c)), we turn right again (direction left). Now moving left along row r', we consider its incident edges. If row r' has another target to the left of c, we would hit it -> a_i=4, which is not allowed (since a_i≤3). So for a_i=3, we must ensure that row r' has no target to the left of c. It could have a target to the right, but that's irrelevant because we move left.
Thus the boomerang's behavior is fully determined by the structure of the bipartite graph and the ordering of edges along rows and columns based on direction.
Interesting: The bipartite graph with max degree 2 means each row can have at most 2 targets, so the path can branch? But degree 2 means each row has at most 2 edges, i.e., at most two columns with targets in that row. Similarly each column at most 2 rows. So the graph is a collection of paths and cycles. This is reminiscent of permutation-like structures.
We can interpret the boomerang as traversing the graph in a particular deterministic way: Starting from a column node, we follow the edge to the row that is the "lowest" neighbor (largest row index). Then from that row, we follow the other edge (if any) to the right if it exists and is to the right; else exit. Then from that column, follow the other edge downward (if any) and lower than the current row; else exit. Then from that row, follow the other edge to the left if exists and is left; else exit. So the boomerang essentially traverses a path in the bipartite graph, alternating between column and row nodes, always choosing the neighbor in the direction of travel based on the ordering (row index increasing/decreasing, column index increasing/decreasing). The path terminates when there is no neighbor in that direction.
Thus a_i is essentially the length of the path (number of edges traversed) starting from column i, with a maximum of 3 (since a_i ≤ 3). Because if the path were longer, a_i would be >3, but the input guarantees a_i ≤3? Actually input a_i are given and can be 0,1,2,3, but we must construct a configuration that yields exactly those a_i. So we must ensure that for each column i, the path length equals a_i, and that the path does not continue further.
Now, note that the path length is the number of targets hit, which equals the number of edges traversed. Starting from column i, we traverse edges until we can't proceed.
Given the degree constraints, the path is essentially a walk that could go up to many steps if the graph has long alternating paths. But we must restrict it so that for columns with a_i ≤ 3, the walk stops after a_i steps. For columns with a_i=0, the column must have degree 0 (no edges) because if there is any edge, the boomerang would hit the lowest one (since it will go up and hit the lowest target? Actually it hits the highest row, which is the largest row index; if column has any target, the boomerang will hit the one with largest row, because it starts from bottom and goes up, hitting the first target it encounters, which is the one with the largest row). So a_i=0 implies column degree 0.
For a_i=1, column degree at least 1, but the row of that edge (the lowest row of column i) must have no other edge to the right of i (i.e., if the row has degree 2, the other edge must be to the left of i, or if degree 1, then fine). Additionally, after that, we must ensure that if we go right from that row and then down from the column of that other edge? Actually for a_i=1, we require that after hitting the first edge, when we go right, we don't encounter another target. That means the row's degree must be 1, OR if degree 2, the other edge is to the left (so not encountered when going right). So condition: In the bipartite graph, for column i with a_i=1, let r be the neighbor (row) connected via the edge that is the lowest row (largest r). Then, among the two possible edges incident to row r, the one with the largest column? Actually we need that when at (r,i) and moving right (i.e., increasing column), we do not encounter another target in row r. Since we are at column i, moving right we check columns > i. So if row r has another target at column c > i, then we would hit it. So we must have that there is no target in row r with column > i. So either row r has degree 1 (only column i) or its other neighbor column j satisfies j < i (so to the left). Also, after turning right, we would go to that column j? Wait if j < i, moving right from i, we go to columns > i, so we wouldn't encounter j because it's left. So that's fine. So a_i=1 can be satisfied with row r having degree 2 as long as the other column is to the left. However, we also need to ensure that after turning right, we don't later hit a target via the column? Actually after turning right, we move horizontally. If there is no target to the right, we exit the grid at right edge. That's it. So a_i=1 is satisfied. So condition: row r must have no target to the right of column i.
Now for a_i=2: Column i must have at least one edge (lowest row r). Row r must have another edge to a column c > i (so that moving right hits it). After hitting (r,c), we turn down. Then we need to ensure that column c has no edge with row > r (i.e., no neighbor with row larger than r) to avoid a third hit. However, column c may have a second edge to a row r' < r (above). That's fine. So condition: column c's lowest neighbor? Actually we need that the neighbor of column c with the smallest row > r does not exist. Since column c's edges are at some rows; we need that among its neighbors, the one with the smallest row greater than r does not exist. That means either column c has degree 1 (only row r), or its other neighbor is at a row < r (above). Because if it has a neighbor at row > r, then that would be hit as the third target. Note: The neighbor at row r is already the current target; we are at (r,c) after hitting it. When we move down from (r,c), we consider rows > r. So we need no neighbor with row > r.
Thus for a_i=2: row r has degree at least 2, with one neighbor i and another neighbor c > i. Column c must have no neighbor with row > r. Also, column c could have a neighbor with row < r, that's fine.
For a_i=3: Column i has lowest neighbor r (so edge (r,i)). Row r has another neighbor c > i (so edge (r,c)). Column c has another neighbor r' > r (so edge (r',c)). Then we need that row r' has no neighbor with column < c (to the left) to avoid fourth hit. Additionally, row r' may have another neighbor to the right, but that's fine. So condition: row r' must have no neighbor to the left of c. That is, if row r' has degree 2, its other neighbor must be > c (or if degree 1, only c). Also note that column c now has two neighbors: r and r' (so degree 2). Column i may have another neighbor above r, but that doesn't affect.
Now we also have the global constraints: each row and column max degree 2. Also, note that for a_i=1 with row r having another neighbor to the left, that would mean row r has degree 2: one with i and one with some j < i. That uses two edges in row r. That's allowed. Also column i could have degree 2 as well if there is another neighbor above r. So overall, the bipartite graph must be constructed with these local constraints.
The problem asks to output any configuration (set of targets) satisfying these constraints. It seems like a combinatorial design. Possibly we can think of constructing the graph component by component where each component is a path of length up to 3 edges, but because rows and columns can be shared, it's more complex.
Observation: The a_i values are given for each column. For columns with a_i=0, they must have degree 0. For columns with a_i>0, they must have at least one edge. Also, the a_i determines the structure of the walk starting from that column.
Given the degree constraints, the graph is a collection of paths and cycles. Since columns with a_i=0 have degree 0, they are isolated vertices (no edges). For columns with a_i>0, they are part of some path component. The walk from a column i corresponds to following edges in a specific direction (lowest neighbor first, then right, then down, then left, etc). But note that the direction is determined by the order of rows and columns (since we always go to the neighbor in the direction of travel). However, in a path component of the bipartite graph (like a line alternating rows and columns), the actual geometric positions (row index, column index) determine the order of traversal. So we cannot just consider the abstract graph; the actual coordinates matter because the "right" and "down" decisions depend on the relative positions.
Thus we need to assign actual row indices and column indices to the vertices (rows are indices 1..n, columns are indices 1..n) and place edges (targets) such that for each column i, the walk length equals a_i.
This looks like we need to assign rows to "connectors" between columns, etc.
Alternate viewpoint: The boomerang behavior is similar to a "matching" of rows and columns with orientation.
We can try to build a configuration using a greedy assignment of rows for each column based on a_i. Since a_i ≤ 3, the maximum length of path is 3. So each column's involvement in the walk is limited. Maybe we can treat each column as either isolated (0), endpoint of a path (1), part of a 2-edge path (2), or part of a 3-edge path (3). The path orientation matters: For a_i=1, the column is like the start of a path that goes to a row, but then stops because row has no right neighbor (or right neighbor is left). Actually it could be that the row has a left neighbor, but that doesn't affect the start. So column i with a_i=1 can be thought of as having an edge to some row r, and that row either has no other edge or has an edge to a column j < i. In the latter case, that column j would be connected to row r. Then what would be a_j? Starting from column j, the boomerang would first hit its lowest neighbor. If column j has an edge to row r, and that row r is the lowest neighbor for column j? But we need to ensure that the boomerang from column j yields the correct a_j. That creates interactions.
So perhaps we can construct independent structures for columns with a_i=1,2,3 while ensuring they don't interfere. Since we can choose rows arbitrarily, we might assign distinct rows for each column's path to avoid interference. However, rows are limited to n, but n up to 1e5, and number of columns is n, so we can assign up to 2n targets (max 2 per row and per column). So we could assign fresh rows for each column's needs. But we must also respect per-row max 2. If we assign distinct rows for each column's first hit, that could be up to n rows, each with at most 2 edges, so it's manageable.
But the main challenge is that columns can share rows and columns, causing potential extra hits. To simplify, maybe we can construct a configuration where for each column i with a_i>0, we use a dedicated row for its first hit, and if needed, dedicated rows/columns for subsequent hits, ensuring that no unintended extra edges exist. That is, we can make sure that rows and columns have degree exactly as needed for the paths, and no extra edges that would cause further hits.
Given a_i ≤ 3, we can aim to create each column's path as an isolated "chain" that does not intersect with others, by using unique rows and columns for each chain. But there are n columns, each could need up to 2 rows and 2 columns? However, we have only n rows and n columns. If we try to assign distinct rows for each column's first hit, that's n rows, which is fine. For a_i=2, we need an extra column for the second hit. If each such column uses a distinct column for the second target, that could be up to n columns. But note that columns are the same set as the input indices. The second column for a_i=2 must be some column index > i (since the second hit is to the right). So we cannot arbitrarily choose a column; it must be > i. Also, for a_i=3, we need a second column c > i and a second row r3 > r1. So we need to assign rows and columns in increasing order.
Thus the problem becomes about matching columns to rows and additional columns and rows to satisfy these ordering constraints while respecting the a_i.
It resembles constructing a permutation or assignment of "right turns". Maybe we can interpret each target as a point. The boomerang from column i essentially moves to the next point in the "chain" if there is one in the appropriate direction.
Another angle: This is similar to the concept of "non-intersecting paths" or "matrix with at most two per row/col". I recall a Codeforces problem "1498E Two Houses"? No.
Maybe we can think in terms of constructing a set of points such that each column i's a_i equals the number of points in the "increasing chain" starting from column i: first point is (max row in column i), then if that row has a point to the right, that's second, then if that column has a point below, that's third. So it's like a partial order.
Let’s denote for each column i, let r_i be the row of the lowest target in column i (if any). Then a_i is determined by whether there exists a target at (r_i, j) with j>i and j minimal? Actually if there is a target at (r_i, j) with j>i, then that's second hit; else a_i=1. If there is such j, then let c = j; then if there exists a target at (r', c) with r' > r_i, then that's third hit; else a_i=2. If such r' exists, then if there exists a target at (r', k) with k < c, that would be fourth hit, but we require a_i=3, so we must ensure no such target exists.
Thus a_i is essentially determined by the presence/absence of these points.
Now, the constraints of at most 2 per row and column become:
-
For each column i, it can have at most two points: one at r_i (lowest) and possibly another at some row above (higher row index smaller?). Actually column i can have at most two points; the lowest is r_i (largest row). If there is a second point, it must be at a row < r_i (above). That second point might be part of some other column's chain.
-
For each row r, it can have at most two points: possibly one at some column i and another at some column j (could be left or right).
Given these, we might try to build the configuration by scanning columns from left to right, or using a greedy matching.
Idea: Since a_i ≤ 3, we can categorize columns into types: 0,1,2,3.
We need to assign for each column i with a_i=1,2,3 at least one point (r_i,i). Additionally:
-
For a_i=2 or 3, we need a second point in the same row r_i, to the right (c_i > i). Let's call that column c_i.
-
For a_i=3, we need a third point in column c_i, below r_i (r_i' > r_i). And also ensure that in row r_i', there is no point to the left of c_i (so either no point, or a point to the right).
Now, these assignments must be consistent with the degree limits and also must not create extra hits for other columns.
In particular, consider a column j that is used as the second column c_i for some i. Then column j will have at least the point (r_i, j). For a_i=2, column j must have no point below r_i (to avoid third hit for column i). However, column j could also be the first column for some other column k (i.e., have its own a_j). In that case, column j would have its own lowest point (r_j, j). If column j is used as c_i, then it already has a point at row r_i. But its lowest point might be either r_i or some other row if there is a point below (higher row). For a_i=2, we require that column j has no point below r_i, so the lowest point in column j must be r_i (or possibly a point above r_i, but then the lowest would still be r_i if there is no point below). However, if column j also has its own target as first hit, that target would be at some row r_j. Since a_j must be satisfied, we need to consider the ordering. If column j has two points, one at r_i and one at r_j (maybe above or below), we need to ensure that the boomerang from column j yields the correct a_j.
Similarly, row r_i is used in the first two hits for column i. Row r_i will have points at columns i and c_i (since a_i>=2). So row r_i has at least those two points. If a_i=3, row r_i has exactly those two? Could have more? Max 2, so row r_i can have exactly these two points (since max 2). So for a_i>=2, row r_i must have exactly two points (i and c_i) because if it had only one, a_i would be 1, but we need a_i>=2. So row r_i must have degree 2, with one at i and one at c_i (c_i>i). That means row r_i is fully occupied (2 targets). Similarly, for a_i=3, row r_i' (the third hit row) will have at least one point at c_i. Could have another point, but to avoid a fourth hit for column i, it must not have a point to the left of c_i. So if it has a second point, it must be to the right of c_i. However, that could affect other columns.
Now, also consider columns with a_i=1: they need a point (r_i,i). Row r_i can have either degree 1 or degree 2. If it has degree 2, the other point must be to the left of i (so that moving right from i doesn't hit it). That other point would be in some column j < i. That column j would then be connected to row r_i. That could affect a_j.
Thus interactions happen when rows are shared between columns. The challenge is to assign rows and additional columns to satisfy all a_i.
Given the problem's complexity, maybe there is a known necessary and sufficient condition on the sequence a_i that can be checked and construction performed greedily. Since n up to 1e5, there must be an O(n) or O(n log n) solution.
Let's think in terms of graph flow: We have columns that need to be matched to rows for first hit, and for a_i>=2, they also need to be matched to a second column (right) and row (for a_i=3). But maybe we can reduce to 2-SAT or something.
Let's analyze the possible structures of the bipartite graph given degree constraints. Each component can be a path or a cycle. Since we have directed constraints (a_i values), maybe each component is a path that starts at some column with a_i=1 and ends at some column with a_i=1? Or something.
Consider a component that is a simple path: column1 - row1 - column2 - row2 - column3 - row3 - ... The geometry (row indices, column indices) must satisfy that along the path, the row indices alternate in a monotonic way? For the boomerang to traverse the path as described starting from a column, the ordering of rows and columns must be consistent: For a column node i, its lowest neighbor (largest row) is the one that will be hit first. In a path, if a column has two neighbors (above and below rows), the lowest one is the one with larger row index. The boomerang from that column will go to that row. Then from that row, it will go to the other neighbor (if any) depending on direction: if the row has two neighbors, one left and one right, the boomerang after coming from the column will be moving right (since it turned right from up). Actually from column, the boomerang is moving up, hits the target, then turns right. So it will move right from that row. So if the row has a second neighbor, it will be hit only if it is to the right of the column. So the row's other neighbor must be to the right for the boomerang to continue. Similarly, after hitting that second column, it turns down, so it will go to the lower neighbor of that column (if any). So for the path to be traversed in the boomerang order from a given starting column, the geometry must follow increasing column and then increasing row, etc.
Thus the boomerang essentially follows a monotonic path: first up to a row, then right to a column, then down to a row, then left to a column, etc. But because a_i is small, the path from any column can only go at most 3 edges. So any component can contain at most a few edges? Actually a component could be longer, but the boomerang from a column might only traverse part of it, depending on starting point and direction. However, for columns with a_i=0, they have no edges, so they are isolated.
Maybe we can construct a configuration where each column i with a_i>0 belongs to a distinct path of length a_i, and these paths do not intersect. However, due to column indices, the second column for a_i>=2 must be > i, so we can pair columns in increasing order: i with some j>i. Similarly for a_i=3, we need a row r' > r_i. So we can assign rows in increasing order as well. Since we have many rows, we can assign fresh rows for each such chain, making sure they don't interfere.
But what about columns with a_i=1? They just need a target. We can assign each a_i=1 column its own unique