Problem: 2060D - Subtract Min Sort
Language: C++20 (GCC 13-64)

Previous submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>
using namespace std;
#define all(a) a.begin(), a.end()
#define f(i, n) for (int i = 0; i < n; i++)
#define f1(i, n) for (int i = 1; i < n; i++)
/*
For using ordered_set :

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
1. *s.find_by_order(k) --> Returns the element present at index k.
2.  s.order_of_key(k) -->  Returns the number of elements smaller than k.
*/
#define int long long
typedef vector<int> vi;
typedef vector<pair<int, int>> vp;
typedef map<int, int> mii;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
#define pb push_back
const int mod = 1e9 + 7;

class solution
{
    static int power(int a, int b, int mod = 1e9 + 7)
    {

        int res = 1;

        while (b)
        {
            if (b & 1)
                res = (res * a) % mod;

            a = (a * a) % mod;

            b >>= 1;
        }

        return res % mod;
    }
    static void yy()
    {
        cout << "YES" << endl;
    }
    static void nn()
    {
        cout << "NO" << endl;
    }

public:
    static void solve()
    {
        int n;
        cin >> n;
        vi a(n);
        f(i, n) cin >> a[i];
        vi b = a;
        sort(all(b));
        bool flag = 1;
        f(i, n)
        {
            if (a[i] != b[i])
            {
                flag = 0;
                break;
            }
        }
        if (flag)
        {
            cout << "YES" << endl;
            return;
        }
        if (a[0] < a[1])
        {
            cout << "YES" << endl;
            return;
        }
        int highest = 0;
        f(i, n - 1)
        {
            if (a[i] == 0 || a[i + 1] == 0)
            {
                continue;
            }
            int x = min(a[i], a[i + 1]);
            while (a[i] - x >= highest && a[i + 1] - x >= highest)
            {
                a[i] -= x;
                a[i + 1] -= x;
                highest = a[i];
            }
        }
        b = a;
        sort(all(b));
        flag = 1;
        f(i, n)
        {
            if (a[i] != b[i])
            {
                flag = 0;
                break;
            }
        }
        if (flag)
        {
            cout << "YES" << endl;
            return;
        }
        cout << "NO" << endl;
    }
};

signed
main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int t;
    cin >> t;
    while (t--)
    {
        solution s1;
        s1.solve();
    }
}


Submission (verdict: Accepted):
#include <bits/stdc++.h>
using namespace std;
#define all(a) a.begin(), a.end()
#define f(i, n) for (int i = 0; i < n; i++)
#define f1(i, n) for (int i = 1; i < n; i++)
/*
For using ordered_set :

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
1. *s.find_by_order(k) --> Returns the element present at index k.
2.  s.order_of_key(k) -->  Returns the number of elements smaller than k.
*/
#define int long long
typedef vector<int> vi;
typedef vector<pair<int, int>> vp;
typedef map<int, int> mii;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
#define pb push_back
const int mod = 1e9 + 7;

class solution
{
    static int power(int a, int b, int mod = 1e9 + 7)
    {

        int res = 1;

        while (b)
        {
            if (b & 1)
                res = (res * a) % mod;

            a = (a * a) % mod;

            b >>= 1;
        }

        return res % mod;
    }
    static void yy()
    {
        cout << "YES" << endl;
    }
    static void nn()
    {
        cout << "NO" << endl;
    }

public:
    static void solve()
    {
        int n;
        cin >> n;
        vi a(n);
        f(i, n) cin >> a[i];
        vi b = a;
        sort(all(b));
        bool flag = 1;
        f(i, n)
        {
            if (a[i] != b[i])
            {
                flag = 0;
                break;
            }
        }
        if (flag)
        {
            cout << "YES" << endl;
            return;
        }
        int highest = 0;
        f(i, n - 1)
        {
            if (a[i] == 0 || a[i + 1] == 0)
            {
                continue;
            }
            int x = min(a[i], a[i + 1]);
            while (a[i] - x >= highest && a[i + 1] - x >= highest)
            {
                a[i] -= x;
                a[i + 1] -= x;
                highest = a[i];
            }
        }
        b = a;
        sort(all(b));
        flag = 1;
        f(i, n)
        {
            // cout << a[i] << " " << b[i] << endl;
            if (a[i] != b[i])
            {
                flag = 0;
                break;
            }
        }
        if (flag)
        {
            cout << "YES" << endl;
            return;
        }
        cout << "NO" << endl;
    }
};

signed
main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int t;
    cin >> t;
    while (t--)
    {
        solution s1;
        s1.solve();
    }
}