ShaysXIA/CodeInsightBench huggingface dataset:
Problem: 2050D - Digital string maximization
Language: C++23 (GCC 14-64, msys2)

Previous submission (verdict: Compilation error):
#include <bits/stdc++.h>
 using namespace std;
 #define ll long long
// const long long MOD = 998244353;
 
// #define MAXN 200001
// int d[MAXN];
// std::vector<int> g[MAXN];
// inline int deg(int u) { return g[u].size(); }
void solve() {
string s;
cin >> s;
for(int i=0;i<s.size();i++){
	int best=s[i]-'0',pos=i;
	for(int j=i;j<min(i+10,(int)s.size());j++)){
		if(s[j] -'0' -(j-i)>best){
			best=s[j]-'0'-(j-i);
			pos=j;
		}
	}
	while(pos > i){
		swap(s[pos],s[pos-1]);
		pos--;
	}
}
cout << s << endl;
}


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

Submission (verdict: Wrong answer on test 1):
#include <bits/stdc++.h>
 using namespace std;
 #define ll long long
// const long long MOD = 998244353;
 
// #define MAXN 200001
// int d[MAXN];
// std::vector<int> g[MAXN];
// inline int deg(int u) { return g[u].size(); }
void solve() {
string s;
cin >> s;
for(int i=0;i<s.size();i++){
	int best=s[i]-'0',pos=i;
	for(int j=i;j<min(i+10,(int)s.size());j++){
		if(s[j] -'0' -(j-i)>best){
			best=s[j]-'0'-(j-i);
			pos=j;
		}
	}
	while(pos > i){
		swap(s[pos],s[pos-1]);
		pos--;
	}
}
cout << s << endl;
}


 
int main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(nullptr), std::cout.tie(nullptr);
	int t; std::cin >> t; while (t--) solve(); return 0;
}
Problem: 2050D - Digital string maximization
Language: C++23 (GCC 14-64, msys2)

Previous submission (verdict: Wrong answer on test 1):
#include <bits/stdc++.h>
 using namespace std;
 #define ll long long
// const long long MOD = 998244353;
 
// #define MAXN 200001
// int d[MAXN];
// std::vector<int> g[MAXN];
// inline int deg(int u) { return g[u].size(); }
void solve() {
string s;
cin >> s;
for(int i=0;i<s.size();i++){
	int best=s[i]-'0',pos=i;
	for(int j=i;j<min(i+10,(int)s.size());j++){
		if(s[j] -'0' -(j-i)>best){
			best=s[j]-'0'-(j-i);
			pos=j;
		}
	}
	while(pos > i){
		swap(s[pos],s[pos-1]);
		pos--;
	}
}
cout << s << endl;
}


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

Submission (verdict: Accepted):
#include <bits/stdc++.h>
 using namespace std;
 #define ll long long
// const long long MOD = 998244353;
 
// #define MAXN 200001
// int d[MAXN];
// std::vector<int> g[MAXN];
// inline int deg(int u) { return g[u].size(); }
void solve() {
string s;
cin >> s;
for(int i=0;i<s.size();i++){
	int best=s[i]-'0',pos=i;
	for(int j=i;j<min(i+10,(int)s.size());j++){
		if(s[j] -'0' -(j-i)>best){
			best=s[j]-'0'-(j-i);
			pos=j;
		}
	}
	while(pos > i){
		swap(s[pos],s[pos-1]);
		pos--;
	}
    s[i]=char(best+'0');
}
cout << s << endl;
}


 
int main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(nullptr), std::cout.tie(nullptr);
	int t; std::cin >> t; while (t--) solve(); return 0;
}
Problem: 2063C - Remove Exactly Two
Language: C++20 (GCC 13-64)

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

