2017-2018 Russian Olympiad in Informatics (ROI 2018), Day 2
A. Addition without carry
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

It's surprising but sometimes we can use the bitwise OR to add positive integers written in the binary notation. For a set of numbers, if there are no two numbers with 1 at the same position (counting positions from the right), then the sum of those numbers is equal to their bitwise OR. Such sets of numbers are called beautiful.

You are given a set of positive integers a1, a2, ..., an. You must build a beautiful set of positive integers b1, b2, ..., bn so that the condition bi ≥ ai holds true, and the sum b1 + b2 + ... + bn is minimized.

Given the binary notation of the numbers a1, a2, ..., an, find the binary notation of the minimum possible value of the sum of numbers in the desired beautiful set b1, b2, ..., bn.

Input

The first line of the input contains an integer n — the size of the given set (2 ≤ n ≤ 300 000). This is also equal to the size of the sought set (bi).

The i-th of the next n lines contains a binary notation of a positive integer ai. The numbers do not contain leading zeros, and the total length of their binary notations does not exceed 300 000.

Output

Print the binary notation of the minimum possible sum of numbers in the desired beautiful set b1, b2, ..., bn. Don't print leading zeros.

Scoring

Let max L be the maximum length of a binary notation of ai.

You will get points for a group if you pass all tests in this group and all groups listed in the dependencies. The group 0 denotes the sample test(s) from the statement.

Examples
Input
2
10
10
Output
110
Input
2
10100
1001
Output
11101
Input
3
1
1
110
Output
1011

B. Decryption
time limit per test
1 second
memory limit per test
512 megabytes
input
standard input
output
standard output

If you keep the first and the last letter of a word unchanged and rearrange other letters arbitrarily, the resulting word should still be readable. Tihs is bcuseae the huamn mnid deos not raed ervey lteter by istlef, but the wrod as a wlohe.

We will explore a similar phenomenon with sequences of sorted numbers after scrambling elements in the middle.

Let's call a sequence of integers correct if the first number is the smallest in the sequence and the last number — largest. For example, sequences $$$[1, 3, 2, 4]$$$ and $$$[1, 2, 1, 2]$$$ are correct, while $$$[1, 3, 2]$$$ and $$$[2, 1, 2]$$$ are not.

There is a sequence of $$$n$$$ numbers $$$a_1, a_2, \ldots, a_n$$$. We want to split it into the minimum number of correct segments. For example, the sequence $$$[2, 3, 1, 1, 5, 1]$$$ can be splitted into three correct segments: $$$[2, 3]$$$, $$$[1, 1, 5]$$$ and $$$[1]$$$.

Determine the minimum possible number of correct segments that the sequence can be split into.

Input

The first line of the input contains an integer $$$n$$$ ($$$1 \le n \le 300\,000$$$) — the size of the sequence.

The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ — the elements of the sequence ($$$1 \le a_i \le 10^9$$$).

Output

Print one integer — the minimum possible number of correct segments that the sequence can be split into.

Scoring

$$$$$$ \begin{array}{|c|c|c|c|} \hline \text{Subtask} & \text{Points} & \text{Constraints} & \text{Dependencies} \\ \hline 1 & 30 & n \le 500 & 0 \\ \hline 2 & 30 & n \le 5\,000 & 0, 1 \\ \hline 3 & 40 & n \le 300\,000 & 0, 1, 2 \\ \hline \end{array}$$$$$$

You will get points for a group if you pass all tests in this group and all groups listed in the dependencies. The group $$$0$$$ denotes the sample test(s) from the statement.

Examples
Input
5
5 4 3 2 1
Output
5
Input
4
1 3 2 4
Output
1
Input
6
2 3 1 1 5 1
Output
3

C. Quick sort
time limit per test
1 second
memory limit per test
512 megabytes
input
standard input
output
standard output

The scientists invented the new UL-2018 processor that is able to quickly process arrays in some special way. A key feature of its architecture is the separation operation of a segment of the array.

Consider an array $$$[a_1, a_2, \ldots, a_n]$$$. The separation operation is characterized by two integers $$$L$$$ and $$$R$$$. Let $$$S(L, R)$$$ denote the operation of the segment $$$[a_L, a_{L+1}, \ldots, a_R]$$$. Executing the $$$S(L, R)$$$ operation reorders the elements in this segment as follows. First, the elements $$$a_{L+1}, a_{L+3}, a_{L+5}, \ldots$$$ are taken, that is elements $$$a_i$$$ that $$$i - L$$$ is odd. Their relative order remains unchanged. Then, the elements $$$a_L, a_{L+2}, a_{L+4}, \ldots$$$ are taken ($$$a_i$$$ that $$$i - L$$$ is even). These retain the relative order too.

For example, consider the array $$$[2, 4, 1, 5, 3, 6, 7, 8]$$$. Applying the operation $$$S(2, 6)$$$ changes the segment $$$[4, 1, 5, 3, 6]$$$ into $$$[1, 3, 4, 5, 6]$$$, so the entire array becomes $$$[2, 1, 3, 4, 5, 6, 7, 8]$$$.

To demonstrate the capabilities of the new processor, the scientists decided to use separation operations to sort an array of distinct numbers. You will be given an array of size $$$n$$$ ($$$1 \le n \le 3\,000$$$) with distinct numbers from $$$1$$$ to $$$n$$$. You must sort it in ascending order using no more than $$$15\,000$$$ separation operations.

