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

#BS0000228. Ngăn ngập hồ (Avoid Flood in The City)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Ngăn ngập hồ (Avoid Flood in The City)

Avoid Flood in The City

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

There are 10910^9 lakes numbered from 11 through 10910^9, all initially empty. The array a1,…,ana_1,\ldots,a_n describes the weather for nn days.

On day ii, a positive aia_i fills lake aia_i; rain on an already full lake causes a flood. When ai=0a_i=0, no rain occurs and exactly one lake must be drained. Draining leaves the chosen lake empty even if it was already empty.

Find a drainage schedule that prevents every flood throughout the period. Any valid schedule is acceptable.

Input

The first line contains the integer nn. The second line contains nn integers a1,…,ana_1,\ldots,a_n in chronological order.

Output

If no schedule exists, print IMPOSSIBLE. Otherwise print nn integers b1,…,bnb_1,\ldots,b_n on one line. Use bi=−1b_i=-1 on rainy days and 1≤bi≤1091\le b_i\le 10^9 on dry days to identify the lake drained.

Subtasks

  • Subtask 1 (20 points): 1≤n≤1001\le n\le100; 0≤ai≤1090\le a_i\le10^9 for every 1≤i≤n1\le i\le n.
  • Subtask 2 (30 points): 1≤n≤1051\le n\le10^5; 0≤ai≤20\le a_i\le2 for every 1≤i≤n1\le i\le n.
  • Subtask 3 (50 points): 1≤n≤1051\le n\le10^5; 0≤ai≤1090\le a_i\le10^9 for every 1≤i≤n1\le i\le n.

Examples

Input

6
1 2 0 0 2 1

Output

-1 -1 2 1 -1 -1

Explanation

Days 1 and 2 fill lakes 1 and 2. Drain lake 2 on day 3 and lake 1 on day 4. Both are empty before their next rains on days 5 and 6. Reversing the two drainage choices also works.

Input

5
1 2 0 1 2

Output

IMPOSSIBLE

Explanation

Both lakes are full after day 2. The sole dry day, day 3, cannot empty both before their next rains.