Balanced Ternary String
Contest: Codeforces Round 531 (Div. 3)
Difficulty rating: 1500
Tags: greedy, strings

You are given a string $$$s$$$ consisting of exactly $$$n$$$ characters, and each character is either '0', '1' or '2'. Such strings are called ternary strings.

Your task is to replace minimum number of characters in this string with other characters to obtain a balanced ternary string (balanced ternary string is a ternary string such that the number of characters '0' in this string is equal to the number of characters '1', and the number of characters '1' (and '0' obviously) is equal to the number of characters '2').

Among all possible balanced ternary strings you have to obtain the lexicographically (alphabetically) smallest.

Note that you can neither remove characters from the string nor add characters to the string. Also note that you can replace the given characters only with characters '0', '1' and '2'.

It is guaranteed that the answer exists.

Input format:
The first line of the input contains one integer $$$n$$$ ($$$3 \le n \le 3 \cdot 10^5$$$, $$$n$$$ is divisible by $$$3$$$) — the number of characters in $$$s$$$.

The second line contains the string $$$s$$$ consisting of exactly $$$n$$$ characters '0', '1' and '2'.

Output format:
Print one string — the lexicographically (alphabetically) smallest balanced ternary string which can be obtained from the given one with minimum number of replacements.

Because $$$n$$$ is divisible by $$$3$$$ it is obvious that the answer exists. And it is obvious that there is only one possible answer.

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

Example input:
3
121

Example output:
021

Example input:
6
000000

Example output:
001122

Editorial:
Let's count how many characters '0', '1' and '2' we have in the string $$$s$$$ and store it in the array $$$cnt$$$. Also let's count our "goal" array $$$cur$$$. Firstly, the array $$$cur$$$ is $$$[\frac{n}{3}, \frac{n}{3}, \frac{n}{3}]$$$.
The main idea of this problem is a pretty standard lexicographically greedy approach. We go from left to right and try to place the minimum possible character at the current position in such a way that placing this character is not breaking conditions of our problem. How can we apply this approach to this problem?
Firstly, let's define a function $$$need(cnt, cur) = \frac{|cnt_0 - cur_0| + |cnt_1 - cur_1| + |cnt_2 - cur_2|}{2}$$$. What does the value of this function mean? It means the number of replacements we need to reach $$$cur$$$ from $$$cnt$$$. Let $$$initNeed = need(cur, cnt)$$$ at the beginning of the program. This value means the minimum number of replacements to obtain some balanced ternary string. Let's maintain the variable $$$curRep$$$ which initially is $$$0$$$ and means the number of replacements we already made.
So, we iterate over all positions $$$i$$$ from $$$1$$$ to $$$n$$$. Firstly, let's decrease $$$cnt_{s_i}$$$. So the array $$$cnt$$$ maintains the current amount of characters on suffix of the string. Now let's iterate over characters $$$j$$$ from $$$0$$$ to $$$2$$$ and try to place every character. If the current character is needed ($$$cur_j > 0$$$), then let's decrease $$$cur_j$$$ and if the number of replacements will still be minimum possible after such replacement ($$$curRep + need(cnt, cur) + (s_i \ne j) = initNeed$$$) then let's place this character, set $$$curRep := curRep + (s_i \ne j)$$$ and go to the next position. This will form lexicographically minimum possible answer with minimum number of replacements.
There is another (simpler) solution from PikMike, you can call him to explain it, I just will add his code to the editorial.