Problem: 2060D - Subtract Min Sort
Language: C++17 (GCC 7-32)

Previous submission (verdict: Runtime error on test 7):
// #pragma GCC optimize(2)

#include <bits/stdc++.h>

using namespace std;

#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

#define pb push_back

#define st first

#define nd second

#define PII pair<ll, ll>

#define D(x, y) cout << x << "=" << y << endl;

#define fcout(x, n) cout << fixed << setprecision(x) << n << endl;

#define rep(i, a, b) for (int i = a; i < b; i++)

#define rep2(i, a, b) for (int i = a; i >= b; i--)

// #define int long long

// #define endl '\n'

typedef long long ll;

typedef unsigned long long ull;

const ll inf = 0x3f3f3f3f;

const int N = 1e5 + 10;

const int M = 1e9+7;

inline ll read()

{

    ll x = 0;

    bool f = 1;

    char c = getchar();

    for (; !isdigit(c); c = getchar())

        if (c == '-')

            f = 0;

    for (; isdigit(c); c = getchar())

        x = (x << 3) + (x << 1) + c - '0';

    return f ? x : 0 - x;

}

inline void out(ll x)

{

    if (x < 0)

        putchar('-'), x = -x;

    if (x > 9)

        out(x / 10);

    putchar(x % 10 + '0');

}

// __int128 rd()

// {

// 	char arr[30];

// 	__int128 res = 0;

// 	scanf("%s", arr);

// 	for (int i = 1; i <= strlen(arr); i++)

// 	{

// 		res *= 10;

// 		res += arr[i]-'0';

// 	}

// 	return res;

// }

// void pt(__int128 num)

// {

// 	if (num > 9) pt(num / 10);

// 	putchar(num % 10 + '0');

// }

ll n, m;

ll a[N];

void solve()

{

    cin>>n;

    rep(i,0,n) cin>>a[i];

    ll x=0;

    rep(i,1,n)

    {

        if(a[i-1]>a[i]) x=i;

    } 

    if(x==0) return cout<<"YES\n",void();

    rep(i,0,x)

    {

        ll mn=min(a[i],a[i+1]);

        a[i]-=mn,a[i+1]-=mn;

    }

    rep(i,1,n)

    {

        if(a[i-1]>a[i]) return cout<<"NO\n",void();

    }

    cout<<"YES\n";

}   

signed main()

{

    IOS

        // freopen("../.vscode/io/in.txt","r",stdin),freopen("../.vscode/io/out.txt","w",stdout);

        ll T = 1;

    cin >> T;

    while (T--)

        solve();

}



Submission (verdict: Accepted):
// #pragma GCC optimize(2)

#include <bits/stdc++.h>

using namespace std;

#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

#define pb push_back

#define st first

#define nd second

#define PII pair<ll, ll>

#define D(x, y) cout << x << "=" << y << endl;

#define fcout(x, n) cout << fixed << setprecision(x) << n << endl;

#define rep(i, a, b) for (int i = a; i < b; i++)

#define rep2(i, a, b) for (int i = a; i >= b; i--)

// #define int long long

// #define endl '\n'

typedef long long ll;

typedef unsigned long long ull;

const ll inf = 0x3f3f3f3f;

const int N = 1e6 + 10;

const int M = 1e9+7;

inline ll read()

{

    ll x = 0;

    bool f = 1;

    char c = getchar();

    for (; !isdigit(c); c = getchar())

        if (c == '-')

            f = 0;

    for (; isdigit(c); c = getchar())

        x = (x << 3) + (x << 1) + c - '0';

    return f ? x : 0 - x;

}

inline void out(ll x)

{

    if (x < 0)

        putchar('-'), x = -x;

    if (x > 9)

        out(x / 10);

    putchar(x % 10 + '0');

}

// __int128 rd()

// {

// 	char arr[30];

// 	__int128 res = 0;

// 	scanf("%s", arr);

// 	for (int i = 1; i <= strlen(arr); i++)

// 	{

// 		res *= 10;

// 		res += arr[i]-'0';

// 	}

// 	return res;

// }

// void pt(__int128 num)

// {

// 	if (num > 9) pt(num / 10);

// 	putchar(num % 10 + '0');

// }

ll n, m;

ll a[N];

void solve()

{

    cin>>n;

    rep(i,0,n) cin>>a[i];

    ll x=0;

    rep(i,1,n)

    {

        if(a[i-1]>a[i]) x=i;

    } 

    if(x==0) return cout<<"YES\n",void();

    rep(i,0,x)

    {

        ll mn=min(a[i],a[i+1]);

        a[i]-=mn,a[i+1]-=mn;

    }

    rep(i,1,n)

    {

        if(a[i-1]>a[i]) return cout<<"NO\n",void();

    }

    cout<<"YES\n";

}   

signed main()

{

    IOS

        // freopen("../.vscode/io/in.txt","r",stdin),freopen("../.vscode/io/out.txt","w",stdout);

        ll T = 1;

    cin >> T;

    while (T--)

        solve();

}