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 <bits/stdc++.h>
using namespace std;

#define pii pair<int, int>
#define mp make_pair

const int maxN = 1e6 + 10;

int n;
int cnt[3], p[3];
int b[maxN + 1], a[maxN + 1];
bool flag[maxN + 1];
char s[maxN + 1];

pii num[3];

inline bool comp(int a, int b) { return cnt[a] < cnt[b]; }

inline void build()
{
	int t = n; n = 0;
	for(int i = 1; i <= t; i++)
		if(!flag[i]) a[++ n] = a[i];
	for(int i = 1; i <= n; i++) flag[i] = false;
}

inline void delc()
{
	int l = 1, r;
	while(l <= n)
	{
		if(a[l] == p[0]) l ++;
		if(l > n) break;
		r = l + 1;
		while(a[r] != p[0] && r <= n) r ++;
		r --;
		if(l != r || l == 1 || r == n)
		{
			if(a[l] == p[2] && !flag[l])
			{
				flag[l] = true;
				cnt[ p[2] ] --;
				if(cnt[ p[1] ] == cnt[ p[2] ]) break;
			}
			if(a[r] == p[2] && !flag[r])
			{
				flag[r] = true;
				cnt[ p[2] ] --;
				if(cnt[ p[1] ] == cnt[ p[2] ]) break;
			}
		}
		l = r + 1;
	}
	build();
	if(cnt[ p[1] ] == cnt[ p[2] ]) return;
	for(int i = 1; i < n; i++)
	{
		if(flag[i]) continue;
		if(a[i] == p[0] && a[i + 1] == p[2])
		{
			flag[i] = flag[i + 1] = true;
			cnt[ p[0] ] --; cnt[ p[2] ] --;
			if(cnt[ p[1] ] == cnt[ p[2] ]) break;
		}
	}
	build();
}

inline void get()
{
	if(cnt[ p[0] ] == cnt[ p[1] ]) return;
	for(int i = 1; i < n; i++)
	{
		if(flag[i]) continue;
		bool f1 = (a[i] == p[1] && a[i + 1] == p[2]), f2 = (a[i] == p[2] && a[i + 1] == p[1]);
		if(f1 || f2)
		{
			if(i > 1 && i + 1 < n && a[i - 1] == p[0] && a[i + 2] == p[0]) continue;
			flag[i] = flag[i + 1] = true;
			cnt[ p[1] ] --; cnt[ p[2] ] --;
			if(cnt[ p[0] ] == cnt[ p[1] ]) break;
		}
	}
	build();
}

int main()
{
	scanf("%s", s + 1);
	
	n = strlen(s + 1);
	a[0] = -1;
	for(int i = 1; i <= n; i++)
	{
		a[i] = s[i] - 'A';
		if(a[i] == a[i - 1]) flag[i] = true;
	}
	build();

	for(int i = 1; i <= n; i++) cnt[ a[i] ] ++;
	for(int i = 0; i < 3; i++) p[i] = i;
	sort(p, p + 3, comp);

	if(cnt[ p[1] ] == cnt[ p[2] ]) get();
	else delc(), get();

	for(int i = 1; i <= n; i++) putchar(a[i] + 'A');
	puts("");
	return 0;
}