We need to determine if we can transform array c to t using allowed operation: choose i (2 ≤ i ≤ n-1) and set c_i = c_{i-1} + c_{i+1} - c_i. This is like a linear transformation.
We note that this operation preserves something: It preserves the sum of the array? Let's check: before: sum = c_{i-1}+c_i+c_{i+1}+others. After: c_{i-1}+(c_{i-1}+c_{i+1}-c_i)+c_{i+1}+others = c_{i-1}+c_{i-1}+c_{i+1}-c_i+c_{i+1}+others = (c_{i-1}+c_i+c_{i+1}) + (c_{i-1}+c_{i+1} - c_i)?? Wait compute: original sum at these three: S = c_{i-1}+c_i+c_{i+1}. New sum: c_{i-1} + (c_{i-1}+c_{i+1}-c_i) + c_{i+1} = 2c_{i-1}+2c_{i+1} - c_i. That is not equal to S generally. So sum changes. Let's test with numbers: c_{i-1}=1,c_i=2,c_{i+1}=3 => S=6; new sum =1+(1+3-2=2)+3=6 => same? 1+2+3=6; 1+2+3=6? Actually 1+(1+3-2)=1+2=3, plus 3 =>6. Yes equal. Let's check: new sum = c_{i-1} + (c_{i-1}+c_{i+1}-c_i) + c_{i+1} = c_{i-1}+c_{i-1}+c_{i+1}-c_i+c_{i+1} = 2c_{i-1}+2c_{i+1} - c_i. Compare to S = c_{i-1}+c_i+c_{i+1}. The difference = (2c_{i-1}+2c_{i+1}-c_i) - (c_{i-1}+c_i+c_{i+1}) = c_{i-1}+c_{i+1} - 2c_i. That's not always zero. But earlier numeric example gave equality: 1+3-2*2=1+3-4=0. So indeed sum is preserved only if c_{i-1}+c_{i+1} = 2c_i, i.e., if the three are in arithmetic progression. But generally sum can change. Let's test another: c_{i-1}=1,c_i=5,c_{i+1}=2 => S=8; new sum: 1+(1+2-5=-2)+2 = 1? Actually new sum = 1+(-2)+2 = 1, so sum changed drastically. So sum is not invariant.
Maybe there is an invariant like the alternating sum? Let's examine effect on differences. Consider array d_i = c_{i+1} - c_i for i=1..n-1. How does operation at i affect differences? Operation modifies c_i only. So differences d_{i-1}=c_i - c_{i-1} and d_i = c_{i+1} - c_i will change. Let's compute new differences:
Before:
d_{i-1} = c_i - c_{i-1}
d_i = c_{i+1} - c_i
After:
c_i' = c_{i-1} + c_{i+1} - c_i
Now:
new d_{i-1}' = c_i' - c_{i-1} = (c_{i-1}+c_{i+1}-c_i) - c_{i-1} = c_{i+1} - c_i = d_i (old)
new d_i' = c_{i+1} - c_i' = c_{i+1} - (c_{i-1}+c_{i+1}-c_i) = c_i - c_{i-1} = d_{i-1} (old)
So the operation swaps the two adjacent differences! Specifically, it swaps d_{i-1} and d_i. Indeed: after operation, d_{i-1}' = d_i, d_i' = d_{i-1}. Other differences remain unchanged.
That's a crucial observation: the operation at position i (2≤i≤n-1) swaps the differences d_{i-1} and d_i. So the multiset of differences? Actually, the sequence of differences can be permuted by adjacent swaps. Since swapping adjacent elements allows generating any permutation, we can reorder the differences arbitrarily? Not exactly, because we can only swap certain pairs? The operation corresponds to swapping d_{i-1} and d_i. That's exactly the operation of adjacent transpositions on the array of differences. But note that we have n-1 differences. However, there is a constraint: the array c is related to differences via cumulative sums: c_k = c_1 + sum_{j=1}^{k-1} d_j. So the transformation on c corresponds to: we can reorder the differences arbitrarily by adjacent swaps (since any permutation can be expressed as adjacent swaps). But careful: can we perform swaps on any adjacent pair of differences? For i from 2 to n-1, swapping d_{i-1} and d_i. That's exactly adjacent transpositions on the list d_1,...,d_{n-1}. So we can achieve any permutation of the differences if we can apply enough adjacent swaps. However, there might be constraints because the operation also affects the actual values? But as we saw, swapping differences doesn't change the set of differences (multiset), but the order can change. So the multiset of differences is invariant? Actually, swapping preserves the multiset obviously. So the multiset of differences (i.e., the unordered set with multiplicities) is invariant under the operation, because swapping two elements doesn't change the multiset. So we have an invariant: the multiset of adjacent differences must be the same between initial and target arrays. But is that sufficient? Also, we need to consider the first element c_1. Because from differences and c_1 we can reconstruct the whole array. Under the operation, does c_1 change? The operation modifies only c_i for i between 2 and n-1; it leaves c_1 and c_n unchanged. Indeed, operation on i does not affect c_1 and c_n. So c_1 and c_n are invariant. But wait: does c_n change? It is not directly modified because operation only changes c_i. So c_n is invariant. However, c_1 and c_n remain fixed. But also note that if we swap differences, the cumulative sum from c_1 yields new array. But does the operation preserve c_1? Yes. So c_1 must equal t_1, and c_n must equal t_n. However, is that necessarily true? Let's test with example: Input 1: c = [7,2,4,12], t=[7,15,10,12]. c_1=7 matches, c_n=12 matches. Example2: c=[4,4,4], t=[1,2,3]; c_1=4 vs t_1=1 not equal, so obviously no. So those are necessary conditions.
But could it be that we can change c_1 or c_n indirectly via sequence? No, because each operation only touches inner indices, so c_1 and c_n are never altered. Therefore, necessary: c_1 == t_1 and c_n == t_n.
Now, if these conditions hold, is it sufficient that the multiset of differences (i.e., the sorted list of d_i for i=1..n-1) matches between c and t? Let's examine.
Given c_1 fixed, and the multiset of differences is the same, can we always permute the differences to achieve the target order? Since we can perform adjacent swaps on differences, any permutation is reachable. However, we must also check that the operations on array c correspond exactly to swapping adjacent differences without any other constraints. Because when we swap d_{i-1} and d_i, we are performing operation at index i. That's allowed for any i from 2 to n-1. So indeed we have full control over adjacent swaps on the differences. Therefore, we can reorder the differences arbitrarily. So if c_1 = t_1 and the multiset of differences is the same, then we can transform c to t by reordering the differences appropriately. Is there any hidden constraint? Let's test with a small case: n=3. Differences: d_1 = c_2-c_1, d_2 = c_3-c_2. Operation allowed only at i=2 (since only inner index): swapping d_1 and d_2. So we can only swap the two differences. So the reachable orders are either original order or swapped order. So condition: either (d_1, d_2) matches target differences, or (d_2, d_1) matches target differences. Since multiset equality means either order, it's sufficient. So for n=3, condition works.
For n=2: there are no inner indices, so no operations allowed. So we must have c_1 == t_1 and c_2 == t_2. Also differences? n-1=1 difference, so multiset is trivially same if c_2-c_1 == t_2-t_1. But if c_1 and c_2 match, difference automatically matches. But our condition of c_1==t_1 and c_n==t_n is necessary and sufficient for n=2? Let's see: if c_1==t_1 and c_2==t_2, then yes. If c_1==t_1 but c_2 != t_2, cannot transform because no operation. So condition is c_1==t_1 and c_2==t_2. Our multiset condition would require d_1 same, but that's equivalent to c_2-c_1 same, which together with c_1 same implies c_2 same. So works.
Now consider larger n. The set of reachable states is exactly: c_1 fixed, c_n fixed, and the differences d_1..d_{n-1} can be permuted arbitrarily via adjacent swaps. Because adjacent swaps generate the symmetric group. So the reachable set is all arrays with same c_1, same c_n, and same multiset of differences. But wait: is there any further invariant? Let's try to find invariants of the operation besides c_1, c_n, and multiset of differences. Since differences can be arbitrarily permuted, the only invariants are those that are symmetric in the differences. For example, the sum of differences? But sum of differences = c_n - c_1, which is fixed because c_n and c_1 are fixed. So that's also invariant, but already captured.
So the necessary and sufficient conditions appear to be:
- c_1 = t_1
- c_n = t_n
- The multiset of adjacent differences for c equals that for t.
But is it always sufficient? Let's test with a tricky case: The operation swapping d_{i-1} and d_i; but note that when we swap, the actual c_i changes, but the differences swap. Could there be any constraint from the actual values being integers? The values are given as integers, but the operation yields integer if differences are integer? Actually, c_i' = c_{i-1}+c_{i+1}-c_i, which is integer if all are integer. So if we start with integer array, all operations yield integers. So no issue.
Also, note that we have to ensure that after permuting differences, we indeed get exactly the target array. Since we set c_1 correct, and the differences order exactly as needed, then the resulting array will match t. So if we can achieve any permutation, we can get exactly target.
But is it always possible to achieve any permutation via adjacent swaps using the allowed operations? The allowed swaps are for positions i=2..n-1, which swap d_{i-1} and d_i. That's exactly swapping adjacent differences at positions (i-1, i) for i from 2 to n-1. So we have the ability to swap any adjacent pair in the list of differences? Yes, because for each adjacent pair (d_j, d_{j+1}) for j=1..n-2, we can perform operation at i=j+1 (since i=j+1 between 2 and n-1). So we have all adjacent transpositions. Therefore, we can generate the full symmetric group on the n-1 differences. So any permutation is reachable.
But wait: Are there any restrictions due to the fact that the operation changes c_i, which in turn might affect future operations in a way that is not simply permutations? But we derived that effect of an operation on the differences is just swapping two adjacent differences. This transformation is independent of the actual values beyond differences. And after swapping, the new array's differences are just swapped; subsequent operations can swap other pairs. So the state is fully described by c_1 and the order of differences. And swapping adjacent differences is reversible. So indeed the reachable states are exactly those with the same c_1 and same multiset of differences (since c_n is determined by sum of differences = c_n - c_1, but if c_1 fixed and multiset fixed, then c_n is determined; but we also require c_n equal to target, which is automatically satisfied if multiset of differences is same? Actually, the multiset of differences does not necessarily imply c_n matches, because sum of differences = c_n - c_1. If the multisets are the same, then sum of differences is equal. So if c_1 matches, then c_n would match automatically if sum of differences matches. But sum of differences is part of the multiset? The multiset determines the sum (since sum is sum of elements in multiset). So if the multisets are equal, then sum(c_diffs) = sum(t_diffs). Then c_n - c_1 = t_n - t_1. If also c_1 = t_1, then c_n = t_n. So the condition c_n = t_n is redundant if we have c_1 and multiset equality. However, in n=2 case, there is no inner index, but we still need c_2 = t_2. Our condition of multiset (single element) being equal ensures difference equal; with c_1 equal, implies c_2 equal. So c_n condition follows.
Thus the necessary and sufficient conditions are:
- c_1 = t_1
- The multiset of differences (c_{i+1} - c_i) for i=1..n-1 must equal the multiset of differences (t_{i+1} - t_i).
But wait, is there any scenario where c_1 = t_1 and multisets equal but we cannot achieve target due to some parity or something? For permutation reachable via adjacent swaps, parity of number of swaps might matter if we consider that each operation is a swap. But we can apply any number of swaps, so we can achieve both even and odd permutations. So any permutation is reachable. So yes.
However, let's double-check with more examples. Consider n=4, c = [a,b,c,d], t = [a,?,?,d]. Differences for c: d1 = b-a, d2 = c-b, d3 = d-c. For t: e1 = t2-t1, e2 = t3-t2, e3 = t4-t3. Condition: multisets equal. That implies {d1,d2,d3} = {e1,e2,e3}. If that holds, can we always reorder differences to match? Since we can swap any adjacent differences via operation at i=2 (swap d1,d2) and i=3 (swap d2,d3). That's enough to generate any permutation of three items. So yes.
Thus solution: read n, arrays c[1..n], t[1..n]. If c[1]!=t[1] or c[n]!=t[n], output "No". Else compute differences for c (size n-1) and for t (size n-1). Sort both diff arrays; if they are identical, output "Yes", else "No".
But is that always correct? Let's test with potential edge cases: Negative differences? c_i can be up to 2e9; differences can be negative. That's fine. Sorting works with negative numbers.
Now, think about possibility that multiple operations might allow more than just permutations? Could we generate new differences that are not just permutations? The operation only swaps differences. So the differences set is invariant. So if multiset differs, impossible. So condition is both necessary and sufficient.
But wait: Could there be a scenario where we have c_1 = t_1 and multiset equal but still impossible because the operation might not allow arbitrary permutations due to the fact that when we swap differences, the actual c_i values may become something that restrict future swaps? However, swapping differences only changes the array but the operation remains defined for any values. There's no restriction that the array must stay within certain bounds; it's always allowed. So we can continue swapping as we like. So we can implement any permutation by applying a sequence of adjacent swaps (like bubble sort) on the differences. Each adjacent swap corresponds to an operation at the appropriate index. However, we must verify that after swapping differences, the index at which we want to perform the next swap corresponds to the current differences. For instance, to swap d_j and d_{j+1} (original indices), we need to perform operation at i=j+1. But after some swaps, the differences have been permuted, so the positions of differences shift. But the operation at index i always swaps the two differences that are currently at positions i-1 and i (i.e., the differences between c_{i-1}, c_i and c_i, c_{i+1}). So if we want to achieve a certain permutation, we can think of performing a series of adjacent swaps on the list of differences. Since each operation is exactly an adjacent swap at positions we can choose (any i from 2 to n-1), we can simulate bubble sort or any sorting algorithm directly on the differences array as it exists in the current state. So we can achieve any permutation. So yes.
Thus the problem reduces to checking these conditions.
But wait, is there any subtlety with the fact that the operation also modifies the c_i values, but that doesn't affect the ability to swap any adjacent pair: given any current differences, we can swap d_{i-1} and d_i by applying operation at i. That's always allowed regardless of values. So indeed we can perform any adjacent swap.
Therefore solution is simple.
But let's test with more examples, maybe including negative numbers.
Example: n=5, c = [0, 10, 0, 10, 0]. Differences: [10, -10, 10, -10]. t = [0, 0, 10, 0, 0]? Compute differences: t2-t1=0, t3-t2=10, t4-t3=-10, t5-t4=0. Multiset: {10, -10, 10, -10} vs {0,10,-10,0}. Not equal, so no.
Another: c = [1,2,3,4] => diffs [1,1,1]; t = [1,3,2,4] => diffs [2,-1,2] => multiset {1,1,1} vs {2,-1,2} not equal, so no. But maybe we can transform? Let's try: c1=1,c4=4, target t1=1,t4=4. Operation at i=2: c2 becomes 1+3-2=2 => array [1,2,3,4]? Actually c2 originally 2, becomes? Wait original [1,2,3,4]; i=2: c2' = c1+c3 - c2 =1+3-2=2, unchanged. i=3: c3' = c2+c4 - c3 =2+4-3=3 unchanged. So cannot change because differences are all equal? Actually if all differences equal, any operation yields same? Let's test operation at i=2 on [1,2,3,4] yields c2=1+3-2=2, same; at i=3 yields c3=2+4-3=3 same. So array unchanged. So indeed can't reach [1,3,2,4] because differences multiset changes.
Now consider c = [0,5,0,5] diffs [5,-5,5]; t = [0,0,5,5] diffs [0,5,0]; multiset {5,-5,5} vs {0,5,0} not equal => no. Try to see if possible: c1=0,c4=5 fixed; try operation at i=2: c2' = 0+0-5 = -5 -> [0,-5,0,5] diffs [-5,5,5]; i=3: c3' = -5+5-0=0 -> back? Actually [0,-5,0,5] i=3: c3' = -5+5-0 =0 -> unchanged? Actually compute: array [0,-5,0,5], i=3: c3 = 0, new = -5+5-0=0 same. i=2 again: c2 = -5, new = 0+0-(-5)=5 => back to [0,5,0,5]. So reachable differences sets are {5,-5,5}, { -5,5,5 }? Actually [0,-5,0,5] diffs = [-5,5,5] which is same multiset as {5,-5,5}. So only that multiset. So cannot get 0 differences.
Thus conditions hold.
But hold on: Is it possible that the operation could change the multiset of differences? We argued swapping preserves multiset. So yes.
Thus solution is O(n log n) due to sorting.
But we should also consider that n up to 1e5, values up to 2e9, differences up to 2e9 in magnitude, fine.
Edge case: n=1? Problem says n≥2, so ignore.
So the answer is straightforward.
However, before finalizing, we should double-check that no other invariants exist. Let's try to compute some other invariant like sum of squares? Does it change? The operation: c_i' = c_{i-1}+c_{i+1}-c_i. Compute sum of squares change: new square: (c_{i-1}+c_{i+1}-c_i)^2 vs c_i^2. The other two unchanged. The difference: (c_{i-1}+c_{i+1}-c_i)^2 - c_i^2 = (c_{i-1}+c_{i+1})^2 - 2c_i(c_{i-1}+c_{i+1}) + c_i^2 - c_i^2 = (c_{i-1}+c_{i+1})^2 - 2c_i(c_{i-1}+c_{i+1}) = (c_{i-1}+c_{i+1})(c_{i-1}+c_{i+1} - 2c_i). Not necessarily zero. So sum of squares not invariant. But maybe some other invariant like something with alternating sum? But swapping differences doesn't preserve alternating sum? Actually alternating sum of differences would change sign under permutations. So not invariant.
But we have proven that the reachable states are exactly those with same c_1 and same multiset of differences. However, we must also check that c_n is not independent: since sum of differences = c_n - c_1, if c_1 matches and multiset matches, then sum matches, so c_n automatically matches. So we don't need to separately check c_n if we check differences multiset and c_1. But it's safer to check both anyway, but if we check differences sorted, we should include c_n check implicitly? Let's see: if we compute differences for c (n-1 values) and for t (n-1 values), and they are equal as multisets, then sum(c_differences) = sum(t_differences). That implies (c_n - c_1) = (t_n - t_1). If also c_1 = t_1, then c_n = t_n. So c_n condition is automatically satisfied if both c_1 and multiset conditions hold. But if c_1 = t_1 but multiset matches but c_n != t_n, that's impossible because sum of differences would not match. So it's consistent.
Nevertheless, it's possible that due to integer overflow or something, but we handle.
Thus algorithm:
- Read n.
- Read arrays c[0..n-1], t[0..n-1].
- If c[0] != t[0]: print No and exit.
- Compute vector diff_c of length n-1: for i=0..n-2, diff_c[i] = c[i+1] - c[i].
- Compute vector diff_t: for i=0..n-2, diff_t[i] = t[i+1] - t[i].
- Sort both vectors.
- If diff_c == diff_t: print Yes else No.
But careful: The differences for t may be computed from t array; if c1 != t1 we already output No. But what if c1==t1 but c_n != t_n? Then diff_c and diff_t would have different sums, so they won't be equal as multisets? Actually they might still be equal if differences compensate? Since sum diff_c = c_n - c_1, sum diff_t = t_n - t_1. If c1 == t1 but c_n != t_n, then sums differ, so multisets cannot be equal (since sum of elements in multiset is different). So sorting will catch it. So we don't need separate c_n check. But it's safe to include.
Potential pitfalls: n up to 1e5, differences up to 2e9, negative okay. Sorting O(n log n) works.
Let's test given examples.
Example 1: n=4, c=[7,2,4,12], diff_c = [ -5, 2, 8]? Actually 2-7=-5, 4-2=2, 12-4=8 => [-5,2,8]. t=[7,15,10,12], diff_t = [8,-5,2]? 15-7=8, 10-15=-5, 12-10=2 => [8,-5,2]. Sorted both: [-5,2,8] vs [-5,2,8] -> equal. c1=7==7. So Yes.
Example 2: n=3, c=[4,4,4], diff_c=[0,0]; t=[1,2,3], diff_t=[1,1]; sorted diff_c [0,0] vs [1,1] not equal, c1=4!=1, already fails first condition. So No.
Thus works.
Now, is there any case where c1 == t1 and diff multiset equal but still unreachable because of something like parity of number of operations? As argued, any permutation of differences is reachable because we can apply adjacent swaps. But think about the fact that swapping differences is not independent: each operation swaps two differences but also modifies c_i. But we only care about the final array matching t. The process of swapping differences is reversible and any permutation can be achieved. For example, to transform diff order [a,b,c] to [b,c,a], we can do sequence: swap positions 1-2 to get [b,a,c]; then swap positions 2-3 to get [b,c,a]. So yes.
Thus solution is correct.
But wait: Could there be a scenario where the multiset is the same but we cannot achieve because c1 is same but c_n differs? Already covered.
Now, we should consider if there is any other operation effect that could modify something else? Let's examine the effect on the differences more thoroughly. Operation at i: c_i' = c_{i-1} + c_{i+1} - c_i. Differences before: d_{i-1} = c_i - c_{i-1}; d_i = c_{i+1} - c_i. After: d_{i-1}' = c_i' - c_{i-1} = (c_{i-1}+c_{i+1}-c_i) - c_{i-1} = c_{i+1} - c_i = d_i. d_i' = c_{i+1} - c_i' = c_{i+1} - (c_{i-1}+c_{i+1}-c_i) = c_i - c_{i-1} = d_{i-1}. So indeed swaps. No other differences change.
So the state is defined by (c1, d1, d2, ..., d_{n-1}) and operations permute d's while leaving c1 unchanged. So the reachable set is exactly all arrays with same c1 and same multiset of d's.
Thus problem solved.
Implementation details:
- Use 64-bit integers (long long in C++) for differences because differences can be up to 2e9, but subtracting two numbers up to 2e9 yields up to +/-2e9, fits 32-bit signed? Actually 2e9 fits in 32-bit signed int (max ~2.147e9), but careful: 2e9 is 2,000,000,000, within 2^31-1=2,147,483,647, so int (32-bit) is okay. But if c_i up to 2e9, difference could be as large as 2e9 - (-2e9) = 4e9? Wait c_i are non-negative (0 ≤ c_i ≤ 2e9). So c_i are between 0 and 2e9. So difference c_{i+1} - c_i can be between -2e9 and 2e9, so within 32-bit signed range. But if we use int, we must be careful that in C++, int is typically 32-bit signed and can hold up to ~2.147e9; -2e9 is fine. However, 2e9 is 2,000,000,000 < 2,147,483,647, so safe. But when computing sum of differences, we might not need sum.
Thus we can use int.
But to avoid overflow in future calculations, we can use long long, but not necessary.
Edge case: n=2, differences arrays of size 1. Sorting and comparing works.
Thus answer is straightforward.
But wait, maybe there is a catch: The operation is allowed only on inner stones, i.e., indices 2 to n-1. For n=2, no operation, so we must check that arrays are identical. Our condition with differences: if c1 == t1 and diff arrays equal (i.e., c2-c1 == t2-t1), then it implies c2 == t2. So correct.
For n=1 not allowed.
Thus code is simple.
Now I'm thinking about a potential alternative viewpoint: This operation is also known as performing an involution that reflects across average: c_i becomes the symmetric point with respect to c_{i-1} and c_{i+1}. It preserves the sum of the endpoints? But we already resolved.
But is it possible that we can also change c_1 indirectly by some sequence? For instance, if we perform operation at i=2, c_1 is unchanged. Could a later operation at i=2 after some other operations affect c_1? No, because operation at i=2 only modifies c_2, leaving c_1. So c_1 always stays the same. Similarly, c_n unchanged. So indeed those are invariants.
Thus the problem reduces to checking the two conditions.
Now, confirm by analyzing some random test: Let's say n=4, c = [0, 10, 0, 0]. c1=0. diffs: [10, -10, 0]. t = [0,0,10,0]? Actually need c_n=0, so t4=0. t1=0. diffs t: [0,10,-10]? That would be [0,10,-10] sorted => [-10,0,10] vs diffs_c sorted: [-10,0,10]? Wait diffs_c: [10, -10, 0] sorted => [-10,0,10]. t: if t = [0,0,10,0], then t2-t1=0, t3-t2=10, t4-t3=-10 => [0,10,-10] sorted [-10,0,10]. So multiset equal. So condition says Yes. Can we actually transform? Let's try: start [0,10,0,0]. Differences: 10, -10, 0. Goal t: [0,0,10,0]. Differences order: 0,10,-10. We need to reorder differences. Operation at i=2 (swap d1,d2): before d1=10, d2=-10. After operation at i=2: swap => d1'=-10, d2'=10. That changes array? Let's compute operation at i=2: c2' = c1 + c3 - c2 = 0+0-10 = -10? Actually c1=0, c3=0, c2=10 -> c2' = -10. So array becomes [0, -10, 0, 0]. Differences: d1 = -10-0=-10, d2 = 0-(-10)=10, d3 = 0-0=0 => [-10,10,0] which matches [ -10,10,0] i.e., order -10,10,0. That's not our target order 0,10,-10. We need order 0,10,-10. Continue: now we have diffs [-10,10,0]. To get [0,10,-10], we need to swap d2 and d3? Because currently d2=10, d3=0; swapping them gives d2=0, d3=10 -> diffs [-10,0,10]. That's not target yet (target is 0,10,-10). Wait target differences: For t = [0,0,10,0], differences: d1=0-0=0, d2=10-0=10, d3=0-10=-10 => [0,10,-10]. That's different from [-10,0,10]? Actually [-10,0,10] sorted same multiset but order differs. Our current array after one operation is [0,-10,0,0] differences [-10,10,0]. To get [0,10,-10] order, we need diffs [0,10,-10]. How to get that? From [-10,10,0] we could swap d1 and d2 (i=2) again? That would revert. Swap d2 and d3 (i=3): operation at i=3: c3' = c2 + c4 - c3 = (-10)+0-0 = -10. So array becomes [0,-10,-10,0]. Let's compute: c1=0, c2=-10, c3=0, c4=0; operation at i=3: c3' = c2 + c4 - c3 = -10+0-0 = -10. New array: [0,-10,-10,0]. Differences: d1 = -10-0 = -10, d2 = -10 - (-10) = 0, d3 = 0 - (-10) = 10 => [-10,0,10]. Still not [0,10,-10]. Try another swap: Now we have [-10,0,10]. Swap d1,d2 (i=2): operation at i=2: c2' = c1 + c3 - c2 = 0 + (-10) - (-10) = 0? Actually c2=-10, c1=0, c3=-10: c2' = 0 + (-10) - (-10) = 0. Array becomes [0,0,-10,0]. Differences: d1 = 0-0=0, d2 = -10-0=-10, d3 = 0 - (-10)=10 => [0,-10,10]. That's [0,-10,10]? Wait order: d1=0, d2=-10, d3=10 -> that is [0,-10,10]. Not target [0,10,-10]. We need d2=10, d3=-10. So we have [0,-10,10] vs target [0,10,-10]. They differ by swapping d2 and d3. So apply swap at i=3: operation at i=3: c3' = c2 + c4 - c3 = 0 + 0 - (-10) = 10. Array becomes [0,0,10,0] which is exactly target! So indeed we reached target. So condition holds.
Thus it works.
Now, is there any case where the multiset matches but we cannot achieve the target because the permutation of differences needed is odd/even? Since we can do any adjacent swap, all permutations are reachable, regardless of parity. So no.
Thus answer: if c1 == t1 and sorted diffs equal => Yes else No.
But wait: there is a nuance: The operation also preserves something like the sum of c_i * (-1)^i? Let's test: operation at i changes c_i to c_{i-1}+c_{i+1}-c_i. Then the alternating sum S = sum_{j} (-1)^j c_j. Compute change: before: contribution from i-1,i,i+1: (-1)^{i-1}c_{i-1} + (-1)^i c_i + (-1)^{i+1}c_{i+1}. After: (-1)^{i-1}c_{i-1} + (-1)^i (c_{i-1}+c_{i+1}-c_i) + (-1)^{i+1}c_{i+1}. Compute difference: after - before = (-1)^i (c_{i-1}+c_{i+1}-c_i) - (-1)^i c_i = (-1)^i (c_{i-1}+c_{i+1}-2c_i). For i parity, this may be non-zero. So not invariant. But maybe sum of all elements times something else? Not needed.
Thus solution stands.
Now, we should consider memory and speed: n up to 1e5, sorting differences O(n log n), fine.
But maybe there is an even simpler condition: the transformation is linear and we can think of it as the differences can be permuted arbitrarily. So we just need to check that c1==t1 and the multiset of differences match. But is there any scenario where the multiset matches but we cannot achieve target because the operation might not allow arbitrary permutations due to the fact that swapping differences changes the values and maybe some swaps become impossible because after swapping some differences, the new values might cause the same operation to have different effect? But as argued, the operation only depends on current values, and swapping differences is exactly what happens; it doesn't matter what the values are, it will always swap the two differences at that location. So we can always perform adjacent swap as long as we have an index i (2..n-1). After some swaps, the differences at positions i-1 and i might be something else, but operation at i will swap them regardless. So it's always possible to perform the swap. There's no restriction like "you cannot apply operation if the stone is inner" - always allowed. So we can always perform any adjacent swap.
Thus it's correct.
But let's double-check with a small n=3 case more carefully: c = [a,b,c], t = [a, c?]. Actually if c1 = t1 and c3 = t3, and multiset of differences {b-a, c-b} must equal {t2-t1, t3-t2}. Since there are only two differences, the multiset equality means either (d1,d2) = (e1,e2) or (d1,d2) = (e2,e1). And we can achieve the swap via operation at i=2. So condition is correct.
Now, we need to ensure that we consider possibility that after swapping differences, the array may have some large numbers, but that's fine.
Thus implementation:
Pseudo:
int n;
cin>>n;
vector c(n), t(n);
for i in 0..n-1: cin>>c[i];
for i in 0..n-1: cin>>t[i];
if (c[0] != t[0]) { cout<<"No"<<endl; return; }
// optionally check c[n-1] != t[n-1] but not necessary.
vector diff_c(n-1), diff_t(n-1);
for i=0..n-2: diff_c[i] = c[i+1] - c[i];
for i=0..n-2: diff_t[i] = t[i+1] - t[i];
sort(diff_c.begin(), diff_c.end());
sort(diff_t.begin(), diff_t.end());
if (diff_c == diff_t) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
But wait: what about n=2? Then diff arrays of size 1. Sorting works. Also we need to check c1==t1? Already done. The diff comparison ensures c2-c1 == t2-t1, which plus c1==t1 gives c2==t2. So Yes if both conditions.
Now, the problem statement says that the synchronization operation is allowed only for inner stones (2 ≤ i ≤ n-1). For n=2, there is no inner stone, so the only possible transformation is the identity. So our condition gives that.
Thus answer is correct.
Now, we should also consider if there might be any other invariant that we missed. Maybe the operation also preserves the total sum modulo something? But not.
To be thorough, we can derive from the swapping property that the reachable states are exactly those with same c1 and same multiset of differences. But is it possible that we cannot achieve all permutations because each operation also changes the actual values of the stones in a way that restricts future operations? However, we can model the process as: we have a sequence of differences d[1..n-1] and an initial c1. The state is (c1, d[1..n-1]). Operation at i swaps d[i-1] and d[i]. This is exactly the same as having a permutation of the differences, with c1 constant. So any permutation of the differences is reachable because we can apply adjacent transpositions, which generate the symmetric group. So yes.
Thus solution is final.
But wait: Is there any restriction that we cannot apply operation on a stone twice? No, we can apply any number of times.
Thus answer.
Now, I'll produce final solution in C++.
Potential pitfalls: Input size up to 1e5, use fast I/O. Use long long to avoid overflow when computing differences, but int is fine too as discussed. But for safety, use long long.
Edge case: n=1 not possible.
Let's test with some custom cases:
Case 1: n=2, c=[0,0], t=[0,0] => diff_c=[0], diff_t=[0], c1==t1 => Yes.
Case 2: n=2, c=[0,1], t=[0,2] => diff_c=[1], diff_t=[2] not equal => No.
Case 3: n=3, c=[0,5,10], t=[0,5,10] => diffs [5,5] both => Yes (identity).
Case 4: n=3, c=[0,5,10], t=[0,10,5]? But need c3 same? t3=5? Wait t must have length 3. t=[0,10,5] => c1=0==0; diff_c=[5,5]; diff_t=[10,-5] -> sorted [5,5] vs [-5,10] not equal => No. Indeed can we transform? Operation at i=2: c2 becomes 0+10-5=5, unchanged, so array stays [0,5,10]. So cannot get [0,10,5]. So No.
Case 5: n=3, c=[1,2,4], diffs [1,2]; t=[1,3,4], diffs [2,1]; sorted both [1,2] => Yes. Check: start [1,2,4]; operation at i=2: c2 = 1+4-2=3 => [1,3,4]. Yes.
Case 6: n=4, c=[0,0,0,0], diffs [0,0,0]; any t with same c1 and c4? For t=[0,0,0,0] diffs zero => Yes. For t=[0,5, -5,0]? But t_i must be between 0 and 2e9? Actually t_i given as 0 ≤ t_i ≤ 2e9, so no negative. So not needed.
Case 7: n=4, c=[1,2,3,4] diffs[1,1,1]; t=[1,3,2,4] diffs[2,-1,2] sorted [1,1,1] vs [-1,2,2] => No. So cannot.
Now think about possibility that the multiset of differences may be same but c1 differs: we output No due to first check.
Thus solution is correct.
But wait: what about the case where n=2 and c1 == t1 but c2 != t2? diff_c = c2-c1, diff_t = t2-t1, diff_c != diff_t => No.
Thus all good.
I'll provide final answer: compare c1 and sorted diffs.
Now, let's confirm with some random tests via reasoning: For n=5, c = [a,b,c,d,e], t = [a,?,?,?, e] with same diffs multiset. Can we always reorder diffs? Yes.
Thus final.
Now, is there any case where differences multiset equal but we cannot achieve due to parity? Because to achieve a particular permutation, we may need an odd number of swaps. But we can perform any number of swaps, so parity not an issue. Because we can always perform an extra swap on any adjacent pair and then swap back? But that would change the array. Actually to achieve a specific permutation, we need a specific sequence of swaps. But any permutation can be expressed as a sequence of adjacent swaps; we can just execute that sequence. So yes.
But maybe there is a subtlety: The operation at index i swaps d_{i-1} and d_i, but after swapping, the indices of differences shift? No, the differences are attached to positions between stones. The operation does not change the positions of the differences; it swaps the values of two adjacent differences. So the positions are fixed; we have a list of values at fixed positions. Swapping adjacent positions changes the order. So it's exactly like sorting an array via adjacent swaps. So all permutations reachable.
Now, the only invariant is the multiset.
Thus solution is O(n log n) time, O(n) memory.
But we can also check without sorting by using frequency maps (like multiset). Sorting is simplest.
Now, potential pitfalls: Large n, but sorting is fine.
Now, I'll produce the final answer.
But wait, the problem might require an approach with linear time? Possibly, but O(n log n) is fine for 1e5.
Edge: need to use 64-bit for differences to avoid overflow? Actually c_i up to 2e9, differences up to 2e9, fits 32-bit signed. But safe to use long long.
Now, I'll implement.
But I recall that there might be a known Codeforces problem: "Synchronization" or something? Actually this is Codeforces Round # problem? Possibly "Magic Stones" problem? I recall a solution involving differences. Yes, that's typical.
Now, one more nuance: The operation also preserves the sum of all elements? Not necessarily, as we saw, but it preserves the sum of c_1 and c_n? No.
But we have c1 and cn fixed.
Now, also check if there is any condition about the parity of number of operations? But no.
Thus final.
I'll produce final answer in C++.
Now, also consider if the input arrays are equal initially, then obviously Yes. That's covered.
Let's implement:
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector c(n), t(n);
for (int i = 0; i < n; ++i) cin >> c[i];
for (int i = 0; i < n; ++i) cin >> t[i];
if (c[0] != t[0]) {
cout << "No\n";
return 0;
}
vector d1, d2;
for (int i = 0; i < n-1; ++i) {
d1.push_back(c[i+1] - c[i]);
d2.push_back(t[i+1] - t[i]);
}
sort(d1.begin(), d1.end());
sort(d2.begin(), d2.end());
if (d1 == d2) {
cout << "Yes\n";
} else {
cout << "No\n";
}
return 0;
}
But we should also check if n==1? Not needed.
Now, is there any scenario where the differences multiset matches but c1 matches but still impossible because the operation might produce some intermediate stone charges that violate some constraints? The problem does not have constraints on intermediate values; only final target must match. So intermediate values can be anything, no restrictions.
Thus solution is correct.
Now, double-check with a custom case: n=3, c=[0,3,6] diffs [3,3]; t=[0,6,12]? but t must be same length, but c3=6, t3=12, c_n differ, but diffs multiset? diffs_t=[6,6] not equal, so No. But also c1 matches? c1=0=t1=0? Actually t1=0, but c3 != t3, so diffs sum differ. So sorting catches it. So fine.
Now, I'll present the answer.
But before finalizing, I want to think if there could be a scenario where the multiset of differences is same but we cannot achieve due to the fact that c1 and c_n are fixed and maybe the ordering of differences is not independent because the operation might also change c1 or c_n indirectly? No.
Alternatively, maybe there is another invariant: the total sum of all elements modulo something? Let's try to see if sum of all elements changes in a particular way. The operation: c_i' = c_{i-1}+c_{i+1}-c_i. Then new total sum S' = S - c_i + (c_{i-1}+c_{i+1}-c_i) = S + c_{i-1}+c_{i+1} - 2c_i. So change in sum is c_{i-1}+c_{i+1} - 2c_i. This is not invariant, but maybe the sum of all elements with alternating signs? Let's compute alternating sum A = sum (-1)^i c_i. The change at i: A' - A = (-1)^i (c_{i-1}+c_{i+1}-2c_i). Not zero generally.
Thus no further invariants.
Thus answer stands.
But one more thought: Could the operation change the multiset of differences? We proved swapping only swaps two differences, thus the multiset is invariant. That's key.
Thus solution is correct.
Now, I will write final answer accordingly.
But the problem statement says that the synchronization operation is applied to an inner stone, and after that, the stone loses its own charge but acquires the charges from neighboring stones. That's the transformation we used.
Edge case: n=2, no inner stone, so no operation allowed. Our method yields Yes only if arrays are identical. Because c1==t1 and diff arrays equal implies c2==t2. So correct.
Thus answer: "Yes" if c[0]==t[0] and sorted diffs equal, else "No".
I'll produce the final solution.
Now, also note that the problem might have constraints that n can be up to 10^5, so we must avoid O(n^2). Sorting O(n log n) is fine.
Potential alternative O(n) solution using frequency count (like using hashmap) but sorting is simpler.
Now, I'd like to test with a random case: n=5, c = [1,5,2,4,3], t = [1,2,3,4,5]? Compute diffs: c: 4, -3, 2, -1 => [4,-3,2,-1]; t: 1,1,1,1 => [1,1,1,1]. Not equal => No.
c = [1,2,4,7,11] diffs [1,2,3,4]; t = [1,3,5,7,11] diffs [2,2,2,4] sorted => [1,2,3,4] vs [2,2,2,4] not equal => No.
c = [1,2,4,7,11]; t = [1,4,2,7,11] diffs c [1,2,3,4]; t: 3,-2,5,4 => [3,-2,5,4] sorted => [-2,3,4,5] not equal => No.
Now, c = [1,2,4,7,11]; t = [1,4,7,2,11] diffs c [1,2,3,4]; t: [3,3,-5,9] sorted => [-5,3,3,9] not equal.
c = [0,10,0,10,0] diffs [10,-10,10,-10]; t = [0,0,10,0,0] diffs [0,10,-10,0] sorted => [-10,-10,10,10] vs [-10,0,0,10]? Actually t diffs: [0,10,-10,0] sorted => [-10,0,0,10]; c diffs sorted => [-10,-10,10,10]. Not equal.
So no.
Thus the solution is correct.
Now, I'd like to also consider if there is any scenario where the sorted differences are equal but c1 != t1. We already check that, so no.
Now, let's implement.
But wait: what about n=0? Not.
Now, I'll produce final answer.
But before finalizing, I'm thinking about a possible oversight: The operation may be applied any number of times, and we claim that the set of reachable states is exactly those with same c1 and same multiset of differences. However, is it possible to also change c1 by some sequence of operations? Could we maybe cause c1 to change if we perform operations that somehow affect c1 indirectly? For example, if we swap differences, does c1 ever change? No, because c1 is never involved in any operation. The operation only modifies c_i for i between 2 and n-1. So c1 is constant. So that's fixed.
Similarly, c_n is constant.
Thus condition necessary.
Now, is the condition of c1 equality necessary? Suppose we have same multiset but c1 different, could we still transform? Since c1 never changes, we need c1 == t1. So necessary.
Now, what about the possibility that after some swaps, we could change the differences multiset? No, swapping preserves multiset.
Thus condition is both necessary and sufficient.
Now, let's try to prove sufficiency: Given same c1 and same multiset of differences, we need to show we can reorder differences to match target order. Since we can swap any adjacent differences, we can sort the differences to any desired order. Specifically, we can think of the process: start with diff_c. We want to transform to diff_t. Because both have same multiset, there exists a permutation that maps one to the other. Since adjacent swaps generate the symmetric group, we can achieve that permutation. However, we must ensure that the swaps we perform correspond to operations on the array that are valid at each step. But we can simply simulate bubble sort: for each position from 1 to n-1, we can bring the required difference to that position by swapping adjacent differences. For example, to set the first difference to target value, we can find that value in the current diff list (some index j) and bubble it to the front by swapping adjacent differences from j down to 1. Each swap corresponds to an operation at index (position of right difference +1?) Actually, if we have diff array indices 1..(n-1). Swapping d_{k} and d_{k+1} corresponds to operation at stone index k+1. So as we bubble, we can apply those operations. They are always allowed because k+1 is between 2 and n-1 (since k ranges from 1 to n-2). So yes.
Thus sufficiency.
Thus final solution.
Now, one more nuance: When we bubble, the indices of differences shift, but we can keep track of positions relative to original? Not needed for existence.
Thus answer is correct.
Now, I'll write the final solution in C++.
Will also include check for c[n-1] != t[n-1] maybe to quickly reject, but not necessary.
Also note that constraints up to 2e9, so use long long.
Implementation:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<ll> c(n), t(n);
for (int i = 0; i < n; ++i) cin >> c[i];
for (int i = 0; i < n; ++i) cin >> t[i];
if (c[0] != t[0]) {
cout << "No\n";
return 0;
}
vector<ll> d1, d2;
for (int i = 0; i < n-1; ++i) {
d1.push_back(c[i+1] - c[i]);
d2.push_back(t[i+1] - t[i]);
}
sort(d1.begin(), d1.end());
sort(d2.begin(), d2.end());
if (d1 == d2) cout << "Yes\n";
else cout << "No\n";
}
That solves.
But I want to double-check with sample input:
Sample1:
4
7 2 4 12
7 15 10 12
c1=7 ok. d1: 2-7=-5,4-2=2,12-4=8 => [-5,2,8] sorted [-5,2,8]. d2: 15-7=8,10-15=-5,12-10=2 => [8,-5,2] sorted [-5,2,8]. Equal -> Yes.
Sample2:
3
4 4 4
1 2 3
c1=4 vs 1 -> No.
Matches.
Now, just to be thorough, think about potential counterexample where sorted differences equal and c1 equal but still impossible. Could there be a scenario where the operation is not allowed because the stone is "inner"? Always inner as long as 2 ≤ i ≤ n-1. So for n≥3 all indices between. For n=2, no operation. Already covered.
But what about n=1? Not possible.
Thus final.
But I recall a known Codeforces problem: "Magic Stones" (CF Round #1110 B? Actually problem 1110C? Not sure). I think the solution indeed is to compare differences and first elements.
Now, I'll produce final answer.
But wait: The problem statement says "zero or more synchronization operations", so empty sequence allowed.
Thus answer.
Now, I'll output final answer in the appropriate format.
Given the reasoning, I'll respond with solution.
Thus final.
```cpp
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<ll> c(n), t(n);
for (int i = 0; i < n; ++i) cin >> c[i];
for (int i = 0; i < n; ++i) cin >> t[i];
// first stone never changes
if (c[0] != t[0]) {
cout << "No\n";
return 0;
}
// compute adjacent differences
vector<ll> d_c, d_t;
for (int i = 0; i < n-1; ++i) {
d_c.push_back(c[i+1] - c[i]);
d_t.push_back(t[i+1] - t[i]);
}
sort(d_c.begin(), d_c.end());
sort(d_t.begin(), d_t.end());
if (d_c == d_t)
cout << "Yes\n";
else
cout << "No\n";
return 0;
}