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: Accepted)

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



class Myon
{
    public Myon() { }
    public static int Main()
    {
        cin = new Scanner();
        new Myon().calc();
        return 0;
    }

    static Scanner cin;


    int N;
    int[] num;
    int[] preP;
    int[] nextP;
    bool[] used;

    int[] cnt;

    Queue<int>[,] q;

    void calc()
    {
        string S = cin.next();
        List<int> numL = new List<int>();
        char pre = 'D';
        for (int i = 0; i < S.Length; i++)
        {
            if (S[i] == pre) continue;
            pre = S[i];
            numL.Add((int)(S[i] - 'A'));
        }

        N = numL.Count;
        num = numL.ToArray();

        q = new Queue<int>[3, 3];
        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                q[i, j] = new Queue<int>();
            }
        }

        preP = new int[N];
        nextP = new int[N];

        cnt = new int[3];
        used = new bool[N];

        for (int i = 0; i < N; i++)
        {
            preP[i] = i - 1;
            nextP[i] = i + 1;
            SetQ(i);

            cnt[num[i]]++;
        }

        StringBuilder sb = new StringBuilder();

        while (cnt[0] != cnt[1] || cnt[0] != cnt[2])
        {
            int[] rank = new int[3] { 0, 1, 2 };
            if (cnt[rank[0]] < cnt[rank[1]]) swap(ref rank[0], ref rank[1]);
            if (cnt[rank[1]] < cnt[rank[2]]) swap(ref rank[1], ref rank[2]);
            if (cnt[rank[0]] < cnt[rank[1]]) swap(ref rank[0], ref rank[1]);

            //Console.ReadLine();
            //Console.WriteLine(cnt[0] + " " + cnt[1] + " " + cnt[2]);

            if (cnt[rank[2]] < cnt[rank[1]])
            {
                //01, 00, 02
                bool ok = false;
                int a, b;
                a = rank[0]; b = rank[1];
                while (!ok && q[a, b].Count > 0) { if (CheckAndDelete(q[a, b].Dequeue(), a, b)) ok = true; }
                a = rank[0]; b = rank[0];
                while (!ok && q[a, b].Count > 0) { if (CheckAndDelete(q[a, b].Dequeue(), a, b)) ok = true; }
                a = rank[0]; b = rank[2];
                while (!ok && q[a, b].Count > 0) { if (CheckAndDelete(q[a, b].Dequeue(), a, b)) ok = true; }
            }
            else if (cnt[rank[0]] > cnt[rank[1]])
            {
                //00, 01, 02

                bool ok = false;
                int a, b;
                a = rank[0]; b = rank[0];
                while (!ok && q[a, b].Count > 0) { if (CheckAndDelete(q[a, b].Dequeue(), a, b)) ok = true; }
                a = rank[0]; b = rank[1];
                while (!ok && q[a, b].Count > 0) { if (CheckAndDelete(q[a, b].Dequeue(), a, b)) ok = true; }
                a = rank[0]; b = rank[2];
                while (!ok && q[a, b].Count > 0) { if (CheckAndDelete(q[a, b].Dequeue(), a, b)) ok = true; }
            }
            else
            {
                //01, 00, 02

                bool ok = false;
                int a, b;
                a = rank[0]; b = rank[1];
                while (!ok && q[a, b].Count > 0) { if (CheckAndDelete(q[a, b].Dequeue(), a, b)) ok = true; }
                a = rank[0]; b = rank[0];
                while (!ok && q[a, b].Count > 0) { if (CheckAndDelete(q[a, b].Dequeue(), a, b)) ok = true; }
                a = rank[0]; b = rank[2];
                while (!ok && q[a, b].Count > 0) { if (CheckAndDelete(q[a, b].Dequeue(), a, b)) ok = true; }
            }
        }

        int start = N;
        for (int i = 0; i < N; i++)
        {
            if(!used[i])
            {
                start = i;
                break;
            }
        }
        while(start != N)
        {
            sb.Append((char)(num[start] + 'A'));
            start = nextP[start];
        }


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

    bool CheckAndDelete(int p, int a, int b)
    {
        if (used[p]) return false;
        if (num[p] != a) return false;
        int g = getQ(p);
        if (g != b) return false;

        int next = nextP[p];

        Delete(p);
        if (a != b) Delete(next);

        return true;
    }

    void Delete(int p)
    {
        int pre = preP[p];
        int next = nextP[p];
        if(pre != -1)
        {
            nextP[pre] = next;
            SetQ(pre);
        }
        if(next != N)
        {
            preP[next] = pre;
            SetQ(next);
        }

        used[p] = true;
        cnt[num[p]]--;
        return; 
    }

    void SetQ(int i)
    {
        int p = getQ(i);
        q[num[i], p].Enqueue(i);
    }

    int getQ(int i)
    {
        int l, r;
        if (preP[i] == -1) l = -1;
        else l = num[preP[i]];
        if (nextP[i] == N) r = -2;
        else r = num[nextP[i]];

        if (l == r) return l;
        else return num[i];
    }


    //swap
    void swap<T>(ref T a, ref T b)
    {
        T c = a;
        a = b;
        b = c;
    } 


}



class Scanner
{
    string[] s;
    int i;

    char[] cs = new char[] { ' ' };

    public Scanner()
    {
        s = new string[0];
        i = 0;
    }

    public string next()
    {
        if (i < s.Length) return s[i++];
        string st = Console.ReadLine();
        while (st == "") st = Console.ReadLine();
        s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries);
        if (s.Length == 0) return next();
        i = 0;
        return s[i++];
    }

    public int nextInt()
    {
        return int.Parse(next());
    }
    public int[] ArrayInt(int N, int add = 0)
    {
        int[] Array = new int[N];
        for (int i = 0; i < N; i++)
        {
            Array[i] = nextInt() + add;
        }
        return Array;
    }

    public long nextLong()
    {
        return long.Parse(next());
    }

    public long[] ArrayLong(int N, long add = 0)
    {
        long[] Array = new long[N];
        for (int i = 0; i < N; i++)
        {
            Array[i] = nextLong() + add;
        }
        return Array;
    }

    public double nextDouble()
    {
        return double.Parse(next());
    }


    public double[] ArrayDouble(int N, double add = 0)
    {
        double[] Array = new double[N];
        for (int i = 0; i < N; i++)
        {
            Array[i] = nextDouble() + add;
        }
        return Array;
    }
}