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

Previous submission (verdict: Wrong answer on test 2):
#include <iostream>

#include <fstream>

#include <deque>

#include <vector>

#include <queue>

#include <string>

#include <cstring>

#include <map>

#include <stack>

#include <set>

#include <algorithm>

#include <cmath>

#include <unordered_map>

using namespace std;



#define endl '\n'

#define int long long

#define fi first

#define se second

#define lc p<<1

#define rc p<<1|1

#define PII pair<int,pair<int,int>>





const int N = 1e3 + 10, M = 2e5 + 10;

const int inf = 0x7f7f7f7f7f7f7f7f;

const int MOD = 998244353;



int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }

int qmi(long long a, long long b) {

    long long res = 1;

    while (b > 0) {

        if (b & 1) res = res * a % MOD;

        a = a * a % MOD;

        b >>= 1;

    }

    return res % MOD;

}

int lcm(int a, int b) { return a * b / gcd(a, b); }

int C(int a, int b, int p) {

    if (b > a) return 0;

    int res = 1;

    for (int i = 1, j = a; i <= b; ++i, --j) {

        res = res * j % p;

        res = res * qmi(i, p - 2) % p;

    }

    return res;

}

int lucas(int a, int b, int p) {               // 注意LL参数类型

    if (a < p && b < p) return C(a, b, p);

    return C(a % p, b % p, p) * lucas(a / p, b / p, p) % p;     // 递归让其到p范围内求解

}

int isqrt_newton(int n) {

    int x = 1;

    bool decreased = false;

    for (;;) {

        int nx = (x + n / x) >> 1;

        if (x == nx || (nx > x && decreased)) break;

        decreased = nx < x;

        x = nx;

    }

    return x;

}



struct node {

    int l, r, sum;

}tr[N * 4];



void build(int p, int l, int r) {

    tr[p] = { l,r,0 };

    if (l == r)return;

    int m = l + r >> 1;

    build(lc, l, m);

    build(rc, m + 1, r);

    tr[p].sum + tr[lc].sum + tr[rc].sum;

}



void update(int p, int x, int k) {

    if (tr[p].l == x && tr[p].r == x) {

        tr[p].sum + k; return;

    }

    int m = tr[p].l + tr[p].r >> 1;

    if (x <= m) update(lc, x, k);

    if (x > m) update(rc, x, k);

    tr[p].sum = tr[lc].sum + tr[rc].sum;

}



int query(int p, int x, int y) {

    if (x <= tr[p].l && tr[p].r <= y)return tr[p].sum;

    int m = tr[p].l + tr[p].r >> 1;

    int sum = 0;

    if (x <= m) sum += query(lc, x, y);

    if (y > m) sum += query(rc, x, y);

    return sum;

}



void solve() {

    int n; cin >> n;

    vector<int>a(n + 1);

    for (int i = 1; i <= n; i++) {cin >> a[i];}

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

        //cout << "i:" << i << endl;

        if (a[i] < a[i - 1] && i > 2) {

            int cnt = min(a[i - 1], a[i - 2]);

            a[i - 1] -= cnt;

            a[i - 2] -= cnt;

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

                cout << "NO" << endl;

                return;

            }

            else {

                cnt = min(a[i],a[i - 1]);

                a[i - 1] -= cnt;

                a[i] -= cnt;

            }

        }

    }

    bool st = false;

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

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

            st = true;

        }

    }

    if (st) {

        cout << "NO" << endl;

    }

    else {

        cout << "YES" << endl;

    }

}



signed main() {

    ios::sync_with_stdio(false);

    cin.tie(NULL); cout.tie(NULL);

    int t = 1;

    cin >> t;

    while (t--) {

        solve();

    }

}

Submission (verdict: Accepted):
#include <iostream>

#include <fstream>

#include <deque>

#include <vector>

#include <queue>

#include <string>

#include <cstring>

#include <map>

#include <stack>

#include <set>

#include <algorithm>

#include <cmath>

#include <unordered_map>

using namespace std;



#define endl '\n'

#define int long long

#define fi first

#define se second

#define lc p<<1

#define rc p<<1|1

#define PII pair<int,pair<int,int>>





const int N = 1e3 + 10, M = 2e5 + 10;

const int inf = 0x7f7f7f7f7f7f7f7f;

const int MOD = 998244353;



int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }

int qmi(long long a, long long b) {

    long long res = 1;

    while (b > 0) {

        if (b & 1) res = res * a % MOD;

        a = a * a % MOD;

        b >>= 1;

    }

    return res % MOD;

}

int lcm(int a, int b) { return a * b / gcd(a, b); }

int C(int a, int b, int p) {

    if (b > a) return 0;

    int res = 1;

    for (int i = 1, j = a; i <= b; ++i, --j) {

        res = res * j % p;

        res = res * qmi(i, p - 2) % p;

    }

    return res;

}

int lucas(int a, int b, int p) {               // 注意LL参数类型

    if (a < p && b < p) return C(a, b, p);

    return C(a % p, b % p, p) * lucas(a / p, b / p, p) % p;     // 递归让其到p范围内求解

}

int isqrt_newton(int n) {

    int x = 1;

    bool decreased = false;

    for (;;) {

        int nx = (x + n / x) >> 1;

        if (x == nx || (nx > x && decreased)) break;

        decreased = nx < x;

        x = nx;

    }

    return x;

}



struct node {

    int l, r, sum;

}tr[N * 4];



void build(int p, int l, int r) {

    tr[p] = { l,r,0 };

    if (l == r)return;

    int m = l + r >> 1;

    build(lc, l, m);

    build(rc, m + 1, r);

    tr[p].sum + tr[lc].sum + tr[rc].sum;

}



void update(int p, int x, int k) {

    if (tr[p].l == x && tr[p].r == x) {

        tr[p].sum + k; return;

    }

    int m = tr[p].l + tr[p].r >> 1;

    if (x <= m) update(lc, x, k);

    if (x > m) update(rc, x, k);

    tr[p].sum = tr[lc].sum + tr[rc].sum;

}



int query(int p, int x, int y) {

    if (x <= tr[p].l && tr[p].r <= y)return tr[p].sum;

    int m = tr[p].l + tr[p].r >> 1;

    int sum = 0;

    if (x <= m) sum += query(lc, x, y);

    if (y > m) sum += query(rc, x, y);

    return sum;

}



void solve() {

    int n; cin >> n;

    vector<int>a(n + 1);

    for (int i = 1; i <= n; i++) {cin >> a[i];}

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

        int cnt = 0;

        if (a[i] < a[i - 1] && i > 2) {

            cnt = min(a[i - 1], a[i - 2]);

            a[i - 1] -= cnt;

            a[i - 2] -= cnt;

        }

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

            cout << "NO" << endl;

            return;

        }

        else {

            cnt = min(a[i], a[i - 1]);

            a[i - 1] -= cnt;

            a[i] -= cnt;

        }

    }

    bool st = false;

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

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

            st = true;

        }

    }

    if (st) {

        cout << "NO" << endl;

    }

    else {

        cout << "YES" << endl;

    }

}



signed main() {

    ios::sync_with_stdio(false);

    cin.tie(NULL); cout.tie(NULL);

    int t = 1;

    cin >> t;

    while (t--) {

        solve();

    }

}