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

Previous submission (verdict: Runtime error on test 1):
#include <bits/stdc++.h>
using namespace std;

bool canMerge(vector<int> group) {
    priority_queue<int, vector<int>, greater<>> pq;
    for (int num : group) pq.push(num);
    while (pq.size() > 1) {
        int a = pq.top(); pq.pop();
        int b = pq.top(); pq.pop();
        if (abs(a - b) > 1) return false;
        pq.push(a + b);
    }
    return true;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin >> t;
    while (t--) {
        int n, m;
        cin >> n >> m;
        vector<int> a(n), b(m);
        long long sum_a = 0, sum_b = 0;
        for (int &x : a) { cin >> x; sum_a += x; }
        for (int &x : b) { cin >> x; sum_b += x; }
        if (sum_a != sum_b) {
            cout << "No\n";
            continue;
        }
        sort(a.begin(), a.end());
        sort(b.begin(), b.end());
        if (b.back() < a.back()) {
            cout << "No\n";
            continue;
        }
        multiset<int> ms(a.begin(), a.end());
        bool ok = true;
        for (int i = m - 1; i >= 0; --i) {
            int target = b[i];
            long long sum = 0;
            vector<int> grp;
            while (!ms.empty() && sum < target) {
                auto it = prev(ms.end());
                int val = *it;
                ms.erase(it);
                sum += val;
                grp.push_back(val);
                if (sum > target) {
                    ok = false;
                    break;
                }
            }
            if (!ok || sum != target) {
                ok = false;
                break;
            }
            if (!canMerge(grp)) {
                ok = false;
                break;
            }
        }
        cout << (ok ? "Yes\n" : "No\n");
    }
    return 0;
}

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

bool canTransform(vector<int>& a, vector<int>& b) {
    // Calculate the sum of both sequences
    long long sumA = accumulate(a.begin(), a.end(), 0LL);
    long long sumB = accumulate(b.begin(), b.end(), 0LL);
    
    if (sumA != sumB) {
        return false;
    }
    
    // Count frequencies in a and b
    map<int, int> freqA, freqB;
    for (int num : a) freqA[num]++;
    for (int num : b) freqB[num]++;
    
    // Iterate through the numbers in a
    for (auto& [num, count] : freqA) {
        if (freqB.count(num)) {
            int minCount = min(count, freqB[num]);
            count -= minCount;
            freqB[num] -= minCount;
        }
        
        // Try to combine numbers to match frequencies in b
        if (count > 0) {
            if (freqB.count(num + 1)) {
                int minCount = min(count, freqB[num + 1]);
                count -= minCount;
                freqB[num + 1] -= minCount;
            }
            if (count > 0) {
                return false;
            }
        }
    }
    
    // Check if all frequencies in b are matched
    for (auto& [num, count] : freqB) {
        if (count > 0) {
            return false;
        }
    }
    
    return true;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int t;
    cin >> t;
    while (t--) {
        int n, m;
        cin >> n >> m;
        vector<int> a(n), b(m);
        for (int i = 0; i < n; ++i) cin >> a[i];
        for (int i = 0; i < m; ++i) cin >> b[i];
        
        if (canTransform(a, b)) {
            cout << "Yes\n";
        } else {
            cout << "No\n";
        }
    }
    
    return 0;
}