Petya has *k* matches, placed in *n* matchboxes lying in a line from left to right. We know that *k* is divisible by *n*. Petya wants all boxes to have the same number of matches inside. For that, he can move a match from its box to the adjacent one in one move. How many such moves does he need to achieve the desired configuration?
The first line contains integer *n* (1 ≤ *n* ≤ 50000). The second line contains *n* non-negative numbers that do not exceed 10<sup class="upper-index">9</sup>, the *i*-th written number is the number of matches in the *i*-th matchbox. It is guaranteed that the total number of matches is divisible by *n*.
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdio>
#include <set>
#include <vector>
#include <map>
#include <cmath>
#include <algorithm>
#include <memory.h>
#include <string>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <ctime>
#include <cassert>
typedef long long ll;
using namespace std;vector <ll> hello;
int main()
{
    int a; cin>> a; ll m=0; 
    for (int g=0; g<a; g++)
    {
        int l; cin >> l; hello.push_back(l); m+=l;
    }ll k= int(m/a); ll ans=0;
    for (int g=0; g<a-1; g++)
    {
        if (hello[g]>k)
        {
            ll io= hello[g]-k;
            ans+=io; hello[g+1]+=io;hello[g]=k; 
        }
        else if (hello[g]<k)
        {
            ll ium=k-hello[g]; ll r=0; 
            for (int kk=g+1; kk<a; kk++)
            {
                if (r+hello[kk]<=ium)
                {
                    ans+= hello[kk]*(kk-g);r+=hello[kk];hello[kk]=0; 
                }
                else
                {
                    ll u= ium-r; r=ium; 
                    hello[kk]-=u;
                    ans+=(kk-g)*u; break; 
                }
            
            }hello[g]=k;
        
        }

    }
    cout << ans << '\n';
}