For example, the mentioned array $$$[2, 4, 1, 5, 3, 6, 7, 8]$$$ can be sorted in two separation operations. First, apply $$$S(2, 6)$$$ to get $$$[2, 1, 3, 4, 5, 6, 7, 8]$$$, and then the operation $$$S(1, 2)$$$ makes the array sorted increasingly: $$$[1, 2, 3, 4, 5, 6, 7, 8]$$$.

Your task is to write a program that for a given array determines the sequence of at most $$$15\,000$$$ separation operations, after which the specified array will be sorted in ascending order. It isn't necessary to minimize the number of operations performed (just use at most $$$15\,000$$$).

Input

The first line of the input contains an integer $$$n$$$ — the number of elements in the given array ($$$1 \le n \le 3000$$$).

The second line contains $$$n$$$ distinct integers $$$a_1, a_2, \ldots, a_n$$$ — the elements of the given array ($$$1 \le a_i \le n$$$, all $$$a_i$$$ are different).

Output

The first line of the output should contain an integer $$$k$$$ — the number of separation operations performed ($$$0 \le k \le 15\,000$$$).

In the following $$$k$$$ lines, you should print the description of the operations, in the order they are executed. Each operation is described by two integers $$$L$$$ and $$$R$$$ — the beginning and the end of the segment to which the separation operation should be applied ($$$1 \le L \le R \le n$$$).

Note that you don't have to minimize the number $$$k$$$. You can output any sequence of separation operations that contains at most $$$15\,000$$$ operations and sorts the given array in ascending order. It is guaranteed that at least one such sequence exists.

Scoring

$$$$$$ \begin{array}{|c|c|c|c|} \hline \text{Subtask} & \text{Points} & \text{Constraints} & \text{Dependencies} \\ \hline 1 & 20 & n \le 100; \ \text{there is a solution with } k = 1 & \\ \hline 2 & 30 & n \le 100 & 0, 1 \\ \hline 3 & 30 & n \le 1\,000 & 0-2 \\ \hline 4 & 20 & n \le 3\,000 & 0-3 \\ \hline \end{array}$$$$$$

You will get points for a group if you pass all tests in this group and all groups listed in the dependencies. The group $$$0$$$ denotes the sample test(s) from the statement.

Examples
Input
5
3 1 4 2 5
Output
1
1 5
Input
8
2 4 1 5 3 6 7 8
Output
2
2 6
1 2
Input
2
2 1
Output
3
1 1
2 2
1 2
Note

The second sample test is analyzed in detail in the problem statement.

In the third sample test, there is a solution with a single separation operation, but since you don't have to minimize the number of operations, the provided output is also correct.

D. Robomarathon
time limit per test
1 second
memory limit per test
512 megabytes
input
standard input
output
standard output

There are n robots participating in the robomarathon, numbered from 1 to n. Robots must cover the same distance, moving along adjacent tracks, each with a width of 1 meter. The i-th robot is in the i-th track and it needs ai seconds to cover the required distance and get to the finish line.

A signal device is installed at the start point of each robot. To make the competition less predictable, the judges decided to turn some devices off before the contest. The remaining devices are active. There must be at least one active device.

When the chief judge starts the robomarathon, all active devices send a signal. A robot begins to move at the moment when it gets a signal from some active device. The signal propagates at the rate of 1 meter per second. The distance between tracks i and j is |i - j| meters.

Let xi denote the distance from the i-th track to the nearest track with an active device. The i-th robot will start moving xi seconds after the start and get to the finish line in ai seconds, so it will finish in fi = xi + ai seconds after the start of the robomarathon.

Let ki be the number of robots that finished strictly before the i-th robot. The place of this robot in the robomarathon is ki + 1. If multiple robots finish at the same time, then all of them have the place k + 1 if k robots have finished before that.

Consider the following example. Let n = 3, a1 = 2, a2 = 3 and a3 = 5. If the only active signal device is in the third track, the first robot starts moving 2 seconds after the start and finishes at f1 = 2 + 2 = 4. The second robot starts moving 1 seconds after the start, f2 = 1 + 3 = 4. The third robot starts moving immediately at the start, f3 = 0 + 5 = 5. Robots 1 and 2 share the first place, while robot 3 takes the third place. If all three devices work correctly, robots finish with times f1 = 2, f2 = 3, f3 = 5. Robot 1 has the first place, robot 2 the second place, and robot 3 the third place.

As you can see from the example, the leaderboard depends on which devices are active. You will be asked to answer one of two possible questions:

  1. For each robot, determine the minimum place it can get.
  2. For each robot, determine the maximum place it can get.

Your task is to write a program that, given the times ai and the type of the request, determines the corresponding best/worst place each robot can take.

Input

The first line of the input contains two integers n and p (1 ≤ n ≤ 400 000, 1 ≤ p ≤ 2) — the number of robots and the type of the request. The value p = 1 means you need to determine the minimum place a robot can take, while p = 2 means you need to determine the maximum place a robot can take.

The second line contains n integers a1, a2, ..., an — the time each robot needs to cover the distance to the finish line (1 ≤ ai ≤ 109).

Output

Print n lines. The i-th line of the output should contain one integer — the minimum or the maximum place of the i-th robot, as the value p specifies.

Scoring

The subtask 3 includes 30 tests, each independently scored 1 point if correct. You get 0 points for them anyway if you don't get the full score in subtask 1.

The subtask 4 includes 50 tests, each independently scored 1 point if correct. You get 0 points for them anyway if you don't get the full score in subtask 2.

The values of n for all tests in subtask 3 are shown in the following table.

The values of n for all tests in subtask 4 are shown in the following table.

Examples
Input
5 1
8 5 5 7 7
Output
3
1
1
2
1
Input
5 2
8 5 5 7 7
Output
5
3
2
4
5