We need to determine whether array a (elements only -1,0,1) can be transformed into array b using operations: choose i<j, add a_i to a_j. Operations can be repeated, any number, any order.
Observation: This is like we can add any multiple of a_i to any later position j. But a_i can change over time as we modify array a. However, note that we can only add a_i (current value at index i) to a later index j. Since a_i can be changed by previous operations (from earlier indices adding to i), the process is not independent. But we can think about simulating from left to right: because operations only affect later indices (i<j). So we cannot change an element once it has been "passed" (since only earlier indices affect later ones, not vice versa). However, note that we can also choose i<j where i may be later than some other indices, but always i<j, so we can affect later positions. But we can also change a_i itself if there is some k<i that adds to i. So effectively, we have a process: we start from leftmost index 1. Its value a1 is fixed initially (since no earlier index to modify it). However, later we could potentially modify a1? Wait: condition i<j. So i must be strictly less than j. So we cannot add something to an index from a later index. Thus earlier indices can be changed only by even earlier indices? For index i, to change it we need some k < i that adds to i. So as we go left to right, we could modify the current index based on previous ones. But after we move past index i, no later operations can affect index i because any operation with j = i would require i < j? Actually to change index i, we need an operation with j = i (target). For that, we need some k < i to be the source. So after we have processed indices up to i, we might still modify i using any source with index < i, even if later indices exist. Because operations can be performed in any order. So we can still modify index i after we have considered later positions? But the operation only adds to a_j; it does not affect a_i (source) except possibly if we also modify source later? But a_i is unchanged as a source. So yes, we can still apply operations that target index i using any source with index < i. So index i can be modified after we have moved past it, as long as we have some earlier indices. So we cannot treat it as fixed left to right in the sense that once we decide its final value we cannot change it later; we can still add to it from earlier indices. However, note that earlier indices themselves might be modified later? For a source index k (< i), it can be modified by even earlier indices. But once we have fixed all modifications to index k, then we can use it to affect later indices. But can we modify index k after we have used it to add to something? Yes, because operations are independent. For instance, we can first use k to add to j, then later modify k by adding from some earlier index, then use the new value of k to add to j again. So the order matters.
We need a feasibility condition.
Let's consider the process more abstractly. The allowed operations: for any i<j, we can increase a_j by a_i. This is like we can add any multiple of the current a_i to a_j, but because a_i may change, it's more dynamic.
We can think of the transformation as: we are allowed to replace the array with a new array such that for each position j, the final value b_j is equal to the initial a_j plus some linear combination of the initial a_i's? Actually, since operations can be applied repeatedly and a_i can change, we need to examine reachable states.
Note that the operation adds a_i to a_j, but a_i itself might have been increased from its original value by previous additions. So effectively, the values evolve as we add contributions from earlier indices to later ones. This is reminiscent of generating any integer combination? Let's think.
If we denote the initial vector a = (a1,...,an). An operation (i,j) corresponds to: a_j := a_j + a_i. This is a linear transformation (adding row i to row j). Since it's over integers and we can apply any sequence, the set of reachable vectors from a is exactly the set of vectors in the orbit of a under the group of lower-triangular matrices with ones on diagonal? Wait: the operation corresponds to adding a_i to a_j; that's like applying an elementary matrix E_{j,i} (with 1 at (j,i)) to the vector: if we represent a as column vector, then after operation, new vector = (I + E_{j,i}) * a, where E_{j,i} has 1 at (j,i). However, that transformation is applied to the current vector, which itself is a product of such matrices acting on the initial vector. Since these matrices are all unit lower-triangular (ones on diagonal, with possibly ones below diagonal), and they generate the group of all unit lower-triangular matrices with integer entries (i.e., any matrix L with 1's on diagonal and arbitrary integers below diagonal). Because we can apply a sequence of operations, and each operation corresponds to adding a multiple (1) of row i to row j. But we can repeatedly add to get any integer coefficient? Let's see: If we apply operation (i,j) k times, that adds k*a_i to a_j, but a_i may change. However, if we fix order: suppose we first finalize a_i (i.e., ensure it's the desired value), then we can add it multiple times to a_j. But can we get arbitrary integer coefficient? The operation adds a_i (current) each time. So if a_i is, say, 1, then adding it 3 times yields +3. If a_i is -1, adding it 3 times yields -3. If a_i is 0, adding it yields 0. However, if a_i changes after we have added some times, it might affect later additions. But if we want to add exactly k times a specific value v (which is the final value of a_i), we could ensure that a_i is not changed after we start adding to a_j. But can we achieve any integer coefficient? Since a_i itself can be only -1,0,1 initially, but can become other values through additions from earlier indices. For example, if we have a1=1, we can add a1 to a2 multiple times to make a2 large. Then a2 could be used as source to add to later indices. So indeed we can create large numbers from earlier ones. So the set of reachable vectors is quite large.
But we have constraints: a_i initially are only -1,0,1. b_i can be arbitrary integers up to 1e9. So we need to see if we can match b.
Let's think about necessary conditions and sufficiency.
Observation: The operation only adds values; it never subtracts. Wait: adding a_i, where a_i could be negative. So we can add negative numbers, effectively subtracting. So we can increase or decrease values.
But note: we can only add a_i, which might be negative, zero, or positive. So we can change values in both directions.
Another crucial point: The operation adds a_i to a_j. So the sum of all elements changes? Let's compute total sum S = sum a_i. After operation (i,j), we add a_i to a_j, so S becomes S + a_i. So the sum changes by a_i. Thus the sum is not invariant. It can change. So no conservation.
But perhaps there is an invariant mod something? Since a_i ∈ {-1,0,1} initially, but later may become other values. Not obvious.
Another viewpoint: The operations are reversible? Not exactly, because we can't subtract. But we can add negative numbers, so effectively we can reverse? Not directly because a_i might become zero etc. But maybe the reachable set is all vectors that can be obtained by multiplying the initial vector by any unit lower-triangular matrix with nonnegative integer entries? Wait, each operation adds the current a_i to a_j. If we consider the transformation as applying a series of elementary column operations? Better: Represent the process as: at the end, each element b_j is equal to some linear combination of the initial a_i's with integer coefficients, but the coefficients may depend on the order. Actually, the process is linear: since each operation is linear (the new vector is linear function of the old vector). If we denote the state vector as v, each operation updates v := (I + e_j e_i^T) v, where e_j e_i^T is matrix with 1 at (j,i). Since all operations are of this form, and they are applied sequentially, the final vector v_final = M v_initial, where M is the product of such matrices (applied in order). Each matrix is unit lower-triangular (ones on diagonal, with a 1 in (j,i) for i<j). However, note that the order matters because these matrices don't commute, but the product of any number of such matrices (with integer entries) is still a unit lower-triangular matrix (ones on diagonal, arbitrary integers below diagonal). Actually, the set of matrices of the form I + N where N is strictly lower-triangular (nilpotent) and integer entries: any product of elementary matrices of type (add multiple of row i to row j) yields a matrix that is unit lower-triangular with integer entries, and any such matrix can be obtained. But careful: elementary matrices we use correspond to adding exactly one copy of row i to row j (i.e., coefficient 1). However, by composing, we can achieve any integer coefficient below diagonal? Consider we want to get a matrix L with entry l_{j,i} = k (for i<j). Can we achieve that? If we perform operation (i,j) k times, but after each operation the value of a_i might have changed if we also modify a_i in between? But if we want the final transformation to be exactly M acting on initial vector, we need to consider that the matrices multiply on the left: v_{t+1} = (I + e_j e_i^T) v_t. Then v_final = (I + e_{j_m} e_{i_m}^T) ... (I + e_{j_1} e_{i_1}^T) v_initial. The product of such matrices yields a unit lower-triangular matrix with integer entries. However, can we produce any integer entries? Let's check: The elementary matrix E = I + e_j e_i^T has a 1 at (j,i). Multiplying two such matrices: (I + e_k e_j^T)(I + e_j e_i^T) = I + e_j e_i^T + e_k e_j^T + e_k e_i^T? Because e_k e_j^T * e_j e_i^T = e_k (e_j^T e_j) e_i^T = e_k 1 e_i^T = e_k e_i^T. So product yields entries: (j,i) = 1, (k,j) = 1, (k,i) = 1. So we get a sum of contributions. By induction, any product yields a unit lower-triangular matrix where each below-diagonal entry is an integer (non-negative?) Actually, from the multiplication, we are adding contributions, and since we only add positive contributions (1s) but never subtract, the entries in the product are non-negative integers? Let's test: Start with identity. Multiply by (I + e_j e_i^T) adds 1 to entry (j,i). Multiply by another such matrix might add more to some entries. Since all matrices have entries 0 or 1 off-diagonal, and we multiply them, the result may have entries that are counts of paths? In the product, entries are sums of products of 1s. All numbers are non-negative integers. Indeed, because we never have negative numbers. So the product matrix L has ones on diagonal and non-negative integers below diagonal. Moreover, any unit lower-triangular matrix with non-negative integer entries can be realized by a sequence? I think yes, because we can perform operations in a certain order to build desired coefficients. For example, to get coefficient 3 at (j,i), we can perform operation (i,j) three times, but careful: after first operation, a_j changes, but when we do operation again, we are adding the current a_i (which might have changed if we also added to i from earlier? But we are using same source i, target j. If we do (i,j) repeatedly without modifying a_i in between, then a_i stays constant (since we never modify i) and a_j increases by a_i each time. But a_i may have been changed earlier from even earlier indices. But if we are only concerned about the final transformation as a linear function of initial a, then doing (i,j) k times sequentially yields: after first, a_j = a_j_initial + a_i_initial. After second, a_j becomes previous a_j + a_i_current. But a_i_current hasn't changed (since we never modify i). So a_j becomes a_j_initial + 2a_i_initial to a_j, and does not affect other entries (except a_j). So the transformation matrix becomes: entry (j,i) = k, and others identity. So we can achieve any non-negative integer coefficient at (j,i) directly by repeating that operation. However, if we later modify a_i from some earlier index, that would affect contributions to a_j if we later add again? But we can control order. So we can realize any matrix with non-negative integer lower-triangular entries by appropriate sequence. For instance, we can construct L by processing columns from left to right: for each i from 1 to n-1, we can use a_i (which at that point may have been modified by contributions from earlier indices) to add to later j to achieve desired coefficient l_{j,i}. Since we can add multiple times, we can achieve any integer coefficient as long as the source a_i's final value is what we want it to be? But the coefficient multiplies the current value of a_i at the time of addition. However, the current value of a_i may itself be a linear combination of earlier initial values. So the final b_j is sum over i ≤ j of (some coefficient) times initial a_i. And these coefficients form a unit lower-triangular matrix with non-negative integer entries? Let's formalize.
Let initial vector a0. After a sequence of operations, final vector v = M a0, where M is a product of elementary matrices (I + e_j e_i^T) with i<j. As argued, such M is a unit lower-triangular matrix (ones on diagonal) with non-negative integer entries below diagonal. Moreover, any such M can be realized by some sequence (e.g., by performing for each i from 1 to n: for each j>i, perform (i,j) exactly M_{j,i} times, but careful: when we perform these operations, a_i may have been changed by earlier operations from indices < i. However, those earlier operations would have added to a_i, altering its value. But the coefficient M_{j,i} is supposed to multiply the initial a_i? Actually, in the matrix representation, if we multiply a0 by M, the entry (j,i) multiplies a0_i and adds to a_j. However, if we perform operation (i,j) after a_i has been changed (i.e., a_i is no longer a0_i but includes contributions from earlier indices), then the contribution to a_j will include not only a0_i but also contributions from earlier ones that were added to a_i. That would correspond to having also contributions from earlier indices via the coefficient from i? Let's check.
Suppose we have two indices: 1 and 2. Initial (a1,a2). Operation (1,2): adds a1 to a2. So after operation, v = [[1,0],[1,1]] * a0. So M = [[1,0],[1,1]]. Entry (2,1)=1. Now suppose we first do operation (1,2) then later operation (1,2) again? That would be (1,2) after a1 unchanged, a2 changed. Second operation adds a1 (still a0_1) to a2 again. So after two operations: a2 = a0_2 + 2*a0_1. The matrix becomes [[1,0],[2,1]]. So M_{2,1}=2. This matches doing (1,2) twice without any other modifications.
Now consider a three-index scenario: indices 1,2,3. Suppose we first do (1,2) (so a2 = a0_2 + a0_1). Then do (2,3) (add a2 to a3). After these two ops: a3 becomes a0_3 + a2 = a0_3 + a0_2 + a0_1. The matrix? Let's compute product: first op: M1 = I + e2 e1^T. second op: M2 = I + e3 e2^T. v = M2 M1 a0. M1 = [[1,0,0],[1,1,0],[0,0,1]]; M2 = [[1,0,0],[0,1,0],[0,1,1]]. Multiply: M = M2 M1 = [[1,0,0],[1,1,0],[1,1,1]]? Actually compute: M2 * M1 = I + e3 e2^T + e2 e1^T + e3 e2^T e2 e1^T? Let's compute properly: M2 M1 = (I+e3 e2^T)(I+e2 e1^T) = I + e2 e1^T + e3 e2^T + e3 e2^T e2 e1^T = I + e2 e1^T + e3 e2^T + e3 e1^T. So entries: (2,1)=1, (3,2)=1, (3,1)=1. So matrix: row2: col1=1; row3: col1=1, col2=1. Indeed a3 = a0_3 + a0_2 + a0_1. That's consistent.
Now if we wanted to get coefficient 2 at (3,1) without (2,1) or (3,2), could we? For example, we could do (1,3) twice: that yields M = I + 2 e3 e1^T. That's also a valid matrix. So we can have any non-negative integer entries.
Now, can we get negative coefficients? Since we only add a_i to a_j, and a_i can be negative, we can effectively subtract if we add a negative number. But the matrix representation yields only non-negative coefficients because we are only adding positive multiples of the current value. But if a_i is negative, adding it subtracts. However, the linear combination coefficients in terms of initial a_i might be negative if we use a_i after it has become negative? But the matrix representation M multiplies the initial vector; if we allow a_i to become negative and then use that to add, then the contribution to later indices may involve negative multiples of some initial a_k? Let's see: suppose a1 = -1 initially. If we add a1 to a2 three times, we get a2 = a0_2 + 3*(-1) = a0_2 -3. So the coefficient for a0_1 is -3? But in matrix representation, we would have M_{2,1} = 3, but since a0_1 = -1, the contribution is 3*(-1) = -3. So the sign is absorbed by the initial a0_1 being negative. So the coefficient is still non-negative, but the initial value may be negative, yielding negative contribution. So in terms of linear combination, final b_j = sum_{i≤j} c_{j,i} * a0_i, where c_{j,i} are non-negative integers, and c_{j,j}=1 (since we start with a_j and never change it except by additions? Wait, we also may add to a_j from earlier indices, but we never replace a_j; we only add to it. So the initial a0_j always contributes exactly once (since we start with a0_j and then we add other contributions). However, we could also add to a_j from later? No, i<j only. So a_j can be increased by contributions from earlier indices, but never decreased except if we add negative values. But the coefficient for a0_j is always 1? Actually, consider if we apply operation (j, k) for some k>j; that does not change a_j. So a_j itself is never the target of an operation from a later index, but it can be the target from earlier indices. However, can a_j be the target from an earlier index? Yes, if i<j, we can add a_i to a_j. That adds to a_j, but does not multiply its own initial value. So the contribution from a0_j to b_j is exactly 1 times a0_j (since we start with a0_j and never replace it; we only add to it). However, what if we add from an index i that itself has been affected by a_j? But i<j, so a_i cannot be affected by a_j because j>i, and we only add from lower index to higher index. So a_i is never influenced by a_j. So a_j's value is a0_j plus sums of contributions from indices i<j. Those contributions are linear combinations of initial a0_i with non-negative integer coefficients. Thus indeed, for each j, we have:
b_j = a0_j + Σ_{i<j} x_{j,i} * a0_i, where x_{j,i} are non-negative integers.
And for each i, we can have multiple contributions to multiple j's.
Moreover, we are free to choose any non-negative integer x_{j,i} (including zero) by appropriate operations, as long as we respect the order? Possibly any set of non-negative integers can be realized, because we can independently decide how many times to add a_i to a_j. However, there may be dependencies: when we add a_i to a_j, the value of a_i at that moment might have been altered by earlier additions. But the coefficient x_{j,i} multiplies the current value of a_i at the time of addition, which itself is a0_i plus contributions from indices < i. So if we want x_{j,i} to be a specific number, we could simply perform (i,j) that many times before any modifications to a_i from indices < i? But we might want to also use a_i after it has been modified to affect later indices. However, we can schedule operations in a specific order: first, set up all modifications to a_i (i.e., add contributions from earlier indices to a_i) to achieve the desired final value of a_i, then use that final value of a_i to add to all later j's. But then the contributions from a_i to later j's will be multiples of its final value, which is itself a linear combination of earlier a0's. That would yield cross terms: for j>i, b_j would get contributions not just from a0_i but also from earlier a0's via a_i. But we can also achieve the same effect by directly adding from those earlier indices to j. Indeed, we can simulate any lower-triangular matrix with non-negative entries by appropriate sequence. In fact, the set of reachable vectors is exactly { M a0 | M is unit lower-triangular with non-negative integer entries }.
We need to confirm that any such M is reachable, and conversely, any reachable vector must be of that form. Let's attempt to prove:
-
Reachable vectors: as argued, each operation corresponds to left-multiplication by a matrix (I + e_j e_i^T) with i<j. All such matrices have ones on diagonal and a single 1 below diagonal. Products of these yield matrices that are unit lower-triangular with non-negative integer entries (since multiplication of matrices with non-negative entries yields non-negative entries, and diagonal remains 1). So any reachable vector is M a0 with M unit lower-triangular with non-negative integer entries.
-
Conversely, given any unit lower-triangular matrix M with non-negative integer entries, can we achieve it? We can construct a sequence of operations using a standard approach: For i from 1 to n-1, for j from i+1 to n, perform operation (i,j) exactly M_{j,i} times. But wait, this assumes that when we perform these operations, the value of a_i is exactly a0_i, unaffected by previous operations. However, before we process column i, we might have already added to a_i from earlier indices? In our loop, when i=1, a1 is unchanged (since no index <1). So we can add from 1 to j M_{j,1} times, and after that, a_j become a0_j + M_{j,1} * a0_1. Then we move to i=2. At this point, a2 may have been modified by additions from index 1? Yes, because we may have done operations (1,2) M_{2,1} times, so a2 = a0_2 + M_{2,1} * a0_1. Now if we then perform operation (2,j) M_{j,2} times, each addition will add the current a2 (which includes contributions from a0_1) to a_j. This will result in a_j gaining M_{j,2} * (a0_2 + M_{2,1} a0_1) = M_{j,2} a0_2 + M_{j,2} M_{2,1} a0_1. But the desired M_{j,1} coefficient from a0_1 to a_j is just M_{j,1} (maybe not including M_{j,2} M_{2,1}). So this naive approach may overcount contributions from a0_1 if M_{2,1} >0. To achieve arbitrary M, we need to control the contributions.
Thus the naive sequential adding does not necessarily give arbitrary M because contributions can cascade. However, note that any unit lower-triangular matrix M can be decomposed into a product of elementary matrices of the form I + α e_j e_i^T with α positive integer? Actually, we can achieve any M by performing operations in a specific order that respects dependencies: we need to avoid adding from a node after it has been augmented by earlier nodes if we want to avoid extra contributions. Alternatively, we could first perform operations that set up the desired contributions in a topological order: we can process from left to right, but when we are at index i, we need to ensure that its value is exactly a0_i (i.e., no contributions from earlier indices) before we use it to add to later indices, if we want the contributions to be directly from a0_i only. But maybe we want contributions that combine both direct and indirect? However, any matrix M can be realized by first performing all operations that involve adding from i to j in an order where we never add to i after using it as source. That is, we can ensure that when we use i as source, its value is exactly the initial a0_i (plus possibly contributions from earlier indices that we have already accounted for in the matrix? But those earlier contributions are part of the matrix: the coefficient M_{j,i} multiplies a0_i, not a0_i plus other stuff. So we need that when we add from i to j, the value we add is exactly a0_i times some factor, not a0_i plus contributions from earlier indices. However, if earlier indices contributed to i, then adding that value would also include contributions from earlier indices, effectively creating extra terms.
Thus to realize exactly M, we must avoid having i receive any additions from earlier indices before we finish using i as source for all desired contributions to later j. But i might need to receive contributions from earlier indices to achieve its own final value b_i. In the target b, we have b_i = a0_i + Σ_{k<i} M_{i,k} a0_k. So i will eventually have some contributions from earlier indices. But if we first add to i from earlier indices, then later use i as source, that would cause extra contributions from those earlier indices to propagate to later j. However, we might want those extra contributions, or maybe not. In the matrix representation, the contributions from a0_k to a_j can come either directly (via M_{j,k}) or indirectly via i (i.e., M_{i,k} times some factor from i to j). In the matrix product, when we multiply elementary matrices, the coefficient M_{j,k} is the sum over all paths from k to j in the DAG of operations, with the product of multiplicities along the path? Actually, each elementary operation corresponds to adding an edge from i to j. If we do operations multiple times, we are essentially building a directed graph (acyclic since only i<j) where each edge (i,j) has a non-negative integer weight (how many times we performed that operation). Then the final value b_j = a0_j + Σ_{paths from i to j} (product of weights along path) * a0_i? Not exactly, because each operation uses the current value of the source, which already includes contributions from all paths ending at the source. This is exactly like a linear system: if we let x_i be final value of index i, then x_i = a0_i + Σ_{k<i} w_{i,k} * x_k, where w_{i,k} is the number of times we performed operation (k,i) (since each operation adds the current value of source). This is a triangular recurrence. Solving gives x = (I - W)^{-1} a0, where W is strictly lower-triangular with non-negative integer entries w_{i,k}. Then (I - W)^{-1} is a unit lower-triangular matrix with non-negative integer entries (since (I - W)^{-1} = I + W + W^2 + ...). So indeed, if we let w_{i,k} be the number of direct additions from k to i, then the final transformation matrix M = (I - W)^{-1} is unit lower-triangular with non-negative integer entries. Conversely, any unit lower-triangular M with non-negative entries can be represented as (I - W)^{-1} for some W with non-negative integer entries, by taking W = I - M^{-1}? But M is invertible over integers? Since M is unit lower-triangular, its inverse exists and is also unit lower-triangular with integer entries (but may have negative entries). For example, M = [[1,0],[2,1]]; then W = ? Solve x = a + W x => (I-W)x = a => M = (I-W)^{-1}. So W = I - M^{-1}. M^{-1} = [[1,0],[-2,1]]. So W = [[0,0],[2,0]] which has non-negative entries? 2 is non-negative. Indeed, W_{2,1}=2. So that matches: we performed (1,2) twice. In general, given M, we can set W = I - M^{-1}. Since M is unit lower-triangular, M^{-1} is also unit lower-triangular with integer entries, but may have negative off-diagonals. However, I - M^{-1} will have zeros on diagonal (since M^{-1} has ones on diagonal), and off-diagonal entries: (I - M^{-1}){j,i} = - (M^{-1}){j,i} for i<j. So for W to have non-negative entries, we need (M^{-1}){2,1}x_21 = 0 => x_21 = -1. Also M_{2,2}*x_22=1 => x_22=1. For i=3: M_{3,1}x_31 =0 => x_31 = 0. Also M_{3,2}x_32 =0 => x_32 = -1. And x_33=1. So X = [[1,0,0],[-1,1,0],[0,-1,1]]. Indeed off-diagonals are -1. So W = I - X = [[0,0,0],[1,0,0],[0,1,0]]? Compute: I - X = [[0,0,0],[1,0,0],[0,1,0]]. That has entries W_{2,1}=1, W_{3,2}=1, and W_{3,1}=0. So W is non-negative. So indeed M inverse has non-positive off-diagonals, making W non-negative.
Is it always true that for any unit lower-triangular matrix M with non-negative entries, its inverse has non-positive off-diagonals? Let's consider M = [[1,0],[3,1]]. Inverse is [[1,0],[-3,1]]; off-diagonal -3 ≤ 0. M = [[1,0,0],[2,1,0],[3,4,1]]. Compute inverse? General property: The inverse of a unit lower-triangular matrix with non-negative entries is an alternating sum: M^{-1} = I - (M-I) + (M-I)^2 - (M-I)^3 + ... Since M-I is strictly lower-triangular with non-negative entries, its powers are also non-negative but may have increasing powers; the alternating sum yields entries that are possibly non-positive? Actually, (I - (M-I) + (M-I)^2 - ...) Since (M-I)^k are non-negative, the series alternates, so the off-diagonal entries of M^{-1} are of the form - (M-I) + (M-I)^2 - (M-I)^3 + ... . For a strictly lower-triangular matrix, (M-I) is nilpotent, so the series is finite. The sign of off-diagonals could be negative or positive depending on the sum. But consider M = I + N, where N is strictly lower-triangular with non-negative entries. Then (I+N)^{-1} = I - N + N^2 - N^3 + ... . Since N has non-negative entries, N^2 etc are non-negative. So the off-diagonal entries are -N + N^2 - N^3 + ... . This could be positive if N^2 > N etc. For example, take N = [[0,0],[2,0]]? Actually N is strictly lower-triangular, so for 2x2, N = [[0,0],[α,0]] with α≥0. Then (I+N)^{-1} = [[1,0],[-α,1]]. Off-diagonal is -α ≤0. For 3x3, consider N with entries: N_{2,1}=a, N_{3,1}=b, N_{3,2}=c. Then N^2: only possibly (3,1) entry = ca? Since N^2_{3,1} = N_{3,2}N_{2,1} = ca. N^3 = 0 (since nilpotent of index at most 3). Then M^{-1} = I - N + N^2. Off-diagonals: (2,1) = -a, (3,2) = -c, (3,1) = -b + ac. So (3,1) could be positive if ac > b. So if b < ac, then (3,1) becomes positive? But we need W = I - M^{-1} = N - N^2? Actually W = I - M^{-1} = N - N^2 + N^3 - ... = N - N^2 + ... . For (3,1) entry of W: W_{3,1} = N_{3,1} - N^2_{3,1} = b - ac. This could be negative if ac > b. But W must be non-negative if it represents the number of direct operations (counts). So for M to be realizable as (I - W)^{-1} with W having non-negative entries, we need N - N^2 + N^3 - ... to have non-negative entries. That imposes constraints on M. Not all unit lower-triangular M with non-negative entries correspond to some W with non-negative entries? But earlier we argued that any sequence of operations yields M = (I - W)^{-1} with W having non-negative integer entries (the direct operation counts). Conversely, given any non-negative integer W (strictly lower-triangular), we can realize it by performing each operation (i,j) exactly W_{j,i} times, in any order (as long as we process in some order that respects that when we do an operation (i,j), we use the current value of a_i, which includes contributions from earlier i's). But that's okay; the resulting final vector will be exactly (I - W)^{-1} a0. So the set of reachable vectors is exactly { (I - W)^{-1} a0 | W is strictly lower-triangular with non-negative integer entries }.
Now, is every unit lower-triangular M with non-negative entries expressible as (I - W)^{-1} for some W with non-negative entries? This is equivalent to requiring that M^{-1} = I - W has off-diagonals ≤ 0 (since W ≥ 0). That is, the off-diagonal entries of M^{-1} must be ≤ 0. Or equivalently, M must be an M-matrix? Actually, for a unit lower-triangular matrix, the condition that its inverse has non-positive off-diagonals is exactly that M is a "monotone" matrix? There's a known property: A Z-matrix (off-diagonals ≤0) that is invertible and inverse has non-negative entries is called an M-matrix. Here we have the opposite: we want inverse to have non-positive off-diagonals. Since M is unit lower-triangular, its inverse always exists and is unit lower-triangular. The off-diagonals of M^{-1} can be positive or negative. So not all M are reachable. For example, can we achieve M where (3,1) coefficient is 0 but we have non-zero (2,1) and (3,2) such that a*c > b? Let's test with a concrete a0 and see if we can get a final b that corresponds to such M. Suppose n=3, a0 = (1,0,0). We want b = (1, 2, 3)? Actually b1 = a0_1 =1. b2 = a0_2 + M_{2,1} a0_1 = 0 + M_{2,1}*1 = M_{2,1}. b3 = a0_3 + M_{3,1} a0_1 + M_{3,2} a0_2 = 0 + M_{3,1}*1 + M_{3,2}*0 = M_{3,1}. So b = (1, M_{2,1}, M_{3,1}). So only b2 and b3 are determined by M_{2,1} and M_{3,1}. In this case, M_{3,2} irrelevant because a0_2=0. But to test constraints, we need a0_2 non-zero to see effect of M_{3,2}. Let's choose a0 = (1,1,0). Then b1 = 1, b2 = 1 + M_{2,1}*1 = 1 + M_{2,1}, b3 = 0 + M_{3,1}*1 + M_{3,2}*1 = M_{3,1} + M_{3,2}. So b3 can be any sum of two non-negative integers. So it seems that any non-negative integers are possible for M_{2,1} and M_{3,1} and M_{3,2}? But is there any restriction like M_{3,1} must be at least M_{2,1} * M_{3,2}? Because from earlier analysis, if we set direct operation counts w_{2,1}=α, w_{3,2}=β, w_{3,1}=γ, then final M = (I - W)^{-1}. Compute: W = [[0,0,0],[α,0,0],[γ,β,0]] (with rows? Actually W_{i,j} for i>j? Better: define w_{i,j} for i>j, number of ops from j to i. Then (I - W) is lower triangular with ones on diagonal and -w entries below. Then M = (I - W)^{-1}. For given w's, we can compute M. For the 3x3 case, we have:
Let W =
[0 0 0;
α 0 0;
γ β 0]
Then I - W =
[1 0 0;
-α 1 0;
-γ -β 1]
Its inverse M = (I-W)^{-1} =
[1 0 0;
α 1 0;
αβ + γ, β, 1] (since solving (I-W) M = I). Check: Multiply (I-W) * M:
Row2: [-α,1,0] dot columns of M:
col1: -α1 + 1α = 0
col2: -α0 +11 =1
col3: -α0+10=0 OK.
Row3: [-γ,-β,1] dot M:
col1: -γ1 + -βα + 1*(αβ+γ) = -γ -αβ + αβ + γ =0
col2: -γ0 + -β1 +1β = -β+β=0
col3: -γ0 + -β0 +11 =1. So correct.
So M_{2,1}=α, M_{3,2}=β, M_{3,1}=αβ+γ. Since γ ≥0, we have M_{3,1} ≥ αβ = M_{2,1} * M_{3,2}. So the reachable matrices M satisfy that for any i<j<k, we have M_{k,i} ≥ M_{k,j} * M_{j,i}? Actually from the pattern, for indices 1<2<3, we have M_{3,1} ≥ M_{3,2} * M_{2,1}. More generally, from the expression M = (I-W)^{-1} = I + W + W^2 + W^3 + ..., we see that M_{j,i} = sum over all paths from i to j of product of w along edges. Since w are non-negative, M_{j,i} is at least the product of w along any particular path. In particular, for any k with i<k<j, the path i->k->j contributes w_{j,k} * w_{k,i}. So M_{j,i} ≥ w_{j,k} * w_{k,i}. But w_{j,k} and w_{k,i} are not directly entries of M, but they are related: w_{j,k} = ??? Actually w are the direct operation counts. They are not directly observable from M, but they must exist such that M = (I-W)^{-1}. The condition that such W exists with non-negative integers is equivalent to M being "totally non-negative" in some sense? More specifically, the mapping from W to M is bijective between strictly lower-triangular non-negative integer matrices W and unit lower-triangular matrices M with non-negative entries that satisfy certain "path" inequalities? Since given M, we can recover W by W = I - M^{-1}. But we need W to have non-negative entries. So M is reachable iff (M^{-1})_{j,i} ≤ 0 for all i<j. That's the condition.
But earlier we gave an example where M = [[1,0,0],[2,1,0],[3,4,1]]. Let's compute M^{-1}. For M with entries: M_{2,1}=2, M_{3,1}=3, M_{3,2}=4. Compute inverse. We can compute via formula: For unit lower triangular, inverse can be computed recursively. For i=1: diagonal 1. For i=2: (M^{-1}){2,1} = -M{2,1} = -2. For i=3: (M^{-1}){3,2} = -M{3,2} = -4. (M^{-1}){3,1} = -M{3,1} + M_{3,2} * M_{2,1} = -3 + 4*2 = 5. So (M^{-1}){3,1} = 5, which is positive. Therefore, W = I - M^{-1} would have entry W{3,1} = - (M^{-1}){3,1} = -5, negative. So such M is not reachable. Indeed, we suspected earlier that condition M{3,1} ≥ M_{3,2} * M_{2,1} is necessary. Here M_{3,1}=3, M_{3,2}=4, M_{2,1}=2, product=8, 3<8, so fails.
Thus reachable M must satisfy that for any i<k<j, M_{j,i} ≥ M_{j,k} * M_{k,i}? Let's test with our derived expression: In the 3x3 case, M_{3,1} must be at least M_{3,2} * M_{2,1} if there is no direct edge from 1 to 3 besides the path via 2? But we also have direct edge w_{3,1} which adds to M_{3,1} directly. Actually from M = I+W+W^2+..., we have M_{j,i} = w_{j,i} + sum_{k} w_{j,k} w_{k,i} + sum_{k,l} ... So indeed, M_{j,i} is at least the sum over all paths of length >=1. In particular, for any intermediate k, the term w_{j,k} w_{k,i} contributes. But w_{j,k} and w_{k,i} are not M entries but can be expressed in terms of M? Perhaps we can recover w via an elimination process: Since M = (I-W)^{-1}, we have W = I - M^{-1}. So condition W_{j,i} ≥ 0 translates to (M^{-1}){j,i} ≤ 0. For unit lower-triangular matrices, the inverse's off-diagonals can be computed via inclusion-exclusion. The condition (M^{-1}){j,i} ≤ 0 is equivalent to certain inequalities among minors? There is known condition: M is the matrix of a poset? Actually, it's related to the fact that M is the incidence matrix of a certain combinatorics.
But maybe we don't need to characterize all reachable M. We need to decide, given initial a (with entries -1,0,1) and target b (arbitrary integers), whether there exists some sequence of operations to achieve b. This is equivalent to: does there exist a unit lower-triangular matrix M with non-negative integer entries such that b = M a (where a is column vector), with the additional condition that M must be "realizable" (i.e., there exists non-negative integer W with M = (I-W)^{-1}). But as argued, realizability condition is exactly that M^{-1} has non-positive off-diagonals, which is equivalent to M being "K-matrix"? However, perhaps we can bypass this complexity by considering a greedy or constructive algorithm.
Given that a_i are only -1,0,1, maybe the necessary and sufficient condition simplifies.
Let's think directly about the process. We can simulate from left to right, deciding b_i one by one, and maintaining some "resources" of what we can add later. Since we can add from any earlier index to later ones, the ability to achieve a certain b_j depends on the values we have at earlier indices after modifications. The key is that we can increase or decrease later values by adding from earlier indices, but earlier indices themselves can be adjusted using even earlier indices.
Observation: Because we can add from i to j any number of times, we can effectively set b_j to any integer value that is reachable given the values we have at indices < j at the moment we finish processing j. However, we have flexibility to adjust earlier indices as needed (subject to their own constraints). Perhaps we can think backwards: from right to left, we can try to satisfy b_n using earlier indices, then b_{n-1}, etc. Because operations only affect later indices, earlier indices are not affected by later ones. So if we process from right to left, we can decide how to adjust the current element using earlier ones without worrying about messing up later ones (since later ones are already fixed). This is similar to constructing a solution by greedily making b_n equal using a_n and contributions from earlier indices, then b_{n-1}, etc. But contributions from earlier indices to later indices are additive and independent? However, when we add from i to j, we change a_j but not a_i. So if we want to achieve b_n, we can use any combination of additions from indices 1..n-1 to index n. The total added to a_n is some integer X, such that a_n + X = b_n. But X must be representable as sum over i<n of k_i * (value of a_i at the time we add), but those values themselves may be changed by previous operations from even earlier indices. However, we can schedule operations to achieve any integer X as long as we have appropriate "resources" from earlier indices. Since we can manipulate earlier indices arbitrarily (subject to their own target b_i), perhaps we can treat each earlier index as providing a "value" that we can add any integer multiple of, but the value itself can be controlled.
But note that the value we can add from index i to later indices is the final value of a_i (since we can add it multiple times). Actually, when we add from i to j, we can add the current a_i at the moment of addition. We can choose to add from i to j at a time when a_i has some specific value. That value may be the final value of a_i (since after we finish all operations, a_i will be its target b_i). But we could also add from i to j before we finalize a_i (i.e., before we add contributions to a_i from earlier indices). If we add after a_i has been increased, then the added amount is larger. So we have control: we can decide the multiple and the exact value of a_i at the time of addition. Essentially, for each i, we can produce any integer combination of the initial a values of indices ≤ i, because we can set a_i to be any integer that is achievable (subject to constraints) and then use that as a source. But a_i itself must eventually become b_i. So we have coupling.
Alternate approach: Since the operations are linear and we have the characterization b = M a0, with M unit lower-triangular and M^{-1} having non-positive off-diagonals. But is that condition automatically satisfied given that a0 entries are limited? Or maybe for any b, we can find such M if and only if some simple condition holds. Let's test with examples.
Given a0 in {-1,0,1}, and b arbitrary. We need to decide existence of M (with the realizability condition) such that b = M a0.
But note that a0 entries are small, and b can be large. This suggests that to achieve large b values, we need to use the multiplicative effect of chained additions. For instance, if we have a1=1, we can add it many times to a2 to make a2 large, then add a2 many times to a3 to make a3 huge, etc. So we can generate exponentially large numbers. But also we can have -1 to subtract.
One necessary condition: The first element a1 cannot be changed (since no index <1). So we must have b1 = a1. Because we cannot modify a1. So check: b1 must equal a1. That's a necessary condition. In the examples: first test: a1=1, b1=1 ok. Second: a1=0, b1=0 ok. Third: a1=1, b1=1 ok. Fourth: a1=-1, b1=-1 ok. Fifth: a1=0, b1=0? Actually test 5: a=[0,1,-1,1,-1]; b=[1,1,-1,1,-1]; b1=1, a1=0 -> not equal, but output is NO. So necessary.
Now for i>1, b_i must equal a_i plus some combination of earlier a's, but the coefficients can be any non-negative integers subject to some constraints from realizability? But maybe the constraints are automatically satisfied if we construct M appropriately, because we can always choose W non-negative to achieve any b that satisfies a certain "reachability" condition? Let's test with small n to get intuition.
Consider n=2. a1∈{-1,0,1}, a2∈{-1,0,1}. We can apply operation (1,2) any number of times. So we can set b2 = a2 + ka1 for any non-negative integer k. b1 = a1. So condition: b1 = a1, and there exists integer k ≥ 0 such that b2 = a2 + ka1. Since a1 may be -1,0,1.
If a1 = 1, then b2 can be any integer ≥ a2 (if we can only add positive multiples) but wait k≥0, so b2 = a2 + k, so b2 can be any integer such that b2 ≥ a2? But also we can only increase a2 by adding 1 each time. So b2 must be ≥ a2 if a1=1? Actually if a1=1, adding 1 increases a2. So b2 can be a2, a2+1, a2+2,... So lower bound a2, no upper bound? But a2 may be negative; adding positive numbers can make it larger. So b2 must be ≥ a2 and congruent? Not exactly congruence: since we add integer multiples of 1, b2 - a2 can be any non-negative integer. So condition: b2 >= a2.
If a1 = -1, then adding -1 subtracts 1. So b2 = a2 + k*(-1) = a2 - k. k ≥ 0, so b2 ≤ a2. So b2 can be any integer ≤ a2.
If a1 = 0, then adding 0 does nothing. So b2 must equal a2.
So for n=2, condition: b1 = a1, and
- if a1 = 1: b2 >= a2.
- if a1 = -1: b2 <= a2.
- if a1 = 0: b2 = a2.
Now test with sample: third test: n=2, a=[1,0], b=[1,41]. a1=1 => need b2 >= a2 (0) and b2 = 41 >= 0, so YES. Fourth test: a=[-1,0], b=[-1,-41]. a1=-1 => need b2 <= a2 (0), b2=-41 <=0, so YES. So matches.
Now n=3. Let's derive conditions. We have a1, a2, a3 in {-1,0,1}. Operations: we can add from 1 to 2, 1 to 3, and 2 to 3. Also we could add from 1 to 2 multiple times, from 2 to 3 multiple times, from 1 to 3 multiple times. But note interactions: adding from 1 to 2 changes a2, which then can affect additions to a3 via 2->3.
We want b1 = a1.
b2 = a2 + x * a1, for some integer x ≥ 0? Actually we can add from 1 to 2 multiple times. But can we also add from 1 to 2 after modifying a1? a1 never changes. So b2 = a2 + k12 * a1, where k12 ≥ 0 integer. So condition similar to n=2: if a1=1 then b2 >= a2; if a1=-1 then b2 <= a2; if a1=0 then b2 = a2.
Now b3 = a3 + k13 * a1 + k23 * a2', where a2' is the final value of a2 (since when we add from 2 to 3, we use the current a2). But we can also add from 1 to 3 directly. And we can choose the order. So b3 = a3 + k13 * a1 + k23 * (a2 + k12 * a1). Also we could add from 2 to 3 multiple times at different times, but overall the total effect is: each time we add from 2 to 3, we add whatever a2 is at that moment. If we add from 2 to 3 after all additions from 1 to 2 are done, then each addition adds a2' = a2 + k12 a1. If we interleave, the sum of additions from 2 to 3 could be a mix of different values of a2 as it changes. However, since additions are linear, the total added from 2 to 3 is some integer times a2 plus some integer times a1? Actually, suppose we perform k12 additions from 1 to 2 (each adds a1 to a2). Suppose we perform k23 additions from 2 to 3, and we may interleave. The total contribution to a3 from 2->3 is sum over each addition of the value of a2 at that time. If we do some of the k23 additions before any 1->2 additions, then those add a2 (initial). If we do some after some 1->2 additions, they add a2 + (some number of 1->2 that have occurred so far) * a1. So the total added from 2->3 can be expressed as k23 * a2 + (some number) * a1, where the some number depends on the interleaving. Specifically, if we let t be the total number of 1->2 additions that occur before each 2->3 addition summed, then the extra a1 contribution is t * a1. The maximum possible extra contribution is when we do all k12 additions before any 2->3 addition, then each 2->3 addition adds a2 + k12 a1, so total extra = k23 * k12 * a1. The minimum extra is when we do all 2->3 additions before any 1->2 addition, then extra = 0. And we can achieve any intermediate integer between 0 and k23 * k12? Possibly by ordering, we can achieve any integer that is a sum of k23 numbers each between 0 and k12, i.e., any integer between 0 and k23k12 inclusive? Not necessarily every integer, but likely we can achieve any integer because we can control how many 1->2 additions happen before each 2->3 addition. Since we can do operations one by one, we can achieve any total extra a1 contribution from the interaction that is between 0 and k12k23, as long as a1 is ±1 or 0. But if a1=0, it's irrelevant. If a1=±1, we can achieve any integer in that range by appropriate sequence? Let's test: Suppose a1=1, a2 initial 0, k12=2, k23=3. We want total extra from a1 to a3 via a2 to be some number E. We can achieve E = 0 (do all 2->3 first), E = 6 (do all 1->2 first, then 2->3 three times, adds 3*(0+2)=6), and maybe intermediate values. For instance, do one 2->3 (adds 0), then do one 1->2 (a2 becomes 1), then do one 2->3 (adds 1), then another 1->2 (a2 becomes 2), then two 2->3 (adds 2 each) total extra = 0+1+2+2 =5. So can we get 1? Possibly: do one 1->2 (a2=1), then three 2->3 (add 1,1,1) extra=3; not 1. To get 1, we might do: one 2->3 (0), one 2->3 (0), then one 1->2 (1), then one 2->3 (1) extra total=1. Yes: two 2->3 before, one after yields extra=1. Actually can we get any integer from 0 to 6? Probably yes because we can treat it as we have k23 steps, each step we add the current a2, which is a2_initial + (number of 1->2 done so far)k23 (all 1->2 first). And because we can arrange the order arbitrarily, we can achieve any integer in between? This is equivalent to having k12 increments of a2 that happen at some times; the total extra is a1 times the sum over 2->3 additions of the number of increments that occurred before each addition. If we think of distributing the k12 increments among k23+1 slots (before first addition, between additions, after last), the total extra = a1 * ( sum_{i=1}^{k23} (number of increments before i-th addition) ). If we let x_0 be number of increments before first addition, x_1 before second, ..., x_{k23} after last? Actually if we have k23 additions, we have k23+1 gaps (including before first and after last). Let u_0 = number of increments done before any addition, u_1 = number done after first addition but before second, ..., u_{k23} = number done after last addition. Then total extra = a1 * ( u_0 * k23? Wait careful: The first addition sees u_0 increments, second addition sees u_0+u_1, third sees u_0+u_1+u_2, ..., last sees u_0+...+u_{k23-1}. So total extra = a1 * [ k23k23? I think yes, because we can adjust u_i to get any sum. For a1=1, the set of achievable extra values is all integers from 0 to k12*k23. Because we can think of distributing the k12 increments among the k23 additions: we can achieve any integer by setting u_0 = extra mod something? Actually, it's known that you can achieve any integer: for example, by doing some 2->3 additions, then some 1->2, then the rest 2->3, etc. But is there any gap? Suppose k12=