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 Java8 (OpenJDK 1.8.0) (judge verdict: Accepted)

import java.util.*;
import java.io.PrintWriter;
public class Main{
	public static void main(String[] args){
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		long[] a=new long[n];
		long sum=0;
		long ans=0;
		for(int i=0; i<n; i++){
			a[i]=sc.nextLong();
			sum+=a[i]-n+1;
		}
		if(sum>0){
			long r=0;
			while(ans!=sum){
				sum--;
		long l=-1;
		r=Long.MAX_VALUE/10;
		while(r-l>1){
			long m=(l+r)/2;
			long count=0;
			for(int i=0; i<n; i++){
				count+=Math.max((a[i]-m+sum+n)/(n+1),0);
			}
			if(count<=sum){
				ans=count;
				r=m;
			}else{
				l=m;
			}
		}
			}
			for(int i=0; i<n; i++){
				a[i]=a[i]+sum-(n+1)*(Math.max((a[i]-r+sum+n)/(n+1),0));
			}
		}
		while(true){
			Arrays.sort(a);
			if(a[n-1]<=n-1){
				break;
			}else{
				a[n-1]-=n;
			}
			for(int i=n-2; i>=0; i--){
				a[i]++;
			}
			ans++;
		}
		System.out.println(ans);
	}
}