Awards For Contestants
Contest: Educational Codeforces Round 30
Difficulty rating: 2300
Tags: brute force, data structures, dp

Alexey recently held a programming contest for students from Berland. n students participated in a contest, i-th of them solved ai problems. Now he wants to award some contestants. Alexey can award the students with diplomas of three different degrees. Each student either will receive one diploma of some degree, or won't receive any diplomas at all. Let cntx be the number of students that are awarded with diplomas of degree x (1 ≤ x ≤ 3). The following conditions must hold:

- For each x (1 ≤ x ≤ 3) cntx > 0;
- For any two degrees x and y cntx ≤ 2·cnty.

Of course, there are a lot of ways to distribute the diplomas. Let bi be the degree of diploma i-th student will receive (or  - 1 if i-th student won't receive any diplomas). Also for any x such that 1 ≤ x ≤ 3 let cx be the maximum number of problems solved by a student that receives a diploma of degree x, and dx be the minimum number of problems solved by a student that receives a diploma of degree x. Alexey wants to distribute the diplomas in such a way that:

1. If student i solved more problems than student j, then he has to be awarded not worse than student j (it's impossible that student j receives a diploma and i doesn't receive any, and also it's impossible that both of them receive a diploma, but bj < bi);
2. d1 - c2 is maximum possible;
3. Among all ways that maximize the previous expression, d2 - c3 is maximum possible;
4. Among all ways that correspond to the two previous conditions, d3 - c - 1 is maximum possible, where c - 1 is the maximum number of problems solved by a student that doesn't receive any diploma (or 0 if each student is awarded with some diploma).

Help Alexey to find a way to award the contestants!

Input format:
The first line contains one integer number n (3 ≤ n ≤ 3000).

The second line contains n integer numbers a1, a2, ..., an (1 ≤ ai ≤ 5000).

Output format:
Output n numbers. i-th number must be equal to the degree of diploma i-th contestant will receive (or  - 1 if he doesn't receive any diploma).

If there are multiple optimal solutions, print any of them. It is guaranteed that the answer always exists.

Constraints:
Time limit: 1.0 s
Memory limit: 256.0 MB

Example input:
4
1 2 3 4

Example output:
3 3 2 1

Example input:
6
1 4 3 1 1 2

Example output:
-1 1 2 -1 -1 3

Editorial:
Let's consider naive solution: make three loops to fix amounts of people to get dimplomas of each degree, take the best. Obviously, sorting the scores will regroup optimal blocks for each degree in such a way that they come in segments of initial array.
We tried to make these solutions fail but underestimated the abilities of contestants to optimize this kind of stuff and couple of such made it to the end of contest. :(
To be honest, we just need to get rid of the last loop. Let bi be the difference between ai and ai + 1 (a is sorted, bn - 1 = an - 1). Then let i2 be the position of the last diploma of second degree and cnt1, cnt2 be the amounts of diplomas of the first ans the second degrees. Thus the best position to put the separator between the third degree and no diploma is the postion with the maximum number in array b over segment $$\max(1,\left\lceil \frac{\max(cnt_1,cnt_2)}{2} \right\rceil), i_2 + \min(n - i_2, 2 \cdot \min(cnt_1,cnt_2))$$. This are the borders of possible amount of the dimplomas of the third degree.
Maximum over segment can be implemented with segment tree, sparse table or even naive square matrix with O(n2) precalc time and O(n2) memory.
Overall complexity: O(n2)/$$O(n^2 \log n)$$.