Submitted Code

Failed on test case after_contest01.txt added after the contest. Since I passed everything else on my own, my consideration was insufficient. First, divide into cases (A) where the answer is negative and (B) where the answer is non-negative. For (A), greedily take in order of smallest absolute value. Let’s think about case (B). First, prepare two queues.

  • Non-negative values sorted in descending order (e.g., 9 6 5 2 1 0 0)
  • Negative values sorted in ascending order (e.g., -8 -7 -3 2)

Simply use the property that the product of negative numbers is positive. When $K$ or more remain, take two values from each of the two queues and compare their sizes. For example, if $9 \cdot 6 = 54 < -8 \cdot -7 = 56$, take two values from the negative queue. On the other hand, if the non-negative queue is larger, take one value from the non-negative queue. To repeat, note that when taking from the non-negative queue, only one value is sufficient. An example is mentioned in https://twitter.com/shirakia/status/1279773457413074944. after_contest01.txt seems to fall into this case.

After that, when taking from the non-negative queue, take one at a time, and when taking from the negative queue, take them as pairs of two. If during the calculation of (B) it’s impossible to make the answer positive, use the result of (A) as the answer.