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

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

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

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;
    for (ll x : a) {
      freq[x]++;
    }
    map<ll, ll> board;
    for (ll x : b) {
      board[x]++;
    }
    bool ok = true;
    while (!board.empty()) {
      auto it = board.rbegin();
      ll x = it->first;
      ll cnt = it->second;
      board.erase(x);
      if (freq.find(x) != freq.end()) {
        ll available = freq[x];
        ll use = min((ll)available, cnt);
        freq[x] -= use;
        cnt -= use;
      }
      if (cnt > 0) {
        if (x == 1) { 
          ok = false;
          break;
        }
        ll l = x / 2;
        ll r = x - l;
        board[l] += cnt;
        board[r] += cnt;
      }
    }
    for (auto &p : freq) {
      if (p.second != 0) {
        ok = false;
        break;
      }
    }
    cout << (ok ? "Yes" : "No") << "\n";
  }
  return 0;
}



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;
}