Problem:
Sixth Sense
Ms. Future is gifted with precognition. Naturally, she is excellent at some card games since she can correctly foresee every player's actions, except her own. Today, she accepted a challenge from a reckless gambler Mr. Past. They agreed to play a simple two-player trick-taking card game.
Cards for the game have a number printed on one side, leaving the other side blank making indistinguishable from other cards.
A game starts with the same number, say $n$, of cards being handed out to both players, without revealing the printed number to the opponent.
A game consists of $n$ tricks. In each trick, both players pull one card out of her/his hand. The player pulling out the card with the larger number takes this trick. Because Ms. Future is extremely good at this game, they have agreed to give tricks to Mr. Past when both pull out cards with the same number. Once a card is used, it can never be used later in the same game. The game continues until all the cards in the hands are used up. The objective of the game is to take as many tricks as possible.
Your mission of this problem is to help Ms. Future by providing a computer program to determine the best playing order of the cards in her hand. Since she has the sixth sense, your program can utilize information that is not available to ordinary people before the game.
Input
The input consists of a single test case of the following format.
$n$
$p_1$ ... $p_n$
$f_1$ ... $f_n$
$n$ in the first line is the number of tricks, which is an integer between 2 and 5000, inclusive. The second line represents the Mr. Past's playing order of the cards in his hand. In the $i$-th trick, he will pull out a card with the number $p_i$ ($1 \leq i \leq n$). The third line represents the Ms. Future's hand. $f_i$ ($1 \leq i \leq n$) is the number that she will see on the $i$-th received card from the dealer. Every number in the second or third line is an integer between 1 and 10 000, inclusive. These lines may have duplicate numbers.
Output
The output should be a single line containing $n$ integers $a_1 ... a_n$ separated by a space, where $a_i$ ($1 \leq i \leq n$) is the number on the card she should play at the $i$-th trick for maximizing the number of taken tricks. If there are two or more such sequences of numbers, output the lexicographically greatest one among them.
Sample Input 1
5
1 2 3 4 5
1 2 3 4 5
Sample Output 1
2 3 4 5 1
Sample Input 2
5
3 4 5 6 7
1 3 5 7 9
Sample Output 2
9 5 7 3 1
Sample Input 3
5
3 2 2 1 1
1 1 2 2 3
Sample Output 3
1 3 1 2 2
Sample Input 4
5
3 4 10 4 9
2 7 3 6 9
Sample Output 4
9 7 3 6 2

Submitted solutions:

code_snippet: #include<bits/stdc++.h>
using namespace std; 
#define int long long 
#pragma GCC optimize("O3,unroll-loops")

const int maxn=5000;
int n;
pair<int,int> a[maxn+2];
int tmp[maxn+2];
vector<int>b;
int mx=0;

bool check(int mid,int pos){
    int id=0;
    int sum=0;

    for(int q=0;q<b.size();q++){
        if(q==mid)continue;

        while(a[id].second==pos || (id<=n && a[id].second<pos))id++;
        
        if(id<=n && a[id].first<b[q]){
            id++;
            sum++;
        }
    }
    
    if(tmp[pos]<b[mid])sum++;
    return sum>=mx;
}

signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    cin>>n;

    for(int q=1;q<=n;q++){
        cin>>a[q].first;
        a[q].second=q;
        tmp[q]=a[q].first;
    }
    for(int q=1;q<=n;q++){
        int x;
        cin>>x;
        b.push_back(x);
    }
    sort(a+1,a+n+1); sort(b.begin(),b.end());

    
    int id=1;
    for(int q=0;q<n;q++){
        if(id<=n && a[id].first<b[q]){
            mx++;
            id++;
        }
    }

    int ans[n+1];
    
    for(int q=1;q<=n;q++){
        int pos=upper_bound(b.begin(),b.end(),tmp[q])-b.begin();
        int l=pos;
        int r=b.size()-1;
        if(pos==b.size()){
            l=0;
        }
            
        int brp=0;
        while(l<=r){
            int mid=(l+r)/2;
            if(check(mid,q)){
                brp=mid;
                l=mid+1;
            }
            else{
                r=mid-1;
            }
        }

           
        ans[q]=b[brp];
        if(tmp[q]<b[brp])mx--;
        //    cout<<mx<<endl;
        b.erase(b.begin()+brp);
    }

    for(int q=1;q<=n;q++){
        cout<<ans[q];
        if(q!=n)cout<<" ";
    }
    cout<<endl;
    
  
}
accuracy: 1
time_ms: 470
memory_kb: 3708
final_rank: 5

code_snippet: #include <bits/stdc++.h>
#define PII pair<int, int>
#define LL long long
using namespace std;
const int MAXN = 5005;
const LL MOD = 998244353;
const LL INF = (LL)1e9 + 5;

bool can_win(vector<int> &sa, vector<int> &sb, int num) {
	assert(sa.size() == sb.size());
	if (num > sa.size()) return false;
	
	int N = sa.size();
	for (int i = 0; i < num; i++) {
		if (sa[i] >= sb[N - num + i]) {
			return false;
		}
	}
	return true;
}

int get_win(vector<int> &sa, vector<int> &sb) {
	int l = 1, r = sa.size(), win = 0;
	while (l <= r) {
		int m = (l + r) >> 1;
		if (can_win(sa, sb, m)) {
			win = m;
			l = m + 1;
		}
		else {
			r = m - 1;
		}
	}
	return win;
}

int N;
vector<int> a, b, sa, sb;
vector<int> vec, ans;

int main() {
	ios_base::sync_with_stdio(0); cin.tie(0);
	cin >> N;
	a.resize(N);
	b.resize(N);
	for (int i = 0; i < N; i++) {
		cin >> a[i];
	}
	for (int i = 0; i < N; i++) {
		cin >> b[i];
	}
	
	sa = a;
	sb = b;
	sort(sa.begin(), sa.end());
	sort(sb.begin(), sb.end());
	
	int tgt = get_win(sa, sb);
//	cout << "Get " << tgt << "!\n";
	for (int i = 0; i < N; i++) {
		int rem_a = a[0];
		vector<int> na = a;
		na.erase(na.begin());
		sa.erase(lower_bound(sa.begin(), sa.end(), rem_a));
//		cout << "At " << i << " " << tgt << "!\n";
		
		int part = upper_bound(sb.begin(), sb.end(), rem_a) - sb.begin();
//		cout << "Part at " << sb[part] << '\n';
		int l = 0, r = part - 1, rem_b = -1;
		while (l <= r) {
			int m = (l + r) >> 1;
//			cout << "Check " << m << " " << sb[m] << '\n';
			vector<int> tmp = sb;
			tmp.erase(tmp.begin() + m);
			
			int nxt = tgt;
			if (can_win(sa, tmp, nxt)) {
				rem_b = max(rem_b, m);
				l = m + 1;
			}
			else {
				r = m - 1;
			}
		}
		
		l = part, r = sb.size() - 1;
		while (l <= r) {
			int m = (l + r) >> 1;
//			cout << "Check " << m << " " << sb[m] << '\n';
			vector<int> tmp = sb;
			tmp.erase(tmp.begin() + m);
			
			int nxt = tgt - 1;
			if (can_win(sa, tmp, nxt)) {
				rem_b = max(rem_b, m);
				l = m + 1;
			}
			else {
				r = m - 1;
			}
		}
		
		a = na;
		ans.push_back(sb[rem_b]);
		if (sb[rem_b] > rem_a) tgt--;
		sb.erase(sb.begin() + rem_b);
//		cout << "Last " << rem_b << ' ' << ans.back() << '\n'; 
	}
	
	for (int i = 0; i < N; i++) {
		cout << ans[i] << (i + 1 < N ? ' ' : '\n');
	}

	return 0;
}
accuracy: 1
time_ms: 80
memory_kb: 3644
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;

// A を入力して goal 個マッチできるかをチェック
bool check(int goal, vector<int> &A, vector<int> &B) {
	if (A.size() < goal) return false;
	for (int i = goal - 1; i >= 0; i--) {
		if (A[i] >= B[B.size() - goal + i]) return false;
	}
	return true;
}

int main() {
	// step 1. input
	int N; cin >> N;
	vector<int> A(N, 0);
	vector<int> T(N, 0);
	for (int i = 0; i < N; i++) cin >> A[i];
	for (int i = 0; i < N; i++) cin >> T[i];

	// step 2. sorting
	vector<vector<int>> B(N + 2);
	for (int i = 0; i < N; i++) {
		B[i] = vector<int>(A.begin() + i, A.begin() + N);
		sort(B[i].begin(), B[i].end());
	}
	sort(T.begin(), T.end());

	// step 3. solve init
	vector<bool> usable(N, true);
	int first_goal = 0;
	for (int i = 1; i <= N; i++) {
		if (check(i, B[0], T) == true) {
			first_goal = i;
		}
		else break;
	}

	// step 4. solve main
	vector<int> Answer(N, 0);
	for (int pos = 0; pos < N; pos++) {
		int pivot = lower_bound(T.begin(), T.end(), A[pos] + 1) - T.begin();
		int maxn = 0;

		// first binary search
		for (int t = 0; t <= 1; t++) {
			int ok = (t == 0 ? -1 : pivot - 1), ng = (t == 0 ? pivot : (int)T.size());
			if (ng - ok <= 1) continue;
			while (ng - ok > 1) {
				int mid = (ng + ok) / 2;
				vector<int> T2 = T; T2.erase(T2.begin() + mid);
				bool ret = check(first_goal - t, B[pos + 1], T2);
				if (ret == true) { ok = mid; }
				else { ng = mid; }
			}
			maxn = max(maxn, ok);
		}

		// final
		Answer[pos] = T[maxn];
		T.erase(T.begin() + maxn);
		if (Answer[pos] > A[pos]) first_goal -= 1;
	}

	// step 5. output
	for (int i = 0; i < N; i++) {
		if (i >= 1) cout << " ";
		cout << Answer[i];
	}
	cout << endl;
	return 0;
}
accuracy: 1
time_ms: 480
memory_kb: 52544
final_rank: 19

code_snippet: #include <bits/stdc++.h>
using namespace std;

// A を入力して goal 個マッチできるかをチェック
bool check(int goal, vector<int> &A, vector<int> &B) {
	if (A.size() < goal) return false;
	for (int i = goal - 1; i >= 0; i--) {
		if (A[i] >= B[B.size() - goal + i]) return false;
	}
	return true;
}

int main() {
	// step 1. input
	int N; cin >> N;
	vector<int> A(N, 0);
	vector<int> T(N, 0);
	for (int i = 0; i < N; i++) cin >> A[i];
	for (int i = 0; i < N; i++) cin >> T[i];

	// step 2. sorting
	vector<vector<int>> B(N + 2);
	for (int i = 0; i < N; i++) {
		B[i] = vector<int>(A.begin() + i, A.begin() + N);
		sort(B[i].begin(), B[i].end());
	}
	sort(T.begin(), T.end());

	// step 3. solve init
	vector<bool> usable(N, true);
	int first_goal = 0;
	for (int i = 1; i <= N; i++) {
		if (check(i, B[0], T) == true) {
			first_goal = i;
		}
		else break;
	}

	// step 4. solve main
	vector<int> Answer(N, 0);
	for (int pos = 0; pos < N; pos++) {
		int pivot = lower_bound(T.begin(), T.end(), A[pos] + 1) - T.begin();
		int maxn = 0;

		// first binary search
		if (0 < pivot) {
			int ok = -1, ng = pivot;
			while (ng - ok > 1) {
				int mid = (ng + ok) / 2;
				vector<int> T2 = T; T2.erase(T2.begin() + mid);
				bool ret = check(first_goal, B[pos + 1], T2);
				if (ret == true) { ok = mid; }
				else { ng = mid; }
			}
			maxn = max(maxn, ok);
		}

		// second binary search
		if (pivot < T.size()) {
			int ok = pivot - 1, ng = (int)T.size();
			while (ng - ok > 1) {
				int mid = (ng + ok) / 2;
				vector<int> T2 = T; T2.erase(T2.begin() + mid);
				bool ret = check(first_goal - 1, B[pos + 1], T2);
				if (ret == true) { ok = mid; }
				else { ng = mid; }
			}
			maxn = max(maxn, ok);
		}

		// final
		Answer[pos] = T[maxn];
		T.erase(T.begin() + maxn);
		if (Answer[pos] > A[pos]) first_goal -= 1;
	}

	// step 5. output
	for (int i = 0; i < N; i++) {
		if (i >= 1) cout << " ";
		cout << Answer[i];
	}
	cout << endl;
	return 0;
}
accuracy: 1
time_ms: 470
memory_kb: 52472
final_rank: 18

code_snippet: #include <bits/stdc++.h>
using namespace std;

// A を入力して goal 個マッチできるかをチェック
bool check(int goal, vector<int> &A, vector<int> &B, vector<bool> &usable) {
	if (A.size() < goal) return false;
	int cur = B.size() - 1;
	for (int i = goal - 1; i >= 0; i--) {
		while (cur >= 0 && usable[cur] == false) cur -= 1;
		if (cur < 0 || B[cur] <= A[i]) return false;
		cur -= 1;
	}
	return true;
}

int main() {
	// step 1. input
	int N; cin >> N;
	vector<int> A(N, 0);
	vector<int> T(N, 0);
	for (int i = 0; i < N; i++) cin >> A[i];
	for (int i = 0; i < N; i++) cin >> T[i];

	// step 2. sorting
	vector<vector<int>> B(N + 2);
	for (int i = 0; i < N; i++) {
		B[i] = vector<int>(N - i, 0);
		for (int j = i; j < N; j++) B[i][j - i] = A[j];
		sort(B[i].begin(), B[i].end());
	}
	sort(T.begin(), T.end());

	// step 3. solve init
	vector<bool> usable(N, true);
	int first_goal = 0;
	for (int i = 1; i <= N; i++) {
		if (check(i, B[0], T, usable) == true) {
			first_goal = i;
		}
		else break;
	}

	// step 4. solve main
	vector<int> Answer(N, 0);
	for (int pos = 0; pos < N; pos++) {
		vector<pair<int, int>> arr(N - pos);
		int arrcnt = 0;
		for (int i = 0; i < N; i++) {
			if (usable[i] == true) {
				arr[arrcnt] = make_pair(T[i], i);
				arrcnt += 1;
			}
		}
		int pivot = lower_bound(arr.begin(), arr.end(), make_pair(A[pos] + 1, -1)) - arr.begin();
		int maxn = 0;

		// first binary search
		if (0 < pivot) {
			int ok = -1, ng = pivot;
			while (ng - ok > 1) {
				int mid = (ng + ok) / 2;
				usable[arr[mid].second] = false;
				bool ret = check(first_goal, B[pos + 1], T, usable);
				if (ret == true) { ok = mid; }
				else { ng = mid; }
				usable[arr[mid].second] = true;
			}
			maxn = max(maxn, ok);
		}

		// second binary search
		if (pivot < arr.size()) {
			int ok = pivot - 1, ng = (int)arr.size();
			while (ng - ok > 1) {
				int mid = (ng + ok) / 2;
				usable[arr[mid].second] = false;
				bool ret = check(first_goal - 1, B[pos + 1], T, usable);
				if (ret == true) { ok = mid; }
				else { ng = mid; }
				usable[arr[mid].second] = true;
			}
			maxn = max(maxn, ok);
		}

		// final
		usable[arr[maxn].second] = false;
		Answer[pos] = arr[maxn].first;
		if (Answer[pos] > A[pos]) first_goal -= 1;
	}

	// step 5. output
	for (int i = 0; i < N; i++) {
		if (i >= 1) cout << " ";
		cout << Answer[i];
	}
	cout << endl;
	return 0;
}
accuracy: 1
time_ms: 610
memory_kb: 52456
final_rank: 20

code_snippet: #include <bits/stdc++.h>

using namespace std;

int main() {
    int n; cin >> n;
    vector<int> p(n);
    for(int& pi : p) cin >> pi;
    vector<int> f(n);
    for(int& fi : f) cin >> fi;
    ranges::sort(f);
    vector<int> p_sorted = p;
    ranges::sort(p_sorted);
    auto process = [&](const vector<int>& p_sorted, const vector<int>& f) {
        int idx = 0;
        int num = 0;
        for(int pi : p_sorted) {
            while(idx < ssize(f) && f[idx] <= pi) idx++;
            if(idx < ssize(f)) {
                num++;
                idx++;
            } else break;
        }
        return num;
    };
    int final_num = process(p_sorted, f);
    int cur = 0;
    vector<int> ans(n);
    for(int i = 0; i < n; i++) {
        p_sorted.erase(ranges::find(p_sorted, p[i]));
        {
            // in case f[i] > p[i]
            int ok = ranges::upper_bound(f, p[i]) - f.begin() - 1, ng = ssize(f);
            while(ng - ok > 1) {
                int mid = midpoint(ok, ng);
                int val = f[mid];
                f.erase(f.begin() + mid);
                if(cur + 1 + process(p_sorted, f) >= final_num) ok = mid;
                else ng = mid;
                f.insert(f.begin() + mid, val);
            }
            if(f[ok] > p[i]) {
                ans[i] = f[ok];
                f.erase(f.begin() + ok);
                cur++;
                continue;
            }
        }
        {
            // in case f[i] <= p[i]
            int ok = 0, ng = ranges::upper_bound(f, p[i]) - f.begin();
            while(ng - ok > 1) {
                int mid = midpoint(ok, ng);
                int val = f[mid];
                f.erase(f.begin() + mid);
                if(cur + process(p_sorted, f) >= final_num) ok = mid;
                else ng = mid;
                f.insert(f.begin() + mid, val);
            }
            ans[i] = f[ok];
            f.erase(f.begin() + ok);
        }
    }
    for(int i = 0; i < n; i++) {
        cout << ans[i] << (i == n-1 ? '\n' : ' ');
    }
}
accuracy: 1
time_ms: 100
memory_kb: 3712
final_rank: 2

code_snippet: #include <bits/stdc++.h>
using namespace std;
 
const int INF = 100000;
 
struct Node { int d, c; };
 
inline Node combineNode(const Node &L, const Node &R) {
    Node res;
    res.d = L.d + R.d;
    res.c = min(L.c + R.d, R.c);
    return res;
}
 
int MAXV = 10000;
vector<Node> segTree;
vector<int> segT;
vector<int> avail;
void buildSegTree(int idx, int l, int r) {
    if(l == r) {
        segTree[idx].d = avail[l];
        segTree[idx].c = segT[l];
        return;
    }
    int mid = (l + r) / 2;
    buildSegTree(idx*2, l, mid);
    buildSegTree(idx*2+1, mid+1, r);
    segTree[idx] = combineNode(segTree[idx*2], segTree[idx*2+1]);
}
 
void updateSegTree(int idx, int l, int r, int pos, int newVal) {
    if(l == r) {
        segTree[idx].d = newVal;
        segTree[idx].c = segT[l];
        return;
    }
    int mid = (l + r) / 2;
    if(pos <= mid)
        updateSegTree(idx*2, l, mid, pos, newVal);
    else
        updateSegTree(idx*2+1, mid+1, r, pos, newVal);
    segTree[idx] = combineNode(segTree[idx*2], segTree[idx*2+1]);
}
 
Node querySegTree() { return segTree[1]; }
 
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
 
    int n;
    cin >> n;
    vector<int> past(n), future(n);
    for (int i = 0; i < n; i++) cin >> past[i];
    for (int i = 0; i < n; i++) cin >> future[i];
 
    vector<int> sortedFuture = future, sortedPast = past;
    sort(sortedFuture.begin(), sortedFuture.end());
    sort(sortedPast.begin(), sortedPast.end());
    int wins = 0;
    int iF = 0, iP = 0;
    while(iF < n && iP < n) {
        if(sortedFuture[iF] > sortedPast[iP]){
            wins++;
            iF++; iP++;
        } else iF++;
    }
    int W = wins;
 
    avail.assign(MAXV+1, 0);
    for (int i = 0; i < n; i++) avail[ future[i] ]++;
 
    segTree.resize(4 * (MAXV+1));
    segT.assign(MAXV+1, 0);
 
    vector<int> oppFreq(MAXV+1, 0);
    for (int i = 1; i < n; i++) oppFreq[past[i]]++;
 
    vector<int> answer(n, 0);

    int currWins = 0;
    for (int i = 0; i < n; i++){
        segT[1] = 0;
        for (int v = 2; v <= MAXV; v++){
            segT[v] = segT[v-1] + oppFreq[v-1];
        }
        buildSegTree(1, 1, MAXV);

        int chosen = -1;
        for (int cand = MAXV; cand >= 1; cand--){
            if(avail[cand] > 0){
                int winThis = (cand > past[i]) ? 1 : 0;
                int newWins = currWins + winThis;
                int required = W - newWins;
                if(required < 0) required = 0;
 
                int oldVal = avail[cand];
                updateSegTree(1, 1, MAXV, cand, oldVal - 1);
 
                Node comp = querySegTree();
                int achievable = min(comp.d, comp.c);
 
                if(achievable >= required){
                    chosen = cand;
                    currWins = newWins;
                    avail[cand]--;
                    break;
                } else updateSegTree(1, 1, MAXV, cand, oldVal);
            }
        }
        if(chosen == -1) chosen = 1;
 
        answer[i] = chosen;
        if(i + 1 < n) oppFreq[past[i+1]]--;
    }
    for (int i = 0; i < n; i++){
        cout << answer[i] << (i+1<n ? " " : "\n");
    }
    return 0;
}
accuracy: 1
time_ms: 1560
memory_kb: 3724
final_rank: 13

code_snippet: #include<bits/stdc++.h>
using namespace std;

