Problem: 2050B - Transfusion
Language: c++23 

Code 1:
#include <bits/stdc++.h>

#include<ext/pb_ds/assoc_container.hpp>

#include<ext/pb_ds/tree_policy.hpp>

using namespace std;

using namespace __gnu_pbds;

typedef long long ll;

typedef unsigned long long ull;

typedef long double lld;

ll expo(ll a, ll b, ll mod) {ll res = 1; while (b > 0) {if (b & 1)res = (res * a) % mod; a = (a * a) % mod; b = b >> 1;} return res;}

ll mminvprime(ll a, ll b) {return expo(a, b - 2, b);}

ll mod_mul(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a * b) % m) + m) % m;}

ll mod_sub(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a - b) % m) + m) % m;}

ll mod_div(ll a, ll b, ll m) {a = a % m; b = b % m; return (mod_mul(a, mminvprime(b, m), m) + m) % m;}  //only for prime m

#define fr(i,n) for (ll i=0;i<n;i++)

#define pb push_back

#define ite vector<int>::iterator

#define sz(x) x.size()

#define ff first

#define ss second

#define all(v) v.begin(), v.end()

#define fv(x,v) for(auto &x:v)

typedef tree<long long, null_type, less_equal<long long>, rb_tree_tag, tree_order_statistics_node_update> pbds; // find_by_order, order_of_key

#define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)

#ifndef ONLINE_JUDGE

#define debug(x) cerr << #x <<" "; _print(x); cerr << endl;

#else

#define debug(x)

#endif

#define mod (ll)(1e9+7)

void _print(ll t) {cerr << t;}

void _print(int t) {cerr << t;}

void _print(string t) {cerr << t;}

void _print(char t) {cerr << t;}

void _print(lld t) {cerr << t;}

void _print(double t) {cerr << t;}

void _print(ull t) {cerr << t;}



template <class T, class V> void _print(pair <T, V> p);

template <class T> void _print(vector <T> v);

template <class T> void _print(set <T> v);

template <class T, class V> void _print(map <T, V> v);

template <class T> void _print(multiset <T> v);

template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}";}

template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}

template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}

template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}

template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}

template <class T, class V> void _print(unordered_map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}

#pragma GCC optimize("unroll-loops,O3,Ofast") //even 10^8+ also works with this

//#pragma GCC target("avx2,avx,fma,bmi,bmi2,lzcnt,popcnt")



int knightx[8]={-1,-2,-2,-1,1,2,2,1};

int knighty[8]={-2,-1,1,2,2,1,-1,-2};



//...............................................................................................................................................



// lambda dfs------------->

// function<int(int,int)> dfs = [&](int node,int par){



// };

// dfs(0,-1);

// ---------------------------------------------------------------------------------------

// //__builtin_popcount(a) number of set bits for int

// //__builtin_popcountll(a) number of set bits for long long

// //<<left shift *2

// //>>Right shift /2

// //char a='A'|(1<<5); in uppercase 5th bit is not set and in lower case 5th bit is set

// //char a='A'|' ';

// //char A='a'&(~(1<<5)); unset the 5th bit

// //char A='a'& '_';

// //if(n&(n-1)==0) then number is power of two

//..................................................................................................

//0xAAAAAAAA

//0x55555555

//if (x.find(s) != string::npos)

//auto lastElement = *--mySet.end();

//mySet.erase(--mySet.end());

//else if (b % a == 0 && c % a == 0 && (b / a - 1) + (c / a - 1) <= 3)

//if rope have to be divide by equal;

//maxSum = *max_element(all(v));

// when modulo comes check for same number

// when we do difference check if its is negative

//__int128 cal = (__int128)n * mid * mid + (__int128)k * mid;

// most number of divisor less than equal to n in vector

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

//       for(int j = i; j <= n; j += i) mx[j] += cnt[i];

//    }

// if(a<b) ans b is prime number than gcd(a,b) is 1 ;

// for (int i = n; i >= 1; i--) {

//     while (b.size() < a[i]) {

//         b.push_back(i);

//     }

// for(int i=(s[0]=='0');i<s.size();

//cout<<(i>=p?'0':s[i]);

// upper bound give just greater than 

// lower bound give equal or greater

//                // Need to handle re-insertion since multimap does not support direct modification.

                //mp.erase(it);

                //mp.emplace(it->first, 'A');

// sum of 1+2+3+4+5.... odd time is exactly divisible by n;

//cout << (char)(digits[i] < 4 ? (digits[i] + '0') : (digits[i] + '1'));

// auto getSum = [&](int l, int r) -> int {

//     return (r * (r + 1) / 2) - (l * (l - 1) / 2); 

