Jumping Choreography

Maintain the minimum total number of jumps for frogs to gather at a tower that also moves, under insertions and deletions.

Hard8MathNumber theoryGreedyNo attempts yetTime limit3sMemory limit512 MB

Problem

A group of frogs performs a line dance. Each frog stands on an integer coordinate, and the dance is a sequence of jumps. The jj-th jump a frog makes has length exactly jj, and the frog picks its direction, left or right, freely. So the first jump has length 1, the second has length 2, and the third has length 3. Nothing restricts the coordinates a jump passes over or lands on, so a frog may visit negative coordinates, and several frogs may stand on the same coordinate.

The dance ends with every frog gathered at the tower position tt, where the frogs stack up. Each frog jumps on its own, and a frog that already stands on tt does not have to jump at all. The cost of the choreography is the total number of jumps made by all frogs, and that total has to be as small as possible.

The king comes to the rehearsal every day and makes one change. He adds a frog, removes a frog, or moves the tower. After each change is applied, report the smallest possible total number of jumps at that moment.

Input

The first line has the number of frogs nn and the initial tower position tt (0n50000 \le n \le 5000, 0t1060 \le t \le 10^6).

The second line has the starting positions p1,,pnp_1, \dots, p_n of the frogs (0pi1060 \le p_i \le 10^6). This line is empty when n=0n = 0.

The third line has the number of changes CC (0C1060 \le C \le 10^6).

Each of the next CC lines holds one change, in one of these three forms (0a1060 \le a \le 10^6).

  • + a adds a frog at position aa.
  • - a removes one frog that started at position aa. At least one frog started at position aa when this change is given.
  • t a moves the tower to position aa.

The king adds or removes a frog at most 5000 times in total.

Output

Apply the CC changes in order. After each change, print the smallest possible total number of jumps on its own line.