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>
#define debug(x) cerr << #x << " " << (x) << endl
using namespace std;

const int N = 1000005;

char str[N];
int seq1[N], seq2[N], seq3[N], seq4[N], occur[3], mn, se, th, cnt1 = 0, cnt2 = 0, cnt3 = 0, cnt4 = 0;
int main () {
	scanf("%s", str);
	for (int i = 0; str[i]; i++) {
		if (!cnt1 || str[i] - 'A' != seq1[cnt1]) seq1[++cnt1] = str[i] - 'A';
	}

	occur[0] = occur[1] = occur[2] = 0;
	for (int i = 1; i <= cnt1; i++) occur[seq1[i]]++;

	mn = 0, se = 1;
	if (occur[0] > occur[1]) swap(mn, se);
	if (occur[2] < occur[mn]) se = mn, mn = 2;
	else if (occur[2] < occur[se]) se = 2;
	th = 3 - mn - se;

	for (int i = 1; i <= cnt1; i++) {
		bool chk = (cnt2 >= 2 && occur[th] > occur[se] && seq2[cnt2] == th);
		if (seq2[cnt2 - 1] == th || seq1[i] == th || seq2[cnt2 - 1] == seq1[i]) chk = false;
		if (chk) seq2[cnt2] = seq1[i], occur[th]--;
		else seq2[++cnt2] = seq1[i];
		if (cnt2 == 1 && seq2[cnt2] == th && occur[th] > occur[se]) cnt2--, occur[th]--;
	}
	if (seq2[cnt2] == th && occur[th] > occur[se]) cnt2--, occur[th]--;

	for (int i = 1; i <= cnt2; i++) {
		bool chk = (cnt3 >= 2 && occur[th] > occur[se] && seq3[cnt3] == th);
		if (seq3[cnt3 - 1] != mn || seq2[i] != mn) chk = false;
		if (chk) cnt3--, occur[th]--, occur[mn]--;
		else seq3[++cnt3] = seq2[i];
	}

	for (int i = 1; i <= cnt3; i++) {
		bool chk = (cnt4 && occur[se] > occur[mn] && seq4[cnt4] != mn);
		if (seq3[i] == mn || seq3[i] == seq4[cnt4]) chk = false;
		if (chk) cnt4--, occur[se]--, occur[th]--;
		else seq4[++cnt4] = seq3[i];
	}

	for (int i = 1; i <= cnt4; i++) printf("%c", seq4[i] + 'A');
	printf("\n");
	return 0;
}