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;
typedef long long int ll;
typedef vector<int> v_i;
typedef vector<long long int> v_ll;
typedef vector<pair<int,int>> vp_i;
typedef vector<pair<long long int,long long int>> vp_ll;
typedef map<int,int> m_i;
typedef map<long long int,long long int> m_ll;
typedef map<pair<int,int>,int> mp_i;
typedef map<pair<long long int,long long int>,long long int> mp_ll;
typedef set<int> s_i;
typedef set<long long int> s_ll;
typedef set<pair<int,int>> sp_i;
typedef set<pair<long long int,long long int>> sp_ll;
typedef priority_queue<ll> maxheap;
typedef priority_queue <ll, vector<ll>, greater<ll>> minheap;
#define mp make_pair
#define fi first
#define se second
#define INF 2e18
#define sorts(v) sort((v).begin(),(v).end())
#define rev(v) reverse((v).begin(),(v).end())
#define count2(v,s) count((v).begin(),(v).end(),(s))
#define finds(v,s) find((v).begin(),(v).end(),(s))
#define fast() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define min3(a,b,c)     min(a,min(b,c))
#define max3(a,b,c)     max(a,max(b,c))
#define min4(a,b,c,d)   min(min(a,b),min(c,d))
#define max4(a,b,c,d)   max(max(a,b),max(c,d))
#define max5(a,b,c,d,e) max(max3(a,b,c),max(d,e))
#define min5(a,b,c,d,e) min(min3(a,b,c),min(d,e))
#define forn(a,b,c) for(ll (c)=(a);(c)<(b);(c)++)
#define forr(a,b,c) for(ll (c)=(a);(i)>=(b);(c)--)
#define rep1(s) for(auto &it:(s))
#define rep2(s) for(auto it=(s).begin();it!=(s).end();it++)
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;}
#ifndef ONLINE_JUDGE
    #define debug()                        cerr<<"[Check]"<<endl;
    #define debugln()                      cerr<<endl;
    #define debug1(a)                      cerr<<#a<<"="<<a<<endl;
    #define debug2(a,b)                    cerr<<#a<<"="<<a<<" "<<#b<<"="<<b<<endl;
    #define debug3(a,b,c)                  cerr<<#a<<"="<<a<<" "<<#b<<"="<<b<<" "<<#c<<"="<<c<<endl;
    #define debug4(a,b,c,d)                cerr<<#a<<"="<<a<<" "<<#b<<"="<<b<<" "<<#c<<"="<<c<<" "<<#d<<"="<<d<<endl;
    #define debug5(a,b,c,d,e)              cerr<<#a<<"="<<a<<" "<<#b<<"="<<b<<" "<<#c<<"="<<c<<" "<<#d<<"="<<d<<" "<<#e<<"="<<e<<endl;
    #define debug_arr1(a,n)                cerr<<#a<<" = [ "; for(ll i=0; i<n; i++){cerr<<a[i]<<" ";}cerr<<"]"<<endl;
    #define debug_v1(v)                    cerr<<#v<<" = [ "; for(ll i=0; i<v.size(); i++){cerr<<v[i]<<" ";}cerr<<"]"<<endl;
    #define debug_arr2(arr,row,column)     cerr<<#arr<<"="<<endl; for(ll i=0; i<row; i++){cerr<<"[ ";for(ll j=0; j<column; j++){cerr<<arr[i][j]<<" ";}cerr<<"]"<<endl;}cerr<<endl;
    #define debug_v2(v)                    cerr<<#v<<"="<<endl; for(ll i=0; i<v.size(); i++){cerr<<"[ ";for(ll j=0; j<v[i].size(); j++){cerr<<v[i][j]<<" ";}cerr<<"]"<<endl;}cerr<<endl;
    #define debug_map(mm)                  cerr<<#mm<<"="<<endl; for(auto it=mm.begin(); it!=mm.end(); it++){cerr<<"{"<<it->first<<" "<<it->second<<"}"<<endl;}cerr<<endl;
    #define debug_set(s)                   cerr<<#s<<" = [ "; for(auto it=s.begin(); it!=s.end(); it++){cerr<<*it<<" ";}cerr<<"]"<<endl;
    #define debug_vect_of_pair(v)          cerr<<#v<<"="<<endl; for(ll i=0; i<v.size(); i++){cerr<<"{"<<v[i].first<<" "<<v[i].second<<"}"<<endl;}cerr<<endl;
    #define debug_adjacency_list(arr,n)    cerr<<#arr<<"="<<endl; for(ll i=1; i<n+1; i++){cerr<<"ind="<<i<<"-> ";for(ll j=0; j<arr[i].size(); j++){cerr<<arr[i][j]<<" ";}cerr<<endl;}
#else
    #define debug()
    #define debugln()
    #define debug1(a)
    #define debug2(a,b)
    #define debug3(a,b,c)
    #define debug4(a,b,c,d)
    #define debug5(a,b,c,d,e)
    #define debug_arr1(a,n)
    #define debug_arr2(arr,row,column)
    #define debug_v1(v)
    #define debug_v2(v)
    #define debug_map(mm)
    #define debug_set(s)
    #define debug_vect_of_pair(v)
    #define debug_adjacency_list(arr,n)
#endif
void tokenize(string const &str, const char* delim,vector<string> &out)
{
    char *token = strtok(const_cast<char*>(str.c_str()), delim);
    while (token != nullptr)
    {
        out.push_back(string(token));
        token = strtok(nullptr, delim);
    }
}
vector<ll> str_p(string data){
    v_ll trav;
    ll i;
    stringstream ss(data);
    while (ss >> i) {
        trav.push_back(i);
        ss.ignore(1);
    }
    return trav;
}
ll factorial(ll n)
{
    return (n==1 || n==0) ? 1: n * factorial(n - 1);
}
string multiline(string s){
    getline(cin, s);
    return s;
}
string str(char c){
    string s;
    stringstream ss;
    ss << c;
    ss >> s;
    return s;
}
bool sortbysec(const pair<int,int> &a,const pair<int,int> &b)
{
    return (a.second < b.second);
}
string to_binary(ll val)
{
    string binary = bitset<30>(val).to_string();
    return binary;
}
ll to_decimal(const string& s)
{
    return (bitset<50>(s).to_ulong());
}
double log_a_to_base_b(double a, double b)
{
    return log(a) / log(b);
}
bool isprime(int x) {if (x == 1) return false; for (int i = 2; i * i <= x; ++i) {if (x % i == 0) return false; } return true; }
ll str_to_in(string s)
{
    stringstream geek(s);
    ll x = 0ll;
    geek >> x;
    return x;
}
bool hdp(double z) {
    if (ceil((float) z / 1.0) == floor((float) z / 1.0)) {
        return false;
    }
    return true;
}
ll sum_v(v_ll v1){
    ll sum=0;
    forn(0,v1.size(),i){
        sum+=v1[i];
    }
    return sum;
}
ll binarySearch(v_ll arr,ll k,ll len){
    ll low = 0;
    ll high = len -1;
    ll mid = (high+low)/2;
    ll index = -1;
    while(low <= high){
        mid =( low + high ) / 2;
        if(arr[mid] <= k){
            index = mid;
            low = mid+1;
        }
        else{
            high=mid-1;
        }
    }
    return (index);
}
ll ceil(ll a,ll b){
    ll val = (a / b) + ((a % b) != 0);
    return val;
}
void karthik(){
    ll n;
    cin>>n;
    v_ll v1(n);
    forn(0,n,i){
        cin>>v1[i];
    }
    v_ll temp=v1;
    sorts(temp);

    ll f=0;
    ll i=0;
    while(i<n && v1[i]==0){
        i++;
    }
    ll z=i;
    for(i=z;i<n-1;i++){
        if(v1[i]>=v1[i+1]){
            if(v1[i]==v1[i+1]){
                v1[i+1]=0;
                forn(i+2,n,j){
                    if(!(v1[j]>=v1[j-1])){
                        f=1;
                    }
                }
            }else{
                f=1;
            }
            break;
        }else{
            v1[i+1]-=v1[i];
        }
    }
    if(f==1){
        cout<<"NO";
    }else{
        cout<<"YES";
    }
    cout<<'\n';
}
int main(){
    fast();
    auto start=chrono::high_resolution_clock::now();
    #ifndef ONLINE_JUDGE
        freopen("debug.txt", "w", stderr);
    #endif
    ll t;cin>>t;
    forn(0,t,i){
        karthik();
    }
    auto stop=chrono::high_resolution_clock::now();
    chrono::duration<double> diff=stop-start;
    #ifndef ONLINE_JUDGE
        cerr<<fixed<<setprecision(3)<<left;
    #endif
    return 0;
}

Submission (verdict: Accepted):
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef vector<int> v_i;
typedef vector<long long int> v_ll;
typedef vector<pair<int,int>> vp_i;
typedef vector<pair<long long int,long long int>> vp_ll;
typedef map<int,int> m_i;
typedef map<long long int,long long int> m_ll;
typedef map<pair<int,int>,int> mp_i;
typedef map<pair<long long int,long long int>,long long int> mp_ll;
typedef set<int> s_i;
typedef set<long long int> s_ll;
typedef set<pair<int,int>> sp_i;
typedef set<pair<long long int,long long int>> sp_ll;
typedef priority_queue<ll> maxheap;
typedef priority_queue <ll, vector<ll>, greater<ll>> minheap;
#define mp make_pair
#define fi first
#define se second
#define INF 2e18
#define sorts(v) sort((v).begin(),(v).end())
#define rev(v) reverse((v).begin(),(v).end())
#define count2(v,s) count((v).begin(),(v).end(),(s))
#define finds(v,s) find((v).begin(),(v).end(),(s))
#define fast() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define min3(a,b,c)     min(a,min(b,c))
#define max3(a,b,c)     max(a,max(b,c))
#define min4(a,b,c,d)   min(min(a,b),min(c,d))
#define max4(a,b,c,d)   max(max(a,b),max(c,d))
#define max5(a,b,c,d,e) max(max3(a,b,c),max(d,e))
#define min5(a,b,c,d,e) min(min3(a,b,c),min(d,e))
#define forn(a,b,c) for(ll (c)=(a);(c)<(b);(c)++)
#define forr(a,b,c) for(ll (c)=(a);(i)>=(b);(c)--)
#define rep1(s) for(auto &it:(s))
#define rep2(s) for(auto it=(s).begin();it!=(s).end();it++)
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;}
#ifndef ONLINE_JUDGE
    #define debug()                        cerr<<"[Check]"<<endl;
    #define debugln()                      cerr<<endl;
    #define debug1(a)                      cerr<<#a<<"="<<a<<endl;
    #define debug2(a,b)                    cerr<<#a<<"="<<a<<" "<<#b<<"="<<b<<endl;
    #define debug3(a,b,c)                  cerr<<#a<<"="<<a<<" "<<#b<<"="<<b<<" "<<#c<<"="<<c<<endl;
    #define debug4(a,b,c,d)                cerr<<#a<<"="<<a<<" "<<#b<<"="<<b<<" "<<#c<<"="<<c<<" "<<#d<<"="<<d<<endl;
    #define debug5(a,b,c,d,e)              cerr<<#a<<"="<<a<<" "<<#b<<"="<<b<<" "<<#c<<"="<<c<<" "<<#d<<"="<<d<<" "<<#e<<"="<<e<<endl;
    #define debug_arr1(a,n)                cerr<<#a<<" = [ "; for(ll i=0; i<n; i++){cerr<<a[i]<<" ";}cerr<<"]"<<endl;
    #define debug_v1(v)                    cerr<<#v<<" = [ "; for(ll i=0; i<v.size(); i++){cerr<<v[i]<<" ";}cerr<<"]"<<endl;
    #define debug_arr2(arr,row,column)     cerr<<#arr<<"="<<endl; for(ll i=0; i<row; i++){cerr<<"[ ";for(ll j=0; j<column; j++){cerr<<arr[i][j]<<" ";}cerr<<"]"<<endl;}cerr<<endl;
    #define debug_v2(v)                    cerr<<#v<<"="<<endl; for(ll i=0; i<v.size(); i++){cerr<<"[ ";for(ll j=0; j<v[i].size(); j++){cerr<<v[i][j]<<" ";}cerr<<"]"<<endl;}cerr<<endl;
    #define debug_map(mm)                  cerr<<#mm<<"="<<endl; for(auto it=mm.begin(); it!=mm.end(); it++){cerr<<"{"<<it->first<<" "<<it->second<<"}"<<endl;}cerr<<endl;
    #define debug_set(s)                   cerr<<#s<<" = [ "; for(auto it=s.begin(); it!=s.end(); it++){cerr<<*it<<" ";}cerr<<"]"<<endl;
    #define debug_vect_of_pair(v)          cerr<<#v<<"="<<endl; for(ll i=0; i<v.size(); i++){cerr<<"{"<<v[i].first<<" "<<v[i].second<<"}"<<endl;}cerr<<endl;
    #define debug_adjacency_list(arr,n)    cerr<<#arr<<"="<<endl; for(ll i=1; i<n+1; i++){cerr<<"ind="<<i<<"-> ";for(ll j=0; j<arr[i].size(); j++){cerr<<arr[i][j]<<" ";}cerr<<endl;}
