PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#LCA0000024. Trình soạn thảo có hoàn tác nhiều cấp (Editor)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Trình soạn thảo có hoàn tác nhiều cấp (Editor)

Editor

Source: Baltic Olympiad in Informatics

Version: Phuoc Hung OJ Extended

Problem Statement

Byteasar is developing a text editor with multi-level undo. Initially the editor state is the integer 00. Each operation is one of two types:

  • An edit has ai>0a_i>0. It sets the editor state to aia_i and has level 00.
  • An undo has ai<0a_i<0. Its level is t=−ait=-a_i. It finds the most recent operation that is still active and has level at most t−1t-1, and marks it undone.

Every new operation starts as active. If an undone operation is itself an undo, the operation it had undone must have its active/undone state flipped. The effect propagates until an edit operation is reached.

The editor's current state is the value established by the most recent active edit; if there is none, it is 00. Every undo operation is guaranteed to find a valid target.

Print the editor state after each operation.

Input

  • The first line contains nn.
  • Each of the next nn lines contains a nonzero integer operation aia_i.

Output

Print nn lines; line ii contains the current value after operation ii.

Subtasks

  • Subtask 1 (20 points): n≤30n\le30; 1≤n≤3⋅1051\le n\le3\cdot10^5; −n≤ai≤n-n\le a_i\le n; aie0a_i e0.
  • Subtask 2 (30 points): n≤1000n\le1000; 1≤n≤3⋅1051\le n\le3\cdot10^5; −n≤ai≤n-n\le a_i\le n; aie0a_i e0.
  • Subtask 3 (50 points): 1≤n≤3⋅1051\le n\le3\cdot10^5; −n≤ai≤n-n\le a_i\le n; aie0a_i e0.

Examples

Input

11
1
2
5
-1
-1
-3
4
-2
-1
-1
1

Output

1
2
5
2
1
2
4
2
1
0
1

Explanation

The first operations set values to 11, then 22, then 55. A level-11 undo returns to 22, then another level-11 undo returns to 11. A level-33 undo can later return to 22 under the nested-level rule.