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

Previous submission (verdict: Wrong answer on test 2):
#pragma GCC optimize("O3,unroll-loops")



#include <bits/stdc++.h>

using namespace std;

using namespace chrono;



#define fastio() ios_base :: sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)

#define pb push_back

#define ppb pop_back

#define all(x) x.begin(),x.end()

#define PI 3.141592653589793238462

#define f first

#define s second

#define ff first

#define ss second

#define sz(x) ((int)(x).size)

#define set_bits __builtin_popcountll

#define nline '\n'

#define yes cout<<"YES"<<nline

#define no cout<<"NO"<<nline

#define meta(x) cout<<"Case #"<<x<<": "



#ifndef ONLINE_JUDGE

#define debug(x) cerr << #x<<" ";_print(x); cerr<<endl ;

#else 

#define debug(x);

#endif



typedef long long ll;

typedef unsigned long long ull;

typedef long double lld;



void _print(ll t) {cerr << t;}

void _print(int t) {cerr << t;}

void _print(string t) {cerr << t;}

void _print(char t) {cerr << t;}

void _print(lld t) {cerr << t;}

void _print(double t) {cerr << t;}

void _print(ull t) {cerr << t;}



template <class T, class V> void _print(pair <T, V> p);

template <class T> void _print(vector <T> v);

template <class T> void _print(set <T> v);

template <class T, class V> void _print(map <T, V> v);

template <class T> void _print(multiset <T> v);

template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}";}

template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}

template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}

template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}

template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}



/*---------------------------------------------------------------------------------------*/

bool isPrime(ll n){if(n==1) return false; for(ll i=2 ; i*i <=n ; ++i){if(n%i == 0) return false;} return true;}

ll expo(ll a, ll b, ll mod) {ll res = 1; while (b > 0) {if (b & 1)res = (res * a) % mod; a = (a * a) % mod; b = b >> 1;} return res;}

ll gcd(ll a, ll b) {if (b > a) {return gcd(b, a);} if (b == 0) {return a;} return gcd(b, a % b);}

vector<ll> sieve(int n) {int*arr = new int[n + 1](); vector<ll> vect; for (int i = 2; i <= n; i++)if (arr[i] == 0) {vect.push_back(i); for (int j = 2 * i; j <= n; j += i)arr[j] = 1;} return vect;}

ll MEX(vector<ll> a){ sort(all(a)); int n=a.size(); if(n==0 || a[0]!=0) return 0; for(int i=1 ; i<n ; ++i) if(a[i]!=a[i-1]+1 && a[i]!=a[i-1]) return a[i-1]+1; return a[n-1]+1;}

/*---------------------------------------------------------------------------------------*/



void solve(){

    int t;  cin>>t;

    while(t--){

        int n;  cin>>n;

        vector<int> a(n);

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

            cin>>a[i];

        int x=a[0] ,i;

        

        bool flag1=1;

        for(i=1 ; i<n ; ++i){

            if(x>a[i]){

                flag1=0;

               break;

            }

            else if(x==a[i]){

                break;

            }

            x=a[i]-a[i-1];

        }

        bool flag2=1;

        for(i ; i<n-1 ; i++){

            if(a[i+1]<a[i]){

                flag2=0;

                break;

            }

        }

        if(flag1 && flag2)

            yes;

        else

            no;

    }

}





int main (){



    #ifndef ONLINE_JUDGE

        freopen("Error.txt", "w" , stderr) ;

    #endif

        fastio();

        auto start1 = high_resolution_clock::now();

        solve();

        auto stop1 = high_resolution_clock::now();

        auto duration = duration_cast<microseconds>(stop1 - start1);

    #ifndef ONLINE_JUDGE

        cerr << "Time: " << duration . count() / 1000 << endl;

    #endif

}

Submission (verdict: Accepted):
#pragma GCC optimize("O3,unroll-loops")



#include <bits/stdc++.h>

using namespace std;

using namespace chrono;



#define fastio() ios_base :: sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)

#define pb push_back

#define ppb pop_back

#define all(x) x.begin(),x.end()

#define PI 3.141592653589793238462

#define f first

#define s second

#define ff first

#define ss second

#define sz(x) ((int)(x).size)

#define set_bits __builtin_popcountll

#define nline '\n'

#define yes cout<<"YES"<<nline

#define no cout<<"NO"<<nline

#define meta(x) cout<<"Case #"<<x<<": "



#ifndef ONLINE_JUDGE

#define debug(x) cerr << #x<<" ";_print(x); cerr<<endl ;

#else 

#define debug(x);

#endif



typedef long long ll;

typedef unsigned long long ull;

typedef long double lld;



void _print(ll t) {cerr << t;}

void _print(int t) {cerr << t;}

void _print(string t) {cerr << t;}

void _print(char t) {cerr << t;}

void _print(lld t) {cerr << t;}

void _print(double t) {cerr << t;}

void _print(ull t) {cerr << t;}



template <class T, class V> void _print(pair <T, V> p);

template <class T> void _print(vector <T> v);

template <class T> void _print(set <T> v);

template <class T, class V> void _print(map <T, V> v);

template <class T> void _print(multiset <T> v);

template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}";}

template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}

template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}

template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}

template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}



/*---------------------------------------------------------------------------------------*/

bool isPrime(ll n){if(n==1) return false; for(ll i=2 ; i*i <=n ; ++i){if(n%i == 0) return false;} return true;}

ll expo(ll a, ll b, ll mod) {ll res = 1; while (b > 0) {if (b & 1)res = (res * a) % mod; a = (a * a) % mod; b = b >> 1;} return res;}

ll gcd(ll a, ll b) {if (b > a) {return gcd(b, a);} if (b == 0) {return a;} return gcd(b, a % b);}

vector<ll> sieve(int n) {int*arr = new int[n + 1](); vector<ll> vect; for (int i = 2; i <= n; i++)if (arr[i] == 0) {vect.push_back(i); for (int j = 2 * i; j <= n; j += i)arr[j] = 1;} return vect;}

ll MEX(vector<ll> a){ sort(all(a)); int n=a.size(); if(n==0 || a[0]!=0) return 0; for(int i=1 ; i<n ; ++i) if(a[i]!=a[i-1]+1 && a[i]!=a[i-1]) return a[i-1]+1; return a[n-1]+1;}

/*---------------------------------------------------------------------------------------*/



void solve(){

    int t;  cin>>t;

    while(t--){

        int n;  cin>>n;

        vector<int> a(n);

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

            cin>>a[i];

        for(int i=0 ; i<n-1 ; ++i){

            int x = min(a[i],a[i+1]);

            a[i]=a[i]-x;

            a[i+1]=a[i+1]-x;

        }

        bool flag=1;

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

            if(a[i]<a[i-1])

                flag=0;

        if(flag)

            yes;

        else

            no;

    }

}





int main (){



    #ifndef ONLINE_JUDGE

        freopen("Error.txt", "w" , stderr) ;

    #endif

        fastio();

        auto start1 = high_resolution_clock::now();

        solve();

        auto stop1 = high_resolution_clock::now();

        auto duration = duration_cast<microseconds>(stop1 - start1);

    #ifndef ONLINE_JUDGE

        cerr << "Time: " << duration . count() / 1000 << endl;

    #endif

}