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 ll long long
#define all(aaa) aaa.begin(), aaa.end()
const int N = 4005;
int dp[N][N], a[2][N], ans[N];
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin >> n;
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < n + n; j++) {
            cin >> a[i][j];
        }
    }
    memset(dp, -1, sizeof(dp));
    dp[1][0] = 0;
    dp[1][1] = 1;
    for (int i = 1; i < n + n; i++) {
        for (int j = 0; j <= n; j++) {
            if (dp[i][j] != -1) {
                for (int k = 0; k <= min(1, n - j); k++) {
                    if (a[dp[i][j]][i - 1] <= a[k][i]) {
                        if (dp[i + 1][j + k] == -1 ||
                            a[dp[i + 1][j + k]][i] > a[k][i]) {
                            dp[i + 1][j + k] = k;
                        }
                    }
                }
            }
        }
    }
    // cout << dp[1][0] << "\n";
    if (dp[n + n][n] == -1) {
        cout << "-1";
    }
    else {
        int x = n + n, y = n;
        while (x > 0) {
            ans[x - 1] = dp[x][y];
            y -= dp[x][y];
            x--;
        }
        for (int i = 0; i < n + n; i++) {
            cout << (ans[i] ? 'B' : 'A');
        }
    }
    
    return 0;
}       
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |