Problem:
Score : 1500 points

Problem Statement

Given is a string S consisting of A,B, and C.

Consider the (not necessarily contiguous) subsequences x of S that satisfy all of the following conditions:

A, B, and C all occur the same number of times in x.

No two adjacent characters in x are the same.

Among these subsequences, find one of the longest. Here a subsequence of S is a string obtained by deleting zero or more characters from S.

Constraints

1 \leq |S| \leq 10^6

S consists of A,B, and C.

Input

Input is given from Standard Input in the following format:

S

Output

Print one longest subsequence that satisfies the conditions.
If multiple solutions exist, any of them will be accepted.

Sample Input 1

ABBCBCAB

Sample Output 1

ACBCAB

Consider the subsequence ACBCAB of S. It satisfies the conditions and is one of the longest with these properties, along with ABCBCA.
On the other hand, the subsequences ABCBCAB and ABBCCA do not satisfy the conditions.

Sample Input 2

ABABABABACACACAC

Sample Output 2

BABCAC

Sample Input 3

ABCABACBCBABABACBCBCBCBCBCAB

Sample Output 3

ACABACABABACBCBCBCBCA

Sample Input 4

AAA

Sample Output 4

It is possible that only the empty string satisfies the condition.

Solution in C# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;

namespace atCoder
{
    class Program
    {
        static void Main(string[] args)
        {
            var input = Console.ReadLine();
            char prevs = (char)0;
            List<int> listNum = new List<int>();
            int[] Counts = new int[3];
            foreach(var s in input)
            {
                if (s != prevs)
                {
                    int a = (int)s-(int)'A';
                    listNum.Add(a);
                    Counts[a]++;
                    prevs = s;
                }
                
            }


            do
            {
                List<int> CountsList = new List<int>();
                for (int n = 0; n < 3; n++)
                {
                    if (CountsList.Contains(Counts[n]) == false)
                    {
                        CountsList.Add(Counts[n]);
                    }
                }
                var Countsof = CountsList.ToArray();
                Array.Sort(Countsof);
                Array.Reverse(Countsof);

                var minCount = Countsof.Min();
                int notSeekIndexStart = -1;
                int notSeekIndexEnd = -1;
                for (int n= 0;n < listNum.Count;n++)
                {
                    if(Counts[listNum[n]] == minCount)
                    {
                        notSeekIndexEnd = n;
                        if (notSeekIndexStart == -1)
                            notSeekIndexStart = n;
                    }

                }

                var hit = false;
                for (int tp = 0; tp < 2; tp++)
                {
                    foreach (var count in Countsof)
                    {
                        List<int> newlistNum = new List<int>();
                        int prevaddn = -1;
                        for (int c = 0; c < listNum.Count; c++)
                        {


                            int n = listNum[c];
                            if (c >= notSeekIndexStart && c <= notSeekIndexEnd && tp == 0)
                            {
                                newlistNum.Add(n);
                                prevaddn = n;
                                continue;
                            }

                            int prevn = c > 0 ? listNum[c - 1] : -1;
                            int nextn = c < listNum.Count - 1 ? listNum[c + 1] : -1;

                            if (Counts[n] == count && (prevn != nextn || nextn < 0 || prevn < 0))
                            {
                                Counts[n]--;
                                hit = true;
                            }
                            else
                            {
                                if (prevaddn != n)
                                {
                                    newlistNum.Add(n);
                                    prevaddn = n;
                                }
                                else
                                {
                                    Counts[n]--;
                                    hit = true;
                                }

                            }

                        }
                        listNum = new List<int>(newlistNum);
                        if (hit) break;
                    }
                    if (hit) break;
                }


            }
            while (Counts[0] != Counts[1] || Counts[0] != Counts[2] || Counts[1] != Counts[2]);
            StringBuilder sb = new StringBuilder();
            foreach (int n in listNum)
            {

                sb.Append((char)('A' + n));
            }

            Console.WriteLine(sb.ToString());
            
        }


    }
}