void solve()
{
    ll n;
    cin >> n;
    vector<vector<ll>> tree(n);
    vector<ll> indegree(n, 0);
    for (ll i = 0; i < n - 1; i++)
    {
        ll u, v;
        cin >> u >> v;
        u--;
        v--;
        tree[u].push_back(v);
        tree[v].push_back(u);
        indegree[u]++;
        indegree[v]++;
    }
    ll ans = 0;
    ll maxi = 0;
    ll node = -1;
    for (ll i = 0; i < n; i++)
    {
        maxi = max(maxi, indegree[i]);
        if (maxi == indegree[i])
        {
            node = i;
        }
    }
    ans = maxi;
    indegree[node] = -1;
    for (auto child : tree[node])
    {
        indegree[child]--;
    }
    maxi = 0;
    node = -1;
    for (ll i = 0; i < n; i++)
    {
        maxi = max(maxi, indegree[i]);
        if (maxi == indegree[i])
        {
            node = i;
        }
    }
    ans += (maxi - 1);
    cout << ans << endl;
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

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

Submission (verdict: Wrong answer on test 4):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

void solve()
{
    ll n;
    cin >> n;
    vector<vector<ll>> tree(n);
    vector<ll> indegree(n, 0);
    for (ll i = 0; i < n - 1; i++)
    {
        ll u, v;
        cin >> u >> v;
        u--;
        v--;
        tree[u].push_back(v);
        tree[v].push_back(u);
        indegree[u]++;
        indegree[v]++;
    }
    ll ans = 0;
    ll maxi = 0;
    ll node = -1;
    for (ll i = 0; i < n; i++)
    {
        maxi = max(maxi, indegree[i]);
        if (maxi == indegree[i])
        {
            node = i;
        }
    }
    ans = maxi;
    indegree[node] = -1;
    for (auto child : tree[node])
    {
        indegree[child]--;
    }
    maxi = -1;
    node = -1;
    for (ll i = 0; i < n; i++)
    {
        maxi = max(maxi, indegree[i]);
        if (maxi == indegree[i])
        {
            node = i;
        }
    }
    ans += (maxi - 1);
    cout << ans << endl;
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    int t = 1;
    cin >> t;
    while (t--)
        solve();
}
Problem: 2063C - Remove Exactly Two
Language: C++20 (GCC 13-64)


Submission (verdict: Wrong answer on test 4):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

void solve()
{
    ll n;
    cin >> n;
    vector<vector<ll>> tree(n);
    vector<ll> indegree(n, 0);
    for (ll i = 0; i < n - 1; i++)
    {
        ll u, v;
        cin >> u >> v;
        u--;
        v--;
        tree[u].push_back(v);
        tree[v].push_back(u);
        indegree[u]++;
        indegree[v]++;
    }
    ll ans = 0;
    ll maxi = 0;
    ll node = -1;
    for (ll i = 0; i < n; i++)
    {
        maxi = max(maxi, indegree[i]);
        if (maxi == indegree[i])
        {
            node = i;
        }
    }
    ans = maxi;
    indegree[node] = -1;
    for (auto child : tree[node])
    {
        indegree[child]--;
    }
    maxi = 0;
    node = -1;
    for (ll i = 0; i < n; i++)
    {
        maxi = max(maxi, indegree[i]);
        if (maxi == indegree[i])
        {
            node = i;
        }
    }
    ans += (maxi - 1);
    cout << ans << endl;
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    int t = 1;
    cin >> t;
    while (t--)
        solve();
}
Problem: 2063C - Remove Exactly Two
Language: C++20 (GCC 13-64)

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

void solve()
{
    ll n;
    cin >> n;
    vector<vector<ll>> tree(n);
    vector<ll> indegree(n, 0);
    for (ll i = 0; i < n - 1; i++)
    {
        ll u, v;
        cin >> u >> v;
        u--;
        v--;
        tree[u].push_back(v);
        tree[v].push_back(u);
        indegree[u]++;
        indegree[v]++;
    }
    ll ans = 0;
    ll maxi = 0;
    ll node = -1;
    for (ll i = 0; i < n; i++)
    {
        maxi = max(maxi, indegree[i]);
        if (maxi == indegree[i])
        {
            node = i;
        }
    }
    ans = maxi;
    indegree[node] = -1;
    for (auto child : tree[node])
    {
        indegree[child]--;
    }
    maxi = -1;
    node = -1;
    for (ll i = 0; i < n; i++)
    {
        maxi = max(maxi, indegree[i]);
        if (maxi == indegree[i])
        {
            node = i;
        }
    }
    ans += (maxi - 1);
    cout << ans << endl;
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

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

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

void solve()
{
    ll n;
    cin >> n;
    vector<vector<ll>> tree(n);
    vector<ll> indegree(n, 0);
    for (ll i = 0; i < n - 1; i++)
    {
        ll u, v;
        cin >> u >> v;
        u--;
        v--;
        tree[u].push_back(v);
        tree[v].push_back(u);
        indegree[u]++;
        indegree[v]++;
    }
    multiset<ll> s;
    for (ll i = 0; i < n; i++)
    {
        s.insert(indegree[i]);
    }
    ll ans = 0;
    for (ll i = 0; i < n; i++)
    {
        s.erase(s.lower_bound(indegree[i]));
        for (auto child : tree[i])
        {
            ans = max(ans, indegree[i] + indegree[child] - 2);
            s.erase(s.lower_bound(indegree[child]));
        }
        if (s.size() > 0)
        {
            ans = max(ans, indegree[i] + *s.rbegin() - 1);
        }
        s.insert(indegree[i]);
        for (auto child : tree[i])
        {
            ans = max(ans, indegree[i] + indegree[child] - 2);
            s.insert(indegree[child]);
        }
    }
    cout << ans << endl;
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    int t = 1;
    cin >> t;
    while (t--)
        solve();
}
Problem: 2049C - MEX Cycle
Language: C++20 (GCC 13-64)


Submission (verdict: Wrong answer on test 1):
#include <bits/stdc++.h>

using namespace std;

#define int long long



void build(vector<int> &segtree, vector<int> &arr, int v, int l, int r) {

    if (l == r) {

        segtree[v]=arr[l];

    } else {

        int mid = (l + r) / 2;

        build(segtree, arr, 2 * v, l, mid);

        build(segtree, arr, 2 * v + 1, mid + 1, r);

        segtree[v] = gcd(segtree[2 * v], segtree[2 * v + 1]);

    }

}

 



 

int query(vector<int> &segtree, vector<int> &arr, int v, int l, int r, int l1, int r1) {

    if (r1 < l || r < l1) {

        return 0;

    } else if (l >= l1 && r <= r1) {

        return segtree[v];

    } else {

        int mid = (l + r) / 2;

        int a = query(segtree, arr, 2 * v, l, mid, l1, r1);

        int b = query(segtree, arr, 2 * v + 1, mid + 1, r, l1, r1);

        return gcd(a, b);

    }

}



int32_t main() {

    ios::sync_with_stdio(false);

    cin.tie(nullptr);

    

    int t;

    cin >> t;

    while (t--) {

      int n,x,y;

      cin>>n>>x>>y;

      x--; y--;

      vector<int> arr(n);



      for(int i=0;i<n;++i)

      {

        if(i%2==0)

            arr[i]=0;

        else arr[i]=1;

      }



      if(n%2==1)

      {

        arr[n-1]=2;

      }



      if(arr[x]==arr[y])

      {

        arr[y]=2;

      }





       for(auto x:arr)

       {

        cout<<x<<" ";

       }

       cout<<endl;

    }

    return 0;

}
Problem: 2052A - Adrenaline Rush
Language: C++20 (GCC 13-64)


Submission (verdict: Idleness limit exceeded on test 1):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(v) v.begin(), v.end()
#define LL_MAX LLONG_MAX
#define LL_MIN LLONG_MIN
#define pb push_back


void solve() {
	int n;
	cin >> n;
	vector<int> final(n);


	for(int i = 0; i < n; i++){
		cin >> final[i] ;
	}

if(n == 1){
	cout << 0 << '\n';
	return;
}
	vector<int>v(n);

	for(int i = n - 1; i >= 0; i--){
		v[i] = i + 1;
	}

	reverse(all(v));

	for(int i = 1; i <= n; i++){
		for(int j = i + 1; j <= n; j++){
			cout << j << " " << i  << '\n';

		}
	}

	for(int i = 0; i < n; i++){
		int j = i;
		while(v[j] != final[i]){
			j++;
		}


		for(int k = j; k > i; k--){
			cout << v[k] << " " << v[k - 1] << '\n';
			swap(v[k],v[k - 1]);

		}


	}



  
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll t;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}
Problem: 2052A - Adrenaline Rush
Language: C++20 (GCC 13-64)

Previous submission (verdict: Wrong answer on test 1):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(v) v.begin(), v.end()
#define LL_MAX LLONG_MAX
#define LL_MIN LLONG_MIN
#define pb push_back


void solve() {
	int n;
	cin >> n;
	vector<int> final(n);


	for(int i = 0; i < n; i++){
		cin >> final[i] ;
	}

if(n == 1){
	cout << 0 << '\n';
	return;
}
	vector<int>v(n);

	for(int i = n - 1; i >= 0; i--){
		v[i] = i + 1;
	}

	reverse(all(v));

	for(int i = 1; i <= n; i++){
		for(int j = i + 1; j <= n; j++){
			cout << j << " " << i  << '\n';

		}
	}

	for(int i = 0; i < n; i++){
		int j = i;
		while(v[j] != final[i]){
			j++;
		}


		for(int k = j; k > i; k--){
			cout << v[k] << " " << v[k - 1] << '\n';
			swap(v[k],v[k - 1]);

		}


	}



  
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll t = 1;
    // cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}


Submission (verdict: Accepted):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(v) v.begin(), v.end()
#define LL_MAX LLONG_MAX
#define LL_MIN LLONG_MIN
#define pb push_back


void solve() {
	int n;
	cin >> n;
	vector<int> final(n);


	for(int i = 0; i < n; i++){
		cin >> final[i] ;
	}

if(n == 1){
	cout << 0 << '\n';
	return;
}
	vector<int>v(n);

	for(int i = n - 1; i >= 0; i--){
		v[i] = i + 1;
	}

	reverse(all(v));
	vector<int>store1;
	vector<int>store2;
int count = 0;
	for(int i = 1; i <= n; i++){
		for(int j = i + 1; j <= n; j++){
			count++;
			store1.push_back(j);
			store2.pb(i);

		}
	}

	for(int i = 0; i < n; i++){
		int j = i;
		while(v[j] != final[i]){
			j++;
		}


		for(int k = j; k > i; k--){
			count++;
			store1.pb(v[k]);
			store2.pb(v[k - 1]);
			swap(v[k],v[k - 1]);

		}


	}
cout << count << '\n';
	for(int i = 0; i < store1.size(); i++){
		cout << store1[i] << " " << store2[i] << '\n';
	}



  
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll t = 1;
    // cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}
Problem: 2052A - Adrenaline Rush
Language: C++20 (GCC 13-64)

Previous submission (verdict: Idleness limit exceeded on test 1):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(v) v.begin(), v.end()
#define LL_MAX LLONG_MAX
#define LL_MIN LLONG_MIN
#define pb push_back


void solve() {
	int n;
	cin >> n;
	vector<int> final(n);


	for(int i = 0; i < n; i++){
		cin >> final[i] ;
	}

if(n == 1){
	cout << 0 << '\n';
	return;
}
	vector<int>v(n);

	for(int i = n - 1; i >= 0; i--){
		v[i] = i + 1;
	}

	reverse(all(v));

	for(int i = 1; i <= n; i++){
		for(int j = i + 1; j <= n; j++){
			cout << j << " " << i  << '\n';

		}
	}

	for(int i = 0; i < n; i++){
		int j = i;
		while(v[j] != final[i]){
			j++;
		}


		for(int k = j; k > i; k--){
			cout << v[k] << " " << v[k - 1] << '\n';
			swap(v[k],v[k - 1]);

		}


	}



  
}

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


Submission (verdict: Wrong answer on test 1):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(v) v.begin(), v.end()
#define LL_MAX LLONG_MAX
#define LL_MIN LLONG_MIN
#define pb push_back


void solve() {
	int n;
	cin >> n;
	vector<int> final(n);


	for(int i = 0; i < n; i++){
		cin >> final[i] ;
	}

if(n == 1){
	cout << 0 << '\n';
	return;
}
	vector<int>v(n);

	for(int i = n - 1; i >= 0; i--){
		v[i] = i + 1;
	}

	reverse(all(v));

	for(int i = 1; i <= n; i++){
		for(int j = i + 1; j <= n; j++){
			cout << j << " " << i  << '\n';

		}
	}

	for(int i = 0; i < n; i++){
		int j = i;
		while(v[j] != final[i]){
			j++;
		}


		for(int k = j; k > i; k--){
			cout << v[k] << " " << v[k - 1] << '\n';
			swap(v[k],v[k - 1]);

		}


	}



  
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll t = 1;
    // cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}
Problem: 2061D - Kevin and Numbers
Language: C++20 (GCC 13-64)

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

#define ll long long
#define all(x) x.begin(),x.end()
#define all_rev(x) x.rbegin(),x.rend()
const int mod = 1e9 + 7;

using namespace std;

bool build(int num, map<int,int>& a) {

    if (num <= 0) return false;

    if (num&1) {

        int x = num/2; int y = num - x;

        if ((a[y] < 1) && (!build(y,a))) return false;
        if ((a[x] < 1) && (!build(x,a))) return false;

        a[num]++; a[x]--; a[y]--; return true;
    }

    int x = num/2;

    if ((a[x] < 2) && (!build(x,a))) return false;
    if ((a[x] < 2) && (!build(x,a))) return false;

    a[num]++; a[x] -= 2; return true;
}

bool solve(map<int,int>& a, map<int,int>& b) {

    for (auto& [val,freq] : b) {

        if (a[val] >= freq) {
            a[val] -= freq;
        }

        else {

            while (a[val] < freq) {
                if (!build(val,a)) return false;
            }

            a[val] -= freq;
        }
    }

    for (auto& [val,freq] : a) {
        if (freq >= 1) return false;
    }

    return true;
}

int main() {

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int t; cin>>t;

    while (t--) {

        int n,m; cin>>n>>m;
        map<int,int> a,b;

        for (int i = 0; i < n; i++) {
            int num; cin>>num; a[num]++;
        }

        for (int i = 0; i < m; i++) {
            int num; cin>>num; b[num]++;
        }

        if (solve(a,b)) cout<<"YES\n";
        else cout<<"NO\n";
    }

    return 0;
}

// 18 8
// 1 1 1 1 1
// 5

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

#define ll long long
#define all(x) x.begin(),x.end()
#define all_rev(x) x.rbegin(),x.rend()
const int mod = 1e9 + 7;

using namespace std;

bool build(int num, map<int,int>& a) {

    if (num <= 1) return false;

    if (num&1) {

        int x = num/2; int y = num - x;

        if ((a[y] < 1) && (!build(y,a))) return false;
        if ((a[x] < 1) && (!build(x,a))) return false;

        a[num]++; a[x]--; a[y]--; return true;
    }

    int x = num/2;

    if ((a[x] < 2) && (!build(x,a))) return false;
    if ((a[x] < 2) && (!build(x,a))) return false;

    a[num]++; a[x] -= 2; return true;
}

bool solve(map<int,int>& a, map<int,int>& b) {

    for (auto& [val,freq] : b) {

        if (a[val] >= freq) {
            a[val] -= freq;
        }

        else {

            while (a[val] < freq) {
                if (!build(val,a)) return false;
            }

            a[val] -= freq;
        }
    }

    for (auto& [val,freq] : a) {
        if (freq >= 1) return false;
    }

    return true;
}

int main() {

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int t; cin>>t;

    while (t--) {

        int n,m; cin>>n>>m;
        map<int,int> a,b;

        for (int i = 0; i < n; i++) {
            int num; cin>>num; a[num]++;
        }

        for (int i = 0; i < m; i++) {
            int num; cin>>num; b[num]++;
        }

        if (solve(a,b)) cout<<"YES\n";
        else cout<<"NO\n";
    }

    return 0;
}

// 18 8
// 1 1 1 1 1
// 5
Problem: 2061D - Kevin and Numbers
Language: C++20 (GCC 13-64)


Submission (verdict: Wrong answer on test 3):
#include<bits/stdc++.h>

#define ll long long
#define all(x) x.begin(),x.end()
#define all_rev(x) x.rbegin(),x.rend()
const int mod = 1e9 + 7;

using namespace std;

bool build(int num, map<int,int>& a) {

    if (num <= 0) return false;

    if (num&1) {

        int x = num/2; int y = num - x;

        if ((a[x] < 1) && (!build(x,a))) return false;
        if ((a[y] < 1) && (!build(y,a))) return false;

        a[num]++; a[x]--; a[y]--; return true;
    }

    int x = num/2;

    if ((a[x] < 2) && (!build(x,a))) return false;
    if ((a[x] < 2) && (!build(x,a))) return false;

    a[num]++; a[x] -= 2; return true;
}

bool solve(map<int,int>& a, map<int,int>& b) {

    for (auto& [val,freq] : b) {

        if (a[val] >= freq) {
            a[val] -= freq;
        }

        else {

            while (a[val] < freq) {
                if (!build(val,a)) return false;
            }

            a[val] -= freq;
        }
    }

    for (auto& [val,freq] : a) {
        if (freq >= 1) return false;
    }

    return true;
}

int main() {

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int t; cin>>t;

    while (t--) {

        int n,m; cin>>n>>m;
        map<int,int> a,b;

        for (int i = 0; i < n; i++) {
            int num; cin>>num; a[num]++;
        }

        for (int i = 0; i < m; i++) {
            int num; cin>>num; b[num]++;
        }

        if (solve(a,b)) cout<<"YES\n";
        else cout<<"NO\n";
    }

    return 0;
}
Problem: 2061D - Kevin and Numbers
Language: C++20 (GCC 13-64)

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

#define ll long long
#define all(x) x.begin(),x.end()
#define all_rev(x) x.rbegin(),x.rend()
const int mod = 1e9 + 7;

using namespace std;

bool build(int num, map<int,int>& a) {

    if (num <= 0) return false;

    if (num&1) {

        int x = num/2; int y = num - x;

        if ((a[x] < 1) && (!build(x,a))) return false;
        if ((a[y] < 1) && (!build(y,a))) return false;

        a[num]++; a[x]--; a[y]--; return true;
    }

    int x = num/2;

    if ((a[x] < 2) && (!build(x,a))) return false;
    if ((a[x] < 2) && (!build(x,a))) return false;

    a[num]++; a[x] -= 2; return true;
}

bool solve(map<int,int>& a, map<int,int>& b) {

    for (auto& [val,freq] : b) {

        if (a[val] >= freq) {
            a[val] -= freq;
        }

        else {

            while (a[val] < freq) {
                if (!build(val,a)) return false;
            }

            a[val] -= freq;
        }
    }

    for (auto& [val,freq] : a) {
        if (freq >= 1) return false;
    }

    return true;
}

int main() {

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int t; cin>>t;

    while (t--) {

        int n,m; cin>>n>>m;
        map<int,int> a,b;

        for (int i = 0; i < n; i++) {
            int num; cin>>num; a[num]++;
        }

        for (int i = 0; i < m; i++) {
            int num; cin>>num; b[num]++;
        }

        if (solve(a,b)) cout<<"YES\n";
        else cout<<"NO\n";
    }

    return 0;
}

Submission (verdict: Runtime error on test 1):
#include<bits/stdc++.h>

#define ll long long
#define all(x) x.begin(),x.end()
#define all_rev(x) x.rbegin(),x.rend()
const int mod = 1e9 + 7;

using namespace std;

bool build(int num, map<int,int>& a) {

    if (num <= 0) return false;

    if (num&1) {

        int x = num/2; int y = num - x;

        if ((a[y] < 1) && (!build(y,a))) return false;
        if ((a[x] < 1) && (!build(x,a))) return false;

        a[num]++; a[x]--; a[y]--; return true;
    }

    int x = num/2;

    if ((a[x] < 2) && (!build(x,a))) return false;
    if ((a[x] < 2) && (!build(x,a))) return false;

    a[num]++; a[x] -= 2; return true;
}

bool solve(map<int,int>& a, map<int,int>& b) {

    for (auto& [val,freq] : b) {

        if (a[val] >= freq) {
            a[val] -= freq;
        }

        else {

            while (a[val] < freq) {
                if (!build(val,a)) return false;
            }

            a[val] -= freq;
        }
    }

    for (auto& [val,freq] : a) {
        if (freq >= 1) return false;
    }

    return true;
}

int main() {

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int t; cin>>t;

    while (t--) {

        int n,m; cin>>n>>m;
        map<int,int> a,b;

        for (int i = 0; i < n; i++) {
            int num; cin>>num; a[num]++;
        }

        for (int i = 0; i < m; i++) {
            int num; cin>>num; b[num]++;
        }

        if (solve(a,b)) cout<<"YES\n";
        else cout<<"NO\n";
    }

    return 0;
}

// 18 8
// 1 1 1 1 1
// 5