Problem: 2061D - Kevin and Numbers
Language: C++23 (GCC 14-64, msys2)

Previous submission (verdict: Wrong answer on test 3):
#include <bits/stdc++.h>
using namespace std;

void solve() {
    int n, m;
    cin >> n >> m;
    vector<long long> a(n), b(m);
    long long sum_a = 0, sum_b = 0;
    for (auto &x : a) {
        cin >> x;
        sum_a += x;
    }
    for (auto &x : b) {
        cin >> x;
        sum_b += x;
    }
    if (sum_a != sum_b) {
        cout << "No\n";
        return;
    }
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    
    int i = 0, j = 0;
    while (i < n && j < m) {
        long long current_sum = 0;
        long long current_max = 0;
        int start_i = i;
        while (i < n) {
            current_sum += a[i];
            current_max = max(current_max, a[i]);
            i++;
            if (current_sum == b[j]) {
                int group_size = i - start_i;
                if (current_max > b[j]) {
                    cout << "No\n";
                    return;
                }
                if (group_size == 2) {
                    if (a[i-1] - a[start_i] > 1) {
                        cout << "No\n";
                        return;
                    }
                }
                long long rem = current_sum - current_max;
                if (rem < group_size - 1) {
                    cout << "No\n";
                    return;
                }
                j++;
                break;
            } else if (current_sum > b[j]) {
                cout << "No\n";
                return;
            }
        }
    }
    cout << (j == m ? "Yes\n" : "No\n");
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin >> t;
    while (t--) solve();
    return 0;
}

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

using namespace std;

void setIO(string prob) {
    freopen((prob + ".in").c_str(), "r", stdin);
    freopen((prob + ".out").c_str(), "w", stdout);
}
void solve(){
    int n, m;
    cin >> n >> m;
    vector <int> a(n), b(m);
    map <int, int> bCnt;
    deque <int> qa, qb;
    for (int i = 0; i < n; i++) cin >> a[i];
    for (int i = 0; i < m; i++){
        cin >> b[i];
        bCnt[b[i]]++;
    }
    sort(a.begin(), a.end());
    for (int i = 0; i < n; i++){
        if (!bCnt.count(a[i]) || bCnt[a[i]] == 0){
            qa.push_back(a[i]);
        } else if (bCnt.count(a[i]) && bCnt[a[i]] > 0){
            bCnt[a[i]]--;
        }
    }
    for (auto& e : bCnt){
        if (e.second > 0){
            for (int i = 0; i < e.second; i++){
                qb.push_back(e.first);
            }
        }
    }
    //now there is the queue
    bool good = false;
    if (qa.empty() && qb.empty()) good = true;
    while (qa.size() > 1){
        int a = qa.front(); qa.pop_front();
        int b = qa.front(); qa.pop_front();
        if (abs(a - b) <= 1){
            //can do stuff
            if (a + b == qb.front()){
                qb.pop_front();
                if (qa.empty() && qb.empty()) good = true;
            } else{
                qa.push_front(a + b);
            }
        }else{
            qa.push_front(b);
        }
    }
    cout << (good ? "Yes" : "No");



}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    //setIO("txt");
    int t;
    cin >> t;
    while (t--){
        solve();
        cout << "\n";
    }
}