Problem: 2061D - Kevin and Numbers
Language: C++17 (GCC 7-32)

Previous submission (verdict: Time limit exceeded on test 31):
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define vl vector<ll>
#define uli unordered_map<ll, ll>

struct Node {
  ll val, cnt;
};

struct cmp {
  bool operator() (const Node &a, const Node &b) {
    return a.val < b.val;
  }
};

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

  int t = 0;
  cin >> t;
  while (t--) {
    int n = 0, m = 0;
    cin >> n >> m;
    vl a(n), b(m);
    ll suma = 0, sumb = 0;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
      suma += a[i];
    }
    for (int i = 0; i < m; i++) {
      cin >> b[i];
      sumb += b[i];
    }
    if (suma != sumb) {
      cout << "No\n";
      continue;
    }
    uli freq;
    freq.reserve(n * 2);
    for (ll x : a) {
      freq[x]++;
    }
    uli board;
    board.reserve(m * 2);
    for (ll x : b) {
      board[x]++;
    }
    priority_queue<Node, vector<Node>, cmp> pq;
    for (auto &p : board) {
      pq.push(Node{p.first, p.second});
    }
    bool possible = true;
    ll matched = 0;
    const ll total = n;
    while (!pq.empty() && matched < total) {
      Node cur = pq.top();
      pq.pop();
      if (board.find(cur.val) == board.end() || board[cur.val] != cur.cnt)
        continue;
      ll x = cur.val;
      ll cnt = cur.cnt;
      if (freq.find(x) != freq.end()) {
        ll use = min(freq[x], cnt);
        freq[x] -= use;
        matched += use;
        cnt -= use;
      }
      board.erase(x);
      if (cnt > 0) {
        if (x == 1) {
          possible = false;
          break;
        }
        ll l = x / 2;
        ll r = x - l;
        board[l] += cnt;
        board[r] += cnt;
        pq.push(Node{l, board[l]});
        pq.push(Node{r, board[r]});
      }
    }
    for (auto &p : freq) {
      if (p.second > 0) {
        possible = false;
        break;
      }
    }
    cout << (possible ? "Yes" : "No") << "\n";
  }
  return 0;
}


Submission (verdict: Accepted):
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define vl vector<ll>

bool rec(ll x, multiset<ll> &s) {
    auto it = s.find(x);
    if (it != s.end()) {
        s.erase(it);
        return true;
    }
    if (x == 1)
        return false;
    ll l = x / 2;
    ll r = x - l;
    return rec(l, s) && rec(r, s);
}
 
int main(void) {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int t = 0;
    cin >> t;
    while(t--) {
        int n = 0, m = 0;
        cin >> n >> m;
        vl a(n), b(m);
        ll suma = 0, sumb = 0;
        for (int i = 0; i < n; i++) {
            cin >> a[i];
            suma += a[i];
        }
        for (int i = 0; i < m; i++) {
            cin >> b[i];
            sumb += b[i];
        }
        if(suma != sumb) {
            cout << "No\n";
            continue;
        }
        multiset<ll> s;
        for(auto x : a)
            s.insert(x);
        sort(b.begin(), b.end(), greater<ll>());
        bool possible = true;
        for(auto x : b) {
            if(!rec(x, s)){
                possible = false;
                break;
            }
        }
        cout << (possible && s.empty() ? "Yes" : "No") << "\n";
    }
    return 0;
}