Problem:
Score : 600 points

Problem Statement

We have a sequence of length N consisting of non-negative integers. Consider performing the following operation on this sequence until the largest element in this sequence becomes N-1 or smaller. (The operation is the same as the one in Problem D.)

Determine the largest element in the sequence (if there is more than one, choose one). Decrease the value of this element by N, and increase each of the other elements by 1.

It can be proved that the largest element in the sequence becomes N-1 or smaller after a finite number of operations.

You are given the sequence a_i. Find the number of times we will perform the above operation.

Constraints

2 ≤ N ≤ 50

0 ≤ a_i ≤ 10^{16} + 1000

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the number of times the operation will be performed.

Sample Input 1

4
3 3 3 3

Sample Output 1

0

Sample Input 2

3
1 0 3

Sample Output 2

1

Sample Input 3

2
2 2

Sample Output 3

2

Sample Input 4

7
27 0 0 0 0 0 0

Sample Output 4

3

Sample Input 5

10
1000 193 256 777 0 1 1192 1234567891011 48 425

Sample Output 5

1234567894848

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include<iostream>
#include<iomanip>
#include<math.h>
#include<vector>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<complex>

#define INF 1000000000
#define MOD 1000000007
#define EPS 1e-8

#define REP(i, m) for(long long i = 0; i < m; ++i)
#define FOR(i, n, m) for(long long i = n; i < m; ++i)
#define ALL(v) v.begin(), v.end()
#define pb push_back

using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef long double ld;

ll solve(vector<ll> a) {
	int n = a.size();
	ll res = 0;
	while(true) {
		ll maxi = a[0];
		REP(i, n) {
			maxi = max(maxi, a[i]);
		}
		if(maxi <= n - 1) break;
		maxi = a[0];
		ll roc = 0;
		REP(i, n) {
			if(a[i] > maxi) {
				maxi = a[i];
				roc = i;
			}
		}
		REP(i, n) {
			if(i == roc) a[i] -= n;
			else ++a[i];
		}
		++res;
	}
	return res;
}

int main() {
	int n;
	cin>>n;
	vector<ll> a(n);
	REP(i, n) cin>>a[i];
	sort(ALL(a), greater<ll>());
	if(a[0] <= n - 1) {
		cout<<0<<endl;
		return 0;
	}
	if(a[0] <= 5000) {
		cout<<solve(a)<<endl;
		return 0;
	}
	ll ans = 0;
	int ub = 1;
	ll mini = a[0];
	ll maxi = a[0];
	ll num = 1;
	while(ub < n) {
		ll buf;
		if((mini - a[ub]) % (n + 1) == 0) {
			buf = (mini - a[ub]) / (n + 1);
		}else {
			buf = (mini - a[ub]) / (n + 1) + 1;
		}
		ans += buf * num;
		REP(i, ub) {
			a[i] -= buf * (n - num + 1);
		}
		FOR(i, ub, n) {
			a[i] += buf * num;
		}
		++num;
		++ub;
		REP(i, ub) {
			mini = min(mini, a[i]);
			maxi = max(maxi, a[i]);
		}
	}
	ll buf = (maxi - (2 * n - 1));
	ans += buf * n;
	REP(i, n) a[i] -= buf;
	ans += solve(a);
	cout<<ans<<endl;
}