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;
const int N = 2e6+5;
int s[N],n;char tmple[N];
int cnt[3];
int p[3]={0,1,2};

inline void _swap(int x,int y){
	swap(p[x],p[y]);
	for(int i=1;i<=n;i++){
		if(s[i]==x)s[i]=y;
		else if(s[i]==y)s[i]=x;
	}
}

int b1,b2,c1,c2;
vector<int> pc2;bool ban[N];

void get(int l,int r){
	if(l>r)return ;
	int cntt[3]={0,0,0};
	for(int i=l;i<=r;i++){
		cntt[s[i]]++;
	}
	if(cntt[2]==0||cntt[1]==0){
		if(cntt[2]&&l>1&&r<n)c2++,pc2.push_back(l-1);
		if(cntt[1]&&l>1&&r<n)b2++;
	}
	if(cntt[2])c1++;
	if(cntt[1])b1++;
}

void Sol(int l,int r){
	if(l>r)return ;
	if(l==r){
		if(l==1||l==n){
			if(s[l]==2&&cnt[2]>cnt[1])cnt[2]--,ban[l]=1;
		}
	}
	if(l==r)return ;
	if(s[l]==2&&cnt[2]>cnt[1])cnt[2]--,ban[l]=1;
	if(s[r]==2&&cnt[2]>cnt[1])cnt[2]--,ban[l]=1;
}

int now,tar;
void Sol2(int l,int r){
	if(l>r)return ;
	while(l<r&&now>tar){
		now--;ban[l]=ban[l+1]=1;l+=2;
	}
}

int main()
{
	scanf("%s",tmple+1);int lll=strlen(tmple+1);
	for(int i=1;i<=lll;i++)if(i==1||tmple[i]!=tmple[i-1])s[++n]=tmple[i]-'A';
	for(int i=1;i<=n;i++)cnt[s[i]]++;
	for(int i=0;i<3;i++)for(int j=i+1;j<3;j++)if(i!=j){
		if(cnt[i]>cnt[j]){
			_swap(i,j);
			swap(cnt[i],cnt[j]);
		}
	}
	// for(int i=1;i<=n;i++)printf("%c",'A'+s[i]);puts("");
	// cout << " " << cnt[0] << ":" << cnt[1] << ":" << cnt[2] << endl;
	int lst=0;
	for(int i=1;i<=n;i++)
		if(s[i]==0){get(lst+1,i-1);lst=i;}
	get(lst+1,n);
	// cout << b1 << " " << b2 << " " << c1 << " " << c2 << endl;
	if(b1<c2){
		int ct=c2-b1;
		for(int i=0;i<ct;i++){
			// cout << pc2[i] << "-";
			ban[pc2[i]]=1,cnt[0]--;
		}
	}
	// puts("");
	/*  */
	lst=-1;int nn=0;
	for(int i=1;i<=n;i++)if(!ban[i]){
		if(lst!=s[i])lst=s[i], s[++nn]=s[i];
	}n=nn;
	memset(ban,0,sizeof(ban));
	
	// for(int i=1;i<=n;i++)printf("%c",'A'+s[i]);puts("");
	// cout << " " << cnt[0] << ":" << cnt[1] << ":" << cnt[2] << endl;
	/*  */
	lst=0;
	for(int i=1;i<=n;i++)
		if(s[i]==0){Sol(lst+1,i-1);lst=i;}
	Sol(lst+1,n);
	/*  */
	lst=-1,nn=0;
	for(int i=1;i<=n;i++)if(!ban[i]){
		if(lst!=s[i])lst=s[i], s[++nn]=s[i];
	}n=nn;
	memset(ban,0,sizeof(ban));
	/*  */
	now=cnt[1],tar=cnt[0];
	lst=0;
	for(int i=1;i<=n;i++)
		if(s[i]==0){Sol2(lst+1,i-1);lst=i;}
	Sol2(lst+1,n);
	for(int i=1;i<=n;i++){
		if(!ban[i])printf("%c",'A'+p[s[i]]);
	}
	puts("");
}