Problem: 2050B - Transfusion
Language: C++17 (GCC 7-32)

Previous submission (verdict: Wrong answer on test 3):
#include <bits/stdc++.h>

using  namespace std;

int main() {

    int t;cin >> t;

    while (t--) {

        int n;cin >> n;

        vector<int>v(n);

        vector<int>v2(n);

        int count = 0;

        for (int i = 0;i < n;i++){cin >> v[i];count += v[i];}

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

        if (count % n){cout << "NO" << endl;continue;}

        int sum = count / n;

        for (int i = 1;i < v.size() - 1;i++) {

            int j1,j2;

            if (v[i - 1] < sum && v[i + 1] > sum) {

                j1 = sum - v[i - 1];

                v[i - 1] += j1;

                v[i + 1] -= j1;

            }else if (v[i - 1] > sum && v[i + 1] < sum){

                j1 = v[i - 1] - sum;

                v[i - 1] -= j1;

                v[i + 1] += j1;

            }

            else {

                j1 = v[i - 1] - sum;

                v[i - 1] -= j1;

                v[i + 1] += j1;

            }

        }

        bool f = true;

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

            if (v[i] != sum){f = false;break;}

        }

        for (int i = 1;i < v.size() - 1;i++) {

            int j1,j2;

            if (v[i - 1] < sum && v[i + 1] > sum) {

                j1 = sum - v[i - 1];

                v[i - 1] += j1;

                v[i + 1] -= j1;

            }else if (v[i - 1] > sum && v[i + 1] < sum){

                j1 = v[i - 1] - sum;

                v[i - 1] -= j1;

                v[i + 1] += j1;

            }

            else {

                j1 = sum - v[i - 1];

                v[i - 1] += j1;

                v[i + 1] -= j1;

            }

        }

        bool t = true;

        for (int i = 0;i < v2.size();i++) {

            if (v2[i] != sum){t = false;break;}

        }

        if (f || t)cout << "YES" << endl;else cout << "NO" << endl;

        v.clear();v2.clear();

    }

}

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

using  namespace std;

int main() {

    int t;cin >> t;

    while (t--) {

        long long n;cin >> n;

        vector<int>v(n);

        vector<int>v2(n);

        long long count = 0;

        for (int i = 0;i < n;i++){cin >> v[i];count += v[i];}

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

        if (count % n){cout << "NO" << endl;continue;}

        long long sum = count / n;

        for (int i = 1;i < v.size() - 1;i++) {

            long long j1,j2;

            if (v[i - 1] < sum && v[i + 1] > sum) {

                j1 = sum - v[i - 1];

                v[i - 1] += j1;

                v[i + 1] -= j1;

            }else if (v[i - 1] > sum && v[i + 1] < sum){

                j1 = v[i - 1] - sum;

                v[i - 1] -= j1;

                v[i + 1] += j1;

            }

            else {

                j1 = v[i - 1] - sum;

                v[i - 1] -= j1;

                v[i + 1] += j1;

            }

        }

        bool f = true;

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

            if (v[i] != sum){f = false;break;}

        }

        for (int i = 1;i < v.size() - 1;i++) {

            int j1,j2;

            if (v[i - 1] < sum && v[i + 1] > sum) {

                j1 = sum - v[i - 1];

                v[i - 1] += j1;

                v[i + 1] -= j1;

            }else if (v[i - 1] > sum && v[i + 1] < sum){

                j1 = v[i - 1] - sum;

                v[i - 1] -= j1;

                v[i + 1] += j1;

            }

            else {

                j1 = sum - v[i - 1];

                v[i - 1] += j1;

                v[i + 1] -= j1;

            }

        }

        bool t = true;

        for (int i = 0;i < v2.size();i++) {

            if (v2[i] != sum){t = false;break;}

        }

        if (f || t)cout << "YES" << endl;else cout << "NO" << endl;

        v.clear();v2.clear();

    }

}