This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
//#define cerr if (false) cerr
#define db(x) cerr << #x << "=" << x << endl
#define db2(x, y) cerr << #x << "=" << x << "," << #y << "=" << y << endl
#define db3(x, y, z) cerr << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z << endl
#define dbv(v) cerr << #v << "="; for (auto _x : v) cerr << _x << ", "; cerr << endl
#define dba(a, n) cerr << #a << "="; for (int _i = 0; _i < (n); ++_i) cerr << a[_i] << ", "; cerr << endl
template <typename A, typename B>
ostream& operator<<(ostream& os, const pair<A, B>& x) {
	return os << "(" << x.first << "," << x.second << ")";
}
typedef long long ll;
typedef long double ld;
int main() {
	int n;
	scanf("%d", &n);
	vector<vector<int>> A(2 * n, vector<int>(2));
	vector<vector<pair<int, int>>> dp(2 * n, vector<pair<int, int>>(2));
	for (int j = 0; j < 2; ++j)
		for (int i = 0; i < 2 * n; ++i) scanf("%d", &A[i][j]);
	dp[0][0] = {1, 1};
	for (int i = 1; i < 2 * n; ++i) {
		for (int j = 0; j < 2; ++j) {
			dp[i][j] = {INT_MAX, INT_MIN};
			for (int k = 0; k < 2; ++k) {
				if (A[i - 1][k] <= A[i][j]) {
					dp[i][j].first = min(dp[i][j].first, dp[i - 1][k].first);
					dp[i][j].second = max(dp[i][j].second, dp[i - 1][k].second);
				}
			}
		}
		if (dp[i][0].first != INT_MAX) {
			dp[i][0].first++;
			dp[i][0].second++;
		}
	}
	bool has = false;
	for (int j = 0; j < 2; ++j) {
		if (dp[2 * n - 1][j].first <= n && n <= dp[2 * n - 1][j].second) {
			has = true;
			break;
		}
	}
	if (!has) printf("-1\n");
	else {
		string ans;
		int as = n;
		int las = INT_MAX;
		for (int i = 2 * n - 1; i >= 0; --i) {
			if (dp[i][0].first <= as && as <= dp[i][0].second && A[i][0] <= las) {
				--as;
				ans += 'A';
				las = A[i][0];
			} else {
				assert(dp[i][1].first <= as && as <= dp[i][1].second && A[i][1] <= las);
				ans += 'B';
				las = A[i][1];
			}
		}
		assert(as == 0);
		reverse(ans.begin(), ans.end());
		printf("%s\n", ans.c_str());
	}
}
Compilation message (stderr)
building4.cpp: In function 'int main()':
building4.cpp:17:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &n);
  ~~~~~^~~~~~~~~~
building4.cpp:21:40: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   for (int i = 0; i < 2 * n; ++i) scanf("%d", &A[i][j]);
                                   ~~~~~^~~~~~~~~~~~~~~~| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |