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: Runtime Error)

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>
#include <iostream>
#include <sstream>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <utility>
#include <numeric>
#include <algorithm>
#include <bitset>
#include <complex>
#include <array>
#include <list>
#include <stack>
#include <valarray>

using namespace std;

typedef unsigned uint;
typedef long long Int;
typedef unsigned long long UInt;

const int INF = 1001001001;
const Int INFLL = 1001001001001001001LL;

template<typename T> void pv(T a, T b) { for (T i = a; i != b; ++i) cout << *i << " "; cout << endl; }
template<typename T> void chmin(T& a, T b) { if (a > b) a = b; }
template<typename T> void chmax(T& a, T b) { if (a < b) a = b; }
int in() { int x; scanf("%d", &x); return x; }
double fin() { double x; scanf("%lf", &x); return x; }
Int lin() { Int x; scanf("%lld", &x); return x; }

int main() {
  int N = in();
  Int A[64], S = 0;
  for (int i = 0; i < N; ++i) {
    A[i] = lin();
    S += A[i];
  }
  sort(A, A + N);
  reverse(A, A + N);

  if (A[0] < N) {
    puts("0");
    return 0;
  }

  Int T[64], s;
  Int K = S - N * (N - 1) - 1;

  Int lo = 0, hi = K + 1;
  while (hi - lo > 1) {
    Int mid = (hi + lo) / 2;
    Int tot = mid, C[64];
    C[0] = mid;
    for (int i = 1; i < N; ++i) {
      C[i] = mid - (A[0] - A[i] + N) / (N + 1) - 1;
      tot += max(C[i], 0LL);
    }
    if (tot < K) {
      lo = mid;
      s = tot;
      for (int i = 0; i < N; ++i) {
        T[i] = A[i] - C[i] * N + (tot - C[i]);
      }
    } else {
      hi = mid;
    }
  }
  assert(accumulate(T, T + N, 0LL) == S - s);
  assert(K - s <= N);

  while (*max_element(T, T + N) >= N) {
    (*max_element(T, T + N)) -= N + 1;
    for (int i = 0; i < N; ++i) {
      ++T[i];
    }
    ++s;
  }

  printf("%lld\n", s);

  return 0;
}