Sample Input

Sample Output

Constraints

You may assume that at any given time, the sequence will contain at least 1 number.
The data in the input is guaranteed to be valid, and will always refer to existing positions in the sequence.

In test data worth ~50\%~ of the points, the sequence may contain up to ~30\,000~ numbers at any given moment.
In test data worth ~100\%~ of the points, the sequence may contain up to ~500\,000~ numbers at any given moment.

In test data worth ~100\%~ of the points, the value of any number in the sequence will be in the range ~[-1\,000, 1\,000]~.
In test data worth ~100\%~ of the points, ~M \le 20\,000~, the sum of all inserted values will not exceed ~4\,000\,000~, and the input will not exceed 20MB.