Problem: 2060D - Subtract Min Sort
Language: C++23 (GCC 14-64, msys2)

Previous submission (verdict: Wrong answer on test 2):
#include <iostream>
#include <vector>
#include <utility>
#include <algorithm>
#include <cassert>
#include <bitset>
#include <cstring>
#include <queue>
#include <set>
#include <cmath>
#include <tuple>
#include <map>

using namespace std;

typedef long long ll;
#define int ll
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<long> vl;
typedef string str;
typedef pair<int, int> pii;
typedef pair<long, long> pllll;

#define all(a) a.begin(), a.end()
#define Debug(x) cout << #x " = " << (x) << '\n'
#define for_n(s, e, i) for (ll i = s; i < e; i++)
#define for_e(e,of) for (auto& e : of)
#define clr(arr) memset(arr,0,sizeof(arr))
#define pb push_back

const ll MOD=1e9+7;
ll madd(ll a, ll b) { return (a + b) % MOD; }

const int directions[4][2]={-1,0,0,-1,0,1,1,0};

const int N=2e5+5;
int n,t,a[N];

signed main(void)
{
    ios_base::sync_with_stdio(false);
	cin.tie(NULL);
    cin>>t;
    while(t--)
    {
        bool valid=true;
        multiset<pii> s;
        cin>>n;
        
        for_n(0,n,i)
        {
            cin>>a[i];
            s.insert({a[i],i});
        }
        vi pre(n);
        pre[n-1]=a[n-1];
        for(int i=n-2;i>=0;i--) pre[i]=min(pre[i],pre[i-1]);
        
        for_n(0,n,i)
        {
            if(a[i]>pre[i])
            {
                auto it=s.upper_bound({a[i]-pre[i],1e10});
    
                if(it==s.end())
                {
                    valid=false;
                    break;
                }
    
                a[i]-=it->first;
                a[it->second]=0;
                s.extract(it);
            }
        }
        cout<<(valid?"YES":"NO")<<'\n';
    }
    return 0;
}


Submission (verdict: Wrong answer on test 2):
#include <iostream>
#include <vector>
#include <utility>
#include <algorithm>
#include <cassert>
#include <bitset>
#include <cstring>
#include <queue>
#include <set>
#include <cmath>
#include <tuple>
#include <map>

using namespace std;

typedef long long ll;
#define int ll
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<long> vl;
typedef string str;
typedef pair<int, int> pii;
typedef pair<long, long> pllll;

#define all(a) a.begin(), a.end()
#define Debug(x) cout << #x " = " << (x) << '\n'
#define for_n(s, e, i) for (ll i = s; i < e; i++)
#define for_e(e,of) for (auto& e : of)
#define clr(arr) memset(arr,0,sizeof(arr))
#define pb push_back

const ll MOD=1e9+7;
ll madd(ll a, ll b) { return (a + b) % MOD; }

const int directions[4][2]={-1,0,0,-1,0,1,1,0};

const int N=2e5+5;
int n,t,a[N];

signed main(void)
{
    ios_base::sync_with_stdio(false);
	cin.tie(NULL);
    cin>>t;
    while(t--)
    {
        bool valid=true;
        multiset<pii> s;
        cin>>n;
        
        for_n(0,n,i)
        {
            cin>>a[i];
            s.insert({a[i],i});
        }
        vi pre(n,1e10);
        pre[n-1]=a[n-1];
        for(int i=n-2;i>=0;i--) pre[i]=min(pre[i],pre[i+1]);

        for_n(0,n,i)
        {
            if(a[i]>pre[i])
            {
                if(a[i]<a[i+1])
                {
                    a[i+1]-=a[i];
                    a[i]=0;
                }
                else
                {
                    valid=false;
                    break;
                }
            }
        }
        // for_n(0,n,i)
        //     cout<<a[i]<<' ';
        cout<<(valid?"YES":"NO")<<'\n';
    }
    return 0;
}