Balanced Ternary String

You are given a string ss consisting of exactly nn 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 nn (3≤n≤3⋅1053≤n≤3⋅105, nn is divisible by 33) — the number of characters in ss.

The second line contains the string ss consisting of exactly nn 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 nn is divisible by 33 it is obvious that the answer exists. And it is obvious that there is only one possible answer.

Examples

Input

Copy

3
121

Output

Copy

021

Input

Copy

6
000000

Output

Copy

001122

Input

Copy

6
211200

Output

Copy

211200

Input

Copy

6
120110

Output

Copy

120120

怎么说呢,我一开始是枚举每种情况(蕴含贪心的思想),但是我的逻辑思维比较混乱,代码足足写了近300行,还是没过,借鉴了一下别人的博客,发现别人思路很条理,代码也很简洁,把我的想的各种情况都包含了(向大佬学习)

大神的思路:

具体思路: 我们先统计出每个字符的个数,想一下,除了三个都相等的情况下,这三个中的某一个肯定是大于n/3的,我们就枚举每一个字符。

如果是2多的话,我们就用1和0从前面进行替换。

如果是1多的话,我们就用2和0进行替换,为了保证字典序最小,我们将0从前面进行替换,2从后面进行替换,

如果是0多的话,我们就从后面开始替换,先从2开始,然后再从1开始。

大神代码:


#include

using namespace std;

const int maxn = 3e5+100;

int num[4];

char str[maxn];

int main()

{

    int len;

    scanf("%d",&len);

    scanf("%s",str);

    for(int i=0; itmp)

    {

        for(int i=len-1; i>=0; i--)

        {

            if(str[i]=='0')

            {

                if(num[2]tmp)

                {

                    num[2]++,num[0]--,str[i]='2';

                }

                else if(num[1]tmp)

                {

                    num[1]++,num[0]--,str[i]='1';

                }

            }

        }

    }

    if(num[1]>tmp)

    {

        for(int i=0; itmp)

                {

                    num[0]++,num[1]--,str[i]='0';

                }

            }

        }

        for(int i=len-1; i>=0; i--)

        {

            if(str[i]=='1')

            {

                if(num[2]tmp)

                {

                    num[2]++;

                    num[1]--;

                    str[i]='2';

                }

            }

        }

    }

    if(num[2]>tmp)

    {

        for(int i=0; itmp)

                {

                    num[0]++;

                    num[2]--;

                    str[i]='0';

                }

                else if(num[1]tmp)

                {

                    num[1]++;

                    num[2]--;

                    str[i]='1';

                }

            }

        }

    }

    printf("%s\n",str);

}

 

你可能感兴趣的:(贪心算法)