A sorting line holds n parcels arranged in a single row, each stamped with a non-negative integer code. A dispatch robot processes the row in repeated steps: on each step, if at least two parcels remain in the row, the robot removes the parcel currently at the front and the parcel currently at the back, writes the front parcel's code directly followed by the back parcel's code — as decimal digit strings, with no separator — to form one merged numeric code, and adds that merged code to a running total. If exactly one parcel remains with nothing left to pair it with, the robot adds that parcel's own code to the total instead, and the row is then empty. This continues, always taking whatever is currently the front and back, until the row has no parcels left. Determine the final total.
The first line contains a single integer n. The second line contains n integers, the codes of the parcels in their original left-to-right order.
A single integer: the final total after processing the entire row as described above.
Example 1
Input
3 15 6 3
Expected
159
Explanation
The front parcel (15) and back parcel (3) are merged first: writing 15's digits then 3's digits gives "153", so 153 is added to the total. Only the middle parcel (6) remains, and since it has nothing left to pair with, it is added directly. The final total is 153 + 6 = 159.
Example 2
Input
4 4 4 4 4
Expected
88
Explanation
First the front (4) and back (4) merge into "44" = 44, added to the total (44). The two remaining parcels, both 4, are now the new front and back; they merge into "44" = 44 again, added to the total. No parcels remain, so the final total is 44 + 44 = 88.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →