#include <bits/stdc++.h>
using namespace std;
typedef int ll;
const ll INF = 1e9+7;
const ll MOD = 998244353;
typedef pair<ll,ll> ii;
#define iii pair<ii,ll>
#define f(i,a,b) for(ll i = a;i < b;i++)
#define pb push_back
#define vll vector<ll>
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
///I hope I will get uprating and don't make mistakes
///I will never stop programming
///sqrt(-1) Love C++
///Please don't hack me
///@TheofanisOrfanou Theo830
///Think different approaches (bs,dp,greedy,graphs,shortest paths,mst)
///Stay Calm
///Look for special cases
///Beware of overflow and array bounds
///Think the problem backwards
///Training
#include "hieroglyphs.h"
vector<int> ucs(std::vector<int> a, std::vector<int> b){
    vector<int>ans;
    map<ll,ll>mp1,mp2;
    for(auto x:a){
        mp1[x]++;
    }
    for(auto x:b){
        mp2[x]++;
    }
    vector<int>A,B;
    vector<int>posi;
    ll z = 0;
    posi.pb(-1);
    for(auto x:a){
        if(mp1[x] <= mp2[x]){
            A.pb(x);
            posi.pb(z);
        }
        z++;
    }
    for(auto x:b){
        if(mp2[x] < mp1[x]){
            B.pb(x);
        }
    }
    while(!B.empty()){
        bool ivra = 0;
        f(j,posi.back() + 1,(ll)(a.size())){
            if(a[j] == B.back()){
                ivra = 1;
                break;
            }
        }
        if(ivra){
            ans.pb(B.back());
            B.pop_back();
        }
        else{
            posi.pop_back();
            ans.pb(A.back());
            A.pop_back();
        }
    }
    while(!A.empty()){
        ans.pb(A.back());
        A.pop_back();
    }
    reverse(all(ans));
    bool ok = 1;
    ll pos = 0;
    for(auto x:a){
        if(pos < ans.size() && ans[pos] == x){
            pos++;
        }
    }
    ok &= pos == (ll)ans.size();
    pos = 0;
    for(auto x:b){
        if(pos < ans.size() && ans[pos] == x){
            pos++;
        }
    }
    ok &= pos == (ll)ans.size();
    if(!ok){
        //ans.clear();
        //ans.pb(-1);
    }
    return ans;
}
/*
int main() {
  int N, M;
  assert(2 == scanf("%d%d", &N, &M));
  std::vector<int> A(N), B(M);
  for (int i = 0; i < N; i++)
    assert(1 == scanf("%d", &A[i]));
  for (int j = 0; j < M; j++)
    assert(1 == scanf("%d", &B[j]));
  fclose(stdin);
  std::vector<int> R = ucs(A, B);
  int T = (int)R.size();
  printf("%d\n", T);
  for (int i = 0; i < T; i++)
    printf("%s%d", (i == 0 ? "" : " "), R[i]);
  printf("\n");
  fclose(stdout);
  return 0;
}
*/
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |