An arcade's bonus-token conveyor carries a line of n multiplier tokens, each showing a positive integer value, moving past a fusion press in order. The press makes exactly one pass over the belt, left to right, and works like this: it opens a new combined token equal to the value of the first token on the belt. For every following token, in order, the press checks whether multiplying that token's value into the currently open combined token would keep the combined token's value at or below the belt's safety ceiling C. If so, it fuses the token in (the combined token's value becomes that product) and moves on. If not, it seals the currently open combined token exactly as it is, opens a brand-new combined token starting from that following token's value, and continues. After the last token has been processed, the press seals whatever combined token is still open.
Report the values of the sealed combined tokens, in the order they were sealed.
Line 1: two integers n C.
Line 2: n integers a_1 a_2 ... a_n, the multiplier tokens in belt order.
The sealed combined-token values, space-separated, in order.
Example 1
Input
4 5 2 2 2 2
Expected
4 4
Explanation
The press opens with 2. The next token is 2: 2*2=4<=5, so it fuses in, giving 4. The next token is 2: 4*2=8>5, so the press seals 4 and opens a new combined token at 2. The last token is 2: 2*2=4<=5, so it fuses in, giving 4, which is then sealed as the final token. The sealed sequence is 4 4.
Example 2
Input
5 10 3 2 4 1 5
Expected
6 4 5
Explanation
Open with 3. Next token 2: 3*2=6<=10, fuse to 6. Next token 4: 6*4=24>10, seal 6 and open a new token at 4. Next token 1: 4*1=4<=10, fuse to 4. Next token 5: 4*5=20>10, seal 4 and open a new token at 5. End of tokens: seal 5. The sealed sequence is 6 4 5.
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 →