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

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

#include<cstdio>

#include <ext/pb_ds/tree_policy.hpp>

#include <ext/pb_ds/assoc_container.hpp>

#define no cout<<"NO\n";

#define yes cout<<"YES\n";

#define ll long long

using namespace std;

using namespace __gnu_pbds;

template<class T>

using Tree=tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;

typedef tree<ll,null_type,less_equal<ll>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;

//ll lcm(ll a,ll b){return (a/__gcd(a,b))*1LL*b;}

map<ll,ll>mp;

bool f;

void rec(ll a,ll b,ll cnt)

{

  //  cout<<a<<" "<<b<<" "<<cnt<<" "<<mp[a]<<" "<<mp[b]<<"\n";

    if(!cnt)return;

    if(!a||!b){f=1;return;}

    mp[a+b]+=cnt;

    int o=mp[a],z=mp[b];

    mp[a]-=cnt;

    mp[b]-=cnt;

    if(a==b)

    {

        if(mp[a]<2*cnt)

            rec(a/2,a/2+a%2,2*cnt-o);

    }

    else

    {

        if(o<cnt)rec(a/2,a/2+a%2,cnt-o);

        if(z<cnt)rec(b/2,b/2+b%2,cnt-z);

    }

}

int main()

{

	ios_base::sync_with_stdio(NULL);

	cin.tie(0); cout.tie(0);

	//freopen("intern.in", "r", stdin);

	//freopen("flags.out", "w", stdout);

	int t;

	cin>>t;

	while(t--)

    {

        mp.clear();

        ll n,m,x;

        cin>>n>>m;

        set<ll>st;

        map<ll,ll>mp1,mp2;

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

            cin>>x,st.insert(x),mp1[x]++;

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

            cin>>x,st.insert(x),mp2[x]++;

        f=0;

        for(auto it:st)

        {

            if(mp1[it]==mp2[it])continue;

            if(mp1[it]<mp2[it])

            {

                x=mp2[it]-mp1[it];

                rec(it/2,it/2+it%2,x);

                mp[it]-=x,mp1[it]=mp2[it];

                continue;

            }

            x=mp1[it]-mp2[it];

            mp1[it]=mp2[it];

            mp[it]+=x;

        }

        for(auto [u,v] : mp)

        {

            if(v){f=1;break;}

        }

        if(f)no

        else yes

    }

}



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

#include<cstdio>

#include <ext/pb_ds/tree_policy.hpp>

#include <ext/pb_ds/assoc_container.hpp>

#define no cout<<"NO\n";

#define yes cout<<"YES\n";

#define ll long long

using namespace std;

using namespace __gnu_pbds;

template<class T>

using Tree=tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;

typedef tree<ll,null_type,less_equal<ll>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;

//ll lcm(ll a,ll b){return (a/__gcd(a,b))*1LL*b;}

int main()

{

	ios_base::sync_with_stdio(NULL);

	cin.tie(0); cout.tie(0);

	//freopen("intern.in", "r", stdin);

	//freopen("flags.out", "w", stdout);

	int t;

	cin>>t;

	while(t--)

    {

        int n,m,x;

        cin>>n>>m;

        multiset<int>st1,st2;

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

            cin>>x,st1.insert(x);

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

            cin>>x,st2.insert(x);

        while(!st1.empty()&&!st2.empty()&&st2.size()<=st1.size())

        {

            auto it1=*st1.rbegin();

            auto it2=*st2.rbegin();

            st1.erase(st1.find(it1));

            st2.erase(st2.find(it2));

            if(it1==it2)continue;

            if(it1>it2)break;

            st1.insert(it1);

            st2.insert(it2/2);

            st2.insert(it2/2+it2%2);

        }

        if(st1.size()+st2.size())no

        else yes

    }

}