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
The first line of the input contains one integer n (3≤n≤3⋅105, 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
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.
Examples
Input
3
121
Output
021
Input
6
000000
Output
001122
Input
6
211200
Output
211200
Input
6
120110
Output
120120
代码
应该是某个代码敲错了,没注意
#include<iostream>
using namespace std;
int arr[1000000];
int main()
{
int n;
cin >> n;
int sum0 = 0;
int sum1 = 0;
int sum2 = 0;
for(int i = 0; i < n; i++)
{
char x;
cin >> x;
arr[i] = x - '0';
//cin >> arr[i];
if(arr[i] == 0) sum0++;
if(arr[i] == 1) sum1++;
if(arr[i] == 2) sum2++;
}
int m = n/3;
if(sum0 > m)
{
if(sum1 >= m)
{
int x = sum0 - m;
int y = sum1 - m;
for(int i = n-1; i >=0; i--)
{
if(arr[i] == 0&&x>0)
{
arr[i] = 2;
x--;
}
if(arr[i] ==1 && y > 0)
{
arr[i] = 2;
y--;
}
}
}
else
{
if(sum2 >= m)
{
int x = sum0 - m;
int y = sum2 - m;
for(int i = n-1; i >=0; i--)
{
if(arr[i] == 0&&x>0)
{
arr[i] = 1;
x--;
}
}
for(int i = 0; i < n; i++)
{
if(arr[i] == 2&&y>0)
{
arr[i] = 1;
y--;
}
}
}
else
{
int x = m - sum1;
int y = m - sum2;
for(int i = n-1; i >=0; i--)
{
if(arr[i]==0&&y>0)
{
arr[i] = 2;
y--;
}
}
for(int i = n-1; i >= 0; i--)
{
if(arr[i] == 0&& x >0)
{
arr[i] = 1;
x--;
}
}
}
}
}
else if(sum0 == m)
{
if(sum1 >= m)
{
int x = sum1 - m;
for(int i = n-1; i >=0; i--)
{
if(arr[i] == 1&&x>0)
{
arr[i] =2;
x--;
}
}
}
else
{
int x = sum2 - m;
for(int i = 0; i < n; i++)
{
if(arr[i] == 2&&x>0)
{
arr[i] = 1;
x--;
}
}
}
}
else
{
if(sum1>=m)
{
if(sum2>=m)
{
int x = sum1-m;
int y = sum2-m;
for(int i = 0; i < n; i++)
{
if(arr[i]==1&&x>0)
{
arr[i] = 0;
x--;
}
if(arr[i]==2&&y>0)
{
arr[i] = 0;
y--;
}
}
}
else
{
int x = m - sum0;
int y = m - sum2;
for(int i = 0; i < n; i++)
{
if(arr[i]==1&&x>0)
{
arr[i] = 0;
x--;
}
}
for(int i = n-1; i >=0; i--)
{
if(arr[i] == 1 && y >0)
{
arr[i] = 2;
y--;
}
}
}
}
else
{
int x = m - sum0;
int y = m - sum1;
for(int i = 0; i < n; i++)
{
if(arr[i] == 2 && x >0 )
{
arr[i] = 0;
x--;
}
}
for(int i = 0; i < n; i++)
{
if(arr[i] == 2 && y >0)
{
arr[i] = 1;
y--;
}
}
}
}
for(int i = 0; i < n; i++)
{
cout << arr[i];
}
return 0;
}