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 ll;

const ll MAX = 1e7 + 7;

vector<ll> sp(MAX);  // Smallest prime factor array

vector<bool> v(MAX); // Boolean array to mark composite numbers



ll power(ll x, ll y, ll p)

{

	ll res = 1; // Initialize result

	x = x % p;



	while (y > 0) 

	{

		if (y & 1)

			res = (res * x) % p;

		y = y >> 1; 

		x = (x * x) % p;

	}

	return res;

}



ll modInverse(ll n, ll p)

{

	return power(n, p - 2, p);

}



ll nCr(ll n, ll r, ll p)

{

	if (n < r)

		return 0;

	if (r == 0)

		return 1;



	vector<ll> fac(n + 1);

	fac[0] = 1;

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

		fac[i] = (fac[i - 1] * i) % p;



	return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p;

}





void Sieve(){

	for (int i = 2; i < MAX; i += 2)	sp[i] = 2;//even numbers have smallest prime factor 2

	for (ll i = 3; i < MAX; i += 2){

		if (!v[i]){

			sp[i] = i;

			for (ll j = i; (j*i) < MAX; j += 2){

				if (!v[j*i])	v[j*i] = true, sp[j*i] = i;

			}

		}

	}

}

int main()

{ int t;

cin>>t;

while(t--){

  int n;

  cin>>n;

  int arr[n];

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

    cin>>arr[i];

  }

  if(arr[0]>arr[1]){

    cout<<"NO"<<endl;

  }

  else{

    int z=-1;

    for(int i=n-2; i>=0; i++){

      if(arr[i]>arr[i+1]){

        z=i;

        break;

      }

    }

    if(z==-1){

      cout<<"YES"<<endl;

      continue;

    }

    //ll sm=arr[0];

    

    

    arr[1]=arr[1]-arr[0];

    arr[0]=0;

    for(int i=1; i<=z; i++){

      if(arr[i]>arr[i+1]&&i!=z){

        arr[i]=arr[i]-arr[i+1];

        arr[i+1]=0;

      }

      else{

        arr[i+1]=arr[i+1]-arr[i];

        arr[i]=0;

      }

    }

    int z2=1;

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

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

        z2=0;

        break;

      }

    }

    if(z2){

      cout<<"YES"<<endl;

    }

    else{

      cout<<"NO"<<endl;

    }

  }

}

	return 0;

}



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

using namespace std;

typedef long long ll;

const ll MAX = 1e7 + 7;

vector<ll> sp(MAX);  // Smallest prime factor array

vector<bool> v(MAX); // Boolean array to mark composite numbers



ll power(ll x, ll y, ll p)

{

	ll res = 1; // Initialize result

	x = x % p;



	while (y > 0) 

	{

		if (y & 1)

			res = (res * x) % p;

		y = y >> 1; 

		x = (x * x) % p;

	}

	return res;

}



ll modInverse(ll n, ll p)

{

	return power(n, p - 2, p);

}



ll nCr(ll n, ll r, ll p)

{

	if (n < r)

		return 0;

	if (r == 0)

		return 1;



	vector<ll> fac(n + 1);

	fac[0] = 1;

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

		fac[i] = (fac[i - 1] * i) % p;



	return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p;

}





void Sieve(){

	for (int i = 2; i < MAX; i += 2)	sp[i] = 2;//even numbers have smallest prime factor 2

	for (ll i = 3; i < MAX; i += 2){

		if (!v[i]){

			sp[i] = i;

			for (ll j = i; (j*i) < MAX; j += 2){

				if (!v[j*i])	v[j*i] = true, sp[j*i] = i;

			}

		}

	}

}

int main()

{ int t;

cin>>t;

while(t--){

  int n;

  cin>>n;

  int arr[n];

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

    cin>>arr[i];

  }

  if(arr[0]>arr[1]){

    cout<<"NO"<<endl;

  }

  else{

    int z=-1;

    for(int i=n-2; i>=0; i++){

      if(arr[i]>arr[i+1]){

        z=i;

        break;

      }

    }

    if(z==-1){

      cout<<"YES"<<endl;

      continue;

    }

    //ll sm=arr[0];

    

    

    arr[1]=arr[1]-arr[0];

    arr[0]=0;

    int z2=1;

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

      if(arr[i]>arr[i+1]){

        // arr[i]=arr[i]-arr[i+1];

        // arr[i+1]=0;

        z2=0;

        break;

      }

      else{

        arr[i+1]=arr[i+1]-arr[i];

        arr[i]=0;

      }

    }

    

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

    //   if(arr[i]<arr[i+1]){

    //     z2=0;

    //     break;

    //   }

    // }

    if(z2){

      cout<<"YES"<<endl;

    }

    else{

      cout<<"NO"<<endl;

    }

  }

}

	return 0;

}