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<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;
int n,pre[1000005],nxt[1000005];
char s[1000005];
bool flag[1000005];
vector<int>BAC,BAB,CAC,ABC,ABA,CBC,ACB,ACA,BCB;
void push(int i)
{
	if(!flag[i])return;
	if(i<1||i>n)return;
	if(s[i]=='A')
	{
		if(s[pre[i]]!=s[nxt[i]])BAC.push_back(i);
		else
		{
			if(s[pre[i]]=='B')BAB.push_back(i);
			else CAC.push_back(i);
		}
	}
	if(s[i]=='B')
	{
		if(s[pre[i]]!=s[nxt[i]])ABC.push_back(i);
		else
		{
			if(s[pre[i]]=='A')ABA.push_back(i);
			else CBC.push_back(i);
		}
	}
	if(s[i]=='C')
	{
		if(s[pre[i]]!=s[nxt[i]])ACB.push_back(i);
		else
		{
			if(s[pre[i]]=='A')ACA.push_back(i);
			else BCB.push_back(i);
		}
	}
}
int main()
{
	scanf("%s",s+1);
	n=(int)strlen(s+1);
	s[0]='D';
	for(int i=1;i<=n;i++)flag[i]=1;
	int last=0;
	for(int i=1;i<=n;i++)
	{
		if(s[i]==s[i-1])
		{
			flag[i]=0;
			continue;
		}
		pre[i]=last;
		nxt[last]=i;
		last=i;
	}
	nxt[last]=n+1;
	pre[n+1]=last;
	int a=0,b=0,c=0;
	for(int i=1;i<=n;i++)
	{
		if(!flag[i])continue;
		push(i);
		if(s[i]=='A')a++;
		if(s[i]=='B')b++;
		if(s[i]=='C')c++;
	}
	while(a!=b||a!=c)
	{
		if(a>=b&&a>=c)
		{
			while(!BAC.empty())
			{
				int node=BAC.back();
				if(flag[node]&&s[pre[node]]!=s[nxt[node]])break;
				BAC.pop_back();
			}
			if(!BAC.empty())
			{
				int node=BAC.back();
				BAC.pop_back();
				flag[node]=0;
				a--;
				pre[nxt[node]]=pre[node];
				nxt[pre[node]]=nxt[node];
				push(pre[node]);
				push(nxt[node]);
			}
			else
			{
				if(b>c)
				{
					while(!BAB.empty())
					{
						int node=BAB.back();
						if(flag[node]&&s[pre[node]]=='B'&&s[nxt[node]]=='B')break;
						BAB.pop_back();
					}
					if(!BAB.empty())
					{
						int node=BAB.back();
						BAB.pop_back();
						a--;
						flag[node]=0;
						pre[nxt[node]]=pre[node];
						nxt[pre[node]]=nxt[node];
						push(pre[node]);
						push(nxt[node]);
						node=pre[node];
						b--;
						flag[node]=0;
						pre[nxt[node]]=pre[node];
						nxt[pre[node]]=nxt[node];
						push(pre[node]);
						push(nxt[node]);
					}
					else
					{
						while(!CAC.empty())
						{
							int node=CAC.back();
							if(flag[node]&&s[pre[node]]=='C'&&s[nxt[node]]=='C')break;
							CAC.pop_back();
						}
						if(!CAC.empty())
						{
							int node=CAC.back();
							CAC.pop_back();
							a--;
							flag[node]=0;
							pre[nxt[node]]=pre[node];
							nxt[pre[node]]=nxt[node];
							push(pre[node]);
							push(nxt[node]);
							node=pre[node];
							c--;
							flag[node]=0;
							pre[nxt[node]]=pre[node];
							nxt[pre[node]]=nxt[node];
							push(pre[node]);
							push(nxt[node]);
						}
					}
				}
				else
				{
					while(!CAC.empty())
					{
						int node=CAC.back();
						if(flag[node]&&s[pre[node]]=='C'&&s[nxt[node]]=='C')break;
						CAC.pop_back();
					}
					if(!CAC.empty())
					{
						int node=CAC.back();
						CAC.pop_back();
						a--;
						flag[node]=0;
						pre[nxt[node]]=pre[node];
						nxt[pre[node]]=nxt[node];
						push(pre[node]);
						push(nxt[node]);
						node=pre[node];
						c--;
						flag[node]=0;
						pre[nxt[node]]=pre[node];
						nxt[pre[node]]=nxt[node];
						push(pre[node]);
						push(nxt[node]);
					}
					else
					{
						while(!BAB.empty())
						{
							int node=BAB.back();
							if(flag[node]&&s[pre[node]]=='B'&&s[nxt[node]]=='B')break;
							BAB.pop_back();
						}
						if(!BAB.empty())
						{
							int node=BAB.back();
							BAB.pop_back();
							a--;
							flag[node]=0;
							pre[nxt[node]]=pre[node];
							nxt[pre[node]]=nxt[node];
							push(pre[node]);
							push(nxt[node]);
							node=pre[node];
							b--;
							flag[node]=0;
							pre[nxt[node]]=pre[node];
							nxt[pre[node]]=nxt[node];
							push(pre[node]);
							push(nxt[node]);
						}
					}
				}
			}
		}
		else if(b>=a&&b>=c)
		{
			while(!ABC.empty())
			{
				int node=ABC.back();
				if(flag[node]&&s[pre[node]]!=s[nxt[node]])break;
				ABC.pop_back();
			}
			if(!ABC.empty())
			{
				int node=ABC.back();
				ABC.pop_back();
				flag[node]=0;
				b--;
				pre[nxt[node]]=pre[node];
				nxt[pre[node]]=nxt[node];
				push(pre[node]);
				push(nxt[node]);
			}
			else
			{
				if(a>c)
				{
					while(!ABA.empty())
					{
						int node=ABA.back();
						if(flag[node]&&s[pre[node]]=='B'&&s[nxt[node]]=='B')break;
						ABA.pop_back();
					}
					if(!ABA.empty())
					{
						int node=ABA.back();
						ABA.pop_back();
						b--;
						flag[node]=0;
						pre[nxt[node]]=pre[node];
						nxt[pre[node]]=nxt[node];
						push(pre[node]);
						push(nxt[node]);
						node=pre[node];
						a--;
						flag[node]=0;
						pre[nxt[node]]=pre[node];
						nxt[pre[node]]=nxt[node];
						push(pre[node]);
						push(nxt[node]);
					}
					else
					{
						while(!CBC.empty())
						{
							int node=CBC.back();
							if(flag[node]&&s[pre[node]]=='C'&&s[nxt[node]]=='C')break;
							CBC.pop_back();
						}
						if(!CBC.empty())
						{
							int node=CBC.back();
							CBC.pop_back();
							b--;
							flag[node]=0;
							pre[nxt[node]]=pre[node];
							nxt[pre[node]]=nxt[node];
							push(pre[node]);
							push(nxt[node]);
							node=pre[node];
							c--;
							flag[node]=0;
							pre[nxt[node]]=pre[node];
							nxt[pre[node]]=nxt[node];
							push(pre[node]);
							push(nxt[node]);
						}
					}
				}
				else
				{
					while(!CBC.empty())
					{
						int node=CBC.back();
						if(flag[node]&&s[pre[node]]=='C'&&s[nxt[node]]=='C')break;
						CBC.pop_back();
					}
					if(!CBC.empty())
					{
						int node=CBC.back();
						CBC.pop_back();
						b--;
						flag[node]=0;
						pre[nxt[node]]=pre[node];
						nxt[pre[node]]=nxt[node];
						push(pre[node]);
						push(nxt[node]);
						node=pre[node];
						c--;
						flag[node]=0;
						pre[nxt[node]]=pre[node];
						nxt[pre[node]]=nxt[node];
						push(pre[node]);
						push(nxt[node]);
					}
					else
					{
						while(!ABA.empty())
						{
							int node=ABA.back();
							if(flag[node]&&s[pre[node]]=='B'&&s[nxt[node]]=='B')break;
							ABA.pop_back();
						}
						if(!ABA.empty())
						{
							int node=ABA.back();
							ABA.pop_back();
							b--;
							flag[node]=0;
							pre[nxt[node]]=pre[node];
							nxt[pre[node]]=nxt[node];
							push(pre[node]);
							push(nxt[node]);
							node=pre[node];
							a--;
							flag[node]=0;
							pre[nxt[node]]=pre[node];
							nxt[pre[node]]=nxt[node];
							push(pre[node]);
							push(nxt[node]);
						}
					}
				}
			}
		}
		else if(c>=a&&c>=b)
		{
			while(!ACB.empty())
			{
				int node=ACB.back();
				if(flag[node]&&s[pre[node]]!=s[nxt[node]])break;
				ACB.pop_back();
			}
			if(!ACB.empty())
			{
				int node=ACB.back();
				ACB.pop_back();
				flag[node]=0;
				c--;
				pre[nxt[node]]=pre[node];
				nxt[pre[node]]=nxt[node];
				push(pre[node]);
				push(nxt[node]);
			}
			else
			{
				if(a>c)
				{
					while(!ACA.empty())
					{
						int node=ACA.back();
						if(flag[node]&&s[pre[node]]=='B'&&s[nxt[node]]=='B')break;
						ACA.pop_back();
					}
					if(!ACA.empty())
					{
						int node=ACA.back();
						ACA.pop_back();
						c--;
						flag[node]=0;
						pre[nxt[node]]=pre[node];
						nxt[pre[node]]=nxt[node];
						push(pre[node]);
						push(nxt[node]);
						node=pre[node];
						a--;
						flag[node]=0;
						pre[nxt[node]]=pre[node];
						nxt[pre[node]]=nxt[node];
						push(pre[node]);
						push(nxt[node]);
					}
					else
					{
						while(!BCB.empty())
						{
							int node=BCB.back();
							if(flag[node]&&s[pre[node]]=='C'&&s[nxt[node]]=='C')break;
							BCB.pop_back();
						}
						if(!BCB.empty())
						{
							int node=BCB.back();
							BCB.pop_back();
							c--;
							flag[node]=0;
							pre[nxt[node]]=pre[node];
							nxt[pre[node]]=nxt[node];
							push(pre[node]);
							push(nxt[node]);
							node=pre[node];
							b--;
							flag[node]=0;
							pre[nxt[node]]=pre[node];
							nxt[pre[node]]=nxt[node];
							push(pre[node]);
							push(nxt[node]);
						}
					}
				}
				else
				{
					while(!BCB.empty())
					{
						int node=BCB.back();
						if(flag[node]&&s[pre[node]]=='C'&&s[nxt[node]]=='C')break;
						BCB.pop_back();
					}
					if(!BCB.empty())
					{
						int node=BCB.back();
						BCB.pop_back();
						c--;
						flag[node]=0;
						pre[nxt[node]]=pre[node];
						nxt[pre[node]]=nxt[node];
						push(pre[node]);
						push(nxt[node]);
						node=pre[node];
						b--;
						flag[node]=0;
						pre[nxt[node]]=pre[node];
						nxt[pre[node]]=nxt[node];
						push(pre[node]);
						push(nxt[node]);
					}
					else
					{
						while(!ACA.empty())
						{
							int node=ACA.back();
							if(flag[node]&&s[pre[node]]=='B'&&s[nxt[node]]=='B')break;
							ACA.pop_back();
						}
						if(!ACA.empty())
						{
							int node=ACA.back();
							ACA.pop_back();
							c--;
							flag[node]=0;
							pre[nxt[node]]=pre[node];
							nxt[pre[node]]=nxt[node];
							push(pre[node]);
							push(nxt[node]);
							node=pre[node];
							a--;
							flag[node]=0;
							pre[nxt[node]]=pre[node];
							nxt[pre[node]]=nxt[node];
							push(pre[node]);
							push(nxt[node]);
						}
					}
				}
			}
		}
	}
	for(int i=1;i<=n;i++)
	  if(flag[i])putchar(s[i]);
	putchar('\n');
	return 0;
}