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

Previous submission (verdict: Wrong answer on test 3):
// Problem: D. Kevin and Numbers
// Contest: Codeforces - IAEPC Preliminary Contest (Codeforces Round 999, Div. 1 + Div. 2)
// URL: https://codeforces.com/contest/2061/problem/D
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
using namespace std;
#define boost                    \
	ios::sync_with_stdio(false); \
	cin.tie(0);                  \
	cout.tie(0)

///-----new feature----///
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
///-end of new feature-///

#define scan(x)                                                                   \
	do                                                                            \
	{                                                                             \
		while ((x = getchar()) < '0')                                             \
			;                                                                     \
		int _;                                                                    \
		for (x -= '0'; '0' <= (_ = getchar()); x = (x << 3) + (x << 1) + _ - '0') \
			;                                                                     \
	} while (0)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x.size())
#define fi first
#define se second
#define pb push_back
#define mp make_pair

#define dbg(x) cerr << "  - " << #x << ": " << x << endl;
#define dbgp(x) cerr << "  - " << #x << ": " << x.fi << " , " << x.se << endl;
#define dbg2(x, y) cerr << "  - " << #x << ": " << x << "\t\t" << #y << ": " << y << endl;
#define dbgv(v, n)                                                      \
	cerr << #v << " : " << "\n";                                        \
	for (int i = 0; i < n; i++)                                         \
		cerr << "\t" << #v << "[" << i << "]" << " : " << v[i] << "\n"; \
	cerr << endl;
#define sep() cerr << "--------------------" << endl;

#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, b, a) for (int i = (b) - 1; i >= (a); i--)
#define GO(c, itr) for (auto itr = (c).begin(); itr != (c).end(); ++itr)

#define nl "\n"
#define YES cout << "YES" << nl
#define NO cout << "NO" << nl
#define yes cout << "yes" << nl
#define no cout << "no" << nl
#define Yes cout << "Yes" << nl
#define No cout << "No" << nl
#define pyprint(x) cout << x << nl
#define errprint(x) cerr << x << nl

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vii;
const ll mod = 1e9 + 7;
const ll inf = 1e18;

//---> Short Data Structures:
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; // aka indexed_set

template <typename T>
using min_heap = priority_queue<T, vector<T>, greater<T>>;
// ordered_set<int> s;
// s.insert(1);
// s.order_of_key(3);
// s.find_by_order(1);
// s.find(1);

//---> Graph :
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

//---> Number Theory :
ll gcd(ll a, ll b)
{
	while (b)
	{
		swap(a, b);
		b %= a;
	}
	return (a);
}
ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }
ll po2(ll b, ll e)
{
	if (e == 0)
		return 1;
	ll res = 1;
	while (e > 1)
	{
		if (e % 2)
			res = (res * b) % mod;
		e /= 2;
		b = (b * b) % mod;
	}
	return (res * b) % mod;
}
ll modInverse(ll a) { return po2(a, mod - 2); }
bool sortFirst(const pair<ll, ll> &a, const pair<ll, ll> &b)
{
	if (a.first == b.first)
	{
		return a.second > b.second;
	}
	return a.first > b.first;
}

void solve() {
    ll n, m;
    cin >> n >> m;
    vector<ll> a(n), b(m);
    map<ll, int> mpa, mpb;

    for (ll i = 0; i < n; i++) {
        cin >> a[i];
        mpa[a[i]]++;
    }
    for (ll j = 0; j < m; j++) {
        cin >> b[j];
        mpb[b[j]]++;
    }

    // Remove common elements from both maps
    for (auto it : mpa) {
        if (mpb[it.first] != 0) {
            int mn = min(mpa[it.first], mpb[it.first]);
            mpa[it.first] -= mn;
            mpb[it.first] -= mn;
        }
    }

    vector<ll> aa, bb;
    for (auto it : mpa) {
        for (int i = 0; i < it.second; i++) {
            aa.pb(it.first);
        }
    }
    for (auto it : mpb) {
        for (int i = 0; i < it.second; i++) {
            bb.pb(it.first);
        }
    }

    sort(all(aa));
    sort(all(bb));
	
    ll ans = 0;
    bool t = false;
    vector<int> vis(bb.size(), 0);
    if(bb.size()>0){
    	for (int i = 0; i < (int)aa.size(); i++) {
    	if(t){
    		if(aa[i]-ans>1){
    			NO;
    			return;
    		}
    	}
    	ans+=aa[i];
    	//dbg(ans);
        auto it = lower_bound(all(bb), ans) - bb.begin();
        //dbg(it);
        while (it < bb.size() && vis[it]) {
            it++;
        }
        t = true;
        if (it < bb.size()&&!vis[it]&&ans==bb[it]) {
            vis[it] = 1;
            ans = 0;
            t = false;
        }
        
    }
    }else{
    	if(aa.size()>0){
    		cout<<"NO"<<endl;
    		return;
    	}else{
    		YES;
    		return;
    	}
    }
    

    for (int i = 0; i < bb.size(); i++) {
        if (vis[i] == 0) {
            cout << "NO\n";
            return;
        }
    }
    cout << "YES\n";
}


int main()
{
	boost;

	int t = 1;
	cin >> t;
	while (t--)
		solve();
	return 0;
}


