You are given a sequence of positive integers a1, a2, ..., an.
While possible, you perform the following operation: find a pair of equal consecutive elements. If there are more than one such pair, find the leftmost (with the smallest indices of elements). If the two integers are equal to x, delete both and insert a single integer x + 1 on their place. This way the number of elements in the sequence is decreased by 1 on each step.
You stop performing the operation when there is no pair of equal consecutive elements.
For example, if the initial sequence is [5, 2, 1, 1, 2, 2], then after the first operation you get [5, 2, 2, 2, 2], after the second — [5, 3, 2, 2], after the third — [5, 3, 3], and finally after the fourth you get [5, 4]. After that there are no equal consecutive elements left in the sequence, so you stop the process.
Determine the final sequence after you stop performing the operation.
Input
The first line contains a single integer n (2 ≤ n ≤ 2·105) — the number of elements in the sequence.
The second line contains the sequence of integers a1, a2, ..., an (1 ≤ ai ≤ 109).
Output
In the first line print a single integer k — the number of elements in the sequence after you stop performing the operation.
In the second line print k integers — the sequence after you stop performing the operation.
Examples
Input
6
5 2 1 1 2 2
Output
2
5 4
Input
4
1000000000 1000000000 1000000000 1000000000
Output
1
1000000002
Input
7
4 10 22 11 12 5 6
Output
7
4 10 22 11 12 5 6
Note
The first example is described in the statements.
In the second example the initial sequence is [1000000000, 1000000000, 1000000000, 1000000000]. After the first operation the sequence is equal to [1000000001, 1000000000, 1000000000]. After the second operation the sequence is [1000000001, 1000000001]. After the third operation the sequence is [1000000002].
In the third example there are no two equal consecutive elements initially, so the sequence does not change.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to simulate the process: repeatedly find the leftmost pair of consecutive equal numbers, replace them with a single number x+1. Continue until no consecutive equal numbers remain.
Constraints: n up to 2e5, ai up to 1e9, but values can increase due to additions. However the process is similar to "addition" like in the game "2048" but merging leftmost pairs. The final sequence length can be small; worst-case if all numbers are equal and power-of-two, you might end up with one number, but merging many can cause many operations? n up to 2e5, operations can be up to n-1 merges (since each reduces length by 1). But straightforward simulation by scanning each time O(n^2) would be too slow.
We need to simulate efficiently. This looks like a classic problem "Final sequence after merging adjacent equal numbers, replacing with x+1, leftmost first". Possibly can be done using a stack or a linked list with careful merging and backtracking? Since leftmost pair chosen each time, we could process left to right with a stack, but careful: when we merge, the new value may become equal to the previous element, which might cause further merging to the left, and that merging should happen immediately because we always pick leftmost pair. Indeed, the process is deterministic: we always look for leftmost equal consecutive pair; after a merge, the leftmost pair could be at the same index or left of the merge point. So essentially it's like scanning left to right and merging when equal, but with a possibility that merging creates a new equality with left neighbor, causing a cascade leftwards.
This is reminiscent of the process of "binary addition" or "canonical form" for such merging rules. In fact, the process is similar to "the state of a system after all possible reductions, where the order is leftmost-first". But note that the process is not confluent? Actually, if you always pick leftmost pair, it's deterministic. The final sequence may be independent of the order? Possibly yes for such additive merging? I'm not sure. But we need to implement as described.
Observations: When we have a sequence, the operation always merges two consecutive equal numbers into one number that is one larger. This is similar to the rules of the "2048" game, but with a leftmost-first merging strategy. Could we model this as a stack where we push numbers, and if the top of stack equals the new number, we pop and push new number+1, and then check again? However, that would correspond to always merging the rightmost pair? Actually, typical stack approach: iterate over the array, maintain a stack of values. For each new element x, while stack not empty and top == x, pop, set x = x+1. Then push x. This simulates merging pairs that become adjacent due to earlier merges, but it processes elements from left to right and merges with the left side immediately. Does that produce the same final sequence as the leftmost-first process? Let's test with example:
Initial: [5,2,1,1,2,2]
Stack process:
Start with empty.
Push 5: stack [5].
Next 2: top 5 !=2, push -> [5,2].
Next 1: top 2 !=1, push -> [5,2,1].
Next 1: top 1 ==1 -> pop, x becomes 2. Now stack [5,2]. Check top 2 ==2? Yes, pop, x becomes 3. Now stack [5]. Check top 5 !=3, push 3 -> [5,3].
Next 2: top 3 !=2, push -> [5,3,2].
Next 2: top 2 ==2 -> pop, x becomes 3. Stack [5,3]. Check top 3 ==3 -> pop, x becomes 4. Stack [5]. top 5 !=4, push -> [5,4].
Result: [5,4] matches final sequence.
Second example: [1e9,1e9,1e9,1e9]
Stack:
Push 1e9 -> [1e9]
Next 1e9: top == -> pop, x=1e9+1 -> stack empty -> push -> [1e9+1]
Next 1e9: top 1e9+1 != 1e9 -> push -> [1e9+1,1e9]
Next 1e9: top ==1e9 -> pop, x=1e9+1 -> stack [1e9+1]; top == (1e9+1) -> pop, x=1e9+2 -> empty -> push -> [1e9+2]
Result [1e9+2] matches.
Third example: [4,10,22,11,12,5,6] all distinct -> stack just pushes all -> same.
Thus stack algorithm yields correct results for these tests. But is it always equivalent to the leftmost-first process? Let's reason.
The leftmost-first process: at each step, find the smallest index i such that a[i] == a[i+1], replace them with a[i]+1 at position i. This effectively merges them, and then the resulting element might equal its left neighbor (i-1) or right neighbor (i+1?) Actually after replacement, the sequence length reduces by 1; the new element is at position i. Its left neighbor (if any) is the previous element at i-1. Its right neighbor is the element that was at i+2 before (since two elements removed). So potential new equality with left or right. But because we always choose leftmost pair, after a merge we should check from the leftmost again. So merges can propagate leftwards. The stack algorithm processes left to right, and when it sees an element, it merges with the stack (which represents the processed prefix after all possible merges that would have occurred according to leftmost-first). The crucial point: when we push a new element onto the stack, we repeatedly merge if top equals new, effectively merging the new element with the rightmost element of the processed prefix, which is exactly the first pair from the left? Let's simulate conceptual equivalency.
Consider the prefix processed so far. In the leftmost-first process, we would have already performed all possible merges within that prefix, resulting in no equal consecutive elements within it. Now we consider the next element x. If x equals the last element of the processed prefix, then that pair is the leftmost pair (since no pairs inside the prefix), so it should be merged immediately. After merging, we get a new value x+1 in place of both, which becomes the new last element of the prefix. This may then equal the previous element (the new last-1) and thus create another leftmost pair (still leftmost because the new equality is earlier than any potential pairs further right). So the merges continue leftwards until no equality. That's exactly the while loop: while stack not empty and top == x: pop, x = x+1, then push x. So the stack algorithm appears to simulate the exact leftmost-first process.
Hence we can implement O(n) time with a stack (or vector) of integers.
But careful: values can get large; each merge increases by 1. In worst case, with 2e5 equal numbers, the final value could be up to 1e9 + log2(n) ? Actually merging equal numbers is like addition of powers: each merge reduces count by 1 and increases value by 1. In worst case, if all numbers are equal to some base b, the process will produce a single number equal to b + log2(n) (if merging pairs repeatedly). Actually let's compute: Starting with m copies of x. The merging is not exactly binary addition because you merge leftmost. But eventually you end with a number x + floor(log2(m))? Not exactly. In example with 4 numbers, we got x+2 (since 2^2 = 4). For 8 numbers all equal, would we get x+3? Possibly yes. Because the process is similar to the binary representation of the count? Actually think: each merge reduces total count by 1 and adds 1 to the value. The sum of values plus the number of elements? There is an invariant? Let's not worry; values can become up to about 1e9 + 18 (since 2e5 ~ 2^18). Actually worst-case could be larger if we merge many times? Each merge increments by 1, and total merges at most n-1, so maximum increment is at most n-1 = 199999. But that would happen if you could merge repeatedly without creating large jumps? Actually each merge increments the value of the new element by 1 relative to the old ones. The maximum possible final value from initial max ai = 1e9, plus at most n-1 = 199999, so about 1e9 + 2e5, which fits in 32-bit signed? 1,000,000,000 + 200,000 = 1,000,200,000, within 2^31-1, but safe to use 64-bit (long long) to avoid overflow if we do something else. But if we start with 1e9 and merge 200k times, could be 1e9+199999, which is < 1.2e9, fits in 32-bit signed int? Actually 2^31-1 = 2,147,483,647, so it's safe. But for safety, we can use long long. However, it's okay.
But wait: In the merging process, numbers can increase further because when you merge two large numbers, you get even larger, and subsequent merges may involve that larger number, potentially adding 1 each time, but the maximum number of increments a particular value can get is limited by the number of merges it participates in. The total number of merges is n-1, each adds 1 to the new value, so the maximum final value is at most max(initial) + (n-1). Indeed, each merge creates a new integer that is one greater than the two merged. So if you start with all numbers equal to M, after merging all, final value is M + number of merges? That would be M + (n-1). But is that always? Let's test with n=4, M=1, merging leftmost: [1,1,1,1] -> merge first two: [2,1,1] -> now we have pair? Actually leftmost pair: 2 and 1 not equal, so next pair is at positions 2 and 3: 1==1 -> merge: [2,2] -> then merge: [3]. That's merges: 3 merges? Actually total merges = 3 (since from 4 to 1). Final value = 3? But M + (n-1) = 1+3=4, not 3. So not that simple. In this case, after first merge we got 2, but then merging two 2's gave 3. The increments: start 1, after first merge we get 2 (increment 1). Then two 2's produce 3 (increment 1). Total increments = 2, but n-1=3. So final value is M + (# of merges that actually increased the maximum value)? Not straightforward. Actually each merge increments the value from x to x+1, but the x's themselves might be results of previous increments. The final value could be M + floor(log2(n))? In n=4, floor(log2(4)) = 2, M + 2 = 3. In n=8, maybe M+3. So it's logarithmic. Because merging equal numbers essentially works like binary addition: the total "mass" (sum of 2^{a_i})? There's an invariant: consider interpreting each number x as representing a "power" of 2? Actually if we treat each occurrence of value x as a "bit" of weight something? In many such merging games, the process resembles adding binary numbers. For example, if we have a list of numbers and we merge two equal ones, it's like adding 1 to the exponent. The final number's value is determined by the maximum exponent reachable. More precisely, if we start with n copies of value v, then after fully merging, the result is v + floor(log2(n)). Because you can pair them up repeatedly. The leftmost-first may produce that result as well. So final values won't be huge. Even if we have varying numbers, the maximum value increase is limited to about log2(n) plus initial max. But could there be a case where merging creates large numbers that then merge with others and cause bigger increments? Suppose we have many numbers arranged to cause repeated merges, like 1,2,1,2,...? Not sure. But the worst-case increment likely bounded by O(log(max value?)). Actually consider sequence: 1,2,3,4,...? No equal. So no merges.
But could we have a sequence where after merging, the value becomes huge? For instance, suppose we have alternating numbers that become equal after previous merges? The increment per merge is 1, but the number of merges is at most n-1, so the maximum possible value is initial max + (n-1). However, earlier we saw with n=4, initial max=1, n-1=3, but final value 3, not 4. So it didn't reach 1+3=4. But could it reach that high? Maybe if merges always involve distinct numbers that become equal only after being increased? Let's attempt to construct: Suppose we have numbers: 1,1,2,2. Process: leftmost pair: first two 1's -> [2,2,2]? Actually [1,1,2,2] -> merge first two 1's -> [2,2,2] -> then leftmost pair: first two 2's -> [3,2] -> no equal? Actually [3,2] no equal, stop. Final values 3 and 2. Max is 3 = 1+2? initial max=2? But we started with max 2? Actually initial max was 2? We had 2,2. Starting max=2, final max=3 = 2+1, not n-1=3. So still limited.
What about: [1,2,1,2,1,2,...] no equal initially. So no merges. So the increment is limited by the number of merges that affect a given number. Each time a number is created by a merge, its value is 1 greater than the merged numbers. The number of merges that a particular value will undergo before being merged again is at most log2(something). But we can't exceed n-1 total increments across all numbers. For any specific number, the maximum value increase is at most log2(n) (since to reach high value, you need many merges that double counts). But maybe a scenario: 1,1,2,3,4,...? Not.
Nevertheless, we can just use long long to be safe.
So the algorithm: read n, read array a. Maintain a vector (stack) res. For each x in a:
while (!res.empty() && res.back() == x) {
res.pop_back();
x++;
}
res.push_back(x);
At the end, output size and elements.
But is this always correct? Let's test some edge cases.
Case: [2,2,1] -> Leftmost-first: first pair indices 1,2: both 2 -> merge to 3 -> sequence becomes [3,1]; no equal, final [3,1]. Stack: push 2 -> [2]; next 2: top==2 -> pop, x=3 -> empty -> push 3 -> [3]; next 1: top 3 !=1 -> push 1 -> [3,1]. OK.
Case: [1,2,2] -> Leftmost pair: indices 2,3: 2==2 -> merge to 3 -> [1,3]; no equal. Stack: push1->[1]; next2: top1!=2 -> push2->[1,2]; next2: top2==2 -> pop, x=3 -> stack[1]; top1!=3 -> push3->[1,3]. OK.
Case: [2,1,1,2] -> Leftmost pair: indices 2,3: 1==1 -> merge to 2 -> [2,2,2]? Actually after merging, we replace indices 2,3 (1,1) with 2: sequence becomes [2,2,2] (positions: original a[1]=2, a[2] and a[3] merged to 2, then a[4]=2). Now leftmost pair: indices 1,2: both 2 -> merge to 3 -> [3,2] -> no equal. So final [3,2]. Stack: push2->[2]; next1: top2!=1->push1->[2,1]; next1: top1==1->pop, x=2 -> stack[2]; top2==2->pop, x=3 -> empty->push3->[3]; next2: top3!=2->push2->[3,2]. OK.
Case: [1,1,1,1] -> Leftmost-first: merge first two: [2,1,1]; leftmost pair now indices 2,3: 1==1 -> merge: [2,2]; merge: [3]. Final [3]. Stack: push1->[1]; next1: top==1->pop, x=2->empty->push2->[2]; next1: top2!=1->push1->[2,1]; next1: top1==1->pop, x=2->stack[2]; top2==2->pop, x=3->empty->push3->[3]. OK.
Thus stack seems to work.
But is there any scenario where leftmost-first would choose a pair not at the boundary between processed prefix and next element? Let's think: After processing prefix, the prefix has no equal consecutive elements. The next element may create an equality with the last element of the prefix. That would be the leftmost pair because all pairs in the prefix are none, and any pair further right hasn't been formed yet because we haven't processed those elements. So indeed the next possible pair is between last of prefix and next element, if they are equal. So the stack algorithm correctly picks leftmost.
But what about merges that propagate leftwards after merging? That's the while loop.
Therefore algorithm is O(n) time and O(n) space (worst-case stack size up to n). However, could there be a scenario where the while loop causes many iterations? Each iteration pops an element, and each element is popped at most once, so total operations O(n). Good.
Now, is there any nuance about leftmost pair when there are multiple equalities after a merge that could also involve the right neighbor? The stack algorithm only checks left neighbor (the processed prefix). But after merging, we don't check the right neighbor because we haven't processed it yet; we will process it when we get to it. However, in the leftmost-first process, after a merge, the new element may become equal to its right neighbor, and if that is to the left of any other pair? But the new element is at position i (the left index of the merged pair). Its right neighbor is the element that was originally at i+2 (since we removed i and i+1). This right neighbor has not been processed yet in our left-to-right scan? Actually in the original sequential process, after merging at i, we need to consider the whole sequence from the beginning again. That includes checking if the new element equals the element at i+1 (the right neighbor). If they are equal, then that would be a pair at indices i and i+1, which is leftmost? Since we would start scanning from index 1. If there is any earlier pair (like at some index < i), that would have been processed first, but we already ensured that the prefix up to i-1 has no equal consecutive pairs? Not necessarily: after the merge, the new element at i could be equal to its left neighbor (i-1) as well, which is at a smaller index. The leftmost-first would check from the smallest index; thus if there is an equality at i-1 and i, it would be before any equality at i and i+1. So the leftmost pair is always either inside the processed prefix (which we already ensured has none) or between the last element of the processed prefix and the new element? But after a merge, the new element becomes part of the processed prefix. So if it becomes equal to its right neighbor (which is not yet processed), that pair will be considered when we later process that right neighbor. However, could that right neighbor be such that the pair (new, right) becomes the leftmost before we process further? Since we always scan from the beginning, after the merge, we would restart scanning from index 1. But if we are simulating with a stack, we don't restart; we continue scanning forward. Would that potentially miss a situation where after merging, the new element equals its right neighbor, and that pair is leftmost (because no earlier pairs), but our algorithm will only consider that pair later when we reach that right neighbor, effectively scanning from left to right anyway. But note: After we merge (pop from stack and increment x) and push x, we haven't yet looked at the next element in the original array. In the actual process, after merging, we would then reexamine the entire sequence, but we could potentially merge with the right neighbor before moving on to later elements. However, our stack algorithm will process the next original element only after handling the current element and possible merges with the stack. This order might not be exactly the same because the actual process might merge the new element with its right neighbor before we even look at the element after that (i.e., i+2). But in our sequential left-to-right processing, when we encounter the element at original index i+2 (the right neighbor), we will then compare it with the top of the stack (which is the merged element). If they are equal, we will merge at that time. But is that equivalent to merging immediately? In the actual process, suppose after merging at i we get new element X at position i. The next element (original i+2) is Y. If X == Y, then there is a pair at positions i and i+1 (since after merge, the length decreased; the element originally at i+2 becomes at i+1). That is the leftmost pair if no earlier equality. The process would then merge X and Y before any further operations involving elements beyond Y. In our stack algorithm, we have just processed original index i+1 (since we merged with the top) and now we are about to process original index i+2. At that point, the stack contains X (as top) and we will compare Y with top. Since X == Y, we will merge them (pop X, increment Y, etc.) before moving on to later indices. So that's exactly what happens: we process original i+2 and merge immediately with the stack top. But what about the fact that in the actual process, after merging at i, we might have to consider if the new element also equals its left neighbor (i-1) before considering its right neighbor? That is covered by the while loop that continues merging with left (stack) side. So the order is: we first resolve any merges with left side, then we move to the next original element (right side). This order might be reversed compared to leftmost-first? Because leftmost-first would check from the beginning; after the merge at i, it would check from index 1. If there is a left neighbor equality (i-1,i), that would be found first because i-1 < i. That's exactly what our while loop does: it checks and merges with left side before we consider the next element. If there is no left equality, then the next candidate is the pair (i, i+1) (i.e., the new element and its right neighbor). In the stack algorithm, after processing the current element (which may have triggered left merges), we then move to the next original element, which corresponds to the right neighbor. At that moment, if there is equality, we will merge (the while loop will again check left side). But is it possible that before we process the right neighbor, there could be another operation that involves elements further to the right (beyond i+2) that become adjacent to something else? No, because the next element we process is the immediate next element in the original order, which is exactly the right neighbor. So that will be considered at the correct time. There is no operation that could involve elements beyond before dealing with the right neighbor because those elements are not adjacent to any pair yet. So the stack algorithm appears to correctly simulate the leftmost-first merging order.
Therefore, the solution is straightforward.
Now, note: The stack algorithm is basically the same as computing the "canonical form" of the sequence under the reduction rule. This is known from Codeforces problem "Merging Arrays" or something similar? Actually there is a problem "D. Merge Equals" from Codeforces? This specific problem might be from a contest, but the solution is indeed a stack.
But we should verify if there is any tricky case where the stack approach fails because leftmost-first might merge a pair that is not the one between the processed prefix and the current element, due to the fact that the processed prefix after previous merges might not be exactly the same as the sequence after all merges up to that point? Let's attempt to construct a counterexample. Suppose we have sequence: [2, 1, 1, 2]. We already tested, stack gives [3,2] correct.
Consider: [1,2,2,1]. Process manually: leftmost pair is at indices 2,3 (2,2) -> merge to 3: sequence becomes [1,3,1]. Now leftmost pair? Check: indices 1,2: 1 and 3 not equal; indices 2,3: 3 and 1 not equal. So final [1,3,1]. Stack: push1->[1]; push2->[1,2]; next2: top2==2 -> pop, x=3 -> stack[1]; top1!=3 -> push3->[1,3]; next1: top3!=1 -> push1->[1,3,1]. OK.
Consider: [1,1,2,2,1,1]. Manual process:
Step1: leftmost pair (1,2) values 1,1 -> merge to 2: sequence = [2,2,2,1,1] (positions: new2 at idx1, then original 2 at idx3? Actually original: idx1=1, idx2=1, idx3=2, idx4=2, idx5=1, idx6=1. After merging first two, we get: [2,2,2,1,1]? Let's do: initial: 1,1,2,2,1,1. Merge idx1-2: new2, then remaining: original idx3=2, idx4=2, idx5=1, idx6=1 => [2,2,2,1,1]. Now leftmost pair: idx1 and idx2 (2,2) -> merge to 3: [3,2,1,1]. Now leftmost pair? idx2 and idx3: 2,1 not equal; idx3 and idx4: 1,1 equal -> merge to 2: [3,2,2]. Now leftmost pair? idx2 and idx3: 2,2 equal -> merge to 3: [3,3]. Then merge to 4: [4]. Final [4]. Stack: let's simulate:
push1 -> [1]
next1: top==1 -> pop, x=2 -> empty -> push2 -> [2]
next2: top2 !=2? Actually top is 2, next element is 2? Wait, sequence: [1,1,2,2,1,1]. After processing first two (we did), we have stack [2]. Next original element: 2. top=2 ==2 => pop, x=3 -> empty -> push3 -> [3]
Next original: 2? Actually after processing third element (the first 2) we merged it and got 3. Now next original element: fourth element is 2. So now stack [3], next =2 -> top3!=2 -> push2 -> [3,2]
Next original: fifth element =1 -> top2!=1 -> push1 -> [3,2,1]
Next original: sixth element =1 -> top1==1 -> pop, x=2 -> stack [3,2]; top2==2 -> pop, x=3 -> stack [3]; top3==3 -> pop, x=4 -> empty -> push4 -> [4]
Result [4]. matches manual.
So stack works.
Thus solution is simple.
But wait, are there any constraints regarding large stack size? Up to 2e5, fine.
Edge Cases: n=2. If equal, merge. stack: push first, second equal -> pop and push x+1. Works.
If all distinct, stack pushes all, final length n.
Now, what about performance of while loop: each iteration pops, and each element is popped at most once, so O(n). However, careful: x can become large, but we only store as integer.
Now, implement:
Read n.
Read array a (list of ints).
Initialize vector stack (or list, but vector with push_back/pop_back is fine).
For each a_i:
long long x = a_i;
while (!stack.empty() && stack.back() == x) {
stack.pop_back();
x++;
}
stack.push_back(x);
After loop, output stack.size() then elements separated by space.
Potential overflow? x can increase multiple times in one while loop. Maximum increment within a single while could be up to the size of the stack, which could be up to n. But the resulting x would be at most 1e9 + 200000, fits in 32-bit signed? 1e9 + 200000 = 1,000,200,000 < 2,147,483,647, so safe for int. But to be safe, use long long or int64_t. Many Codeforces solutions use int, but we can use long long.
Now, consider if the input numbers can be up to 1e9, merging can produce up to 1e9 + log2(2e5) ~ 1e9 + 18, safe for int. But what about if initial numbers are 1e9, and we merge many times? As argued, maximum is at most 1e9 + 199999, still within 2^31-1. So int is safe. However, using long long is fine.
Now, confirm with second example: all 1e9, final 1e9+2, matches.
Thus solution is done.
But wait: The problem statement says "While possible, you perform the following operation: find a pair of equal consecutive elements. If there are more than one such pair, find the leftmost (with the smallest indices of elements)." This is exactly what we simulated.
I recall a Codeforces problem "A. Neko Finds Grapes"? No. Actually there is a problem "C. Merge Equal Numbers" or something. But anyway.
We should also consider if the stack approach might produce a different result if there is a scenario where after merging, the new element equals both left and right, and the leftmost-first would merge with left first, but our algorithm also merges with left first due to while loop. So consistent.
Therefore final answer: implement as described.
Now, we also need to be mindful of input size and output speed. Use fast I/O (ios_base::sync_with_stdio(false); cin.tie(nullptr);). And print newline after output.
Let's test with a more complex random sequence to ensure no issues.
Write a brute-force simulator for small n to compare. But for reasoning, we can think.
One might argue: The stack algorithm is equivalent to always merging the rightmost possible pair? But we've seen it matches leftmost-first. Actually the while loop merges with the immediate left neighbor repeatedly, which corresponds to merging the pair that is at the boundary, which is indeed the leftmost because there is no pair before that (since the prefix has no equal consecutive). So it's correct.
But is there a scenario where the leftmost pair is not at the boundary but somewhere inside the prefix? That would mean the prefix has a pair of equal consecutive elements, which contradicts our invariant that we maintain the prefix with no equal consecutive. So the invariant holds.
Now, the stack algorithm enforces that after processing each element, the stack has no consecutive equal elements. This is exactly the condition we want at the end of the process for the whole sequence. And it's exactly the final sequence because we process all elements. Since the process is deterministic and the stack algorithm mirrors the steps, it should output the correct final sequence.
Double-check with more tricky case: Suppose we have sequence: [2, 2, 2, 2]. Leftmost-first:
Step1: merge first two 2's -> [3, 2, 2]
Step2: now leftmost pair? Check indices: 1 and 2: 3 and 2 not equal; indices 2 and 3: 2 and 2 equal -> merge to 3: [3, 3]
Step3: merge to 4: [4]
Stack: push2 -> [2]; next2: top==2 -> pop, x=3 -> empty -> push3 -> [3]; next2: top3!=2 -> push2 -> [3,2]; next2: top2==2 -> pop, x=3 -> stack[3]; top3==3 -> pop, x=4 -> empty -> push4 -> [4]. Yes.
Case: [1, 1, 2, 2, 3, 3] maybe? Leftmost-first:
Step1: merge 1,1 -> 2: [2,2,2,3,3] (since 1,1 become 2, then rest 2,2,3,3 -> actually original: 1,1,2,2,3,3. After merging first two: new2, then remaining: original 2,2,3,3 => [2,2,2,3,3])
Now leftmost pair: indices 1,2: 2==2 -> merge to 3: [3,2,3,3] (since after merge: new3, then remaining: original third 2? Wait careful: after first merge, sequence: [2 (new), 2 (original third?), 2 (original fourth?), 3,3]. Actually let's index: original: a1=1, a2=1, a3=2, a4=2, a5=3, a6=3.
After merging a1,a2: new b1=2. Now sequence: b1=2, then a3=2, a4=2, a5=3, a6=3 => [2,2,2,3,3].
Now leftmost pair: b1(2) and a3(2) -> merge b1 and a3 to become c1=3. Sequence becomes: c1=3, then the remaining a4=2, a5=3, a6=3 => [3,2,3,3].
Now leftmost pair: check indices: 1 and 2: 3 vs 2 no; 2 and 3: 2 vs 3 no; 3 and 4: 3 vs 3 yes -> merge to 4: [3,2,4]
Now leftmost pair: none (3,2 diff; 2,4 diff). So final [3,2,4].
Stack: push1->[1]; next1: top1==1 -> pop, x=2 -> empty -> push2->[2]; next2: top2==2 -> pop, x=3 -> empty -> push3->[3]; next2: top3!=2 -> push2->[3,2]; next3: top2!=3 -> push3->[3,2,3]; next3: top3==3 -> pop, x=4 -> stack[3,2]; top2!=4 -> push4->[3,2,4]. matches.
Thus all good.
Now, we must produce final answer. Implementation straightforward.
But we need to be careful about using long long for x to avoid overflow if we add many times in one while loop? As argued, maximum value is about initial max + 2e5, which fits in int. But we can use long long.
Now, I will write the solution.
Complexities: O(n) time, O(n) memory.
Double-check: if we use vector, the while loop may cause many pops, but each element popped once.
Potential pitfalls: The condition stack.back() == x compares integer values. If we use int, fine. Use long long for consistency.
Now, let's ensure the output formatting: first line k, second line k integers separated by spaces, with a trailing space? Usually fine. In the examples, there is a trailing space after the last number? They printed "5 4 " (with space after 4). But it's acceptable either way. We'll output without trailing space but with newline.
Now, can we prove the algorithm's correctness formally? Might not be needed but good to have in solution explanation.
Proof sketch:
We maintain a stack S representing the prefix of the original array after processing all merges that would occur in the leftmost-first process up to that point. Invariant: S contains no two equal consecutive elements (since all possible merges within the prefix have been performed). Now consider the next element x. In the original process, since there is no equal consecutive pair within S, the leftmost pair, if any, must involve the last element of S and x (if they are equal) because any pair further right hasn't been formed yet. So we repeatedly merge the pair (last of S, x) when they are equal, each time replacing them with x+1. This is exactly the while loop. After the loop, we push the resulting value onto S, and the invariant holds (no consecutive equal). By induction, after processing all elements, S is the final sequence.
Thus the algorithm is correct.
Now, test with some random small n and compare to brute-force simulation for sanity. I can code a brute for n up to maybe 10. But reasoning seems solid.
Edge Cases: n=2e5, all distinct, stack grows to 2e5; no problem.
Now, but is there any case where the stack approach could get stuck in an infinite loop? No, because while loop reduces stack size and increases x, ensuring termination.
Now, we should consider if the process could have a scenario where after merging, the new x becomes equal to the element that was two positions left? But that would be covered by the while loop because after popping, we compare with the new top, which is the element immediately to the left. So yes.
Thus final answer.
Now, I'll write the solution in C++.
Implementation details:
code
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<long long> stk;
for (int i = 0; i < n; ++i) {
long long x;
cin >> x;
while (!stk.empty() && stk.back() == x) {
stk.pop_back();
++x;
}
stk.push_back(x);
}
cout << stk.size() << "\n";
for (int i = 0; i < (int)stk.size(); ++i) {
if (i > 0) cout << " ";
cout << stk[i];
}
cout << "\n";
return 0;
}
That's it.
But wait: The problem statement says "While possible, you perform the following operation: find a pair of equal consecutive elements. If there are more than one such pair, find the leftmost (with the smallest indices of elements)." The operation is to delete both and insert a single integer x+1 on their place. So the process ends when no consecutive equal elements remain. Our algorithm ensures that. Good.
Now, can we prove that the order of merges doesn't affect final result? Actually the leftmost-first order is specified, but we are simulating it exactly with stack.
One more thought: Could there be a scenario where after merging a pair, the new element becomes equal to an element that is not the immediate left neighbor but some element further left due to earlier merges? That's exactly what the while loop does: after popping the top (immediate left), we compare with new top (next left), and so on. So covered.
Thus solution accepted.
Now, I'll produce final answer with explanation.
But also note: There's a known solution using a stack. Also we might need to handle large input, use fast I/O.
I think it's correct.
Let's test with a few more examples manually:
Case: [1,2,2,1,1,2,2,1]. Could be messy. Let's simulate brute? But trust.
Now, let's try to simulate manually using the described process to see if we get same.
Original: 1,2,2,1,1,2,2,1.
Step1: leftmost pair? Look for consecutive equal: at indices 2-3: 2,2 -> merge to 3 -> new sequence: [1,3,1,1,2,2,1] (since we replaced positions 2,3 with 3; then remaining: original 4=1,5=1,6=2,7=2,8=1). Now sequence: 1,3,1,1,2,2,1.
Step2: leftmost pair? indices 3-4: 1,1 -> merge to 2 -> [1,3,2,2,2,1]? Let's recompute: After step1: indices: 1:1, 2:3, 3:1, 4:1, 5:2, 6:2, 7:1. Merge indices 3-4 (1,1) -> new value 2 at index3. Then remaining: index5 (2), index6 (2), index7 (1) -> sequence: [1,3,2,2,2,1]. Now step3: leftmost pair? Check: idx1-2:1,3 no; idx2-3:3,2 no; idx3-4:2,2 equal -> merge to 3 -> [1,3,3,2,1] (since merging idx3-4: two 2's become 3 at idx3; then remaining idx5 (2), idx6 (1) -> [1,3,3,2,1]. Step4: leftmost pair? idx2-3:3,3 equal -> merge to 4 -> [1,4,2,1] (merge idx2-3: 3,3 become 4 at idx2; then remaining idx4 (2), idx5? Actually after merge: we had indices: 1:1, 2:3, 3:3, 4:2, 5:1. Merge 2-3 -> new value 4 at idx2; remaining idx4 (2), idx5 (1) -> [1,4,2,1]. Step5: leftmost pair? check: 1-4? 1,4 no; 4-2 no; 2-1 no. So final [1,4,2,1]? But stack gave [1,4,1]? Wait stack gave [1,4,1]. So discrepancy: manual process gave [1,4,2,1] vs stack [1,4,1]. Let's double-check my manual process carefully, as I might have made an error.
Original: a = [1,2,2,1,1,2,2,1] (8 elements). We'll carefully simulate leftmost-first.
We'll maintain an array. I'll simulate step by step with indices.
Step 0: [1,2,2,1,1,2,2,1] (len=8)
Step 1: Find leftmost consecutive equal pair. Scan i=1 to len-1:
i=1: 1 and 2 -> not equal.
i=2: 2 and 2 -> equal. So merge indices 2 and 3 (if 1-indexed? Actually careful: indices: 1:1, 2:2, 3:2, 4:1, 5:1, 6:2, 7:2, 8:1. The pair (2,3) both 2. So leftmost pair is at positions 2 and 3. Perform operation: remove those two, insert x+1 = 3 at that position. So new sequence: position 1 stays 1, then new element 3, then the rest from original positions 4..8: [1,1,2,2,1] (since original pos4=1, pos5=1, pos6=2, pos7=2, pos8=1). So after step1: [1,3,1,1,2,2,1] (len=7). Good.
Step 2: Find leftmost equal pair.
i=1: 1 and 3 -> no.
i=2: 3 and 1 -> no.
i=3: 1 and 1 -> yes (positions 3 and 4). So merge them: they are both 1, replace with 2. Remove positions 3 and 4 (the two 1's). New sequence: positions before: 1:1, 2:3; then new element 2; then remaining from original after removal: originally after step1 we had positions: 1:1, 2:3, 3:1, 4:1, 5:2, 6:2, 7:1. So removing positions 3 and 4 leaves: pos1=1, pos2=3, then pos5=2, pos6=2, pos7=1. After insertion, we get: [1,3,2,2,2,1]? Wait: we insert the new element (2) at the location of the removed pair. So the sequence becomes: [1,3] + [2] + [2,2,1] (since the remaining after pos4 are pos5,pos6,pos7: 2,2,1). So we have: 1,3,2,2,2,1. That's length 6. So step2 result: [1,3,2,2,2,1] (len=6). I earlier had [1,3,2,2,2,1]? I wrote [1,3,2,2,2,1]? Actually earlier I wrote [1,3,2,2,2,1]? I wrote after step2: [1,3,2,2,2,1]? Let's see: in my earlier manual, I wrote [1,3,2,2,2,1]? No, I wrote [1,3,2,2,2,1] after step2? I said: [1,3,2,2,2,1]? I had: [1,3,2,2,2,1]? In the text I wrote: "step2: ... -> [1,3,2,2,2,1]" maybe not. I earlier wrote: [1,3,2,2,2,1]? Actually I wrote: "After step1: [1,3,1,1,2,2,1]. Step2: ... merge to 2 -> [1,3,2,2,2,1]". Yes that matches: [1,3,2,2,2,1]. Good.
Step3: Find leftmost equal pair in [1,3,2,2,2,1].
Scan i=1: 1,3 no.
i=2: 3,2 no.
i=3: 2,2 yes (positions 3 and 4). So merge these two 2's into 3. Remove positions 3 and 4 (the two 2's), insert 3. Sequence before removal: positions: 1:1, 2:3, 3:2, 4:2, 5:2, 6:1. Remove 3 and 4 -> remaining: pos1=1, pos2=3, pos5=2, pos6=1. Insert 3 -> sequence: [1,3,3,2,1] (len=5). So step3 result: [1,3,3,2,1].
Step4: Find leftmost equal pair in [1,3,3,2,1].
i=1: 1,3 no.
i=2: 3,3 yes (positions 2 and 3). Merge into 4. Remove positions 2 and 3, insert 4. Remaining: pos1=1, pos4=2, pos5=1. So after insertion: [1,4,2,1] (len=4). Step4 result: [1,4,2,1].
Step5: Find leftmost equal pair in [1,4,2,1].
i=1: 1,4 no.
i=2: 4,2 no.
i=3: 2,1 no.
No pair, stop. So final sequence is [1,4,2,1]. That's length 4.
But stack gave [1,4,1] length 3. So there is a discrepancy! Let's recalc stack simulation for that sequence.
Wait, I earlier said stack gave [1,4,1] but I recalc now gave [1,4,2,1]. Let's double-check step for a7 and a8. I had a6=2, a7=2, a8=1? Actually the original list: indices: 1:1, 2:2, 3:2, 4:1, 5:1, 6:2, 7:2, 8:1. So a6 is 2, a7 is 2, a8 is 1. In my earlier stack simulation, after processing a5 (which was 1), I had stack [1,3,2]. Then I processed a6=2: top=2 ==2 -> pop, x=3; stack becomes [1,3]; top=3 ==3 -> pop, x=4; stack becomes [1]; push4 => [1,4]. Good. Then a7=2: top=4 !=2 -> push2 => [1,4,2]. Then a8=1: top=2 !=1 -> push1 => [1,4,2,1]. So final stack [1,4,2,1], which matches the manual simulation result [1,4,2,1]! Good, so earlier I mistakenly said [1,4,1]. So the stack matches manual.
Thus algorithm correct.
Therefore final solution is as above.
But wait, I recall some Codeforces problems where they ask for final sequence after such merges and the solution is indeed a stack. However, there is nuance: In some versions, they ask to output the final sequence after performing operations "while possible", but they do not specify leftmost-first; they might say "any pair" and the result might be independent, but here leftmost-first is specified. Nevertheless, the stack works for leftmost-first.
Now we should also consider if the algorithm can be implemented with a vector, but also with a linked list? But vector is fine.
Thus final answer.
Now, I'll produce a clear explanation.
But maybe there is a subtlety: When we pop and increment x, we use x = x+1. However, note that after popping, the next top might also be equal to the new x, leading to another merge. But it's possible that after merging two equal numbers, the resulting number could be equal to the number that was two positions to the left, but not to the immediate left? That's what while loop handles.
Thus solution is done.
Now, it's safe.
One more test: n=1? But constraint n>=2, so okay.
Now, I'll produce final output.
But the problem might be from Codeforces and the solution might need to be in C++ (which we are using). Good.
Now, I'll write final answer with explanation. ```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
code
int n;
cin >> n;
vector<long long> stk;
for (int i = 0; i < n; ++i) {
long long x;
cin >> x;
// merge with the left as long as possible
while (!stk.empty() && stk.back() == x) {
stk.pop_back();
++x;
}
stk.push_back(x);
}
cout << stk.size() << "\n";
for (size_t i = 0; i < stk.size(); ++i) {
if (i > 0) cout << " ";
cout << stk[i];
}
cout << "\n";
return 0;