// };

//        result.insert(result.end(), levelNodes.begin(), levelNodes.end());



//        leftToRight = !leftToRight;

// do {

//             string concatenated = "";

//             for (const string& bin : perm) {

//                 concatenated += bin;

//             }

//             maxValue = max(maxValue, binaryToDecimal(concatenated));

//         } while (next_permutation(perm.begin(), perm.end()));

#define vin(v) for(auto &x : v) cin >> x;

int binaryToDecimal(const string& binary) {

        return stoi(binary, nullptr, 2);

    }

string toBinary(int n) {

        string binary = "";

        while (n > 0) {

            binary = to_string(n % 2) + binary;

            n /= 2;

        }

        return binary.empty() ? "0" : binary;

    }

class DisjointSet {

    vector<int> rank, parent, size; 



public: 

    DisjointSet(int n) {

        rank.resize(n+1, 0); 

        parent.resize(n+1);

        size.resize(n+1); 

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

            parent[i] = i; 

            size[i] = 1; 

        }

    }



    int findUPar(int node) {

        if(node == parent[node])

            return node; 

        return parent[node] = findUPar(parent[node]); // Path compression

    }



    void unionByRank(int u, int v) {

        int ulp_u = findUPar(u); 

        int ulp_v = findUPar(v); 

        if(ulp_u == ulp_v) return; 

        if(rank[ulp_u] < rank[ulp_v]) {

            parent[ulp_u] = ulp_v; 

        }

        else if(rank[ulp_v] < rank[ulp_u]) {

            parent[ulp_v] = ulp_u; 

        }

        else {

            parent[ulp_v] = ulp_u; 

            rank[ulp_u]++; 

        }

    }



    void unionBySize(int u, int v) {

        int ulp_u = findUPar(u); 

        int ulp_v = findUPar(v); 

        if(ulp_u == ulp_v) return; 

        if(size[ulp_u] < size[ulp_v]) {

            parent[ulp_u] = ulp_v; 

            size[ulp_v] += size[ulp_u]; 

        }

        else {

            parent[ulp_v] = ulp_u;

            size[ulp_u] += size[ulp_v]; 

        }

    }

    vector<vector<int>> groups() {

        int n = parent.size();

        vector<int> leader_buf(n), group_size(n);

        

        // Find leaders for each node

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

            leader_buf[i] = findUPar(i);

            group_size[leader_buf[i]]++;

        }

        

        // Create groups

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

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

            result[i].reserve(group_size[i]);

        }

        

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

            result[leader_buf[i]].push_back(i);

        }



        // Remove empty groups

        result.erase(

            remove_if(result.begin(), result.end(), 

                      [](const vector<int>& v) { return v.empty(); }),

            result.end()

        );



        return result;

    }

};

class Solution

{

public:

    //Function to find sum of weights of edges of the Minimum Spanning Tree.

    int spanningTree(int V, vector<vector<int>> adj[])

    {

        // 1 - 2 wt = 5

        /// 1 - > (2, 5)

        // 2 -> (1, 5)



        // 5, 1, 2

        // 5, 2, 1

        vector<pair<int, pair<int, int>>> edges;

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

            for (auto it : adj[i]) {

                int adjNode = it[0];

                int wt = it[1];

                int node = i;



                edges.push_back({wt, {node, adjNode}});

            }

        }

        DisjointSet ds(V);

        sort(edges.begin(), edges.end());

        int mstWt = 0;

        for (auto it : edges) {

            int wt = it.first;

            int u = it.second.first;

            int v = it.second.second;



            if (ds.findUPar(u) != ds.findUPar(v)) {

                mstWt += wt;

                ds.unionBySize(u, v);

            }

        }



        return mstWt;

    }

};

void solve() {

    int n; cin >> n;

    vector<int> v(n);

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

    

    ll sum = accumulate(v.begin(), v.end(), 0LL);

    if (sum % n != 0) {

        cout << "NO" << endl;

        return;

    }

    

    ll p = sum / n;

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

        if (v[i - 1] < p) {

            int diff = p - v[i - 1];

            v[i + 1] -= diff;

            v[i - 1] = p;

        } else if (v[i - 1] > p) {

            int diff = v[i - 1] - p;

            v[i + 1] += diff;

            v[i - 1] = p;

        }

    }

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

        if (v[i] != p) {

            cout << "NO" << endl;

            return;

        }

    }

    

    cout << "YES" << endl;

}

int main(){

#ifndef ONLINE_JUDGE

    freopen("Error.txt","w",stderr);

#endif

    

    int t=1;

    cin>>t;

    while(t--){

        solve();

    }

} 

Code 2:
#include <bits/stdc++.h>

