#include <bits/stdc++.h>
using namespace std;
int main() {
    int n; cin >> n;
    vector<array<int, 2> > a(2*n+1);
    for(int i=1; i<=2*n; i++) cin >> a[i][0];
    for(int i=1; i<=2*n; i++) cin >> a[i][1];
    vector<vector<int> > dpL(2*n+1, vector<int>(2, 1e9));
    vector<vector<int> > dpR(2*n+1, vector<int>(2, -1e9));
    
    dpL[1] = { 0, 1 };
    dpR[1] = { 0, 1 };
    for(int i=2; i<=2*n; i++) {
        for(int j=0; j<2; j++) {
            for(int k=0; k<2; k++) {
                if(a[i][j] >= a[i-1][k]) {
                    dpL[i][j] = min(dpL[i][j], dpL[i-1][k] + j);
                    dpR[i][j] = max(dpR[i][j], dpR[i-1][k] + j);
                }
            }
        }
    }
    string ans(2*n+1, 'A');
    int l=n, p=2*n, last = 2e9;
    for(; p; p--) {
        int ok=0;
        for(int i=0; i<2&&!ok; i++) {
            if(dpL[p][i] <= l && l <= dpR[p][i] && a[p][i] <= last) {
                ok = 1;
                last = a[p][i];
                l -= i;
                ans[p] += i;
            }
        }
        if(!ok) {
            cout << -1;
            return 0;
        }
    }
    for(int i=1; i<=2*n; i++) cout << ans[i];
}
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |