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

Previous 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 (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 = true;



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

            }

            sum=pow(-1,v[i].size()+1)*sum;

        

            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: Accepted):
#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 (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 = true;



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

            sum = 0;

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

                if (j % 2 == 0)

                    sum += v[i][j];

                else

                    sum -= v[i][j];

            }

            sum=pow(-1,v[i].size()+1)*sum;

        

            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;

}