#include<ext/pb_ds/assoc_container.hpp>

#include<ext/pb_ds/tree_policy.hpp>

using namespace std;

using namespace __gnu_pbds;

typedef long long ll;

typedef unsigned long long ull;

typedef long double lld;

ll expo(ll a, ll b, ll mod) {ll res = 1; while (b > 0) {if (b & 1)res = (res * a) % mod; a = (a * a) % mod; b = b >> 1;} return res;}

ll mminvprime(ll a, ll b) {return expo(a, b - 2, b);}

ll mod_mul(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a * b) % m) + m) % m;}

ll mod_sub(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a - b) % m) + m) % m;}

ll mod_div(ll a, ll b, ll m) {a = a % m; b = b % m; return (mod_mul(a, mminvprime(b, m), m) + m) % m;}  //only for prime m

#define fr(i,n) for (ll i=0;i<n;i++)

#define pb push_back

#define ite vector<int>::iterator

#define sz(x) x.size()

#define ff first

#define ss second

#define all(v) v.begin(), v.end()

#define fv(x,v) for(auto &x:v)

typedef tree<long long, null_type, less_equal<long long>, rb_tree_tag, tree_order_statistics_node_update> pbds; // find_by_order, order_of_key

#define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)

#ifndef ONLINE_JUDGE

#define debug(x) cerr << #x <<" "; _print(x); cerr << endl;

#else

#define debug(x)

#endif

#define mod (ll)(1e9+7)

void _print(ll t) {cerr << t;}

void _print(int t) {cerr << t;}

void _print(string t) {cerr << t;}

void _print(char t) {cerr << t;}

void _print(lld t) {cerr << t;}

void _print(double t) {cerr << t;}

void _print(ull t) {cerr << t;}



template <class T, class V> void _print(pair <T, V> p);

template <class T> void _print(vector <T> v);

template <class T> void _print(set <T> v);

template <class T, class V> void _print(map <T, V> v);

template <class T> void _print(multiset <T> v);

template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}";}

template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}

template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}

template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}

template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}

template <class T, class V> void _print(unordered_map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}

#pragma GCC optimize("unroll-loops,O3,Ofast") //even 10^8+ also works with this

//#pragma GCC target("avx2,avx,fma,bmi,bmi2,lzcnt,popcnt")



int knightx[8]={-1,-2,-2,-1,1,2,2,1};

int knighty[8]={-2,-1,1,2,2,1,-1,-2};



//...............................................................................................................................................



// lambda dfs------------->

// function<int(int,int)> dfs = [&](int node,int par){



// };

// dfs(0,-1);

// ---------------------------------------------------------------------------------------

// //__builtin_popcount(a) number of set bits for int

// //__builtin_popcountll(a) number of set bits for long long

// //<<left shift *2

// //>>Right shift /2

// //char a='A'|(1<<5); in uppercase 5th bit is not set and in lower case 5th bit is set

// //char a='A'|' ';

// //char A='a'&(~(1<<5)); unset the 5th bit

// //char A='a'& '_';

// //if(n&(n-1)==0) then number is power of two

//..................................................................................................

//0xAAAAAAAA

//0x55555555

//if (x.find(s) != string::npos)

//auto lastElement = *--mySet.end();

//mySet.erase(--mySet.end());

//else if (b % a == 0 && c % a == 0 && (b / a - 1) + (c / a - 1) <= 3)

//if rope have to be divide by equal;

//maxSum = *max_element(all(v));

// when modulo comes check for same number

// when we do difference check if its is negative

//__int128 cal = (__int128)n * mid * mid + (__int128)k * mid;

// most number of divisor less than equal to n in vector

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

//       for(int j = i; j <= n; j += i) mx[j] += cnt[i];

//    }

// if(a<b) ans b is prime number than gcd(a,b) is 1 ;

// for (int i = n; i >= 1; i--) {

//     while (b.size() < a[i]) {

//         b.push_back(i);

//     }

// for(int i=(s[0]=='0');i<s.size();

//cout<<(i>=p?'0':s[i]);

// upper bound give just greater than 

// lower bound give equal or greater

//                // Need to handle re-insertion since multimap does not support direct modification.

                //mp.erase(it);

                //mp.emplace(it->first, 'A');

// sum of 1+2+3+4+5.... odd time is exactly divisible by n;

//cout << (char)(digits[i] < 4 ? (digits[i] + '0') : (digits[i] + '1'));

// auto getSum = [&](int l, int r) -> int {

//     return (r * (r + 1) / 2) - (l * (l - 1) / 2); 

// };

//        result.insert(result.end(), levelNodes.begin(), levelNodes.end());



