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++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
#include<set>
#include<string>
#include<queue>
#include<stack>
#include<complex>
#include<numeric>
using namespace std;
#define MOD 1000000007
#define MOD2 998244353
#define PRIM 3
#define INF (1<<29)
#define LINF (1LL<<60)
#define EPS (1e-10)
#define PI 3.1415926535897932384626433832795028
typedef long long Int;
typedef pair<Int, Int> P;
typedef long double Real;
typedef complex<Real> CP;

struct BidirectionalList;

set<BidirectionalList*> pos[4][4][4];
int cnt[3];

struct BidirectionalList{
    BidirectionalList *prev, *next;
    int val;

    BidirectionalList(int v):val(v), prev(nullptr), next(nullptr){}
    BidirectionalList *remove(){
        auto n = this->next, p = this->prev;
        if(n)n->prev = p;
        if(p)p->next = n;
        return n;
    }

    void insert(BidirectionalList &new_next){
        auto n = this->next;
        if(n)n->prev = &new_next;                   
        new_next.next = n;
        this->next = &new_next;
        new_next.prev = this;
    }

    void set_triple(){
        if(val < 3)
            pos[prev->val][val][next->val].insert(this);
    }

    void unset_triple(){
        if(val < 3)
            pos[prev->val][val][next->val].erase(this);
    }
};

// remove b
bool check(int a, int b, int c){
    if(pos[a][b][c].empty())return false;
    if(a != c){
        auto tmp = *pos[a][b][c].begin();pos[a][b][c].erase(pos[a][b][c].begin());
        auto apos = tmp->prev;
        auto cpos = tmp->next;
        apos->unset_triple();
        cpos->unset_triple();
        tmp->unset_triple();

        tmp->remove();

        apos->set_triple();
        cpos->set_triple();
        cnt[b]--;
    }
    if(a == c){
        auto tmp = *pos[a][b][c].begin();pos[a][b][c].erase(pos[a][b][c].begin());
        auto apos = tmp->prev;
        auto cpos = tmp->next;
        auto cnextpos = cpos->next;
        apos->unset_triple();
        cpos->unset_triple();
        cnextpos->unset_triple();
        tmp->unset_triple();

        tmp->remove();
        cpos->remove();

        apos->set_triple();
        cnextpos->set_triple();
        cnt[b]--;
        cnt[c]--;
    }
    return true;
}

int main(){
    string s;
    cin >> s;
    BidirectionalList start(3), end(3);
    start.insert(end);
    auto last = &start;
    for(char c:s){
        if(c-'A' == last->val)continue;
        BidirectionalList *tmp = new BidirectionalList(c-'A');       
        last->insert(*tmp);
        last = tmp;
    }

    int n = s.size();
    for(auto it = start.next;it != &end;it = it->next){
        cnt[it->val]++;
        it->set_triple();
    }

    if(cnt[0] == 0 || cnt[1] == 0 || cnt[2] == 0){
        cout << endl;
        return 0;
    }

    while(cnt[0] != cnt[1] || cnt[1] != cnt[2]){
        int ord[] = {0, 1, 2};
        if(cnt[ord[0]] < cnt[ord[1]])swap(ord[0], ord[1]);
        if(cnt[ord[1]] < cnt[ord[2]])swap(ord[1], ord[2]);
        if(cnt[ord[0]] < cnt[ord[1]])swap(ord[0], ord[1]);
        if(check(3, ord[0], ord[1]))continue;
        if(check(3, ord[0], ord[2]))continue;
        if(check(ord[1], ord[0], 3))continue;
        if(check(ord[2], ord[0], 3))continue;
        if(check(ord[2], ord[0], ord[1]))continue;
        if(check(ord[1], ord[0], ord[2]))continue;
        if(check(ord[0], ord[2], ord[0]))continue;
        if(check(ord[2], ord[0], ord[2]))continue;
        if(n-- == 0)break;
    }

    for(auto it = start.next;it != &end;it = it->next){
        cout << char('A' + it->val);
    }cout << endl;

    
    return 0;
}