#else
    #define debug()
    #define debugln()
    #define debug1(a)
    #define debug2(a,b)
    #define debug3(a,b,c)
    #define debug4(a,b,c,d)
    #define debug5(a,b,c,d,e)
    #define debug_arr1(a,n)
    #define debug_arr2(arr,row,column)
    #define debug_v1(v)
    #define debug_v2(v)
    #define debug_map(mm)
    #define debug_set(s)
    #define debug_vect_of_pair(v)
    #define debug_adjacency_list(arr,n)
#endif
void tokenize(string const &str, const char* delim,vector<string> &out)
{
    char *token = strtok(const_cast<char*>(str.c_str()), delim);
    while (token != nullptr)
    {
        out.push_back(string(token));
        token = strtok(nullptr, delim);
    }
}
vector<ll> str_p(string data){
    v_ll trav;
    ll i;
    stringstream ss(data);
    while (ss >> i) {
        trav.push_back(i);
        ss.ignore(1);
    }
    return trav;
}
ll factorial(ll n)
{
    return (n==1 || n==0) ? 1: n * factorial(n - 1);
}
string multiline(string s){
    getline(cin, s);
    return s;
}
string str(char c){
    string s;
    stringstream ss;
    ss << c;
    ss >> s;
    return s;
}
bool sortbysec(const pair<int,int> &a,const pair<int,int> &b)
{
    return (a.second < b.second);
}
string to_binary(ll val)
{
    string binary = bitset<30>(val).to_string();
    return binary;
}
ll to_decimal(const string& s)
{
    return (bitset<50>(s).to_ulong());
}
double log_a_to_base_b(double a, double b)
{
    return log(a) / log(b);
}
bool isprime(int x) {if (x == 1) return false; for (int i = 2; i * i <= x; ++i) {if (x % i == 0) return false; } return true; }
ll str_to_in(string s)
{
    stringstream geek(s);
    ll x = 0ll;
    geek >> x;
    return x;
}
bool hdp(double z) {
    if (ceil((float) z / 1.0) == floor((float) z / 1.0)) {
        return false;
    }
    return true;
}
ll sum_v(v_ll v1){
    ll sum=0;
    forn(0,v1.size(),i){
        sum+=v1[i];
    }
    return sum;
}
ll binarySearch(v_ll arr,ll k,ll len){
    ll low = 0;
    ll high = len -1;
    ll mid = (high+low)/2;
    ll index = -1;
    while(low <= high){
        mid =( low + high ) / 2;
        if(arr[mid] <= k){
            index = mid;
            low = mid+1;
        }
        else{
            high=mid-1;
        }
    }
    return (index);
}
ll ceil(ll a,ll b){
    ll val = (a / b) + ((a % b) != 0);
    return val;
}
void karthik(){
    ll n;
    cin>>n;
    v_ll v1(n);
    forn(0,n,i){
        cin>>v1[i];
    }
    v_ll temp=v1;
    sorts(temp);

    ll f=0;
    ll i=0;
    while(i<n && v1[i]==0){
        i++;
    }
    ll z=i;
    for(i=z;i<n-1;i++){
        if(v1[i]>v1[i+1]){
            
                f=1;
            
            break;
        }else{
            v1[i+1]-=v1[i];
        }
    }
    if(f==1){
        cout<<"NO";
    }else{
        cout<<"YES";
    }
    cout<<'\n';
}
int main(){
    fast();
    auto start=chrono::high_resolution_clock::now();
    #ifndef ONLINE_JUDGE
        freopen("debug.txt", "w", stderr);
    #endif
    ll t;cin>>t;
    forn(0,t,i){
        karthik();
    }
    auto stop=chrono::high_resolution_clock::now();
    chrono::duration<double> diff=stop-start;
    #ifndef ONLINE_JUDGE
        cerr<<fixed<<setprecision(3)<<left;
    #endif
    return 0;
}