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

Previous submission (verdict: Compilation error):
#include <bits/stdc++.h>

using namespace std;



int main() {

    int t;

    cin >> t;

    while (t--) {

        int n;

        cin >> n;

        int a[n];

        vector<vector<int>> v(n);   

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

            cin >> a[i];

        }

        if(is_sorted(a, a + n)) {

            cout << "YES" << endl;

            continue;

        }

        if (n > 1 && a[0] > a[1]) {

            cout << "NO" << endl;

            continue;

        }

        bool b = false;

        int c = 0;

        v[0].push_back(a[0]);

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

            if (a[i - 1] <= a[i] && b == false) {

                v[c].push_back(a[i]);

                b = true;

            }

            else if (b == true) {

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

                    v[c].push_back(a[i]);

                else {

                    b = false;

                    c++;

                    v[c].push_back(a[i]);

                }

            }

            else {

                c++;

                v[c].push_back(a[i]);

            }

        }

        int sum = 0;

        bool poss = false;

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

            for (int j = 0; j < v[i].size(); j++) {

                if (j % 2 == 0)

                    sum += v[i][j];

                else

                    sum -= v[i][j];

            }

.

            if (!v[i + 1].empty() && sum <= v[i + 1][0]) {

                poss = true;

                v[i + 1][0] = v[i + 1][0] - sum;

            }

            else {

                poss = false;

                break;

            }

        }

        cout << (poss ? "YES" : "NO") << endl;

    }

    return 0;

}



Submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>

using namespace std;



int main() {

    int t;

    cin >> t;

    while (t--) {

        int n;

        cin >> n;

        int a[n];

        vector<vector<int>> v(n);   

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

            cin >> a[i];

        }

        if(is_sorted(a, a + n)) {

            cout << "YES" << endl;

            continue;

        }

        if (n > 1 && a[0] > a[1]) {

            cout << "NO" << endl;

            continue;

        }

        bool b = false;

        int c = 0;

        v[0].push_back(a[0]);

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

            if (a[i - 1] <= a[i] && b == false) {

                v[c].push_back(a[i]);

                b = true;

            }

            else if (b == true) {

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

                    v[c].push_back(a[i]);

                else {

                    b = false;

                    c++;

                    v[c].push_back(a[i]);

                }

            }

            else {

                c++;

                v[c].push_back(a[i]);

            }

        }

        int sum = 0;

        bool poss = false;

        // Note: iterate only over the filled segments (from 0 to c-1)

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

            for (int j = 0; j < v[i].size(); j++) {

                if (j % 2 == 0)

                    sum += v[i][j];

                else

                    sum -= v[i][j];

            }

            // Before accessing, ensure v[i+1] is not empty.

            if (!v[i + 1].empty() && sum <= v[i + 1][0]) {

                poss = true;

                v[i + 1][0] = v[i + 1][0] - sum;

            }

            else {

                poss = false;

                break;

            }

        }

        cout << (poss ? "YES" : "NO") << endl;

    }

    return 0;

}