Submission (verdict: Wrong answer on test 3):
// Problem: D. Kevin and Numbers
// Contest: Codeforces - IAEPC Preliminary Contest (Codeforces Round 999, Div. 1 + Div. 2)
// URL: https://codeforces.com/contest/2061/problem/D
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
using namespace std;
#define boost                    \
	ios::sync_with_stdio(false); \
	cin.tie(0);                  \
	cout.tie(0)

///-----new feature----///
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
///-end of new feature-///

#define scan(x)                                                                   \
	do                                                                            \
	{                                                                             \
		while ((x = getchar()) < '0')                                             \
			;                                                                     \
		int _;                                                                    \
		for (x -= '0'; '0' <= (_ = getchar()); x = (x << 3) + (x << 1) + _ - '0') \
			;                                                                     \
	} while (0)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x.size())
#define fi first
#define se second
#define pb push_back
#define mp make_pair

#define dbg(x) cerr << "  - " << #x << ": " << x << endl;
#define dbgp(x) cerr << "  - " << #x << ": " << x.fi << " , " << x.se << endl;
#define dbg2(x, y) cerr << "  - " << #x << ": " << x << "\t\t" << #y << ": " << y << endl;
#define dbgv(v, n)                                                      \
	cerr << #v << " : " << "\n";                                        \
	for (int i = 0; i < n; i++)                                         \
		cerr << "\t" << #v << "[" << i << "]" << " : " << v[i] << "\n"; \
	cerr << endl;
#define sep() cerr << "--------------------" << endl;

#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, b, a) for (int i = (b) - 1; i >= (a); i--)
#define GO(c, itr) for (auto itr = (c).begin(); itr != (c).end(); ++itr)

#define nl "\n"
#define YES cout << "YES" << nl
#define NO cout << "NO" << nl
#define yes cout << "yes" << nl
#define no cout << "no" << nl
#define Yes cout << "Yes" << nl
#define No cout << "No" << nl
#define pyprint(x) cout << x << nl
#define errprint(x) cerr << x << nl

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vii;
const ll mod = 1e9 + 7;
const ll inf = 1e18;

//---> Short Data Structures:
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; // aka indexed_set

template <typename T>
using min_heap = priority_queue<T, vector<T>, greater<T>>;
// ordered_set<int> s;
// s.insert(1);
// s.order_of_key(3);
// s.find_by_order(1);
// s.find(1);

//---> Graph :
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

//---> Number Theory :
ll gcd(ll a, ll b)
{
	while (b)
	{
		swap(a, b);
		b %= a;
	}
	return (a);
}
ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }
ll po2(ll b, ll e)
{
	if (e == 0)
		return 1;
	ll res = 1;
	while (e > 1)
	{
		if (e % 2)
			res = (res * b) % mod;
		e /= 2;
		b = (b * b) % mod;
	}
	return (res * b) % mod;
}
ll modInverse(ll a) { return po2(a, mod - 2); }
bool sortFirst(const pair<ll, ll> &a, const pair<ll, ll> &b)
{
	if (a.first == b.first)
	{
		return a.second > b.second;
	}
	return a.first > b.first;
}

void solve() {
    ll n, m;
    cin >> n >> m;
    vector<ll> a(n), b(m);
    map<ll, int> mpa, mpb;

    // Read input and count occurrences
    for (ll i = 0; i < n; i++) {
        cin >> a[i];
        mpa[a[i]]++;
    }
    for (ll j = 0; j < m; j++) {
        cin >> b[j];
        mpb[b[j]]++;
    }

    // Remove common elements from both maps
    for (auto it : mpa) {
        if (mpb[it.first] != 0) {
            int mn = min(mpa[it.first], mpb[it.first]);
            mpa[it.first] -= mn;
            mpb[it.first] -= mn;
        }
    }

    vector<ll> aa, bb;
    for (auto it : mpa) {
        for (int i = 0; i < it.second; i++) {
            aa.push_back(it.first);
        }
    }
    for (auto it : mpb) {
        for (int i = 0; i < it.second; i++) {
            bb.push_back(it.first);
        }
    }

    sort(aa.begin(), aa.end());
    sort(bb.begin(), bb.end());

    ll ans = 0;
    bool t = false;
    vector<int> vis(bb.size(), 0);

    if (!bb.empty()) {
        for (int i = 0; i < (int)aa.size(); i++) {
            if (t && aa[i] - ans > 1) {
                cout << "NO\n";
                return;
            }
            ans += aa[i];

            auto it = lower_bound(bb.begin(), bb.end(), ans) - bb.begin();
            while (it < bb.size() && vis[it]) {
                it++;
            }
            t = true;
            if (it < bb.size() && !vis[it] && ans == bb[it]) {
                vis[it] = 1;
                ans = 0;
                t = false;
            }
        }
    } else {
        if (!aa.empty()) {
            cout << "NO\n";
            return;
        } else {
            cout << "YES\n";
            return;
        }
    }

    for (int i = 0; i < bb.size(); i++) {
        if (vis[i] == 0) {
            cout << "NO\n";
            return;
        }
    }
    cout << "YES\n";
}

int main()
{
	boost;

	int t = 1;
	cin >> t;
	while (t--)
		solve();
	return 0;
}