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++ (GCC 5.4.1) (judge verdict: Time Limit Exceeded)

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
struct S{
	int len;
	char s;
	bool del;
}T[1000010];int tot;
int n;
char str[1000010];
int cnt[233];
int main()
{
	scanf("%s",str+1);
	n=strlen(str+1);
	for(int i=1;i<=n;i++)
		if(str[i]!=str[i-1])
			cnt[str[i]]++;
	char ca='A',cb='B',cc='C';
	if(cnt[ca]>cnt[cb])swap(ca,cb);
	if(cnt[cb]>cnt[cc])swap(cb,cc);
	if(cnt[ca]>cnt[cb])swap(ca,cb);
	tot=1;
	int s1=0,s2=0,s3=0;
	for(int i=1;i<=n;i++)
	{
		if(str[i]==str[i-1])continue;
		if(str[i]==ca){
			if(T[tot].len==1){
				if(T[tot].s==cb)s2++;
				else s3++;
			}
			else if(T[tot].len>1)s1++;
			tot++;
		}
		else{
			T[tot].len++;
			if(T[tot].len==1)
				T[tot].s=str[i];
		}
	}
	if(T[tot].len==1){
		if(T[tot].s==cb)s2++;
		else s3++;
	}
	else if(T[tot].len>1)s1++;
	
	if(s1+s2<s3)
	{
		if(T[tot].s==cc&&T[tot].len==1)
		{
			T[tot].len--;
			s3--;
			cnt[cc]--;
		}
	}
	for(int i=1;i<tot&&s1+s2<s3;i++)
		if(T[i].len==1&&T[i].s==cc)
		{
			s3--;
			if(i!=1&&i!=tot){
				cnt[ca]--;
				T[i].del=true;
			}
			else T[i].len--;
			cnt[cc]--;
		}
	int now=1;
	if(cnt[cb]>cnt[cc]){
		printf("A\n");
		return 0;
	}
	while(cnt[cb]!=cnt[cc])
	{
		if(T[now].del){
			now++;
			continue;
		}
		if(T[now].len<=1)now++;
		else if(T[now].s==cc){
			T[now].len--;
			T[now].s=cb;
			cnt[cc]--;
		}
		else if(T[now].len%2==0)
		{
			T[now].len--;
			cnt[cc]--;
		}
		else now++;
	}
	now=1;
	if(cnt[ca]>cnt[cb]){
		printf("A\n");
		return 0;
	}
	while(cnt[ca]!=cnt[cb])
	{
		if(T[now].del||T[now].len<=1){
			now++;
			continue;
		}
		if(T[now].len>2){
			T[now].len-=2;
			cnt[cb]--;
			cnt[cc]--;
		}
		else if(T[now].len==2&&(now==1||now==tot))
		{
			T[now].del=true;
			cnt[cb]--;
			cnt[cc]--;
		}
	}
	for(int i=1;i<=tot;i++)
	{
		if(T[i].del)continue;
		if(i!=1)printf("%c",ca);
		for(int j=0;j<T[i].len;j++)
		{
			if(j&1)printf("%c",cb+cc-T[i].s);
			else printf("%c",T[i].s);
		}
	}
	printf("\n");
	return 0;
}