#include <bits/stdc++.h>
using namespace std;

#define N 500010
#define M 10010
#define S 5
int n, q, c[N];
int p[M][25], pos[N], lpos[M], rpos[M];

int main()
{
	while (scanf("%d", &n) != EOF)
	{
		for (int i = 1; i <= n; ++i) pos[i] = (i - 1) / S + 1;
		for (int i = 1; i <= pos[n]; ++i)
		{
			rpos[pos[i]] = i;
			if (i == 1 || pos[i] != pos[i - 1])
				lpos[pos[i]] = i;

		}
		for (int i = 1; i <= n; ++i) scanf("%d", c + i);
		[]()
		{
			memset(p, 0, sizeof p);
			for (int i = 1; i <= pos[n]; ++i)
			{
				for (int j = lpos[i]; j <= rpos[i]; ++j)
				{
					int tmp = c[j];
					for (int k = 20; k >= 0; --k) if ((tmp >> k) & 1)
					{
						if (!p[i][k])
						{
							p[i][k] = tmp;
							break;
						}
						else
							tmp ^= p[i][k];
						if (!tmp) break;
					}
				}
			}
		}();
		scanf("%d", &q);
		for (int qq = 1, l, r; qq <= q; ++qq)
		{
			scanf("%d%d", &l, &r);
			printf("%d\n", [](int l, int r)
			{
				int pp[25];
			    memset(pp, 0, sizeof pp);
				if (pos[l] == pos[r])
				{
					for (int i = l; i <= r; ++i)
					{
						int tmp = c[i];
						for (int j = 20; j >= 0; --j) if ((tmp >> j) & 1)
						{
							if (!pp[j])
							{
								pp[j] = tmp;
								break;
							}
							else
								tmp ^= pp[j];
							if (!tmp) break;
						}
					}
				}
				else
				{
					for (int i = l; i <= rpos[pos[l]]; ++i)
					{
						int tmp = c[i];
						for (int j = 20; j >= 0; --j) if ((tmp >> j) & 1)
						{
							if (!pp[j])
							{
								pp[j] = tmp;
								break;
							}
							else
								tmp ^= pp[j];
							if (!tmp) break;
						}
					}
					for (int i = lpos[pos[r]]; i <= r; ++i)
					{
						int tmp = c[i];
						for (int j = 20; j >= 0; --j) if ((tmp >> j) & 1)
						{
							if (!pp[j])
							{
								pp[j] = tmp;
								break;
							}
							else
								tmp ^= pp[j];
							if (!tmp) break;
						}
					}
					for (int i = pos[l] + 1; i < pos[r]; ++i)
					{
						for (int j = 20; j >= 0; --j) if (p[i][j])
						{
							int tmp = p[i][j];
							for (int k = 20; k >= 0; --k) if ((tmp >> k) & 1)
							{
								if (!pp[k])
								{
									pp[k] = tmp;
									break;
								}
								else
									tmp ^= pp[k];
								if (!tmp) break;
							}
						}
					}
				}
				int res = 0;
				for (int i = 20; i >= 0; --i) if ((res ^ pp[i]) > res)
					res ^= pp[i];
				//cout << res << endl;
				return res;
			}(l, r));
		}
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define N 500010
#define S 300
int n, q, c[N];
int p[S][25], pos[N], lpos[S], rpos[S];

int main()
{
	while (scanf("%d", &n) != EOF)
	{
		for (int i = 1; i <= n; ++i) pos[i] = (i - 1) / S + 1;
		for (int i = 1; i <= pos[n]; ++i)
		{
			rpos[pos[i]] = i;
			if (i == 1 || pos[i] != pos[i - 1])
				lpos[pos[i]] = i;

		}
		for (int i = 1; i <= n; ++i) scanf("%d", c + i);
		[]()
		{
			memset(p, 0, sizeof p);
			for (int i = 1; i <= pos[n]; ++i)
			{
				for (int j = lpos[i]; j <= rpos[i]; ++j)
				{
					int tmp = c[j];
					for (int k = 20; k >= 0; --k) if ((tmp >> k) & 1)
					{
						if (!p[k])
						{
							p[i][k] = tmp;
							break;
						}
						else
							tmp ^= p[i][k];
						if (!tmp) break;
					}
				}
			}
		}();
		scanf("%d", &q);
		for (int qq = 1, l, r; qq <= q; ++qq)
		{
			scanf("%d%d", &l, &r);
			printf("%d\n", [](int l, int r)
			{
				int pp[25];
			    memset(pp, 0, sizeof pp);
				if (pos[l] == pos[r])
				{
					for (int i = l; i <= r; ++i)
					{
						int tmp = c[i];
						for (int j = 20; j >= 0; --j) if ((tmp >> j) & 1)
						{
							if (!pp[j])
							{
								pp[j] = tmp;
								break;
							}
							else
								tmp ^= pp[j];
							if (!tmp) break;
						}
					}
				}
				else
				{
					for (int i = l; i <= rpos[pos[l]]; ++i)
					{
						int tmp = c[i];
						for (int j = 20; j >= 0; --j) if ((tmp >> j) & 1)
						{
							if (!pp[j])
							{
								pp[j] = tmp;
								break;
							}
							else
								tmp ^= pp[j];
							if (!tmp) break;
						}
					}
					for (int i = lpos[pos[r]]; i <= r; ++i)
					{
						int tmp = c[i];
						for (int j = 20; j >= 0; --j) if ((tmp >> j) & 1)
						{
							if (!pp[j])
							{
								pp[j] = tmp;
								break;
							}
							else
								tmp ^= pp[j];
							if (!tmp) break;
						}
					}
					for (int i = pos[l] + 1; i < pos[r]; ++i)
					{
						for (int j = 20; j >= 0; --j) if (p[i][j])
						{
							int tmp = p[i][j];
							for (int k = 20; k >= 0; --k) if ((tmp >> k) & 1)
							{
								if (!pp[k])
								{
									pp[k] = tmp;
									break;
								}
								else
									tmp ^= pp[k];
								if (!tmp) break;
							}
						}
					}
				}
				int res = 0;
				for (int i = 20; i >= 0; --i) if ((res ^ pp[i]) > res)
					res ^= pp[i];
				//cout << res << endl;
				return res;
			}(l, r));
		}
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define N 500010
#define S 2000
int n, q, c[N];
int p[S][25], pos[N], lpos[S], rpos[S];

int main()
{
	while (scanf("%d", &n) != EOF)
	{
		for (int i = 1; i <= n; ++i) pos[i] = (i - 1) / S + 1;
		for (int i = 1; i <= pos[n]; ++i)
		{
			rpos[pos[i]] = i;
			if (i == 1 || pos[i] != pos[i - 1])
				lpos[pos[i]] = i;

		}
		for (int i = 1; i <= n; ++i) scanf("%d", c + i);
		[]()
		{
			memset(p, 0, sizeof p);
			for (int i = 1; i <= pos[n]; ++i)
			{
				for (int j = lpos[i]; j <= rpos[i]; ++j)
				{
					int tmp = c[j];
					for (int k = 20; k >= 0; --k) if ((tmp >> k) & 1)
					{
						if (!p[k])
						{
							p[i][k] = tmp;
							break;
						}
						else
							tmp ^= p[i][k];
						if (!tmp) break;
					}
				}
			}
		}();
		scanf("%d", &q);
		for (int qq = 1, l, r; qq <= q; ++qq)
		{
			scanf("%d%d", &l, &r);
			printf("%d\n", [](int l, int r)
			{
				int pp[25];
			    memset(pp, 0, sizeof pp);
				if (pos[l] == pos[r])
				{
					for (int i = l; i <= r; ++i)
					{
						int tmp = c[i];
						for (int j = 20; j >= 0; --j) if ((tmp >> j) & 1)
						{
							if (!pp[j])
							{
								pp[j] = tmp;
								break;
							}
							else
								tmp ^= pp[j];
							if (!tmp) break;
						}
					}
				}
				else
				{
					for (int i = l; i <= rpos[pos[l]]; ++i)
					{
						int tmp = c[i];
						for (int j = 20; j >= 0; --j) if ((tmp >> j) & 1)
						{
							if (!pp[j])
							{
								pp[j] = tmp;
								break;
							}
							else
								tmp ^= pp[j];
							if (!tmp) break;
						}
					}
					for (int i = lpos[pos[r]]; i <= r; ++i)
					{
						int tmp = c[i];
						for (int j = 20; j >= 0; --j) if ((tmp >> j) & 1)
						{
							if (!pp[j])
							{
								pp[j] = tmp;
								break;
							}
							else
								tmp ^= pp[j];
							if (!tmp) break;
						}
					}
					for (int i = pos[l] + 1; i < pos[r]; ++i)
					{
						for (int j = 20; j >= 0; --j) if (p[i][j])
						{
							int tmp = p[i][j];
							for (int k = 20; k >= 0; --k) if ((tmp >> k) & 1)
							{
								if (!pp[k])
								{
									pp[k] = tmp;
									break;
								}
								else
									tmp ^= pp[k];
								if (!tmp) break;
							}
						}
					}
				}
				int res = 0;
				for (int i = 20; i >= 0; --i) if ((res ^ pp[i]) > res)
					res ^= pp[i];
				//cout << res << endl;
				return res;
			}(l, r));
		}
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define N 500010
int n, q, c[N];
int pos[N][25], p[N][25];

void work(int x)
{
	for (int i = 0; i <= 20; ++i)
	{
		pos[x][i] = pos[x - 1][i];
		p[x][i] = p[x - 1][i];
	}
	int tmp = c[x];
	int id = x;
	for (int i = 20; i >= 0; --i)
	{
		if ((tmp >> i) & 1)
		{
			if (!p[x][i])
			{
				p[x][i] = tmp;
				pos[x][i] = id;
				return;
			}
			if (pos[x][i] < id)
			{
				swap(tmp, p[x][i]);
				swap(id, pos[x][i]);
			}
			tmp ^= p[x][i];
		}
	}
}

int ask(int l, int r)
{
	int res = 0;
	for (int i = 20; i >= 0; --i) if (pos[r][i] >= l && (res ^ p[r][i]) > res)
		res ^= p[r][i];
	return res;
}

int main()
{
	while (scanf("%d", &n) != EOF)
	{
		for (int i = 1; i <= n; ++i) scanf("%d", c + i);
		for (int i = 0; i < 25; ++i) pos[0][i] = p[0][i] = 0;
		for (int i = 1; i <= n; ++i) work(i);
		scanf("%d", &q);
		for (int qq = 1, l, r; qq <= q; ++qq)
		{
			scanf("%d%d", &l, &r);
			printf("%d\n", ask(l, r));
		}

	}
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = 5e5+5;
int r[maxn][22];//表示令i为右端的区间且线性基第i位有值的最大l断点在哪
int p[maxn][22];//当前线性基的值
int n,a[maxn],m;
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++)scanf("%d",&a[i]);
	for(int i=1;i<=n;i++){
		int id=i,x=a[i];
		for(int j=20;j>=0;j--)r[i][j]=r[i-1][j],p[i][j]=p[i-1][j];
		for(int j=20;j>=0;j--){
			if((x>>j)&1){
				if(!p[i][j]){
					p[i][j]=x;
					r[i][j]=id;
					break;
				}
				swap(x,p[i][j]);
				swap(id,r[i][j]);
				x^=p[i][j];
			}
		}
	}
	scanf("%d",&m);
	while(m--){
		int L,R;scanf("%d%d",&L,&R);
		int ans=0;
		for(int i=20;i>=0;i--){
			if(r[R][i]>=L&&(ans^p[R][i])>ans){
				ans ^=p[R][i];
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = 5e5+5;
int r[maxn][22];//表示令i为右端的区间且线性基第i位有值的最大l断点在哪
int p[maxn][22];//当前线性基的值
int n,a[maxn],m;
int main() {
	scanf("%d",&n);
	for(int i=1; i<=n; i++)scanf("%d",&a[i]);
	for(int i=1; i<=n; i++) {
		int id=i,x=a[i];
		for(int j=20; j>=0; j--)r[i][j]=r[i-1][j],p[i][j]=p[i-1][j];
		for(int j=20; j>=0; j--) {
			if((x>>j)&1) {
				if(!p[i][j]) {
					p[i][j]=x;
					r[i][j]=id;
					break;
				}
				if(r[i][j]<id) {
					swap(x,p[i][j]);
					swap(id,r[i][j]);
					x^=p[i][j];
				}
			}
		}
	}
	scanf("%d",&m);
	while(m--) {
		int L,R;
		scanf("%d%d",&L,&R);
		int ans=0;
		for(int i=20; i>=0; i--) {
			if(r[R][i]>=L) {
				ans =max(ans,ans^p[R][i]);
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = 5e5+5;
int r[maxn][22];//表示令i为右端的区间且线性基第i位有值的最大l断点在哪
int p[maxn][22];//当前线性基的值
int n,a[maxn],m;
int main() {
	scanf("%d",&n);
	for(int i=1; i<=n; i++)scanf("%d",&a[i]);
	for(int i=1; i<=n; i++) {
		int id=i,x=a[i];
		for(int j=20; j>=0; j--)r[i][j]=r[i-1][j],p[i][j]=p[i-1][j];
		for(int j=20; j>=0; j--) {
			if((x>>j)&1) {
				if(!p[i][j]) {
					p[i][j]=x;
					r[i][j]=id;
					break;
				}
				if(r[i][j]<id) {//按位置的优先级排序
					swap(x,p[i][j]);
					swap(id,r[i][j]);
				}
				x^=p[i][j];
			}
		}
	}
	scanf("%d",&m);
	while(m--) {
		int L,R;
		scanf("%d%d",&L,&R);
		int ans=0;
		for(int i=20; i>=0; i--) {
			if(r[R][i]>=L) {
				ans =max(ans,ans^p[R][i]);
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <chrono>
#include <random>

std::mt19937 rng((int) std::chrono::steady_clock::now().time_since_epoch().count());

const int me = 20;

struct Gauss {
	int table[me];

	Gauss() {
		for(int i = 0; i < me; i++) {
			table[i] = 0;
		}
	}

	int size() {
		int ans = 0;
		for(int i = 0; i < me; i++) {
			if(table[i]) ans++;
		}
		return ans;
	}

	bool can(int x) {
		for(int i = me-1; i >= 0; i--) {
			x = std::min(x, x ^ table[i]);
		}
		return x == 0;
	}

	void add(int x) {
		for(int i = me-1; i >= 0 && x; i--) {
			if(table[i] == 0) {
				table[i] = x;
				x = 0;
			} else {
				x = std::min(x, x ^ table[i]);
			}
		}
	}

	int best() {
		int x = 0;
		for(int i = me-1; i >= 0; i--) {
			x = std::max(x, x ^ table[i]);
		}
		return x;
	}
};

const int ms = 1001000;

int l[ms], r[ms];
int a[ms];
Gauss ans[ms];
Gauss temp[ms];

void solve(std::vector<int> &qries, int lp, int rp) {
	if(qries.empty()) return;
	std::vector<int> left, right;
	if(rp - lp <= 20) {
		for(auto q : qries) {
			for(int i = l[q]; i < r[q]; i++) {
				ans[q].add(a[i]);
			}
		}
		return;
	}
	int mid = (lp + rp) / 2;
	// solving for suffix
	Gauss wtf;
	for(int i = mid-1; i >= lp; i--) {
		wtf.add(a[i]);
		temp[i] = wtf;
	}
	// solving for prefix
	wtf = Gauss();
	for(int i = mid; i < rp; i++) {
		wtf.add(a[i]);
		temp[i] = wtf;
	}
	for(auto q : qries) {
		if(r[q] <= mid) {
			left.push_back(q);
		} else if(l[q] >= mid) {
			right.push_back(q);
		} else {
			for(int i = 0; i < me; i++) {
				ans[q].add(temp[l[q]].table[i]);
				ans[q].add(temp[r[q] - 1].table[i]);
			}
		}
	}
	qries.clear();
	solve(left, lp, mid);
	solve(right, mid, rp);
}

int main() {
	std::ios_base::sync_with_stdio(false); std::cin.tie(NULL);
	int n;
	std::cin >> n;
	for(int i = 0; i < n; i++) {
		std::cin >> a[i];
	}
	int q;
	std::cin >> q;
	std::vector<int> qr;
	for(int i = 0; i < q; i++) {
		std::cin >> l[i] >> r[i];
		l[i]--;
		qr.push_back(i);
	}
	solve(qr, 0, n);
	for(int i = 0; i < q; i++) {
		std::cout << ans[i].best() << '\n';
	}
}


#include <iostream>
#include <vector>
#include <chrono>
#include <random>

std::mt19937 rng((int) std::chrono::steady_clock::now().time_since_epoch().count());

const int me = 20;

struct Gauss {
	int table[me];

	Gauss() {
		for(int i = 0; i < me; i++) {
			table[i] = 0;
		}
	}

	int size() {
		int ans = 0;
		for(int i = 0; i < me; i++) {
			if(table[i]) ans++;
		}
		return ans;
	}

	bool can(int x) {
		for(int i = me-1; i >= 0; i--) {
			x = std::min(x, x ^ table[i]);
		}
		return x == 0;
	}

	void add(int x) {
		for(int i = me-1; i >= 0 && x; i--) {
			if(table[i] == 0) {
				table[i] = x;
				x = 0;
			} else {
				x = std::min(x, x ^ table[i]);
			}
		}
	}

	int best() {
		int x = 0;
		for(int i = me-1; i >= 0; i--) {
			x = std::max(x, x ^ table[i]);
		}
		return x;
	}
};

struct Merger {
	Gauss operator() (Gauss a, Gauss b) {
		for(int i = 0; i < me; i++) {
			a.add(b.table[i]);
		}
		return a;
	}
};

template <class T, class Merger>
class SparseTable {
public:
	void init(std::vector<T> a) {
		int e = 0;
		int n = a.size();
		while((1 << e) / 2 < a.size()) {
			e++;
		}
		table.resize(e, std::vector<T>(n));
		get.assign(n + 1, -1);
		for(int i = 0; i < n; i++) {
			table[0][i] = a[i];
			get[i+1] = get[(i+1)/2] + 1;
		}
		for(int i = 0; i + 1 < e; i++) {
			for(int j = 0; j + (1 << i) < n; j++) {
				table[i+1][j] = merge(table[i][j], table[i][j + (1 << i)]);
			}
		}
	}

	T qry(int l, int r) {
		int e = get[r - l];
		return merge(table[e][l], table[e][r - (1 << e)]);
	}
private:
	std::vector<std::vector<T>> table;
	std::vector<int> get;
	Merger merge;
};

int main() {
	std::ios_base::sync_with_stdio(false); std::cin.tie(NULL);
	int n;
	std::cin >> n;
	std::vector<Gauss> a(n);
	for(int i = 0; i < n; i++) {
		int x;
		std::cin >> x;
		a[i].add(x);
	}
	SparseTable<Gauss, Merger> table;
	table.init(a);
	int q;
	std::cin >> q;
	while(q--) {
		int l, r;
		std::cin >> l >> r;
		std::cout << table.qry(l - 1, r).best() << '\n';
	}
}


#include <iostream>
#include <vector>
#include <chrono>
#include <random>

std::mt19937 rng((int) std::chrono::steady_clock::now().time_since_epoch().count());

const int me = 20;

struct Gauss {
	int table[me];

	Gauss() {
		for(int i = 0; i < me; i++) {
			table[i] = 0;
		}
	}

	int size() {
		int ans = 0;
		for(int i = 0; i < me; i++) {
			if(table[i]) ans++;
		}
		return ans;
	}

	bool can(int x) {
		for(int i = me-1; i >= 0; i--) {
			x = std::min(x, x ^ table[i]);
		}
		return x == 0;
	}

	void add(int x) {
		for(int i = me-1; i >= 0 && x; i--) {
			if(table[i] == 0) {
				table[i] = x;
				x = 0;
			} else {
				x = std::min(x, x ^ table[i]);
			}
		}
	}

	int best() {
		int x = 0;
		for(int i = me-1; i >= 0; i--) {
			x = std::max(x, x ^ table[i]);
		}
		return x;
	}
};

Gauss tree[1001000];

int main() {
	std::ios_base::sync_with_stdio(false); std::cin.tie(NULL);
	int n;
	std::cin >> n;
	for(int i = 0; i < n; i++) {
		int x;
		std::cin >> x;
		tree[i + n].add(x);
	}
	for(int i = n-1; i > 0; i--) {
		tree[i] = tree[i+i];
		for(int j = 0; j < me; j++) {
			tree[i].add(tree[i+i+1].table[j]);
		}
	}
	int q;
	std::cin >> q;
	while(q--) {
		int l, r;
		std::cin >> l >> r;
		l--;
		Gauss cur;
		for(l += n, r += n; l < r; l /= 2, r /= 2) {
			if(l & 1) {
				for(int i = 0; i < me; i++) {
					cur.add(tree[l].table[i]);
				}
				l++;
			}
			if(r & 1) {
				r--;
				for(int i = 0; i < me; i++) {
					cur.add(tree[r].table[i]);
				}
			}
		}
		std::cout << cur.best() << '\n';
	}
}


#include <bits/stdc++.h>

using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  int costs[n + 1];
  for (int i = 1; i <= n; i++) {
    cin >> costs[i];
  }
  int q;
  cin >> q;
  vector<int> ends[n + 1];
  int lefts[q];
  vector<int> ans(q);
  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    ends[r].push_back(i);
    lefts[i] = l;
  }

  vector<int> last_for_bit(30);
  vector<int> position(30);
  for (int r = 1; r <= n; r++) {
    int x = costs[r];
    int p = r;
    for (int b = 20; b >= 0; b--) {
      if ((1 << b) & x) {
        if (!last_for_bit[b]) {
          last_for_bit[b] = x;
          position[b] = p;
          break;
        }
        if (position[b] < p) {
          swap(last_for_bit[b], x);
          swap(position[b], p);
        }
        x ^= last_for_bit[b];
      }
    }

    for (int i : ends[r]) {
      for (int b = 20; b >= 0; b--) {
        if (lefts[i] <= position[b]) {
          ans[i] = max(ans[i], ans[i] ^ last_for_bit[b]);
        }
      }
    }
  }

  cout.tie(nullptr);
  for (int i = 0; i < q; i++) {
    cout << ans[i] << endl;
  }
}


#include <bits/stdc++.h>

using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  int costs[n + 1];
  for (int i = 1; i <= n; i++) {
    cin >> costs[i];
  }
  int q;
  cin >> q;
  vector<int> ends[n + 1];
  int lefts[q];
  vector<int> ans(q);
  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    ends[r].push_back(i);
    lefts[i] = l;
  }

  vector<int> last_for_bit(30);
  vector<int> position(30);
  for (int r = 1; r <= n; r++) {
    int x = costs[r];
    int p = r;
    for (int b = 20; b >= 0; b--) {
      if ((1 << b) & x) {
        if (!last_for_bit[b]) {
          last_for_bit[b] = x;
          position[b] = p;
          break;
        }
        if (position[b] < p) {
          swap(last_for_bit[b], x);
          swap(position[b], p);
        }
        x ^= last_for_bit[b];
      }
    }

    for (int i : ends[r]) {
      for (int b = 20; b >= 0; b--) {
        if (lefts[i] <= position[b]) {
          ans[i] = max(ans[i], ans[i] ^ last_for_bit[b]);
        }
      }
    }
  }

  cout.tie(nullptr);
  string s;
  for (int i = 0; i < q; i++) {
    s += to_string(ans[i]);
    s += '\n';
  }
  cout << s;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  int costs[n + 1];
  for (int i = 1; i <= n; i++) {
    cin >> costs[i];
  }
  int q;
  cin >> q;
  vector<int> ends[n + 1];
  int lefts[q];
  vector<int> ans(q);
  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    ends[r].push_back(i);
    lefts[i] = l;
  }

  vector<int> last_for_bit(30);
  vector<int> position(30);
  for (int r = 1; r <= n; r++) {
    int x = costs[r];
    int p = r;
    for (int b = 20; b >= 0; b--) {
      if ((1 << b) & x) {
        if (!last_for_bit[b]) {
          last_for_bit[b] = x;
          position[b] = p;
          break;
        }
        if (position[b] < p) {
          swap(last_for_bit[b], x);
          swap(position[b], p);
        }
        x ^= last_for_bit[b];
      }
    }

    for (int i : ends[r]) {
      for (int b = 20; b >= 0; b--) {
        if (lefts[i] <= position[b]) {
          ans[i] = max(ans[i], ans[i] ^ last_for_bit[b]);
        }
      }
    }
  }

  for (int i = 0; i < q; i++) {
    cout << ans[i] << endl;
  }
}


#include <bits/stdc++.h>

using namespace std;

int main() {
  int n;
  cin >> n;
  int costs[n + 1];
  for (int i = 1; i <= n; i++) {
    cin >> costs[i];
  }
  int q;
  cin >> q;
  vector<int> ends[n + 1];
  int lefts[q];
  vector<int> ans(q);
  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    ends[r].push_back(i);
    lefts[i] = l;
  }

  vector<int> last_for_bit(30);
  vector<int> position(30);
  for (int r = 1; r <= n; r++) {
    int x = costs[r];
    int p = r;
    for (int b = 20; b >= 0; b--) {
      if ((1 << b) & x) {
        if (!last_for_bit[b]) {
          last_for_bit[b] = x;
          position[b] = p;
          break;
        }
        if (position[b] < p) {
          swap(last_for_bit[b], x);
          swap(position[b], p);
        }
        x ^= last_for_bit[b];
      }
    }

    for (int i : ends[r]) {
      for (int b = 20; b >= 0; b--) {
        if (lefts[i] <= position[b]) {
          ans[i] = max(ans[i], ans[i] ^ last_for_bit[b]);
        }
      }
    }
  }

  for (int i = 0; i < q; i++) {
    cout << ans[i] << endl;
  }
}


#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 1 + 2e5;
const int LogN = 20;
vector <int> e[N];
int t[LogN], v;

void add(vector <int> &v, int x) {
    for (int i = LogN - 1; i >= 0; --i)
    if ((x & t[i]) > 0) {
        if (v[i] == 0)
            v[i] = x;
        x ^= v[i];
    }
}

vector <int> merg(vector <int> a, vector <int> b) {
    for (int i = 0; i < b.size(); ++i)
    if (b[i] > 0)
        add(a, b[i]);
    return a;
}

void als(int i, int j) {
    i = i + v - 2;
    add(e[i], j);
    while (i > 0) {
        i = (i - 1) / 2;
        add(e[i], j);
    }
}

vector <int> ct(int v, int vl, int vr, int l, int r) {
    if (l <= vl && vr <= r)
        return e[v];
    else if (l >= (vl + vr) / 2)
            return ct(2*v + 2, (vl + vr) / 2, vr, l, r);
        else if (r <= (vl + vr) / 2)
                return ct(2*v + 1, vl, (vl + vr) / 2, l, r);
            else
                return merg(ct(2*v + 1, vl, (vl + vr) / 2, l, r), ct(2*v + 2, (vl + vr) / 2, vr, l, r));
}

int main() {
    int i, j, n, k, l, r;
    //freopen("input.txt", "r", stdin);
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    cin >> n;
    v = 1;
    while (v < n) v *= 2;
    for (i = 0; i < 2 *v; ++i)
        e[i].resize(LogN);
    t[0] = 1;
    for (i = 1; i < LogN; ++i)
        t[i] = t[i - 1] * 2;
    for (i = 1; i <= n; ++i) {
        cin >> j;
        als(i, j);
    }
    cin >> k;
    for (i = 0; i < k; ++i) {
            if (i > 250000)
                exit(1);
        cin >> l >> r;
        vector <int> vc = ct(0, 1, v + 1, l, r + 1);
        int x = 0;
        for (j = LogN - 1; j >= 0; --j)
        if ((x & t[j]) == 0)
            x ^= vc[j];
        cout << x << "\n";
    }
}


#include <bits/stdc++.h>
#pragma GCC optimize 03
using namespace std;
const int N = 1e6 + 1 + 2e5;
const int LogN = 20;
vector <int> e[N];
int t[LogN], v;

void add(vector <int> &v, int x) {
    for (int i = LogN - 1; i >= 0; --i)
    if ((x & t[i]) > 0) {
        if (v[i] == 0)
            v[i] = x;
        x ^= v[i];
        if (x == 0)
            return;
    }
}

vector <int> merg(vector <int> a, vector <int> b) {
    for (int i = 0; i < b.size(); ++i)
    if (b[i] > 0) {
        for (int j = i; j >= 0; --j)
        if ((b[i] & t[j]) > 0) {
            if (a[j] == 0) {
                a[j] = b[i];
                break;
            }
            b[i] ^= a[j];
        }
    }
    return a;
}

void als(int i, int j) {
    i = i + v - 2;
    add(e[i], j);
    while (i > 0) {
        i = (i - 1) / 2;
        add(e[i], j);
    }
}

vector <int> ct(int v, int vl, int vr, int l, int r) {
    if (l <= vl && vr <= r)
        return e[v];
    else if (l >= (vl + vr) / 2)
            return ct(2*v + 2, (vl + vr) / 2, vr, l, r);
        else if (r <= (vl + vr) / 2)
                return ct(2*v + 1, vl, (vl + vr) / 2, l, r);
            else
                return merg(ct(2*v + 1, vl, (vl + vr) / 2, l, r), ct(2*v + 2, (vl + vr) / 2, vr, l, r));
}

int main() {
    int i, j, n, k, l, r;
    //freopen("input.txt", "r", stdin);
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    cin >> n;
    v = 1;
    while (v < n) v *= 2;
    for (i = 0; i < 2 *v; ++i)
        e[i].resize(LogN);
    t[0] = 1;
    for (i = 1; i < LogN; ++i)
        t[i] = t[i - 1] * 2;
    for (i = 1; i <= n; ++i) {
        cin >> j;
        als(i, j);
    }
    cin >> k;
    for (i = 0; i < k; ++i) {
        cin >> l >> r;
        vector <int> vc = ct(0, 1, v + 1, l, r + 1);
        int x = 0;
        for (j = LogN - 1; j >= 0; --j)
        if ((x & t[j]) == 0)
            x ^= vc[j];
        cout << x << "\n";
    }
}


#include <bits/stdc++.h>
#define fi first
#define se second
using namespace std;
const int N = 1e6 + 2;
const int LogN = 20;
vector <int> e[N];
int t[LogN], v, a[N], ans[N];

void add(vector <int> &v, int x) {
    for (int i = LogN - 1; i >= 0; --i)
    if ((x & t[i]) > 0) {
        if (v[i] == 0)
            v[i] = x;
        x ^= v[i];
    }
}

vector <int> merg(vector <int> a, vector <int> b) {
    for (int i = 0; i < b.size(); ++i)
    if (b[i] > 0)
        add(a, b[i]);
    return a;
}

int find_mx(vector <int> v) {
    int x = 0;
    for (int i = LogN - 1; i >= 0; --i)
    if ((x & t[i]) == 0)
        x ^= v[i];
    return x;
}

int cnt = 0;
void do_w(int vl, int vr, vector <pair<pair<int, int>, int> > q) {
    ++cnt;
    int i, j;
    if (vr - vl == 1) {
        for (int i = 0; i < q.size(); ++i)
            ans[q[i].se] = a[vl];
        return;
    }
    vector <pair<pair<int, int>, int> > v1, v2, v3;
    int mid = (vl + vr) / 2;
    for (i = 0; i < q.size(); ++i)
    if (q[i].fi.se < mid)
        v1.push_back(q[i]);
    else if (q[i].fi.fi >= mid)
            v2.push_back(q[i]);
        else
            v3.push_back(q[i]);
    if (v1.size() > 0)
        do_w(vl, mid, v1);
    if (v2.size() > 0)
        do_w(mid, vr, v2);
    for (i = 0; i < LogN; ++i)
        e[mid][i] = 0;
    for (i = mid - 1; i >= vl; --i) {
        e[i] = e[i + 1];
        add(e[i], a[i]);
    }
    add(e[mid], a[mid]);
    for (i = mid + 1; i < vr; ++i) {
        e[i] = e[i - 1];
        add(e[i], a[i]);
    }
    for (i = 0; i < v3.size(); ++i) {
        ans[v3[i].se] = find_mx(merg(e[v3[i].fi.fi], e[v3[i].fi.se]));
    }
}
int main() {
    int i, j, n, k, l, r;
    //freopen("input.txt", "r", stdin);
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    cin >> n;
    v = 1;
    while (v < n) v *= 2;
    t[0] = 1;
    for (i = 1; i < LogN; ++i)
        t[i] = t[i - 1] * 2;
    for (i = 1; i <= n; ++i)
        cin >> a[i];
    for (i = 0; i <= v + 1; ++i)
        e[i].resize(LogN);
    cin >> k;
    vector <pair<pair<int, int>, int> > que;
    for (i = 0; i < k; ++i) {
        cin >> l >> r;
        que.push_back({{l, r}, i});
    }
    do_w(1, v + 1, que);
    for (i = 0; i < k; ++i)
        cout << ans[i] << "\n";
}


#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 1 + 2e5;
const int LogN = 20;
vector <int> e[N];
int t[LogN], v;

void add(vector <int> &v, int x) {
    for (int i = LogN - 1; i >= 0; --i)
    if ((x & t[i]) > 0) {
        if (v[i] == 0)
            v[i] = x;
        x ^= v[i];
    }
}

vector <int> merg(vector <int> a, vector <int> b) {
    for (int i = 0; i < b.size(); ++i)
    if (b[i] > 0)
        add(a, b[i]);
    return a;
}

void als(int i, int j) {
    i = i + v - 2;
    add(e[i], j);
    while (i > 0) {
        i = (i - 1) / 2;
        add(e[i], j);
    }
}

vector <int> ct(int v, int vl, int vr, int l, int r) {
    if (l <= vl && vr <= r)
        return e[v];
    else if (l >= (vl + vr) / 2)
            return ct(2*v + 2, (vl + vr) / 2, vr, l, r);
        else if (r <= (vl + vr) / 2)
                return ct(2*v + 1, vl, (vl + vr) / 2, l, r);
            else
                return merg(ct(2*v + 1, vl, (vl + vr) / 2, l, r), ct(2*v + 2, (vl + vr) / 2, vr, l, r));
}

int main() {
    int i, j, n, k, l, r;
    freopen("input.txt", "r", stdin);
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    cin >> n;
    v = 1;
    while (v < n) v *= 2;
    for (i = 0; i < 2 *v; ++i)
        e[i].resize(LogN);
    t[0] = 1;
    for (i = 1; i < LogN; ++i)
        t[i] = t[i - 1] * 2;
    for (i = 1; i <= n; ++i) {
        cin >> j;
        als(i, j);
    }
    cin >> k;
    for (i = 0; i < k; ++i) {
        cin >> l >> r;
        vector <int> vc = ct(0, 1, v + 1, l, r + 1);
        int x = 0;
        for (j = LogN - 1; j >= 0; --j)
        if ((x & t[j]) == 0)
            x ^= vc[j];
        cout << x << "\n";
    }
}


#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 1 + 2e5;
const int LogN = 20;
vector <int> e[N];
int t[LogN], v;

void add(vector <int> &v, int x) {
    for (int i = LogN - 1; i >= 0; --i)
    if ((x & t[i]) > 0) {
        if (v[i] == 0)
            v[i] = x;
        x ^= v[i];
    }
}

void add1(vector <int> &v, int x, int c) {
    for (int i = c; i >= 0; --i)
    if ((x & t[i]) > 0) {
        if (v[i] == 0)
            v[i] = x;
        x ^= v[i];
    }
}

vector <int> merg(vector <int> a, vector <int> b) {
    for (int i = 0; i < b.size(); ++i)
    if (b[i] > 0)
        add1(a, b[i], i);
    return a;
}

void als(int i, int j) {
    i = i + v - 2;
    add(e[i], j);
    while (i > 0) {
        i = (i - 1) / 2;
        add(e[i], j);
    }
}

vector <int> ct(int v, int vl, int vr, int l, int r) {
    if (l <= vl && vr <= r)
        return e[v];
    else if (l >= (vl + vr) / 2)
            return ct(2*v + 2, (vl + vr) / 2, vr, l, r);
        else if (r <= (vl + vr) / 2)
                return ct(2*v + 1, vl, (vl + vr) / 2, l, r);
            else
                return merg(ct(2*v + 1, vl, (vl + vr) / 2, l, r), ct(2*v + 2, (vl + vr) / 2, vr, l, r));
}

int main() {
    int i, j, n, k, l, r;
    //freopen("input.txt", "r", stdin);
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    cin >> n;
    v = 1;
    while (v < n) v *= 2;
    for (i = 0; i < 2 *v; ++i)
        e[i].resize(LogN);
    t[0] = 1;
    for (i = 1; i < LogN; ++i)
        t[i] = t[i - 1] * 2;
    for (i = 1; i <= n; ++i) {
        cin >> j;
        als(i, j);
    }
    cin >> k;
    for (i = 0; i < k; ++i) {
            if (i > 250000)
                exit(1);
        cin >> l >> r;
        vector <int> vc = ct(0, 1, v + 1, l, r + 1);
        int x = 0;
        for (j = LogN - 1; j >= 0; --j)
        if ((x & t[j]) == 0)
            x ^= vc[j];
        cout << x << "\n";
    }
}


#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 1 + 2e5;
const int LogN = 20;
vector <int> e[N];
int t[LogN], v;

void add(vector <int> &v, int x) {
    for (int i = LogN - 1; i >= 0; --i)
    if ((x & t[i]) > 0) {
        if (v[i] == 0)
            v[i] = x;
        x ^= v[i];
    }
}

void add1(vector <int> &v, int x, int c) {
    for (int i = c; i >= 0; --i)
    if ((x & t[i]) > 0) {
        if (v[i] == 0)
            v[i] = x;
        x ^= v[i];
    }
}

vector <int> merg(vector <int> a, vector <int> b) {
    for (int i = 0; i < b.size(); ++i)
    if (b[i] > 0)
        add1(a, b[i], i);
    return a;
}

void als(int i, int j) {
    i = i + v - 2;
    add(e[i], j);
    while (i > 0) {
        i = (i - 1) / 2;
        add(e[i], j);
    }
}

vector <int> ct(int v, int vl, int vr, int l, int r) {
    if (l <= vl && vr <= r)
        return e[v];
    else if (l >= (vl + vr) / 2)
            return ct(2*v + 2, (vl + vr) / 2, vr, l, r);
        else if (r <= (vl + vr) / 2)
                return ct(2*v + 1, vl, (vl + vr) / 2, l, r);
            else
                return merg(ct(2*v + 1, vl, (vl + vr) / 2, l, r), ct(2*v + 2, (vl + vr) / 2, vr, l, r));
}

int main() {
    int i, j, n, k, l, r;
    //freopen("input.txt", "r", stdin);
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    cin >> n;
    v = 1;
    while (v < n) v *= 2;
    for (i = 0; i < 2 *v; ++i)
        e[i].resize(LogN);
    t[0] = 1;
    for (i = 1; i < LogN; ++i)
        t[i] = t[i - 1] * 2;
    for (i = 1; i <= n; ++i) {
        cin >> j;
        als(i, j);
    }
    cin >> k;
    for (i = 0; i < k; ++i) {
        cin >> l >> r;
        vector <int> vc = ct(0, 1, v + 1, l, r + 1);
        int x = 0;
        for (j = LogN - 1; j >= 0; --j)
        if ((x & t[j]) == 0)
            x ^= vc[j];
        cout << x << "\n";
    }
}


#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 1 + 2e5;
const int LogN = 20;
vector <int> e[N];
int t[LogN], v;

void add(vector <int> &v, int x) {
    for (int i = LogN - 1; i >= 0; --i)
    if ((x & t[i]) > 0) {
        if (v[i] == 0)
            v[i] = x;
        x ^= v[i];
        if (x == 0)
            return;
    }
}

void add1(vector <int> &v, int x, int c) {
    for (int i = c; i >= 0; --i)
    if ((x & t[i]) > 0) {
        if (v[i] == 0)
            v[i] = x;
        x ^= v[i];
        if (x == 0)
            return;
    }
}

vector <int> merg(vector <int> a, vector <int> b) {
    for (int i = 0; i < b.size(); ++i)
    if (b[i] > 0)
        add1(a, b[i], i);
    return a;
}

void als(int i, int j) {
    i = i + v - 2;
    add(e[i], j);
    while (i > 0) {
        i = (i - 1) / 2;
        add(e[i], j);
    }
}

vector <int> ct(int v, int vl, int vr, int l, int r) {
    if (l <= vl && vr <= r)
        return e[v];
    else if (l >= (vl + vr) / 2)
            return ct(2*v + 2, (vl + vr) / 2, vr, l, r);
        else if (r <= (vl + vr) / 2)
                return ct(2*v + 1, vl, (vl + vr) / 2, l, r);
            else
                return merg(ct(2*v + 1, vl, (vl + vr) / 2, l, r), ct(2*v + 2, (vl + vr) / 2, vr, l, r));
}

int main() {
    int i, j, n, k, l, r;
    //freopen("input.txt", "r", stdin);
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    cin >> n;
    v = 1;
    while (v < n) v *= 2;
    for (i = 0; i < 2 *v; ++i)
        e[i].resize(LogN);
    t[0] = 1;
    for (i = 1; i < LogN; ++i)
        t[i] = t[i - 1] * 2;
    for (i = 1; i <= n; ++i) {
        cin >> j;
        als(i, j);
    }
    cin >> k;
    for (i = 0; i < k; ++i) {
        cin >> l >> r;
        vector <int> vc = ct(0, 1, v + 1, l, r + 1);
        int x = 0;
        for (j = LogN - 1; j >= 0; --j)
        if ((x & t[j]) == 0)
            x ^= vc[j];
        cout << x << "\n";
    }
}


#define _CRT_SECURE_NO_WARNINGS
//#pragma comment(linker, "/STACK:100000000")
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include <cstring>
#include <set>
#include <stdio.h>
#include <vector>
#include <map>

#define ull unsigned ll
#define ll long long
#define ld long double
#define FOR(i, a, b) for(int i=(int)(a); i<(int)(b); ++i)
#define FORN(i, b) for(int i=0; i<(int) (b); i++)
#define FORZ(i, a, b) for (int i=(int)(a); i>=(int)(b); --i)
#define mp make_pair
#define fs first
#define sc second
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vii vector<int>
#define vll vector<ll>
#define pb push_back
#define ppb pop_back
#define bg begin()
#define ed end()
#define sz size()
#define rsz resize
#define all(a) (a).bg, (a).ed
#define len length()
#define ext return 0;
#define pi 3.1415
ll gcd(ll a, ll b) { if (a == 0 || b == 0) return (a + b); if (a > b) return (gcd(a%b, b)); return (gcd(a, b % a)); }
int sqr(int x) { return (x*x); }

#define Nmax 100009
#define INF 1000000007
#define y1 yy1
#define eps 1e-5
void out() { std::cout << "NO\n"; exit(0); }
using namespace std;

int n, a[Nmax + 1];
ll s = 0, ans = 0;

int main(){
	ios::sync_with_stdio(0);

	cin >> n;
	FORN(i, n) {
		cin >> a[i];
		s += a[i];
	}
	s /= n;
	FORN(i, n) {
		ans += abs(s - a[i]);
		a[i + 1] -= abs(s - a[i]);
	}
	cout << ans << endl;

	ext;
}


#define _CRT_SECURE_NO_WARNINGS
//#pragma comment(linker, "/STACK:100000000")
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include <cstring>
#include <set>
#include <stdio.h>
#include <vector>
#include <map>

#define ull unsigned ll
#define ll long long
#define ld long double
#define FOR(i, a, b) for(int i=(int)(a); i<(int)(b); ++i)
#define FORN(i, b) for(int i=0; i<(int) (b); i++)
#define FORZ(i, a, b) for (int i=(int)(a); i>=(int)(b); --i)
#define mp make_pair
#define fs first
#define sc second
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vii vector<int>
#define vll vector<ll>
#define pb push_back
#define ppb pop_back
#define bg begin()
#define ed end()
#define sz size()
#define rsz resize
#define all(a) (a).bg, (a).ed
#define len length()
#define ext return 0;
#define pi 3.1415
ll gcd(ll a, ll b) { if (a == 0 || b == 0) return (a + b); if (a > b) return (gcd(a%b, b)); return (gcd(a, b % a)); }
int sqr(int x) { return (x*x); }

#define Nmax 100009
#define INF 1000000007
#define y1 yy1
#define eps 1e-5
void out() { std::cout << "NO\n"; exit(0); }
using namespace std;

int n, a[Nmax + 1];
ll s = 0, ans = 0;

int main(){
	ios::sync_with_stdio(0);

	cin >> n;
	FORN(i, n) {
		cin >> a[i];
		s += a[i];
	}
	s /= n;
	FORN(i, n) {
		if (s - a[i] > 0) {
			ans += s - a[i];
			a[i + 1] -= (s - a[i]);
		}
 else
 {
	 ans += a[i] - s;
	 a[i + 1] += (a[i] - s);
 }
	}
	cout << ans << endl;

	ext;
}


#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define MAX(a,b) ((a) > (b) ? (a) : (b))
#define MIN(a,b) ((a) < (b) ? (a) : (b))
#define MEM(a,b) memset((a),(b),sizeof(a))
const double eps = 1e-10;
const int N = 1e5;
LL  a[N + 10];
int main()
{
    //freopen("input.txt", "r", stdin);
    int n;
    LL sum = 0;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
        sum += a[i];
    }
    LL avg = sum / n;
    int l, r;
    l = 0;
    r = 0;
    LL ans = 0;
    while (l < n && r < n)
    {
        if (a[l] >= avg)
        {
            l++;
            continue;
        }
        if (a[r] <= avg)
        {
            r++;
            continue;
        }
        LL tmp = min(a[r] - avg, avg - a[l]);
        ans += tmp*abs(l - r);
        a[r] -= tmp;
        a[l] += tmp;
    }
    cout << ans << endl;

    return 0;
}


#include <iostream>
#include <cmath>
using namespace std;

int main(){

    int n;
    cin >> n;
    int sum = 0;
    int k = 0;
    int d = 0;
    int *list = new int[n];
    for (int i = 0; i < n; i++){
        cin >> list[i];
        k += list[i];
    }
    d = k/ n;
    for (int i = 0; i < n-1; i++){
        if (list[i] > d){
           sum += list[i] - d;
           list[i+1] += list[i] - d;
        }
        else if (list[i] < d){
           sum += d - list[i];
           list[i+1] -= d - list[i];
        }
    }
    cout<<sum;
    return 0;
}


#include <iostream>
#include <cmath>
using namespace std;

int main(){

    int n;
    cin >> n;
    long long sum = 0;
    long long k = 0;
    long long d = 0;
    long long *list = new long long[n];
    for (int i = 0; i < n; i++){
        cin >> list[i];
        k += list[i];
    }
    d = k/ n;
    for (int i = 0; i < n-1; i++){
        if (list[i] > d){
           sum += list[i] - d;
           list[i+1] += list[i] - d;
        }
        else if (list[i] < d){
           sum += d - list[i];
           list[i+1] -= d - list[i];
        }
    }
    cout<<sum;
    return 0;
}


#include <stdio.h>
#include <vector>
#include <algorithm>

long long int mod(long long int i){
	if(i>0)return i;
	return -i;
}
void solve(int n){
	int i,j,b[1000],m,k,l,r;
	long long int a[100000];
	long long int sum=0,res=0;
	for(i=0;i<n;i++){
		scanf("%I64d",&a[i]);
		sum+=a[i];
	}
	sum/=n;
	for(i=0;i<n-1;i++){
		res+=mod(a[i]-sum);
		//printf("%I64d %d\n",res,i);
		a[i+1]+=a[i]-sum;
	}

	printf("%I64d\n",res);

}

int main() {
#pragma comment(linker, "/STACK:268435456")
#ifndef ONLINE_JUDGE
	freopen("input.txt", "rt", stdin);
	freopen("output.txt", "wt", stdout);
#endif
	int n;
	while(scanf("%d",&n)!=EOF){
		solve(n);
	}
	return 0;
}


#include <stdio.h>
#include <vector>
#include <algorithm>
int v[1000],p[1000];

int find(int i){
	if(p[i]==i)return i;
	return (p[i]=find(p[i]));
}

void un(int i1,int i2){
	int j1=find(i1);
	int j2=find(i2);
	if(j1!=j2){
		p[j2]=j1;
		v[j1]+=v[j2];
	}
}

int mod(int i){
	if(i>0)return i;
	return -i;
}

void solve(int n){
	int i,j,a[100000],b[1000],m,k,l,r;
	long long int sum=0,res=0;
	for(i=0;i<n;i++){
		scanf("%d",&a[i]);
		sum+=a[i];
	}
	sum/=n;
	for(i=0;i<n-1;i++){
		res+=mod(a[i]-sum);
		//printf("%I64d %d\n",res,i);
		a[i+1]+=a[i]-sum;
	}

	printf("%I64d\n",res);

}

int main() {
#pragma comment(linker, "/STACK:268435456")
#ifndef ONLINE_JUDGE
	freopen("input.txt", "rt", stdin);
	freopen("output.txt", "wt", stdout);
#endif
	int n;
	while(scanf("%d",&n)!=EOF){
		solve(n);
	}
	return 0;
}


# include <iostream>
# include <cmath>
# include <vector>
# include <string>
using namespace std;

long long  arr[50001];

int main()
{
	//freopen("m.in","r",stdin);

	long long  n;
	cin>>n;

	long long sum = 0;
	for (int i=0;i<n;i++)
	{
		cin>>arr[i];
		sum += arr[i];
	}

	long long res = 0;

	long long one = sum / n;

	for (int i=0;i<n;i++)
	{
		if (arr[i] == one)	continue;

		if (arr[i] < one)
		{
			res += (one - arr[i]);
			arr[i+1] -= (one - arr[i]);
			arr[i] = one;
		}
		else
		{
			res += (arr[i] - one);
			arr[i+1] += (arr[i] - one);
			arr[i] = one;
		}
	}

	cout<<res<<endl;

return 0;
}


#include <cstdio>
#include <iostream>
#include <string>
#include <cmath>
#include <vector>
#include <map>
#include <set>
#include <cstring>
#include <queue>

 using namespace std;

 const int N = 50001;

 int n;
 long long a[N];

 void NextElement(int& j)
 {
     while (j < n && a[j] == 0)
         j++;
 }

int main()
{
#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    scanf("%d", &n);
    long long sum = 0;
    for (int i = 0; i < n; ++i)
    {
        scanf("%I64d", &a[i]);
        sum += a[i];
    }
    long long value = sum / n;
    int j = 0;
    long long ans = 0;
    for (int i = 0; i < n - 1; ++i)
    {
        if (i == j)
            j++;
        NextElement(j);
        if (a[i] >= value)
        {
            a[i + 1] += a[i] - value;
            ans += a[i] - value;
        }
        else
        {
            long long s = value - a[i];
            while (j < n && s > 0)
            {
                int t = min(s, a[j]);
                s -= t;
                ans += 1ll * (j - i) * t;
                a[j] -= t;
                if (a[j] == 0)
                    j++;
            }
        }
    }
    printf("%I64d\n", ans);

    return 0;
}


#include <cstdio>
#include <iostream>
#include <string>
#include <cmath>
#include <vector>
#include <map>
#include <set>
#include <cstring>
#include <queue>

 using namespace std;

 const int N = 50001;

 int n;
 int a[N];

 void NextElement(int& j)
 {
     while (j < n && a[j] == 0)
         j++;
 }

int main()
{
#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    scanf("%d", &n);
    long long sum = 0;
    for (int i = 0; i < n; ++i)
    {
        scanf("%d", &a[i]);
        sum += a[i];
    }
    int value = sum / n;
    int j = 1;
    NextElement(j);
    long long ans = 0;
    for (int i = 0; i < n - 1; ++i)
    {
        if (i == j)
            j++;
        NextElement(j);
        if (a[i] >= value)
        {
            a[i + 1] += a[i] - value;
            ans += a[i] - value;
        }
        else
        {
            int s = value - a[i];
            while (j < n && s > 0)
            {
                int t = min(s, a[j]);
                s -= t;
                ans += 1ll * (j - i) * t;
                a[j] -= t;
                if (a[j] == 0)
                    j++;
            }
        }
    }
    printf("%I64d\n", ans);

    return 0;
}


#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
using namespace std;

int main()
{
    long long a [ 50005 ];
    long long i , j , k , ans =0, sum, n;
    cin >> n;
    for ( i = 0; i < n; i++ )
    {
        cin >> a [ i ];
        ans += a [ i ];
    }
    ans = ans / n;
    sum = 0;
    for ( i = 0; i < n; i++ )
    {
        sum += abs(ans - a [ i ]);
    }
    cout << sum << endl;

}


#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
using namespace std;

int main()
{
    long long a [ 50005 ];
    long long i , j , k , ans =0, sum, n,l=0;
    cin >> n;
    for ( i = 0; i < n; i++ )
    {
        cin >> a [ i ];
        ans += a [ i ];
    }
    ans = ans / n;
    sum = 0;
    for ( i = 0; i < n; i++ )
    {
        l+=a[i]-ans;
        sum += abs(l);
    }
    cout << sum << endl;

}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <set>

#pragma warning(disable : 4996)

#define put push_back

using namespace std;

long long n, sum = 0, a[50100], res = 0;

int main(){
 //   freopen("input.txt", "r", stdin);
 //   freopen("output.txt", "w", stdout);
    cin >> n;
    for (int i = 0; i < n; i++){
        cin >> a[i];
        sum += a[i];
    }
    sum /= n;
    for (int i = 0; i < n; i++){
        if (a[i] < sum){
            a[i + 1] -= sum - a[i];
            res += sum - a[i];
            a[i] = sum;
        }else{
            a[i + 1] += a[i] - sum;
            res += a[i] - sum;
            a[i] = sum;
        }
    }
    cout << res;
    return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <set>

#pragma warning(disable : 4996)

#define put push_back

using namespace std;

int n, sum = 0, a[50100], res = 0;

int main(){
  //  freopen("input.txt", "r", stdin);
  //  freopen("output.txt", "w", stdout);
    cin >> n;
    for (int i = 0; i < n; i++){
        cin >> a[i];
        sum += a[i];
    }
    sum /= n;
    for (int i = 0; i < n; i++){
        if (a[i] < sum){
            a[i + 1] -= sum - a[i];
            res += sum - a[i];
            a[i] = sum;
        }else{
            a[i + 1] += a[i] - sum;
            res += a[i] - sum;
            a[i] = sum;
        }
    }
    cout << res;
    return 0;
}


#include <iostream>

using namespace std;

int main(){
	long long a;
	long long n;
	long long sum=0;
	cin>>n;
	long long *mas = new long long[n];
	for(long long i=0;i<=n-1;i++){
		cin >> mas[i];
		sum+=mas[i];
	}
	long long ave=(sum/n);
	sum=0;
	for(long long i=0;i<=n-1;i++){
		a=mas[i]-ave;
		mas[i+1]+=a;
		sum+=abs(a);
	}
	cout << sum <<endl;
	return 0;
}


#include <iostream>

using namespace std;

int main(){
	int a;
	int n;
	int sum=0;
	cin>>n;
	int *mas = new int[n];
	for(int i=0;i<=n-1;i++){
		cin >> mas[i];
		sum+=mas[i];
	}
	int ave=(sum/n);
	sum=0;
	for(int i=0;i<=n-1;i++){
		a=mas[i]-ave;
		mas[i+1]+=a;
		sum+=abs(a);
	}
	cout << sum <<endl;
	return 0;
}


#include<iostream>
#include<algorithm>
#include<deque>
using namespace std;

deque<pair<int, int>> need_more;
long long a[100000]; int n;

int main()
{
    //freopen("input.txt","r",stdin);
    scanf("%d",&n);
    long long sum = 0;
    for(int i=0; i<n; i++)
    {
        scanf("%I64d",&a[i]);
        sum += a[i];
    }
    long long need = sum/n;
    long long ans = 0;
    long long extra = 0;
    for(int i=0; i<n; i++)
    {
        if(extra != 0)
        {
            ans += extra;
            a[i] += extra;
            extra = a[i] - need;
            if(extra < 0)
            {
                need_more.push_back(make_pair(i, -extra));
                extra = 0;
            }
        }
        else
        {
            while(!need_more.empty() && a[i] > 0)
            {
                pair<int, int> cur = need_more.front();
                need_more.pop_front();
                long long mini = min((long long)cur.second, a[i]);
                a[i] -= mini;
                cur.second -= mini;
                ans += (i - cur.first) * 1LL * mini;
                if(cur.second > 0)
                    need_more.push_front(cur);
            }
            if(a[i] > need)
            {
                extra += a[i] - need;
            }
            else
                if(a[i] < need)
                {
                    need_more.push_back(make_pair(i, need - a[i]));
                }
        }
    }

    while(need_more.size() > 0)
    {
        pair<int, int> cur = need_more.front();
        need_more.pop_front();
        ans += (n-1-cur.first) * 1LL * (cur.second);
    }

    printf("%I64d",ans);

    return 0;
}


#include <iostream>

using namespace std;

long long counts[50010];
long long sum = 0;

int main()
{
	int n;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		cin >> counts[i];
		sum += counts[i];
	}
	long long d = sum / n;
	long long ans = 0;
	sum = 0;
	for (int i = 0; i < n; i++)
	{
		sum += counts[i];
		ans += (i + 1)*d - sum;
	}
	cout << (ans < 0 ? -ans : ans);
	return 0;
}


#include <iostream>

using namespace std;

long long counts[50010];
long long sum = 0;

int main()
{
	int n;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		cin >> counts[i];
		sum += counts[i];
	}
	long long d = sum / n;
	long long ans = 0;
	sum = 0;
	for (int i = 0; i < n; i++)
	{
		sum += counts[i];
		ans += (i + 1)*d - sum;
	}
	cout << ans;
	return 0;
}


#include <iostream>

using namespace std;

long long counts[50010];
long long sum = 0;

int main()
{
	int n;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		cin >> counts[i];
		sum += counts[i];
	}
	long long d = sum / n;
	long long ans = 0;
	sum = 0;
	for (int i = 0; i < n; i++)
	{
		ans += abs(sum);
		sum += counts[i] - d;
	}
	cout << ans;
	return 0;
}


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;

long long a[50010];
long long n,sum,ave,ans;

int main()
{
    long long i,j,k;
    cin>>n;
    for(i=1;i<=n;i++)scanf("%64d",&a[i]),sum+=a[i];
    ave=sum/n;
    for(i=1;i<=n;i++)
    {
        if(a[i]<ave)ans+=ave-a[i],a[i+1]-=ave-a[i];
        else ans+=a[i]-ave,a[i+1]+=a[i]-ave;
    }
    cout<<ans<<endl;
    return 0;
}


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;

long long a[50000];
long long n,sum,ave,ans;

int main()
{
    long long i,j,k;
    cin>>n;
    for(i=1;i<=n;i++)scanf("%64d",&a[i]),sum+=a[i];
    ave=sum/n;
    for(i=1;i<=n;i++)
    {
        if(a[i]<ave)ans+=ave-a[i],a[i+1]-=ave-a[i];
        else ans+=a[i]-ave,a[i+1]+=a[i]-ave;
    }
    cout<<ans<<endl;
    return 0;
}


#include <stdio.h>
#include <fstream>
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <deque>
#include <math.h>
#include <iomanip>
#include<map>
#include <string>
#include<list>
#include<stack>
#include<algorithm>
#include <queue>
using namespace std;

#define PI 3.14159265358979323846
#define MIN(a,b) ((a>b)? b : a)
#define MAX(a,b) ((a<b)? b : a)
#define MOD(a) ((a>0)? a : -a)
#define ll long long
#define ld long double

// M_PI

#pragma comment(linker, "/STACK:167772160")

/*
int compare (const void* a, const void* b){
    return ((*(pair<int, int>*)b).first-(*(pair<int, int>*)a).first);

}*/

/*
int compstr(const void* a, const void* b){
    if(*(string*)a>*(string*)b)
        return 1;
    else if(*(string*)a==*(string*)b)
        return 0;
    else
        return -1;
}
*/

int main(){
    ll sum(0), res(0), num(0);
    int n, mas[50000];
    scanf("%d", &n);
    for(int i(0); i<n;++i){
        scanf("%d", &mas[i]);
        sum+=mas[i];
    }
    sum/=n;
    for(int i(0); i<n;++i){
        mas[i]-=sum;
        res+=MOD(num);
        num+=mas[i];
    }
    printf("%I64d", res);
    return 0;
}


#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<cmath>
#define rep(i, n)   for(int i = 0; i < n; i ++)

using namespace std;

typedef long long ll;
ll num[18];
int DP(ll x) {
    if(x == 0)  return 0;
    if(x == 1)  return 1;
    int res, cost;
    ll t;
    int k = lower_bound(num, num + 16, x) - num;
    cost = x / num[k - 1] * k;
    res = cost + DP(x % num[k - 1]);
    cost = k + 1;
    t = num[k];
    while(t - num[k - 1] >= x)  t -= num[k - 1], cost += k;
    t -= x;
    res = min(res, cost + DP(t));
    return res;
}

int main() {
    num[0] = 1;
    for(int i = 1; i <= 15; i ++)   num[i] = num[i -1] * 10 + 1;
    ll n;
    cin >> n;
    cout << DP(n) << endl;
}


#include<stdio.h>
int main()
{
    long long n,i,k=0,min=0,temp=0;
    long long a[1000000] = {0};
    scanf("%I64d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%I64d",&a[i]);
        k+= a[i];
    }
    k = k/n;
    for(i=0;i<n;i++)
    {
        if(temp < 0)
        min+= temp * -1;
        else
        min+= temp;
        temp+=a[i]-k;

    }
    printf("%I64d",min);
    return 0;
}


#include <vector>
#include <string>
#include <cstring>
#include <iostream>
#include <stdlib.h>
#include <iomanip>
#include <algorithm>

using namespace std;

#define ll long long

int main() {
	int N;

	cin >> N;

	ll A[50001];

	ll total = 0;
	for (int i = 0; i < N; ++i) {
		cin >> A[i];
		total += A[i];
	}

	ll x = total / N;

	ll res = 0;
	ll bal = 0;
	for (int i = 0; i < N; ++i) {
		res += abs(bal);
		bal += x - A[i];
	}

	cout << res << endl;

	return 0;
}


#include <vector>
#include <string>
#include <cstring>
#include <iostream>
#include <stdlib.h>
#include <iomanip>
#include <algorithm>

using namespace std;

#define ll long long

int main() {
	int N;

	cin >> N;

	ll A[50001];

	ll total = 0;
	for (int i = 0; i < N; ++i) {
		cin >> A[i];
		total += A[i];
	}

	ll x = total / N;

	ll res = 0;
	for (int i = 0; i < N - 1; ++i) {
		if (A[i] > x) {
			int d = A[i] - x;
			res += d;
			A[i + 1] += d;
		} else if (A[i] < x) {
			int d = x - A[i];
			res += d;
			A[i + 1] -= d;
		}
	}

	cout << res << endl;

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstring>
#include <stack>
#include <list>
#include <cstdio>
#include <vector>
#include <deque>
#include <queue>
#include <set>
#include <hash_map>
#include <algorithm>
#include <cmath>
#include <sstream>
#include <list>
#include <map>
#include <sstream>
#include <string>
#include <cstdlib>
#include <bitset>
#include <iomanip>
#include <ctime>
using namespace std;
#pragma comment(linker,"/STACK:17000000")
const double eps = 1e-9; const double inf = 1e+9;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<P, int> P2;
typedef double db;
#define f first
#define s second

int n;
ll a[50001];
ll k = 0;
int main() {
    scanf("%d",&n);
    for (int i = 0, t; i < n; i++){
        scanf("%I64d", &a[i]);
        k += a[i];
    }
    k /= n;
    ll ans = 0, z = 0;
    for (int i = 0; i < n; i++){
        ans += z;
        z += (a[i] - k);
    }
    printf("%I64d",abs(ans));
}
/*

*/


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstring>
#include <stack>
#include <list>
#include <cstdio>
#include <vector>
#include <deque>
#include <queue>
#include <set>
#include <hash_map>
#include <algorithm>
#include <cmath>
#include <sstream>
#include <list>
#include <map>
#include <sstream>
#include <string>
#include <cstdlib>
#include <bitset>
#include <iomanip>
#include <ctime>
using namespace std;
#pragma comment(linker,"/STACK:17000000")
const double eps = 1e-9; const double inf = 1e+9;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<P, int> P2;
typedef double db;
#define f first
#define s second

int n;
ll a[50001];
ll k = 0;
int main() {
    scanf("%d",&n);
    for (int i = 0, t; i < n; i++){
        scanf("%I64d", &a[i]);
        k += a[i];
    }
    k /= n;
    ll ans = 0, z = 0;
    for (int i = 0; i < n; i++){
        z += (a[i] - k);
        ans += abs(z);
    }
    printf("%I64d",abs(ans));
}
/*

*/


#define _CRT_SECURE_NO_WARNINGS
#define _SCL_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cstring>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <bitset>
#include <ctime>
#include <cmath>
#include <cassert>
#include <algorithm>
#include <unordered_set>
using namespace std;

typedef long long ll;
const int size = 100007;
const ll modulo = 1000000007;
const ll oo = 1e18;
const double EPS = 1e-13;
const double PI = acos(-1.0);

int main()
{
	ios_base::sync_with_stdio(false); cin.tie(0);
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
#endif
	ll n;
	cin >> n;
	vector<ll> a(n+2);
	ll sum = 0;
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
		sum += a[i];
	}
	ll d = sum / n;
	ll ans = 0, borrowed = 0;
	for (int i = 0; i < n; i++)
	{
		if (a[i] > d)
		{
			ll odd = a[i] - d;
			ll toLeft = min(borrowed, odd);
			ll toRight = odd - toLeft;
			a[i + 1] += toRight;
			ans += toRight;
			borrowed -= toLeft;
			ans += borrowed;
		}
		else
		{
			ll needed = d - a[i];
			borrowed += needed;
			ans += borrowed;
		}
	}
	cout << ans << endl;
	return 0;
}


#include<iostream>
#include<math.h>
using namespace std;

long long int nNumber_1 (int n);
long long int a_mu_b (int a, int b);
int count_Number (long long int n);
int truong_hop (long long int n, long long int a1, long long int a2, long long int a3);
struct box {
    long int position;
    long long int content;
};
long int giatri (box a, box b);
int main()
{
    long int boxes;
    long long int sum(0);
    cin>>boxes;
    long long int *mang = new long long int [boxes];
    for (int i = 0; i < boxes; i++) {
        cin>>mang[i];
        sum = sum + mang[i];
    }
    sum = sum/boxes;
    if (sum == 0) {
        cout<<sum<<endl;
        return 0;
    }
    long long int moves(0);
    box *mang_box_empty = new box [boxes];
    box *mang_box_full = new box [boxes];
    long long int transfer(0), need(0), supply(0);
    unsigned int jFull(0), jEmpty(0);
    unsigned int positionBegin(1);
    for (int i = 0; i < boxes; i++){
        if ( mang[i] > sum){
            jFull++;
            mang_box_full[jFull].position = i;
            mang_box_full[jFull].content = mang[i];

        }
        else if ( mang[i] < sum) {
            jEmpty++;
            mang_box_empty[jEmpty].position = i;
            mang_box_empty[jEmpty].content = mang[i];
        }
    }
    if (jFull == 0) {
        cout<<moves<<endl;
        return 0;
    }
    for (int i = 1; i <= jEmpty; i++){
        while (mang_box_empty[i].content < sum) {
            need = sum - mang_box_empty[i].content;
            supply = mang_box_full[positionBegin].content - sum;
            if (need <= supply) {
                transfer = need;
                mang_box_empty[i].content = sum;
                mang_box_full[positionBegin].content -= transfer;
                moves = moves + transfer * giatri (mang_box_empty[i], mang_box_full[positionBegin]);
                if (mang_box_full[positionBegin].content == sum){
                    positionBegin += 1;
                }
            }
            else if (need > supply){
                transfer = supply;
                mang_box_empty[i].content += transfer;
                mang_box_full[positionBegin].content -= transfer;
                moves = moves + transfer * giatri (mang_box_empty[i], mang_box_full[positionBegin]);
                positionBegin += 1;
            }
        }
    }
    cout<<moves<<endl;
    return 0;

}
long int giatri (box a, box b){
    if (a.position > b.position ) return (a.position - b.position);
    else if (b.position > a.position ) return (b.position - a.position);

}


#include<iostream>
#include<math.h>
using namespace std;
int main()
{
    int boxes;
    cin>>boxes;
    int *mang = new int [boxes];
    for (int i = 0; i < boxes; i++)
        cin>>mang[i];
    int sum(0);
    for (int i = 0; i < boxes; i++)
        sum = sum + mang[i];
    int result(0);
    sum = sum/boxes;
    for (int i = 0; i < boxes; i++){
        result += abs((mang[i]-sum));
    }
    cout<<result<<endl;

    return 0;

}


#include<iostream>
#include<math.h>
using namespace std;
int main()
{
    int boxes;
    cin>>boxes;
    int *mang = new int [boxes];
    for (int i = 0; i < boxes; i++)
        cin>>mang[i];
    int sum(0);
    for (int i = 0; i < boxes; i++)
        sum = sum + mang[i];
    sum = sum/boxes;
    int moves(0);
    int transfer(0), need(0), supply(0);
    for (int i = 0; i < boxes; i++){
        if ( mang[i] < sum) {
            for (int j = 0; j < boxes; j++){
                if (j == i) continue;
                if ( mang[j] > sum) {
                    need = sum - mang[i];
                    supply = mang[j] - sum;
                    if ( need <= supply){
                        transfer = need;
                        mang[i] = sum;
                        mang[j] = mang[j] - transfer;
                        moves = moves + transfer*abs(j - i);
                        break;
                    }
                    else if (need > supply){
                        transfer = supply;
                        mang[i] = mang[i] + transfer;
                        mang[j] = mang[j] - transfer;
                        moves = moves + transfer*abs(j - i);
                        continue;
                    }
                }
            }
        }
    }
    cout<<moves<<endl;
    return 0;

}


#include<iostream>
#include<math.h>
using namespace std;
int main()
{
    long int boxes;
    cin>>boxes;
    long long int *mang = new long long int [boxes];
    for (int i = 0; i < boxes; i++)
        cin>>mang[i];
    int sum(0);
    for (int i = 0; i < boxes; i++)
        sum = sum + mang[i];
    sum = sum/boxes;
    long long int moves(0);
    long long int transfer(0), need(0), supply(0);
    for (int i = 0; i < boxes; i++){
        if ( mang[i] < sum) {
            for (int j = 0; j < boxes; j++){
                if (j == i) continue;
                if ( mang[j] > sum) {
                    need = sum - mang[i];
                    supply = mang[j] - sum;
                    if ( need <= supply){
                        transfer = need;
                        mang[i] = sum;
                        mang[j] = mang[j] - transfer;
                        moves = moves + transfer*abs(j - i);
                        break;
                    }
                    else if (need > supply){
                        transfer = supply;
                        mang[i] = mang[i] + transfer;
                        mang[j] = mang[j] - transfer;
                        moves = moves + transfer*abs(j - i);
                        continue;
                    }
                }
            }
        }
    }
    cout<<moves<<endl;
    return 0;

}


#include<iostream>
#include<math.h>
using namespace std;
int main()
{
    long int boxes;
    cin>>boxes;
    long long int *mang = new long long int [boxes];
    for (int i = 0; i < boxes; i++)
        cin>>mang[i];
    long long int sum(0);
    for (int i = 0; i < boxes; i++)
        sum = sum + mang[i];
    sum = sum/boxes;
    long long int moves(0);
    long long int transfer(0), need(0), supply(0);
    for (int i = 0; i < boxes; i++){
        if ( mang[i] < sum) {
            for (int j = 0; j < boxes; j++){
                if (j == i) continue;
                if ( mang[j] > sum) {
                    need = sum - mang[i];
                    supply = mang[j] - sum;
                    if ( need <= supply){
                        transfer = need;
                        mang[i] = sum;
                        mang[j] = mang[j] - transfer;
                        moves = moves + transfer*abs(j - i);
                        break;
                    }
                    else if (need > supply){
                        transfer = supply;
                        mang[i] = mang[i] + transfer;
                        mang[j] = mang[j] - transfer;
                        moves = moves + transfer*abs(j - i);
                        continue;
                    }
                }
            }
        }
    }
    cout<<moves<<endl;
    return 0;

}


#include<iostream>
#include<math.h>
using namespace std;
int main()
{
    long int boxes;
    cin>>boxes;
    long long int *mang = new long long int [boxes];
    for (int i = 0; i < boxes; i++)
        cin>>mang[i];
    long long int sum(0);
    for (int i = 0; i < boxes; i++)
        sum = sum + mang[i];
    sum = sum/boxes;
    long long int moves(0);
    if (sum == 0) {cout<<moves<<endl; return 0;}
    long long int transfer(0), need(0), supply(0);
    for (int i = 0; i < boxes; i++){
        if ( mang[i] < sum) {
            for (int j = 0; j < boxes; j++){
                if (j == i) continue;
                if ( mang[j] > sum) {
                    need = sum - mang[i];
                    supply = mang[j] - sum;
                    if ( need <= supply){
                        transfer = need;
                        mang[i] = sum;
                        mang[j] = mang[j] - transfer;
                        moves = moves + transfer*abs(j - i);
                        break;
                    }
                    else if (need > supply){
                        transfer = supply;
                        mang[i] = mang[i] + transfer;
                        mang[j] = mang[j] - transfer;
                        moves = moves + transfer*abs(j - i);
                        continue;
                    }
                }
            }
        }
    }
    cout<<moves<<endl;
    return 0;

}


#include <iostream>
using namespace std;

#define ll long long
#define fr(i,n) for(int i=0;i<n;i++)
#define fru(i,a,b) for(int i=a;i<=b;i++)

#define N 50010

int n, k, m, v;
int a[N]={0};
ll res = 0, ost = 0, s = 0;

void Run() {
  cin>>n;
  fru(i,1,n){
      cin>>a[i];
      s += a[i];
  }
  ll mod = s/n;
  fru(i,1,n){
      res += abs(mod - a[i]);
  }
cout<<res;
}

int main() {
ios_base::sync_with_stdio(0);
    Run();
    return 0;
}


#include <iostream>
using namespace std;

#define ll long long
#define fr(i,n) for(int i=0;i<n;i++)
#define fru(i,a,b) for(int i=a;i<=b;i++)

#define N 50010

int n, k, m, v;
int a[N]={0};
ll res = 0, ost = 0, s = 0;

void Run() {
  cin>>n;
  fru(i,1,n){
      cin>>a[i];
      s += a[i];
  }
  ll mod = s/n;
  fru(i,1,n){
      ost += a[i] - mod;
      res += abs(ost);
  }
cout<<res;
}

int main() {
ios_base::sync_with_stdio(0);
    Run();
    return 0;
}


#include <iostream>
using namespace std;

#define ll long long
#define fr(i,n) for(int i=0;i<n;i++)
#define fru(i,a,b) for(int i=a;i<=b;i++)

#define N 50010

int n, k, m, v;
int a[N]={0};
ll res = 0, ost = 0, s = 0;

void Run() {
  cin>>n;
  fru(i,1,n){
      cin>>a[i];
      s += a[i];
  }
  int mod = s/n;
  fru(i,1,n){
      res += abs(mod - a[i]);
      a[i + 1] += a[i] - mod;
  }
cout<<res;
}

int main() {
ios_base::sync_with_stdio(0);
    Run();
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <string>

using namespace std;

int n, a[50005];
__int64 ans, s[50005];

int main()
{
    cin >> n;
    for (int i = 1; i <= n; ++i)
    {
        cin >> a[i];
        s[i] = s[i-1] + a[i];
    }
    __int64 m = s[n] / n;
    for (int i = 1; i < n; ++i)
    {
        __int64 t = m - a[i];
        a[i] += t;
        a[i+1] -= t;
        ans += abs(t);
    }
    cout << ans;
}


#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <string>

using namespace std;

int n;
__int64 ans, s, a[50005];

int main()
{
    cin >> n;
    for (int i = 1; i <= n; ++i)
    {
        cin >> a[i];
        s += a[i];
    }
    __int64 m = s / n;
    for (int i = 1; i < n; ++i)
    {
        __int64 t = m - a[i];
        a[i] += t;
        a[i+1] -= t;
        ans += abs(t);
    }
    cout << ans;
}


#include<iostream>
#include<stack>
#include<string>
#include<map>
#include<math.h>
#include<cmath>
#include<vector>
#include<algorithm>
#include<cmath>
#include<queue>
#include<locale>
#include<ctype.h>
#include<set>
#include<deque>
using namespace std;

int main()
{
    long long n,arr[50001],inEach,sum=0,step =0;
    cin>>n;
    for(int i=0;i<n;i++)
    {
    cin>>arr[i];
    sum+=arr[i];
    }
    inEach = sum/n;
    for(int i=0;i<n-1;i++)
    {
        if(arr[i]>inEach)
        {
        arr[i+1]+=(arr[i]-inEach);
        step+=arr[i]-inEach;
        arr[i]-=arr[i]-inEach;
        }
        if(arr[i]<inEach)
        {
        arr[i+1]-=abs(arr[i]-inEach);
        step+=abs(arr[i]-inEach);
        arr[i]+=abs(arr[i]-inEach);
        }
    }
        cout<<step<<endl;
return 0;
}


#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <math.h>
#include <map>
#include <memory>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <conio.h>
#include <streambuf>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> veci;
typedef vector<ull> vecull;
typedef pair<int,int> prii;
typedef map<int,int> mapii;
typedef set<int> seti;
typedef multiset<int> mseti;

const int mod = 1e9 + 7;
#ifdef _MSC_VER
    #define typeof(a) decltype(a)
#endif
#define mp(a, b) make_pair(a, b)
#define in(a, min, max) (a > min && a < max)
#define out(a, min, max) (a < min || a > max)
#define fru(i, n) for (int (i) = 0; i < (n); i++)
#define frus(i, st, n, inc) for (int (i) = (st); (i) < (n); (i)+=(inc))
#define crrdvec(name, type, size) vector<type> name(size); fru(zzzz, size) {cin >> name[zzzz];}
#define frd(i, n) for (int (i) = n - 1; i >= 0; i--)
#define sqr(x) (x)*(x)
#define each(e,s) for(typeof(e) e=(s).begin(); e!=(s).end(); ++e)
#define toeach(s,add) { each(e, s) (*e)+=add; }
#define sum(s, n) { s+=(n); if (s > mod) s-=mod; }
#define oiarr(a, n) { fru(i, n) cout << a[i]; cout << endl; }
#define iiarr(a, n) { fru(i, n) cin >> a[i]; }
#define srtu(a) { sort((a).begin(), (a).end(), std::greater<typeof((a).front())>()); }
#define srtd(a) { sort((a).begin(), (a).end(), std::less<typeof((a).front())>()); }

int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#endif

    ios_base::sync_with_stdio(0);
    int n;
    cin >> n;
    crrdvec(a, int, n);
    ull s = 0;
    fru(i, n)
    {
        s+=a[i];
    }
    int k = s/n;
    int res = 0;
    fru(i, n)
    {
        res += abs(a[i] - k);
    }
    cout << res;

#ifndef ONLINE_JUDGE
    _getch();
#endif
    return 0;
}


#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <math.h>
#include <map>
#include <memory>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <conio.h>
#include <streambuf>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> veci;
typedef vector<ull> vecull;
typedef vector<vector<int>> veci2;
typedef vector<pair<int, bool>> vecif;
typedef vector<bool> vecb;
typedef pair<int,int> prii;
typedef map<int,int> mapii;
typedef set<int> seti;
typedef multiset<int> mseti;

const int mod = 1e9 + 7;
template<class T> inline T abs(T t) {return ((t<0)?(-t):t);}
#ifdef _MSC_VER
    #define typeof(a) decltype(a)
#endif
#define mp(a, b) make_pair(a, b)
#define in(a, min, max) (a > min && a < max)
#define out(a, min, max) (a < min || a > max)
#define fru(i, n) for (int (i) = 0; i < (n); i++)
#define frus(i, st, n, inc) for (int (i) = (st); (i) < (n); (i)+=(inc))
#define crrdvec(name, type, size) vector<type> name(size); fru(zzzz, size) {cin >> name[zzzz];}
#define crrd(name, type) type name; cin >> name;
#define frd(i, n) for (int (i) = n - 1; i >= 0; i--)
#define sqr(x) (x)*(x)
#define each(e,s) for(typeof(e) e=(s).begin(); e!=(s).end(); ++e)
#define toeach(s,add) { each(e, s) (*e)+=add; }
#define sum(s, n) { s+=(n); if (s > mod) s-=mod; }
#define oiarr(a, n) { fru(zzzz, n) cout << a[zzzz] << " "; cout << endl; }
#define iiarr(a, n) { fru(zzzz, n) cin >> a[zzzz]; }
#define srtd(a) { sort((a).begin(), (a).end(), std::greater<typeof((a).front())>()); }
#define srtu(a) { sort((a).begin(), (a).end(), std::less<typeof((a).front())>()); }
#define summvec(name, vec) int name = 0; fru(zzzz, vec.size()) name += vec[zzzz];
#define f first
#define s second

int x = 1, y = 1;
int n,m,k;
bool r = true;

int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(0);
    crrd(n, int);
    crrdvec(a, int, n);
    summvec(summ, a);
    summ /= n;
    ll res = 0;
    ll d = 0;
    fru(i, n)
    {
        d += (a[i] - summ);
        res += abs(d);
    }
    cout << res;

    return 0;
}


#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <math.h>
#include <map>
#include <memory>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <conio.h>
#include <streambuf>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> veci;
typedef vector<ull> vecull;
typedef vector<vector<int>> veci2;
typedef vector<pair<int, bool>> vecif;
typedef vector<bool> vecb;
typedef pair<int,int> prii;
typedef map<int,int> mapii;
typedef set<int> seti;
typedef multiset<int> mseti;

const int mod = 1e9 + 7;
template<class T> inline T abs(T t) {return ((t<0)?(-t):t);}
#ifdef _MSC_VER
    #define typeof(a) decltype(a)
#endif
#define mp(a, b) make_pair(a, b)
#define in(a, min, max) (a > min && a < max)
#define out(a, min, max) (a < min || a > max)
#define fru(i, n) for (int (i) = 0; i < (n); i++)
#define frus(i, st, n, inc) for (int (i) = (st); (i) < (n); (i)+=(inc))
#define crrdvec(name, type, size) vector<type> name(size); fru(zzzz, size) {cin >> name[zzzz];}
#define crrd(name, type) type name; cin >> name;
#define frd(i, n) for (int (i) = n - 1; i >= 0; i--)
#define sqr(x) (x)*(x)
#define each(e,s) for(typeof(e) e=(s).begin(); e!=(s).end(); ++e)
#define toeach(s,add) { each(e, s) (*e)+=add; }
#define sum(s, n) { s+=(n); if (s > mod) s-=mod; }
#define oiarr(a, n) { fru(zzzz, n) cout << a[zzzz] << " "; cout << endl; }
#define iiarr(a, n) { fru(zzzz, n) cin >> a[zzzz]; }
#define srtd(a) { sort((a).begin(), (a).end(), std::greater<typeof((a).front())>()); }
#define srtu(a) { sort((a).begin(), (a).end(), std::less<typeof((a).front())>()); }
#define summvec(name, vec) ll name = 0; fru(zzzz, vec.size()) name += vec[zzzz];
#define f first
#define s second

int x = 1, y = 1;
int n,m,k;
bool r = true;

int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(0);
    crrd(n, int);
    crrdvec(a, int, n);
    summvec(summ, a);
    summ /= n;
    ll res = 0;
    ll d = 0;
    fru(i, n)
    {
        d += (a[i] - summ);
        res += abs(d);
    }
    cout << res;

    return 0;
}


#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <math.h>
#include <map>
#include <memory>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <conio.h>
#include <streambuf>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> veci;
typedef vector<ull> vecull;
typedef vector<vector<int>> veci2;
typedef vector<pair<int, bool>> vecif;
typedef vector<bool> vecb;
typedef pair<int,int> prii;
typedef map<int,int> mapii;
typedef set<int> seti;
typedef multiset<int> mseti;

const int mod = 1e9 + 7;
#ifdef _MSC_VER
    #define typeof(a) decltype(a)
#endif
#define mp(a, b) make_pair(a, b)
#define in(a, min, max) (a > min && a < max)
#define out(a, min, max) (a < min || a > max)
#define fru(i, n) for (int (i) = 0; i < (n); i++)
#define frus(i, st, n, inc) for (int (i) = (st); (i) < (n); (i)+=(inc))
#define crrdvec(name, type, size) vector<type> name(size); fru(zzzz, size) {cin >> name[zzzz];}
#define crrd(name, type) type name; cin >> name;
#define frd(i, n) for (int (i) = n - 1; i >= 0; i--)
#define sqr(x) (x)*(x)
#define each(e,s) for(typeof(e) e=(s).begin(); e!=(s).end(); ++e)
#define toeach(s,add) { each(e, s) (*e)+=add; }
#define sum(s, n) { s+=(n); if (s > mod) s-=mod; }
#define oiarr(a, n) { fru(zzzz, n) cout << a[zzzz] << " "; cout << endl; }
#define iiarr(a, n) { fru(zzzz, n) cin >> a[zzzz]; }
#define srtd(a) { sort((a).begin(), (a).end(), std::greater<typeof((a).front())>()); }
#define srtu(a) { sort((a).begin(), (a).end(), std::less<typeof((a).front())>()); }
#define summvec(name, vec) int name = 0; fru(zzzz, vec.size()) name += vec[zzzz];
#define f first
#define s second

int x = 1, y = 1;
int n,m,k;
bool r = true;

int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(0);
    crrd(n, int);
    crrdvec(a, int, n);
    summvec(summ, a);
    summ /= n;
    ull res = 0;
    fru(i, n - 1)
    {
        a[i+1] -= summ - a[i];
        res += abs(summ - a[i]);
    }
    res += abs(summ - a[n - 1]);
    cout << res;

    return 0;
}


#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <math.h>
#include <map>
#include <memory>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <conio.h>
#include <streambuf>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> veci;
typedef vector<ull> vecull;
typedef vector<vector<int>> veci2;
typedef vector<pair<int, bool>> vecif;
typedef vector<bool> vecb;
typedef pair<int,int> prii;
typedef map<int,int> mapii;
typedef set<int> seti;
typedef multiset<int> mseti;

const int mod = 1e9 + 7;
#ifdef _MSC_VER
    #define typeof(a) decltype(a)
#endif
#define mp(a, b) make_pair(a, b)
#define in(a, min, max) (a > min && a < max)
#define out(a, min, max) (a < min || a > max)
#define fru(i, n) for (int (i) = 0; i < (n); i++)
#define frus(i, st, n, inc) for (int (i) = (st); (i) < (n); (i)+=(inc))
#define crrdvec(name, type, size) vector<type> name(size); fru(zzzz, size) {cin >> name[zzzz];}
#define crrd(name, type) type name; cin >> name;
#define frd(i, n) for (int (i) = n - 1; i >= 0; i--)
#define sqr(x) (x)*(x)
#define each(e,s) for(typeof(e) e=(s).begin(); e!=(s).end(); ++e)
#define toeach(s,add) { each(e, s) (*e)+=add; }
#define sum(s, n) { s+=(n); if (s > mod) s-=mod; }
#define oiarr(a, n) { fru(zzzz, n) cout << a[zzzz] << " "; cout << endl; }
#define iiarr(a, n) { fru(zzzz, n) cin >> a[zzzz]; }
#define srtd(a) { sort((a).begin(), (a).end(), std::greater<typeof((a).front())>()); }
#define srtu(a) { sort((a).begin(), (a).end(), std::less<typeof((a).front())>()); }
#define summvec(name, vec) int name = 0; fru(zzzz, vec.size()) name += vec[zzzz];
#define f first
#define s second

int x = 1, y = 1;
int n,m,k;
bool r = true;

int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(0);
    crrd(n, int);
    crrdvec(a, int, n);
    summvec(summ, a);
    summ /= n;
    int res = 0;
    fru(i, n - 1)
    {
        a[i+1] -= summ - a[i];
        res += abs(summ - a[i]);
    }
    res += abs(summ - a[n - 1]);
    cout << res;

    return 0;
}


/*
@Author: Naseef
Chittagong University of Engineering & Technology
*/

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cstring>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <bitset>
#include <ctime>
#include <cmath>
#include <cassert>
#include <algorithm>
using namespace std;

#define pb(p) push_back(p)
#define fr(i,a,b) for(int i=a;i<b;i++)
#define frd(i,a,b) for(int i=a; i>=b i--)
#define PI 2*acos(0.0)
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()

int MIN (int a, int b) {
  int x= (b>a)?a:b;
  return x;
}

int MAX(int a, int b) {
  int x= (b<a)?a:b;
  return x;
}

#define maxn 10000000

/*vector<int>prime;
__int64 count[maxn+10];
bool visit[maxn+10];

void make_prime(){

		/*for( i=2;i*i<=MAX;i++ ){
        if( ntP[i] ) continue;
        for( j=i*i;j<=MAX;j+=i ) ntP[j] = true;
    int i,j;
    int pos1=sqrt((double)maxn)+1;
	visit[1]=true;
	visit[4]=true;
	visit[6]=true;
	visit[8]=true;
    for(i=3;i<=pos1;i++)
	{
		if(visit[i]==false)
		{
			for(j=i*i;j<=maxn;j=j+i+i)
			visit[j]=true;
		}
	}
    prime.push_back(2);
    for(j=3;j<=maxn;j+=2)
        if( visit[j]==false )prime.push_back(j);
}
*/
typedef long long ll;
const int size = 1000007;
const ll modulo = 1000000007;
const ll inf = 1e18;
const double eps = 1e-6;
int n,m,a[1000009],b[1000009],k,n25,n50,n100;

/*
int gcd(int a,int b)
{
    while(b>0)
    {
        a%=b;
        a^=b;
        b^=a;
        a^=b;
    }
    return a;
}

ll lcm(ll a,ll b)
{
    return ((a*b)/gcd(a,b));
}*/

struct AA
{
	int v,ab;

} ans[10];

bool cmp(AA x, AA y)
{
	return x.v<y.v;
}
int main()
{
    #ifdef WRITE_IN_FILE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif
	string str[10];

	while(cin>>n)
	{
		ll sum = 0;
		fr(i,0,n)
		{
			ll xx;
			cin>>xx;
			a[i]=xx;
			sum+=xx;
		}

		ll av = sum/n;
		ll dff = 0;

		fr(i,0,n)
		{
			dff+=(abs(av-a[i]));
		}

		cout<<dff<<endl;
	}
	return 0;
}


/*
@Author: Naseef
Chittagong University of Engineering & Technology
*/

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cstring>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <bitset>
#include <ctime>
#include <cmath>
#include <cassert>
#include <algorithm>
using namespace std;

#define pb(p) push_back(p)
#define fr(i,a,b) for(int i=a;i<b;i++)
#define frd(i,a,b) for(int i=a; i>=b i--)
#define PI 2*acos(0.0)
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()

int MIN (int a, int b) {
  int x= (b>a)?a:b;
  return x;
}

int MAX(int a, int b) {
  int x= (b<a)?a:b;
  return x;
}

#define maxn 10000000

/*vector<int>prime;
__int64 count[maxn+10];
bool visit[maxn+10];

void make_prime(){

		/*for( i=2;i*i<=MAX;i++ ){
        if( ntP[i] ) continue;
        for( j=i*i;j<=MAX;j+=i ) ntP[j] = true;
    int i,j;
    int pos1=sqrt((double)maxn)+1;
	visit[1]=true;
	visit[4]=true;
	visit[6]=true;
	visit[8]=true;
    for(i=3;i<=pos1;i++)
	{
		if(visit[i]==false)
		{
			for(j=i*i;j<=maxn;j=j+i+i)
			visit[j]=true;
		}
	}
    prime.push_back(2);
    for(j=3;j<=maxn;j+=2)
        if( visit[j]==false )prime.push_back(j);
}
*/
typedef long long ll;
const int size = 1000007;
const ll modulo = 1000000007;
const ll inf = 1e18;
const double eps = 1e-6;
int n,m,a[1000009],b[1000009],k,n25,n50,n100;

/*
int gcd(int a,int b)
{
    while(b>0)
    {
        a%=b;
        a^=b;
        b^=a;
        a^=b;
    }
    return a;
}

ll lcm(ll a,ll b)
{
    return ((a*b)/gcd(a,b));
}*/

struct AA
{
	int v,ab;

} ans[10];

bool cmp(AA x, AA y)
{
	return x.v<y.v;
}
int main()
{
    #ifdef WRITE_IN_FILE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif
	string str[10];

	while(cin>>n)
	{
		ll sum = 0;
		fr(i,0,n)
		{
			ll xx;
			cin>>xx;
			a[i]=xx;
			sum+=xx;
		}

		ll av = sum/n;
		ll dff = 0;

		ll biog = 0;

		fr(i,0,n)
		{
			a[i] -= biog;
			biog = av - a[i];
			dff += abs(biog);
		}

		cout<<dff<<endl;
	}
	return 0;
}


/*
@Author: Naseef
Chittagong University of Engineering & Technology
*/

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cstring>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <bitset>
#include <ctime>
#include <cmath>
#include <cassert>
#include <algorithm>
using namespace std;

#define pb(p) push_back(p)
#define fr(i,a,b) for(int i=a;i<b;i++)
#define frd(i,a,b) for(int i=a; i>=b i--)
#define PI 2*acos(0.0)
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()

int MIN (int a, int b) {
  int x= (b>a)?a:b;
  return x;
}

int MAX(int a, int b) {
  int x= (b<a)?a:b;
  return x;
}

#define maxn 10000000

/*vector<int>prime;
__int64 count[maxn+10];
bool visit[maxn+10];

void make_prime(){

		/*for( i=2;i*i<=MAX;i++ ){
        if( ntP[i] ) continue;
        for( j=i*i;j<=MAX;j+=i ) ntP[j] = true;
    int i,j;
    int pos1=sqrt((double)maxn)+1;
	visit[1]=true;
	visit[4]=true;
	visit[6]=true;
	visit[8]=true;
    for(i=3;i<=pos1;i++)
	{
		if(visit[i]==false)
		{
			for(j=i*i;j<=maxn;j=j+i+i)
			visit[j]=true;
		}
	}
    prime.push_back(2);
    for(j=3;j<=maxn;j+=2)
        if( visit[j]==false )prime.push_back(j);
}
*/
typedef long long ll;
const int size = 1000007;
const ll modulo = 1000000007;
const ll inf = 1e18;
const double eps = 1e-6;
ll n,m,a[100009],b[100009],k,n25,n50,n100;

/*
int gcd(int a,int b)
{
    while(b>0)
    {
        a%=b;
        a^=b;
        b^=a;
        a^=b;
    }
    return a;
}

ll lcm(ll a,ll b)
{
    return ((a*b)/gcd(a,b));
}*/

struct AA
{
	int v,ab;

} ans[10];

bool cmp(AA x, AA y)
{
	return x.v<y.v;
}
int main()
{
    #ifdef WRITE_IN_FILE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif
	string str[10];

	while(cin>>n)
	{
		ll sum = 0;
		fr(i,0,n)
		{
			ll xx;
			cin>>xx;
			a[i]=xx;
			sum+=xx;
		}

		ll av = (ll)sum/(ll)n;
		ll dff = 0;

		ll biog = 0;

		fr(i,0,n)
		{
			a[i] -= biog;
			biog = av - (ll)a[i];
			dff += abs(biog);
		}

		cout<<dff<<endl;
	}
	return 0;
}


/*
@Author: Naseef
Chittagong University of Engineering & Technology
*/

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cstring>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <bitset>
#include <ctime>
#include <cmath>
#include <cassert>
#include <algorithm>
using namespace std;

#define pb(p) push_back(p)
#define fr(i,a,b) for(int i=a;i<b;i++)
#define frd(i,a,b) for(int i=a; i>=b i--)
#define PI 2*acos(0.0)
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()

int MIN (int a, int b) {
  int x= (b>a)?a:b;
  return x;
}

int MAX(int a, int b) {
  int x= (b<a)?a:b;
  return x;
}

#define maxn 10000000

/*vector<int>prime;
__int64 count[maxn+10];
bool visit[maxn+10];

void make_prime(){

		/*for( i=2;i*i<=MAX;i++ ){
        if( ntP[i] ) continue;
        for( j=i*i;j<=MAX;j+=i ) ntP[j] = true;
    int i,j;
    int pos1=sqrt((double)maxn)+1;
	visit[1]=true;
	visit[4]=true;
	visit[6]=true;
	visit[8]=true;
    for(i=3;i<=pos1;i++)
	{
		if(visit[i]==false)
		{
			for(j=i*i;j<=maxn;j=j+i+i)
			visit[j]=true;
		}
	}
    prime.push_back(2);
    for(j=3;j<=maxn;j+=2)
        if( visit[j]==false )prime.push_back(j);
}
*/
typedef long long ll;
const int size = 1000007;
const ll modulo = 1000000007;
const ll inf = 1e18;
const double eps = 1e-6;
int n,m,a[1000009],b[1000009],k,n25,n50,n100;

/*
int gcd(int a,int b)
{
    while(b>0)
    {
        a%=b;
        a^=b;
        b^=a;
        a^=b;
    }
    return a;
}

ll lcm(ll a,ll b)
{
    return ((a*b)/gcd(a,b));
}*/

struct AA
{
	int v,ab;

} ans[10];

bool cmp(AA x, AA y)
{
	return x.v<y.v;
}
int main()
{
    #ifdef WRITE_IN_FILE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif
	string str[10];

	while(cin>>n)
	{
		ll sum = 0;
		fr(i,0,n)
		{
			ll xx;
			cin>>xx;
			a[i]=xx;
			sum+=xx;
		}

		ll av = (ll)sum/(ll)n;
		ll dff = 0;

		ll biog = 0;

		fr(i,0,n)
		{
			a[i] -= biog;
			biog = av - (ll)a[i];
			dff += abs(biog);
		}

		cout<<dff<<endl;
	}
	return 0;
}


#include <iostream>
#include <vector>

using namespace std;

int main()
{
    int n;
    cin >> n;
    if (n < 1 || n > 50000) {
        cout<<0<<endl;
        return 0;
    }

    //int arr[n];
    vector<long> arr;
    arr.resize(n);
    long long Tong = 0;
    for (int i = 0; i < n; ++i) {
        cin >> arr[i];
        if (arr[i] < 0 || arr[i] > 1000000000) {
            cout << 0 << endl;
            return 0;
        }
        Tong += arr[i];
    }

    if(Tong % n != 0)
    {
        cout << 0 <<endl;
        return 0;
    }

    long t = Tong / n;
    long long numMov = 0;
    long temp = 0;
    for (int i = 0; i < n-1; ++i) {
        if (arr[i] > t) {
            temp = arr[i] - t;
            numMov += temp;
            arr[i+1] += temp;
        }
        else
            if(arr[i] < t)
            {
                temp = t - arr[i];
                numMov += temp;
                arr[i+1] -= temp;
            }
    }
    cout << numMov << endl;
    return 0;
}


#include <iostream>
#include <vector>

using namespace std;

int main()
{
    unsigned int n = 0;
    cin >> n;
    if(n < 1 || n > 50000)
    {
        cout << 0 << endl;
        return 0;
    }

    vector<long long> arr;
    arr.resize(n);
    long long Tong = 0;

    for (unsigned int i = 0; i < n; ++i) {
        cin >> arr[i];
        if (arr[i] > 1000000000) {
            cout << 0 <<endl;
            return 0;
        }
        Tong += arr[i];
    }

    if (Tong % n != 0) {
        cout << 0 << endl;
        return 0;
    }

    long long t = Tong/n;
    long long numMov = 0;
    unsigned long temp = 0;
    for (unsigned int i = 0; i < n; ++i) {
        if (arr[i] > t) {
            temp = arr[i] - t;
            numMov += temp;
            arr[i+1] += temp;
        }
        else
            if (arr[i] < t) {
                temp = t - arr[i];
                numMov += temp;
                arr[i+1] -= temp;
            }
    }
    cout << numMov << endl;
    return 0;
}


#include <iostream>
#include <vector>

using namespace std;

int main()
{
    unsigned int n = 0;
    cin >> n;
    if(n < 1 || n > 50000)
    {
        cout << 0 << endl;
        return 0;
    }

    vector<long long> arr;
    arr.resize(n);
    long long Tong = 0;

    for (unsigned int i = 0; i < n; ++i) {
        cin >> arr[i];
        if (arr[i] < 0 || arr[i] > 1000000000) {
            cout << 0 <<endl;
            return 0;
        }
        Tong += arr[i];
    }

    if (Tong % n != 0) {
        cout << 0 << endl;
        return 0;
    }

    long long t = Tong/n;
    long long numMov = 0;
    long long temp = 0;
    for (unsigned int i = 0; i < n; ++i) {
        if (arr[i] > t) {
            temp = arr[i] - t;
            numMov += temp;
            arr[i+1] += temp;
        }
        else
            if (arr[i] < t) {
                temp = t - arr[i];
                numMov += temp;
                arr[i+1] -= temp;
            }
    }
    cout << numMov << endl;
    return 0;
}


#include <iostream>

using namespace std;

int main()
{
    int n;
    cin >> n;
    if (n < 1 || n > 50000) {
        cout<<"Error!"<<endl;
        return 0;
    }

    int arr[n];
    int Tong = 0;
    for (int i = 0; i < n; ++i) {
        cin >> arr[i];
        if (arr[i] < 0) {
            cout << "Error!" << endl;
            return 0;
        }
        Tong += arr[i];
    }

    if(Tong % n != 0)
    {
        cout << "Error!" <<endl;
        return 0;
    }

    int t = Tong / n;
    int numMov = 0;
    int temp = 0;
    for (int i = 0; i < n-1; ++i) {
        if (arr[i] > t) {
            temp = arr[i] - t;
            numMov += temp;
            arr[i+1] += temp;
        }
        else
            if(arr[i] < t)
            {
                temp = t - arr[i];
                numMov += temp;
                arr[i+1] -= temp;
            }
    }
    cout << numMov << endl;
    return 0;
}


#include <iostream>
#include <vector>

using namespace std;

int main()
{
    int n;
    cin >> n;
    if (n < 1 || n > 50000) {
        cout<<0<<endl;
        return 0;
    }

    //int arr[n];
    vector<int> arr;
    arr.resize(n);
    int Tong = 0;
    for (int i = 0; i < n; ++i) {
        cin >> arr[i];
        if (arr[i] < 0 || arr[i] > 1000000000) {
            cout << 0 << endl;
            return 0;
        }
        Tong += arr[i];
    }

    if(Tong % n != 0)
    {
        cout << 0 <<endl;
        return 0;
    }

    int t = Tong / n;
    int numMov = 0;
    int temp = 0;
    for (int i = 0; i < n-1; ++i) {
        if (arr[i] > t) {
            temp = arr[i] - t;
            numMov += temp;
            arr[i+1] += temp;
        }
        else
            if(arr[i] < t)
            {
                temp = t - arr[i];
                numMov += temp;
                arr[i+1] -= temp;
            }
    }
    cout << numMov << endl;
    return 0;
}


#include <iostream>
#include <vector>

using namespace std;

int main()
{
    int n;
    cin >> n;
    if (n < 1 || n > 50000) {
        cout<<0<<endl;
        return 0;
    }

    //int arr[n];
    vector<int> arr;
    arr.resize(n);
    int Tong = 0;
    for (int i = 0; i < n; ++i) {
        cin >> arr[i];
        if (arr[i] < 0 || arr[i] > 1000000000) {
            cout << 0 << endl;
            return 0;
        }
        Tong += arr[i];
    }

    if(Tong % n != 0)
    {
        cout << 0 <<endl;
        return 0;
    }

    long t = Tong / n;
    long long numMov = 0;
    long temp = 0;
    for (int i = 0; i < n-1; ++i) {
        if (arr[i] > t) {
            temp = arr[i] - t;
            numMov += temp;
            arr[i+1] += temp;
        }
        else
            if(arr[i] < t)
            {
                temp = t - arr[i];
                numMov += temp;
                arr[i+1] -= temp;
            }
    }
    cout << numMov << endl;
    return 0;
}


#include <iostream>
#include <vector>

using namespace std;

int main()
{
    unsigned int n = 0;
    cin >> n;
    if(n < 1 || n > 50000)
    {
        cout << 0 << endl;
        return 0;
    }

    vector<unsigned long> arr;
    arr.resize(n);
    long long Tong = 0;

    for (unsigned int i = 0; i < n; ++i) {
        cin >> arr[i];
        if (arr[i] > 1000000000) {
            cout << 0 <<endl;
            return 0;
        }
        Tong += arr[i];
    }

    if (Tong % n != 0) {
        cout << 0 << endl;
        return 0;
    }

    unsigned long t = Tong/n;
    long long numMov = 0;
    unsigned long temp = 0;
    for (unsigned int i = 0; i < n; ++i) {
        if (arr[i] > t) {
            temp = arr[i] - t;
            numMov += temp;
            arr[i+1] -= temp;
        }
        else
            if (arr[i] < t) {
                temp = t - arr[i];
                numMov += temp;
                arr[i+1] -= temp;
            }
    }
    cout << numMov << endl;
    return 0;
}


#include <iostream>
#include <vector>

using namespace std;

int main()
{
    int n;
    cin >> n;
    if (n < 1 || n > 50000) {
        cout<<0<<endl;
        return 0;
    }

    //int arr[n];
    vector<long> arr;
    arr.resize(n);
    int Tong = 0;
    for (int i = 0; i < n; ++i) {
        cin >> arr[i];
        if (arr[i] < 0 || arr[i] > 1000000000) {
            cout << 0 << endl;
            return 0;
        }
        Tong += arr[i];
    }

    if(Tong % n != 0)
    {
        cout << 0 <<endl;
        return 0;
    }

    long t = Tong / n;
    long long numMov = 0;
    long temp = 0;
    for (int i = 0; i < n-1; ++i) {
        if (arr[i] > t) {
            temp = arr[i] - t;
            numMov += temp;
            arr[i+1] += temp;
        }
        else
            if(arr[i] < t)
            {
                temp = t - arr[i];
                numMov += temp;
                arr[i+1] -= temp;
            }
    }

    cout << numMov << endl;
    return 0;
}


#include <iostream>
#include <vector>

using namespace std;

int main()
{
    unsigned int n = 0;
    cin >> n;
    if(n < 1 || n > 50000)
    {
        cout << 0 << endl;
        return 0;
    }

    vector<unsigned long> arr;
    arr.resize(n);
    long long Tong = 0;

    for (unsigned int i = 0; i < n; ++i) {
        cin >> arr[i];
        if (arr[i] > 1000000000) {
            cout << 0 <<endl;
            return 0;
        }
        Tong += arr[i];
    }

    if (Tong % n != 0) {
        cout << 0 << endl;
        return 0;
    }

    unsigned long t = Tong/n;
    long long numMov = 0;
    unsigned long temp = 0;
    for (unsigned int i = 0; i < n; ++i) {
        if (arr[i] > t) {
            temp = arr[i] - t;
            numMov += temp;
            arr[i+1] += temp;
        }
        else
            if (arr[i] < t) {
                temp = t - arr[i];
                numMov += temp;
                arr[i+1] -= temp;
            }
    }
    cout << numMov << endl;
    return 0;
}


#include <iostream>
#include <vector>

using namespace std;

int main()
{
    int n;
    cin >> n;
    if (n < 1 || n > 50000) {
        cout<<0<<endl;
        return 0;
    }

    //int arr[n];
    vector<int> arr;
    arr.resize(n);
    int Tong = 0;
    for (int i = 0; i < n; ++i) {
        cin >> arr[i];
        if (arr[i] < 0 || arr[i] > 1000000000) {
            cout << 0 << endl;
            return 0;
        }
        Tong += arr[i];
    }

    if(Tong % n != 0)
    {
        cout << 0 <<endl;
        return 0;
    }

    long t = Tong / n;
    long numMov = 0;
    long temp = 0;
    for (int i = 0; i < n-1; ++i) {
        if (arr[i] > t) {
            temp = arr[i] - t;
            numMov += temp;
            arr[i+1] += temp;
        }
        else
            if(arr[i] < t)
            {
                temp = t - arr[i];
                numMov += temp;
                arr[i+1] -= temp;
            }
    }
    cout << numMov << endl;
    return 0;
}


#include <iostream>
#include <vector>

using namespace std;

int main()
{
    int n;
    cin >> n;
    if (n < 1 || n > 50000) {
        cout<<"Error!"<<endl;
        return 0;
    }

    //int arr[n];
    vector<int> arr;
    arr.resize(n);
    int Tong = 0;
    for (int i = 0; i < n; ++i) {
        cin >> arr[i];
        if (arr[i] < 0) {
            cout << "Error!" << endl;
            return 0;
        }
        Tong += arr[i];
    }

    if(Tong % n != 0)
    {
        cout << "Error!" <<endl;
        return 0;
    }

    int t = Tong / n;
    int numMov = 0;
    int temp = 0;
    for (int i = 0; i < n-1; ++i) {
        if (arr[i] > t) {
            temp = arr[i] - t;
            numMov += temp;
            arr[i+1] += temp;
        }
        else
            if(arr[i] < t)
            {
                temp = t - arr[i];
                numMov += temp;
                arr[i+1] -= temp;
            }
    }
    cout << numMov << endl;
    return 0;
}