//        leftToRight = !leftToRight;

// do {

//             string concatenated = "";

//             for (const string& bin : perm) {

//                 concatenated += bin;

//             }

//             maxValue = max(maxValue, binaryToDecimal(concatenated));

//         } while (next_permutation(perm.begin(), perm.end()));

#define vin(v) for(auto &x : v) cin >> x;

int binaryToDecimal(const string& binary) {

        return stoi(binary, nullptr, 2);

    }

string toBinary(int n) {

        string binary = "";

        while (n > 0) {

            binary = to_string(n % 2) + binary;

            n /= 2;

        }

        return binary.empty() ? "0" : binary;

    }

class DisjointSet {

    vector<int> rank, parent, size; 



public: 

    DisjointSet(int n) {

        rank.resize(n+1, 0); 

        parent.resize(n+1);

        size.resize(n+1); 

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

            parent[i] = i; 

            size[i] = 1; 

        }

    }



    int findUPar(int node) {

        if(node == parent[node])

            return node; 

        return parent[node] = findUPar(parent[node]); // Path compression

    }



    void unionByRank(int u, int v) {

        int ulp_u = findUPar(u); 

        int ulp_v = findUPar(v); 

        if(ulp_u == ulp_v) return; 

        if(rank[ulp_u] < rank[ulp_v]) {

            parent[ulp_u] = ulp_v; 

        }

        else if(rank[ulp_v] < rank[ulp_u]) {

            parent[ulp_v] = ulp_u; 

        }

        else {

            parent[ulp_v] = ulp_u; 

            rank[ulp_u]++; 

        }

    }



    void unionBySize(int u, int v) {

        int ulp_u = findUPar(u); 

        int ulp_v = findUPar(v); 

        if(ulp_u == ulp_v) return; 

        if(size[ulp_u] < size[ulp_v]) {

            parent[ulp_u] = ulp_v; 

            size[ulp_v] += size[ulp_u]; 

        }

        else {

            parent[ulp_v] = ulp_u;

            size[ulp_u] += size[ulp_v]; 

        }

    }

    vector<vector<int>> groups() {

        int n = parent.size();

        vector<int> leader_buf(n), group_size(n);

        

        // Find leaders for each node

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

            leader_buf[i] = findUPar(i);

            group_size[leader_buf[i]]++;

        }

        

        // Create groups

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

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

            result[i].reserve(group_size[i]);

        }

        

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

            result[leader_buf[i]].push_back(i);

        }



        // Remove empty groups

        result.erase(

            remove_if(result.begin(), result.end(), 

                      [](const vector<int>& v) { return v.empty(); }),

            result.end()

        );



        return result;

    }

};

class Solution

{

public:

    //Function to find sum of weights of edges of the Minimum Spanning Tree.

    int spanningTree(int V, vector<vector<int>> adj[])

    {

        // 1 - 2 wt = 5

        /// 1 - > (2, 5)

        // 2 -> (1, 5)



        // 5, 1, 2

        // 5, 2, 1

        vector<pair<int, pair<int, int>>> edges;

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

            for (auto it : adj[i]) {

                int adjNode = it[0];

                int wt = it[1];

                int node = i;



                edges.push_back({wt, {node, adjNode}});

            }

        }

        DisjointSet ds(V);

        sort(edges.begin(), edges.end());

        int mstWt = 0;

        for (auto it : edges) {

            int wt = it.first;

            int u = it.second.first;

            int v = it.second.second;



            if (ds.findUPar(u) != ds.findUPar(v)) {

                mstWt += wt;

                ds.unionBySize(u, v);

            }

        }



        return mstWt;

    }

};

void solve(){

    int n;cin>>n;

    vector<int>v(n);

    vin(v);

    ll sum=accumulate(v.begin(),v.end(),0);

    set<int>s;

    if(sum%n)

    {

        cout<<"NO"<<endl;

        return;

    }

    else

    {

        ll p=sum/n;

        debug(p)

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

        {



            if(v[i-1]<p)

            {

                v[i+1]-=(p-v[i-1]);

                v[i-1]=p;

                s.insert({v[i-1]});



            }

            else

            {

                v[i+1]+=(v[i-1]-p);

                v[i-1]=p;

                s.insert({v[i-1]});

            }

        }

        s.insert({v[n-1]});

        if(s.size()>1)

        {

            debug(v)

            cout<<"NO"<<endl;

        }

        else

        {

            cout<<"YES"<<endl;

        }

    }





}

int main(){

#ifndef ONLINE_JUDGE

    freopen("Error.txt","w",stderr);

#endif

    

    int t=1;

    cin>>t;

    while(t--){

        solve();

    }

} 

Answer: 1