Editorial
The answer is impossible if more than lemon cookies or more than melon cookies are already fixed.
Otherwise, exactly undecided positions must become melon cookies. Changing position from lemon to melon adds cost , so the earliest available positions should be chosen.
The time complexity is per test case and the extra space complexity is .
Solution written by GPT5