int main(){
	int n;
	cin >> n;
	vector<int> p(n), f(n);
	for(int i = 0; i < n; ++i) cin >> p[i];
	for(int i = 0; i < n; ++i) cin >> f[i];
	auto sp = p;
	auto sf = f;
	ranges::sort(sp);
	ranges::sort(sf);
	int mx = 0;
	int sp_idx = n-1;
	for(int i = 0; i < n && sp_idx >= 0; ++i){
		while(sp_idx >= 0 && sp[sp_idx] >= sf[n-i-1]) --sp_idx;
		if(sp_idx >= 0){
			++mx;
			--sp_idx;
		}
	}
	vector<int> ans(n);
	vector<int> rem = sf;

	auto check = [&](int tmp)->bool{
		sp = p;
		sort(sp.begin()+tmp, sp.end());
		auto srem = rem;
		ranges::sort(srem);
		int cnt = 0;
		for(int i = 0; i < tmp; ++i){
			if(sp[i] < ans[i]){
				++cnt;
			}
		}
		sp_idx = n-1;
		for(int i = tmp; i < n && sp_idx >= tmp; ++i){
			while(sp_idx >= tmp && sp[sp_idx] >= srem[n-i-1]) --sp_idx;
			if(sp_idx >= tmp){
				++cnt;
				--sp_idx;
			}
		}
		//cout << tmp << ' ' << cnt << endl;
		//for(auto i: sp) cout << i << ' ';
		//cout << endl;
		//for(auto i: srem) cout << i << ' ';
		//cout << endl;
		return mx == cnt;
	};

	for(int i = 0; i < n; ++i){
		int left = -1, right = n-i;

		for(int j = 0; j < n-i; ++j){
			if(p[i] < rem[j]){
				right = j;
				break;
			}
		}

		if(right < n-i){
			left = right-1;
			right = n-i;
		}
		while(right-left > 1){
			int mid = (left+right)>>1;
			int rec = rem[mid];
			auto srem = rem;
			rem.erase(ranges::find(rem, rec));
			ans[i] = rec;
			if(check(i+1)){
				left = mid;
			}else{
				right = mid;
			}
			rem = srem;
		}
		ans[i] = rem[left];
		rem.erase(ranges::find(rem, rem[left]));
	}
	for(int i = 0; i < n; ++i){
		cout << ans[i] << " \n"[i==n-1];
	}
	return 0;
}
accuracy: 1
time_ms: 4240
memory_kb: 3840
final_rank: 17

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define rep2(i,l,n) for(int i=l;i<n;i++)
#define all(v) v.begin(), v.end()
#define lower_index(v,x) lower_bound(all(v),x)-v.begin();
#define upper_index(v,x) upper_bound(all(v),x)-v.begin();
int main() {
    int n;cin >> n;
    vector<int> p(n);
    vector<int> f(n);
    rep(i, n)cin >> p[i];
    rep(i, n)cin >> f[i];
    sort(all(f));
    reverse(all(p));
    auto maxwin=[](vector<int> &p,vector<int> &f){
        sort(all(p));
        sort(all(f));
        int ret=0;
        rep(i,ssize(p)){
            if(f[i]>p[ret])ret++;
        }
        return ret;
    };
    int m=0;
    {
        auto p2=p;
        sort(all(p2));
        m=maxwin(p2,f);
    }
    // cerr<<m<<endl;
    vector ans(0,0);
    int nowwin=0;
    rep(i,n){
        int id=upper_index(f,p.back());
        int ok1=-1,ok2=-1;
        p.pop_back();
        auto p2=p;
        sort(all(p2));
        if(id<ssize(f)){
            vector f2(n-1-i,0);
            for(int j=0,t=0;j<ssize(f);j++){
                if(j==id)continue;
                f2[t++]=f[j];
            }
            // for(auto i:f2)cerr<<i<<' ';
            // cerr<<endl;
            if(nowwin+1+maxwin(p2,f2)==m){
                ok1=id;
                int ng1=ssize(f);
                while(ng1-ok1>1){
                    int mid=(ng1+ok1)/2;
                    for(int j=0,t=0;j<ssize(f);j++){
                        if(j==mid)continue;
                        f2[t++]=f[j];
                    }
                    if(nowwin+1+maxwin(p2,f2)==m)ok1=mid;
                    else ng1=mid;
                }
            }
        }
        {
            vector f2(n-1-i,0);
            rep(i,ssize(f)-1){
                f2[i]=f[i+1];
            }
            if(nowwin+maxwin(p2,f2)==m){
                ok2=0;
                int ng2=id;
                while(ng2-ok2>1){
                    int mid=(ng2+ok2)/2;
                    for(int j=0,t=0;j<ssize(f);j++){
                        if(j==mid)continue;
                        f2[t++]=f[j];
                    }
                    if(nowwin+maxwin(p2,f2)==m)ok2=mid;
                    else ng2=mid;
                }
            }
        }
        int ok=max(ok1,ok2);
        // cerr<<id<<' ';
        // cerr<<ok<<endl;
        ans.push_back(f[ok]);
        if(ok==ok1)nowwin++;
        vector nf(ssize(f)-1,0);
        for(int j=0,t=0;j<ssize(f);j++){
            if(j==ok)continue;
            nf[t++]=f[j];
        }
        f=nf;
    }
    rep(i,n){
        if(i)cout<<' ';
        cout<<ans[i];
    }
    cout<<'\n';
}
accuracy: 1
time_ms: 2050
memory_kb: 3712
final_rank: 14

code_snippet: #include<bits/stdc++.h>
#define rep(i,s,n) for (int i = (int)(s); i < (int)(n); i++)
#define rrep(i,s,n) for (int i = (int)(n); i --> (int)(s);)
#define all(v) begin(v),end(v)
using namespace std;
using ll = long long;
using vl=vector<ll>;
using pll=pair<ll,ll>;
using vp=vector<pll>;
// bool chmin(auto &a, auto b){ return a > b ? a = b, 1 : 0; }
// bool chmax(auto &a, auto b){ return a < b ? a = b, 1 : 0; }

template<typename T>
bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}

template<typename T>
bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

int main() {
    cin.tie(0)->sync_with_stdio(0);

    auto f=[&](vl a,vl b,ll k) {
        sort(all(a));
        sort(all(b));
        assert(a.size()==b.size());
        ll sz=a.size();
        bool B=true;
        rep(i,0,k) B&=(a[k-i-1]<b[sz-i-1]);
        return B;
    };

    ll n;
    cin>>n;
    vl a(n),b(n);
    rep(i,0,n) cin>>a[i];
    rep(i,0,n) cin>>b[i];

    sort(all(b));
    rrep(k,0,n+1){
        if (f(a,b,k)) {
            vl rs;
            rep(_,0,n){
                ll m=a.size();
                vp sa(m);
                rep(i,0,m) sa[i]={a[i],i};
                sort(all(sa));

                vector<vl> tmp(m+1,vl(3));
                rep(x,0,m) {
                    ll y=m-k+x;
                    rep(j,0,3) {
                        if (y-j<0||y-j>=m||sa[x].first>=b[y-j]) tmp[x+1][j]=1;
                    }
                }
                rep(x,0,m)rep(j,0,3) tmp[x+1][j]+=tmp[x][j];

                ll x;
                rep(i,0,m) if (sa[i].second==0) x=i;
                auto check=[&](ll y,ll d) {
                    if (d>m-1) return false;
                    if (d==0) return true;
                    ll I=(x==0?1:0);
                    ll J=(m-d<=y?m-d-1:m-d);
                    while (J<m){
                        ll t=m-J;
                        if (I<x) chmin(t,x-I);
                        if (J<y) chmin(t,y-J);
                        bool B1=false,B2;
//                        rep(j,0,t) B1|=(sa[I+j].first>=b[J+j]);
                        ll J_=m-k+I;
                        B2=(tmp[I+t][J_-J]-tmp[I][J_-J]>0);
/*                        if (B1!=B2) {
                            cout<<B1<<' '<<B2<<' '<<m<<' '<<x<<' '<<y<<' '<<I<<' '<<J<<' '<<t<<' '<<k<<endl;
                            for (auto hoge:sa) cout<<hoge.first<<' ';cout<<endl;
                            for (auto hoge:b) cout<<hoge<<' ';cout<<endl;
                        }
                        assert(B1==B2);*/
                        if (B2) return false;
                        I+=t,J+=t;
                        if (I==x) I++;
                        if (J==y) J++;
                    }
                    return true;
                };
                rrep(j,0,b.size()) {
                    if (check(j,k-(b[j]>a[0]))) {
                        k-=(b[j]>a[0]);
                        rs.emplace_back(b[j]);
                        a.erase(a.begin());
                        b.erase(b.begin()+j);
                        break;
                    }
                }
                assert(a.size()==m-1);
            }
            assert(rs.size()==n);
            rep(i,0,n) cout<<rs[i]<<" \n"[i==n-1];
            break;
        }
    }
}
accuracy: 1
time_ms: 850
memory_kb: 4096
final_rank: 9

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define ALL(v) v.begin(),v.end()
#define dbg(x) cerr << #x << ": " << x << endl;

int main() {
    int n;
    cin >> n;
    vector<int> p(n), f(n);
    for (int i = 0; i < n; ++i) cin >> p[i];
    for (int i = 0; i < n; ++i) cin >> f[i];
    sort(ALL(f));
    int maxwin = 0;
    {
    multiset<int> ms(ALL(f));
    for (int i = 0; i < n; ++i)
    {
        auto itr = ms.upper_bound(p[i]);
        if (itr != ms.end())
        {
            ++maxwin;
            ms.erase(itr);
        }
    }
    }
    vector<int> ans(n, -1), now = f;
    auto check = [&](const multiset<int> &me, const multiset<int> &opp, int cnt) -> bool
    {      
        int tmp = 0;
        auto me_iter = me.begin();
        for (auto opp_iter = opp.begin(); opp_iter != opp.end(); ++opp_iter) {
            while (me_iter != me.end() && *me_iter <= *opp_iter) ++me_iter;
            if (me_iter == me.end()) break;
            ++tmp;
            ++me_iter;
        }
        return tmp >= cnt;
    };

    multiset<int> me(ALL(f));
    multiset<int> opp(ALL(p));
    int win_num = maxwin;
    for (int i = 0; i < n; ++i)
    {   
        opp.erase(opp.find(p[i]));
        vector<int> vme(ALL(me));
        //win
        int l = upper_bound(ALL(vme),p[i]) - vme.begin(), r = vme.size();
        while (r - l > 1)
        {
            int mid = (l + r) / 2;

            me.erase(me.find(vme[mid]));

            if (check(me,opp,win_num-1)) l = mid;
            else r = mid;

            me.insert(vme[mid]);
        }

        if (l != vme.size()) {
            me.erase(me.find(vme[l]));
            if (check(me,opp,win_num-1)) {
                --win_num;
                ans[i] = vme[l];
                continue;
            }
            me.insert(vme[l]);
        }

        //lose
        l = 0, r = upper_bound(ALL(vme),p[i]) - vme.begin();
        while (r - l > 1)
        {
            int mid = (l + r) / 2;

            me.erase(me.find(vme[mid]));

            if (check(me,opp,win_num)) l = mid;
            else r = mid;

            me.insert(vme[mid]);
        }

        me.erase(me.find(vme[l]));
        ans[i] = vme[l];
    }

    for (int i = 0; i < n; i++) {
        cout << ans[i] << " \n"[i==n-1];
    }
}
accuracy: 1
time_ms: 3180
memory_kb: 3996
final_rank: 16

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define SZ(v) (int)v.size()
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))
#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())
#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())

using uint = unsigned int;
using ll = long long int;
using ull = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;

template <typename T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T, typename U> T ceil(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U> T floor(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T> int popcnt(T x) {
    return __builtin_popcountll(x);
}
template <typename T> int topbit(T x) {
    return (x == 0 ? -1 : 63 - __builtin_clzll(x));
}
template <typename T> int lowbit(T x) {
    return (x == 0 ? -1 : __builtin_ctzll(x));
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << "P(" << p.first << ", " << p.second << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
    os << "{";
    for (int i = 0; i < vec.size(); i++) {
        os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
    }
    os << "}";
    return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
    os << "{";
    for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
        os << "(" << itr->first << ", " << itr->second << ")";
        itr++;
        if (itr != map_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {
    os << "{";
    for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
        os << *itr;
        ++itr;
        if (itr != set_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
#ifdef LOCAL
#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)
#else
#define show(...) true
#endif
template <typename T> void _show(int i, T name) {
    cerr << '\n';
}
template <typename T1, typename T2, typename... T3>
void _show(int i, const T1 &a, const T2 &b, const T3 &...c) {
    for (; a[i] != ',' && a[i] != '\0'; i++)
        cerr << a[i];
    cerr << ":" << b << " ";
    _show(i + 1, a, c...);
}

/**
 * @brief template
 */

int Solve(vector<int> A, vector<int> B) {
    int MAX = 0;
    int ID=0;
    rep(i,0,A.size()) {
        while(ID != A.size() && B[ID] <= A[i]) ID++;
        chmax(MAX, ID-i);
    }
    return (int)A.size() - MAX;
}

int main() {
    int N;
    cin >> N;
    vector<int> A(N), B(N), SA(N);
    rep(i,0,N) cin >> A[i], SA[i] = A[i];
    rep(i,0,N) cin >> B[i];
    sort(ALL(SA)), sort(ALL(B));
    int X = Solve(SA,B);
    vector<int> ANS(N);
    rep(i,0,N) {
        int M = A.size();
        int L = LB(B,A[0]+1);
        bool check = false;
        if (L < M) {
            vector<int> NA, NB, NSA;
            bool check2 = false;
            rep(j,0,M) if (j != 0) NA.push_back(A[j]);
                rep(j,0,M) {
                    if (!check2 && SA[j] == A[0]) check2 = true;
                    else NSA.push_back(SA[j]);
                }
            rep(j,0,M) if (j != L) NB.push_back(B[j]);
            if (Solve(NSA,NB) + 1 == X) check = true;
        }
        int P;
        if (check) {
            int OK = L, NG = M;
            while(NG-OK > 1) {
                int MID = (OK + NG) / 2;
                vector<int> NA, NB, NSA;
                bool check2 = false;
                rep(j,0,M) if (j != 0) NA.push_back(A[j]);
                rep(j,0,M) {
                    if (!check2 && SA[j] == A[0]) check2 = true;
                    else NSA.push_back(SA[j]);
                }
                rep(j,0,M) if (j != MID) NB.push_back(B[j]);
                if (Solve(NSA,NB) + 1 == X) OK = MID;
                else NG = MID;
            }
            P = OK;
            {
                vector<int> NA, NB, NSA;
                bool check2 = false;
                rep(j,0,M) if (j != 0) NA.push_back(A[j]);
                rep(j,0,M) {
                    if (!check2 && SA[j] == A[0]) check2 = true;
                    else NSA.push_back(SA[j]);
                }
                rep(j,0,M) if (j != P) NB.push_back(B[j]);
                ANS[i] = B[P];
                swap(A,NA), swap(B,NB), swap(SA, NSA);
                X--;
            }
        }
        else {
            int OK = 0, NG = L;
            while(NG-OK > 1) {
                int MID = (OK + NG) / 2;
                vector<int> NA, NB, NSA;
                bool check2 = false;
                rep(j,0,M) if (j != 0) NA.push_back(A[j]);
                rep(j,0,M) {
                    if (!check2 && SA[j] == A[0]) check2 = true;
                    else NSA.push_back(SA[j]);
                }
                rep(j,0,M) if (j != MID) NB.push_back(B[j]);
                if (Solve(NSA,NB) == X) OK = MID;
                else NG = MID;
            }
            P = OK;
            {
                vector<int> NA, NB, NSA;
                bool check2 = false;
                rep(j,0,M) if (j != 0) NA.push_back(A[j]);
                rep(j,0,M) {
                    if (!check2 && SA[j] == A[0]) check2 = true;
                    else NSA.push_back(SA[j]);
                }
                rep(j,0,M) if (j != P) NB.push_back(B[j]);
                ANS[i] = B[P];
                swap(A,NA), swap(B,NB), swap(SA,NSA);
            }
        }
    }
    rep(i,0,N) cout << ANS[i] << (i == N-1 ? '\n' : ' ');
}
accuracy: 1
time_ms: 1210
memory_kb: 3648
final_rank: 12

code_snippet: #include <bits/stdc++.h>
using namespace std;

using ll = long long;

ll N; 
vector<ll> P, F;

// P[id+1 : N)
ll get_max(ll id, multiset<ll> mf) {
  ll mx = *mf.rbegin();
  for (ll i = id + 1; i < N; ++i) {
    ll p = P[i];
    auto itr = mf.upper_bound(p);
    if (itr != mf.end()) mf.erase(itr);
  }
  ll ret = *mf.rbegin();
  if (P[id] < mx && P[id] >= ret) ret = mx;
  
  return ret;
}

const ll amax = 1010;

void solve() {
  cin >> N;
  P.resize(N), F.resize(N);
  for (auto &p : P) cin >> p;
  for (auto &f : F) cin >> f;
  
  multiset<ll> mf;
  for (auto f : F) mf.insert(f);
  
  vector<ll> res;
  for (ll i = 0; i < N; ++i) {
    ll m = get_max(i, mf);
    res.emplace_back(m);
    auto itr = mf.lower_bound(m);
    mf.erase(itr);
  }
  
  for (int i = 0; i < N; ++i) {
    cout << res[i] << " \n"[i+1==N];
  }
  
  return;
}

int main() {
  solve();
  
  return 0;
}
accuracy: 1
time_ms: 990
memory_kb: 4096
final_rank: 10

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()

int solve(vector<int> a, vector<int> b){
    int ind = 0, ret=0;
    for(int i=0; i<a.size(); ++i){
        while(ind<b.size() && b[ind]<=a[i]){
            ++ind;
        }
        if(ind<b.size()) ++ret;
        ++ind;
    }
    return ret;
}
 
int main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    int n;
    cin >> n;
    vector<int> p(n), f(n);
    for(int &i : p) cin >> i;
    for(int &i : f) cin >> i;

    sort(all(f));

    vector<int> sorted_p = p;
    sort(all(sorted_p));

    vector<int> ans(n);

    for(int i=0; i<n; ++i){
        int mx = solve(sorted_p, f);
        int num = p[i];
        sorted_p.erase(lower_bound(all(sorted_p), p[i]));
        vector<int> tmp = f; 
        auto it = upper_bound(all(tmp),num);
        int l=0, r=f.size()-1, take=0;
        if(it != tmp.end()){
            tmp.erase(it);
            if(solve(sorted_p, tmp)+1 == mx) l = upper_bound(all(f), num) - f.begin(), ++take;
            else r = upper_bound(all(f), num) - f.begin() - 1;
        }
        while(r>l){
            int mid = (l+r+1)/2;
            vector<int> tmp = f;
            tmp.erase(lower_bound(all(tmp), f[mid]));
            if(solve(sorted_p, tmp) + take == mx) l = mid;
            else r = mid-1;
        }
        ans[i] = f[l];
        f.erase(lower_bound(all(f), f[l]));
    }

    for(int i=0; i<n-1; ++i){
        cout << ans[i] << " ";
    }
    cout << ans[n-1] << '\n';

    return 0;
}
accuracy: 1
time_ms: 120
memory_kb: 3672
final_rank: 3

code_snippet: #include <bits/stdc++.h>
using namespace std;

//calc bestscore of Ms.F
int best_score(vector<int> P,vector<int> F){
    int N=F.size();
    
    int res=0,k=0;
    for(int i=0;i<2*N;i++){
        bool f=0;
        if(P.size()==0)f=1;
        else if(F.size()==0)f=0;
        else if(P.back()<F.back())f=1;
        if(f){
            F.pop_back();
            k++;
        }
        else{
            P.pop_back();
            if(k>0){
                res++;
                k--;
            }
        }
    }
    return res;
}

vector<int> exclude(vector<int> A,int x){
    int n=A.size();
    vector<int> NA(n-1);
    bool ex=0;
    for(int i=0;i<n;i++){
        if(A[i]==x&&!ex)ex=1;
        else NA[i-ex]=A[i];
    }
    return NA;
}

int main() {
    int N;
    cin>>N;
    vector<int> P(N),F(N);
    for(int i=0;i<N;i++)cin>>P[i];
    for(int i=0;i<N;i++)cin>>F[i];
    // for(int i=0;i<N;i++)P[i]=rand()%10000+1;
    // for(int i=0;i<N;i++)F[i]=rand()%10000+1;
    vector<int> original_P=P;
    sort(P.begin(),P.end());
    sort(F.begin(),F.end());
    int BS=best_score(P,F);
    vector<int> ANS;
    int won=0;
    for(int i=0;i<N;i++){
        int p=original_P[0];
        P=exclude(P,p);
        original_P=exclude(original_P,p);
        int left=upper_bound(F.begin(),F.end(),p)-F.begin();
        //win
        {
            int L=left-1;
            int R=F.size();
            while(R-L>1){
                int M=(R+L)/2;
                if(best_score(P,exclude(F,F[M]))+won+1==BS){
                    L=M;
                }
                else{
                    R=M;
                }
            }
            if(L>=left){
                won++;
                ANS.push_back(F[L]);
                F=exclude(F,F[L]);
            }
        }
        //lose
        if(ANS.size()<=i){
            int L=0;
            int R=left;
            while(R-L>1){
                int M=(R+L)/2;
                if(best_score(P,exclude(F,F[M]))+won==BS){
                    L=M;
                }
                else{
                    R=M;
                }
            }
            ANS.push_back(F[L]);
            F=exclude(F,F[L]);
        }
    }
    for(int i=0;i<N;i++)cout<<ANS[i]<<" \n"[i==N-1];
}
accuracy: 1
time_ms: 830
memory_kb: 3648
final_rank: 8

code_snippet: #include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define per(i, n) for (int i = (n)-1; i >= 0; i--)
#define rep2(i, l, r) for (int i = (l); i < (r); i++)
#define per2(i, l, r) for (int i = (r)-1; i >= (l); i--)
#define each(e, x) for (auto &e : x)
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define eb emplace_back
#define MM << ' ' <<
#define TT template <typename T>
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

TT bool chmin(T &x, T y) { return x > y ? (x = y, true) : false; }
TT bool chmax(T &x, T y) { return x < y ? (x = y, true) : false; }

TT using minheap = priority_queue<T, vector<T>, greater<T>>;
TT using maxheap = priority_queue<T>;

TT void print(vector<T> v, T x = 0) {
    int n = sz(v);
    rep(i, n) cout << v[i] + x << (i == n - 1 ? '\n' : ' ');
    if (n == 0) cout << '\n';
}

TT void printn(vector<T> v, T x = 0) {
    int n = sz(v);
    rep(i, n) cout << v[i] + x << '\n';
}

TT void rearrange(vector<T> &v) {
    sort(all(v));
    v.erase(unique(all(v)), end(v));
}

TT int lb(vector<T> &v, T x) { return lower_bound(all(v), x) - begin(v); }
TT int ub(vector<T> &v, T x) { return upper_bound(all(v), x) - begin(v); }

const int inf = (1 << 30) - 1;
const ll INF = (1LL << 60) - 1;

struct io_setup {
    io_setup() {
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        cout << fixed << setprecision(15);
    }
} io_setup;

int main() {
    auto check = [](const vector<int> &a, const vector<int> &b) {
        int n = sz(a), idx = 0, ret = n;
        per(i, n) {
            while (idx < n && a[idx] < b[i]) idx++;
            chmin(ret, idx + i);
        }
        return ret;
    };
    int n;
    cin >> n;
    vector<int> p(n), f(n);
    rep(i, n) cin >> p[i];
    rep(i, n) cin >> f[i];
    sort(rall(f));
    auto cp = p;
    sort(all(cp));
    int x = check(cp, f);
    vector<int> ans;
    int nx = 0;
    rep(i, n) {
        vector<int> cnp;
        int v = p[0];
        rep2(j, 1, n - i) cnp.eb(p[j]);
        sort(all(cnp));
        int ok = n + 1, ng = -1;
        int l = 0;
        while (l < n - i && f[l] > v)
            l++;
        ok = l, ng = -1;
        while (abs(ok - ng) > 1) {
            int mid = (ok + ng) / 2;
            vector<int> nf;
            rep(j, n - i) if (j != mid) nf.eb(f[j]);
            (nx + check(cnp, nf) + 1 == x ? ok : ng) = mid;
        }
        if (ok == l) {
            ok = n - i - 1, ng = -1;
            while (abs(ok - ng) > 1) {
                int mid = (ok + ng) / 2;
                vector<int> nf;
                rep(j, n - i) if (j != mid) nf.eb(f[j]);
                (nx + check(cnp, nf) == x ? ok : ng) = mid;
            }
        }
        nx += (v < f[ok]);
        ans.eb(f[ok]);
        vector<int> np, nf;
        rep2(j, 1, n - i) np.eb(p[j]);
        rep(j, n - i) if (j != ok) nf.eb(f[j]);
        swap(p, np), swap(f, nf);
    }
    print(ans);
}
accuracy: 1
time_ms: 1150
memory_kb: 3652
final_rank: 11

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll =long long;
#define rep(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define all(p) p.begin(),p.end()

int main(){
    int N;
    cin>>N;
    vector<int> A(N*2);
    rep(i,0,N*2) cin>>A[i];
    vector<int> order(N*2),rev(N*2);
    rep(i,0,N*2) order[i]=i;
    sort(all(order),[&](int l,int r){
        if(A[l]==A[r]) return l<r;
        return A[l]>A[r];
    });
    rep(i,0,N*2) rev[order[i]]=i;
    vector<int> seen(N*2),ans(N);
    rep(i,0,N){
        int mim=0,val=0,l=0,r=0,ind=-1;
        rep(j,0,N*2){
            if(seen[j]) continue;
            if(order[j]<N) val--;
            else val++;
            if(mim==val) r=j+1;
            if(mim>val) mim--,l=j+1,r=j+1;
        }
        if(l<=rev[i]&&rev[i]<r){
            for(int j=r-1;j>=l;j--){
                if(seen[j]) continue;
                if(order[j]<N) val++;
                else val--,ind=j;
                if(val==0&&j<rev[i]) break;
            }
        }
        else if(r<=rev[i]){
            ind=r;
            while(seen[ind]) ind++;
        }
        else{
            for(int j=N*2-1;j>=r;j--){
                if(seen[j]) continue;
                if(order[j]>=N){
                    ind=j;
                }
            }
            for(int j=l-1;j>=0;j--){
                if(seen[j]) continue;
                if(order[j]>=N&&j<rev[i]) ind=j;
            }
        }
        assert(ind!=-1);
        seen[ind]=1;
        seen[rev[i]]=1;
        ans[i]=A[order[ind]];
    }
    rep(i,0,N){
        cout<<ans[i];
        cout<<(N-i-1?" ":"\n");
    }
}
accuracy: 1
time_ms: 190
memory_kb: 3608
final_rank: 4

code_snippet: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <tuple>
#include <cstdint>
#include <cstdio>
#include <map>
#include <cstdint>
#include <queue>
#include <set>
#include <stack>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <cctype>
#include <functional>
#include <ctime>
#include <fstream>
#include <cmath>
#include <limits>
#include <chrono>
#include <numeric>
#include <type_traits>
#include <iomanip>
#include <float.h>
#include <math.h>
#include <cassert>
#include <random>

#include <cstdint>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
using ll = long long;




ll ll_gcd(ll a, ll b) {
	if (a < b) return ll_gcd(b, a);
	ll r;
	while ((r = a % b)) {
		a = b;
		b = r;
	}
	return b;
}

long long modpow(long long a, long long n, long long mod) {
	if (n < 0)return 0;
	long long res = 1;
	while (n > 0) {
		if (n & 1) res = res * a % mod;
		a = a * a % mod;
		n >>= 1;
	}
	return res;
}

long long modinv(long long a, long long mod) {
	return modpow(a, mod - 2, mod);
}

ll merge_cnt(vector<ll>& a) {
	int n = a.size();
	if (n <= 1) { return 0; }

	ll cnt = 0;
	vector<ll> b(a.begin(), a.begin() + n / 2);
	vector<ll> c(a.begin() + n / 2, a.end());

	cnt += merge_cnt(b);
	cnt += merge_cnt(c);

	int ai = 0, bi = 0, ci = 0;
	while (ai < n) {
		if (bi < b.size() && (ci == c.size() || b[bi] <= c[ci])) {
			a[ai++] = b[bi++];
		}
		else {
			cnt += n / 2 - bi;
			a[ai++] = c[ci++];
		}
	}
	return cnt;
}

template< typename T >
size_t longest_increasing_subsequence(const vector< T >& a, bool strict) {
	vector< T > lis;
	for (auto& p : a) {
		typename vector< T >::iterator it;
		if (strict) it = lower_bound(begin(lis), end(lis), p);
		else it = upper_bound(begin(lis), end(lis), p);
		if (end(lis) == it) lis.emplace_back(p);
		else *it = p;
	}
	return lis.size();
}


constexpr uint32_t PrimitiveRoot(uint32_t mod) {
	using u64 = uint64_t;
	if (mod == 2) return 1;
	u64 ds[32] = {};
	int idx = 0;
	u64 m = mod - 1;
	for (u64 i = 2; i * i <= m; ++i) {
		if (m % i == 0) {
			ds[idx++] = i;
			while (m % i == 0) m /= i;
		}
	}
	if (m != 1) ds[idx++] = m;

	uint32_t pr = 2;
	while (1) {
		int flg = 1;
		for (int i = 0; i < idx; ++i) {
			u64 a = pr, b = (mod - 1) / ds[i], r = 1;
			while (b) {
				if (b & 1) r = r * a % mod;
				a = a * a % mod;
				b >>= 1;
			}
			if (r == 1) {
				flg = 0;
				break;
			}
		}
		if (flg == 1) break;
		++pr;
	}
	return pr;
}


struct edge { ll to; ll cost; };
typedef pair<ll, ll> P;
struct graph {
	ll V;
	vector<vector<edge> > G;
	vector<ll> d;

	graph(ll n) {
		init(n);
	}

	void init(ll n) {
		V = n;
		G.resize(V);
		d.resize(V);
		for (int i = 0; i < V; i++) {
			d[i] = 2000000000000000000;
		}
	}

	void add_edge(ll s, ll t, ll cost) {
		edge e;
		e.to = t, e.cost = cost;
		G[s].push_back(e);
	}

	void dijkstra(ll s) {
		for (int i = 0; i < V; i++) {
			d[i] = 2000000000000000000;
		}
		d[s] = 0;
		priority_queue<P, vector<P>, greater<P> > que;
		que.push(P(0, s));
		while (!que.empty()) {
			P p = que.top(); que.pop();
			ll v = p.second;
			if (d[v] < p.first) continue;
			for (auto e : G[v]) {
				if (d[e.to] > d[v] + e.cost) {
					d[e.to] = d[v] + e.cost;
					que.push(P(d[e.to], e.to));
				}
			}
		}
	}
};

/*struct UnionFind {
	vector <ll> par;
	vector <ll> siz;
	UnionFind(ll sz_) : par(sz_), siz(sz_, 1LL) {
		for (ll i = 0; i < sz_; ++i) par[i] = i;
	}
	void init(ll sz_) {
		par.resize(sz_);
		siz.assign(sz_, 1LL);
		for (ll i = 0; i < sz_; ++i) par[i] = i;
	}
	ll root(ll x) {
		while (par[x] != x) {
			x = par[x] = par[par[x]];
		}
		return x;
	}
	bool merge(ll x, ll y) {
		x = root(x);
		y = root(y);
		if (x == y) return false;
		if (siz[x] < siz[y]) swap(x, y);
		siz[x] += siz[y];
		par[y] = x;
		return true;
	}

	bool issame(ll x, ll y) {
		return root(x) == root(y);
	}

	ll size(ll x) {
		return siz[root(x)];
	}
};*/




void sccdfs(vector<vector<int>>& z,int &now, vector<bool> &a,vector<int> &cnt) {
	for (int i = 0; i < z[now].size(); i++) {
		if (!a[z[now][i]]) {
			a[z[now][i]] =true;
			sccdfs(z, z[now][i], a, cnt);
		}
	}
	cnt.push_back(now);
	return;
}

void sccdfs2(vector<vector<int>>& z, int& now, vector<bool>& b, vector<int> &ans) {
	for (int i = 0; i < z[now].size(); i++) {
		if (!b[z[now][i]]) {
			b[z[now][i]] = true;
			sccdfs2(z, z[now][i], b, ans);
		}
	}
	ans.push_back(now);
	return;
}

vector<vector<int>> scc(vector<vector<int>> &z) {
	int n = z.size();
	vector<bool> a(n,false);
	vector<int> p;
	int cnt = 0;
	for (int i = 0; i < n; i++) {
		if (!a[i]){
			a[i] = true;
			sccdfs(z, i, a, p);
		}
	}
	vector<vector<int>> ans;
	vector<bool> b(n, false);
	vector<vector<int>> x(n);
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < z[i].size(); j++) {
			x[z[i][j]].push_back(i);
		}
	}
	for (int i = 0; i < n; i++) {
		if (!b[p[n-i-1]]) {
			vector<int> f;
			b[p[n - i - 1]] = true;
			sccdfs2(x, p[n - i - 1], b, f);
			ans.push_back(f);
		}
	}
	return ans;
}



int main() {
	ios::sync_with_stdio(false);
	std::cin.tie(nullptr);
	int n;
	cin >> n;
	vector<int> z(n);
	vector<int> x(n);
	for (int i = 0; i < n; i++) {
		cin >> z[i];
	}
	for (int i = 0; i < n; i++) {
		cin >> x[i];
	}
	vector<int> a = z;
	sort(a.begin(), a.end());
	sort(x.begin(), x.end());
	ll aok = 0;
	ll ang = n+1;
	while (ang - aok > 1) {
		ll mid = (aok + ang) / 2;
		bool s = true;
		for (int i = 0; i < mid; i++) {
			if (x[n - mid + i] <= a[i])s = false;
		}
		if (s)aok = mid;
		else ang = mid;
	}
	vector<int> ans(n);
	vector<bool> b(n,true);
	ll now = 0;
	for (int i = 0; i < n; i++) {
		vector<int> f;
		vector<int> g;
		vector<int> ga;
		for (int j = 0; j < n; j++) {
			if (b[j])f.push_back(x[j]);
			if (i <= j)g.push_back(z[j]);
			if (i < j)ga.push_back(z[j]);
		}
		sort(ga.begin(), ga.end());

		int dd = -1;
		for (int j = 0; j < f.size(); j++) {
			if (f[j] <= g[0])dd = j;
		}
		ll ok = dd;
		ll ng = n-i;
		while (ng-ok>1) {
			ll mid = (ok + ng) / 2;
			ll need = aok - now;
			need--;
			vector<int> h;
			for (int j = 0; j < f.size(); j++) {
				if (j!=mid)h.push_back(f[j]);
			}
			bool s = true;
			for (int j = 0; j < need; j++) {
				if(need>=n-i||j>=n-i||h[n-i-1-need+j]<=ga[j])s=false;
			}
			if (s)ok = mid;
			else ng = mid;
			
		}
		if (ok == dd) {
			ok = 0;
			ng = dd+1;
			while (ng - ok > 1) {
				ll mid = (ok + ng) / 2;
				ll need = aok - now;
				vector<int> h;
				for (int j = 0; j < f.size(); j++) {
					if (j != mid)h.push_back(f[j]);
				}
				bool s = true;
				for (int j = 0; j < need; j++) {
					if (need >= n - i || j >= n - i || h[n - i - 1 - need + j] <= ga[j])s = false;
				}
				if (s)ok = mid;
				else ng = mid;

			}
		}
		ans[i] = f[ok];
		if (f[ok] > g[0])now++;
		for (int j = 0; j < n; j++) {
			if (f[ok] == x[j]&&b[j]) {
				b[j] = false;
				break;
			}
		}
	}
	for (int i = 0; i < n; i++) {
		cout << ans[i];
		if (i != n - 1)cout << " ";
	}
	cout << endl;
}
accuracy: 1
time_ms: 750
memory_kb: 3672
final_rank: 7

code_snippet: #include <bits/stdc++.h>

#define rep(i, s, n) for (int i = int(s); i < int(n); i++)
#define rrep(i, s, n) for (int i = int(n) - 1; i >= int(s); i--)
#define all(v) (v).begin(), (v).end()

using ll = long long;
using ull = unsigned long long;
using ld = long double;

template <class T> bool chmin(T &a, T b) {
    if (a <= b) return false;
    a = b;
    return true;
}

template <class T> bool chmax(T &a, T b) {
    if (a >= b) return false;
    a = b;
    return true;
}

using namespace std;

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    int n; cin >> n;
    vector<int> p(n), f(n);
    rep(i,0,n) cin >> p[i];

    rep(i,0,n) cin >> f[i];
    sort(f.begin(), f.end());

    vector<pair<int,int>> naosu;
    rep(i,0,n){
        naosu.push_back(pair(p[i], i));
    }
    sort(naosu.begin(), naosu.end());
   
    vector<int> cores(n);
    rep(i,0,n){
        cores[i] = naosu[i].second;
    }
    
    vector<bool> used(n);
    vector<bool> used_p(n);
    vector<int> ans(n);

    auto hantei = [&]() -> int {
        int p1 = 0;
        int p2 = 0;
        int ret = 0;
        while (p1 < n){
            if (used_p[cores[p1]]){
                p1++;
                continue;
            }
            while(p2 < n && (f[p2] <= p[cores[p1]] || used[p2])){
                p2++;
            }
            if (p2 < n){
                ret++;
                p2++;
            }
            p1++;
        }
        return ret;
    };

    auto tries = [&](int x, int i) -> bool {
        assert(!used[x]);
        int k1 = hantei();
        used[x] = true;
        used_p[i] = true;
        int k2 = hantei() + (f[x] > p[i]);
        used[x] = false;
        used_p[i] = false;
        return k2 == k1;
    };

    rep(i,0,n){
        int ind = -1;
        rep(j,0,n){
            if (!used[j] && p[i] < f[j]){
                ind = j;
                break;
            }
        }
        if (ind >= 0 && tries(ind, i)){
            // UPPER GA OK.
            // OK ----- / ----- NG
            vector<int> dars;
            rep(j,ind,n){
                if (!used[j]) dars.push_back(j);
            }
            int m = dars.size();
            assert(m >= 1);
            int ub = m;
            int lb = 0;

            while(ub - lb > 1){
                int targ = (ub + lb) / 2;
                if (tries(dars[targ], i)) lb = targ;
                else ub = targ;
            }

            used[dars[lb]] = true;
            ans[i] = f[dars[lb]];
        }else{
            // LOWER GA OK.
            // OK ----- / ----- NG
            if (ind == -1) ind = n;
            vector<int> dars;
            rep(j,0,ind){
                if (!used[j]) dars.push_back(j);
            }

            int m = dars.size();
            assert(m >= 1);
            int ub = m;
            int lb = 0;

            while(ub - lb > 1){
                int targ = (ub + lb) / 2;
                if (tries(dars[targ], i)) lb = targ;
                else ub = targ;
            }

            used[dars[lb]] = true;
            ans[i] = f[dars[lb]];
        }
        used_p[i] = true;
    }

    rep(i,0,n){
        cout << ans[i];
        if (i==n-1) cout << '\n';
        else cout << ' ';
    }
}
accuracy: 1
time_ms: 2360
memory_kb: 3572
final_rank: 15

code_snippet: #include "bits/stdc++.h"

using namespace std;

using ll=long long;

using ld=long double;

void solve(){
    int n;cin>>n;
    vector<int>p(n),q(n);
    for (int i = 0; i < n; ++i) {
        cin>>p[i];
    }
    for (int i = 0; i < n; ++i) {
        cin>>q[i];
    }
    vector<int>used(n);
    auto f=[&](vector<int>&p,vector<int>&q,vector<int>&ord){
        int ret=0;
        int sz=p.size();
        int j=0;
        int sz2=q.size();
        for(auto i:ord){
            while(j<sz2 and (p[i]>=q[j] or used[j]))j++;
            if(j>=sz2)break;
            ret++;
            j++;
        }
        return ret;
    };
    std::reverse(p.begin(), p.end());
    std::sort(q.begin(), q.end());
    vector<int>tmp(n);
    std::iota(tmp.begin(), tmp.end(),0);
    std::sort(tmp.begin(), tmp.end(),[&](int i,int j){return p[i]<p[j];});
    int k=f(p,q,tmp);
    int now=0;
    vector<int>ans(n);
    for (int i = 0; i < n; ++i) {
        int sz=q.size();
        int ng=sz;
        int ok=0;
        used.assign(sz,0);
        auto b=p.back();p.pop_back();
        vector<int>ord(sz-1);
        std::iota(ord.begin(), ord.end(),0);
        std::sort(ord.begin(), ord.end(),[&](int i,int j){return p[i]<p[j];});
        if(b<q.back())now++;
        while(ng-ok>1){
            int cen=(ok+ng)/2;
            used[cen]=1;
            if(now+f(p,q,ord)>=k){
                ok=cen;
            }
            else{
                ng=cen;
            }
            used[cen]=0;
        }
        ans[i]=q[ok];
        q.erase(q.begin()+ok);
    }
    for (int i = 0; i < n; ++i) {
        cout<<ans[i]<<" \n"[i==n-1];
    }
}



int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t=1;
//    cin>>t;
    while(t--){
        solve();
    }
}
accuracy: 1
time_ms: 630
memory_kb: 3588
final_rank: 6