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 (Python):

def updiv(x,y):
    if x % y == 0:
        return x // y
    else:
        return x // y + 1

def want(K):

    for i in range(N):

        num = 0
        kai = N * K + i
        
        for j in range(N):
            num += max(0 , updiv( a[j] + kai - (N-1) , N + 1))

        
        if num <= kai:
            return kai

    return None

N = int(input())

a = list(map(int,input().split()))

l = -1
r = sum(a)

while r-l != 1:

    m = (l+r)//2

    ind = want(m)

    if ind == None:
        l = m
    else:
        r = m

    

print (want(r))

Solution (Python):
import sys
import numpy as np

stdin = sys.stdin

ri = lambda: int(rs())
rl = lambda: list(map(int, stdin.readline().split()))  
rs = lambda: stdin.readline().rstrip()  

N = ri()
A = np.array(rl())
answer = 0
while A.max() >= N:
    B = A // N
    count = B.sum()
    answer += count
    A = A - B * N + (count - B)

print(answer)

Solution (Python):

import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline
INF = 2**62-1


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, A):
    ans = 0
    while True:
        A.sort()
        l = A.pop()
        if l < N:
            break
        x = (l-N)//N + 1
        ans += x
        l -= x*N
        A = [a+x for a in A]
        A.append(l)


    return ans


def main():
    N = read_int()
    A = read_int_n()
    print(slv(N, A))


if __name__ == '__main__':
    main()

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def main():
    n = int(input())
    aa = LI()
    change = True
    ps = 0
    ans = 0
    cnt = [0] * n
    while change:
        change = False
        sumk = 0
        for i in range(n):
            aa[i] += ps
            a = aa[i]
            if a <= n - 1: continue
            change = True
            k = a // n
            aa[i] -= (n + 1) * k
            cnt[i] += k
            sumk += k
            ans += k
        
        ps = sumk
    print(ans)

main()

Solution (Python):
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda:sys.stdin.readline().rstrip()
def resolve():
    n=int(input())
    A=list(map(int,input().split()))
    s=sum(A)

    while(max(A)>=n):
        k=sum(a//n for a in A)
        for i in range(n):
            A[i]+=(k-A[i]//n)-n*(A[i]//n)

    print(s-sum(A))
resolve()

Solution (Python):
def main():
    n=int(input())
    a=list(map(int,input().split()))
    
    def judge(k):
        cnt=0
        for i in a:
            cnt+=max((i-n+k)//(n+1)+1,0)
        if cnt>k:
            return False
        else:
            return True

    
    
    def b_search(a,b,value,lim):
        med=(a+b)//2
        if a==b:return a
        elif lim=='upper':
            if a+1==b:
                if value(b):return b
                else:return a
            elif value(med):return b_search(med,b,value,lim)
            else:return b_search(a,med-1,value,lim)
        else:
            if a+1==b:
                if value(a):return a
                else:return b
            elif value(med):return b_search(a,med,value,lim)
            else:return b_search(med+1,b,value,lim)

    j=b_search(0,10**18,judge,'lower')
    ans=j
    for i in range(j-1,max(j-3000,-1),-1):
        if judge(i):
            ans=i
    print(ans)
main()

Solution (Python):
N = int(input())
a = [int(i) for i in input().split()]

ng, ok = -1, sum(a)

while ok - ng > 1 :
    m = (ok + ng) // 2
    
    for i in range(-N, 1) :
        if m + i <= ng :
            continue
            
        c = 0
        for j in range(N) :
            c += (a[j] + m + i - N) // (N + 1) + 1
        if c <= m + i :
            ok = m + i
            break
    else :
        ng = m
        
print(ok)

Solution (Python):

N = int(input())

a = list(map(int,input().split()))
a.sort(reverse=True)

ans = 0

while True:
    f = True
    for i in range(N):
        if a[i] >= N:
            f = False
    if f:
        break
    for i in range(N):
        if a[i] >= N:
            t = a[i]//N
            ans += t
            a[i] -= t*(N+1)
            for j in range(N):
                a[j] += t
print(ans)

Solution (Python):
from numpy import int64

N, A = open(0)
N, A = int(N), int64(A.split())

ans = 0
S = -1
while S:
    B, A = A // N, A % N
    S = B.sum()
    ans += S
    A += S - B

print(ans)

Solution (Python):
from numpy import*
N,*A=open(0)
N,A,S,a=int(N),loadtxt(A,int),1,0
while S:S=sum(A//N);a+=S;A=A%N+S-A//N
print(a)

Solution (Python):
from numpy import*
N,*A=open(0)
N,A,S,a=int(N),loadtxt(A,int),1,0
while S:B=A//N;S=sum(B);a+=S;A=A%N+S-B
print(a)

Solution (Python):
from numpy import*
N,*A=open(0)
N,A,S,a=int(N),loadtxt(A,int),1,0
while S:B=A//N;S=sum(B);a+=S;A=A%N+S-B
print(a)

Solution (Python):
def main():
    N, *A = map(int, open(0).read().split())

    ans = 0
    S = -1
    while S:
        S = sum(a // N for a in A)
        ans += S
        A = [a % N + (S - a // N) for a in A]

    print(ans)

main()

Solution (Python):
N, *A = map(int, open(0).read().split())

ans = 0
S = -1
while S:
    S = sum(a // N for a in A)
    ans += S
    A = [a % N + (S - a // N) for a in A]

print(ans)

Solution (Python):
from numpy import*
N,*A=open(0)
N,A,S,a=int(N),loadtxt(A,int),1,0
while S:B=A//N;S=sum(B);a+=S;A+=S-B*-~N
print(a)

Solution (Python):
from numpy import*
N,*A=open(0)
N,A,S,a=int(N),loadtxt(A,int),1,0
while S:B=A//N;S=B.sum();a+=S;A+=S-B*-~N
print(a)

Solution (Python):
from numpy import int64

A = int64(open(0).read().split())[1:]
N = len(A)

ans = 0
S = 1
while S:
    S = (A // N).sum()
    ans += S
    A += S - (A // N) * (N + 1)

print(ans)

Solution (Python):
N, *A = map(int, open(0).read().split())

ans = 0
S = 1
while S:
    S = sum(a // N for a in A)
    ans += S
    A = [a + S - (a // N) * (N + 1) for a in A]

print(ans)

Solution (Python):
N=int(input())
o=0
from heapq import heappop,heappush
q=[]
for a in map(int,input().split()):
    heappush(q,-a)
while -q[0]+o>=N:
    a=heappop(q)
    x,y=(-a+o)//N,(-a+o)%N
    o+=x
    heappush(q,-y+o)
print(o)

Solution (Python):
import sys
input = sys.stdin.readline

N = int(input())
A = list(map(int, input().split()))

A.sort(reverse=True)
count = 0
B = [A[0]]
for i in range(N-1):
    a = A[i+1]+count
    C = []
    for b in B:
        x = (b-a)//(N+1)
        C.append(x)
    s = sum(C)
    count += s
    new_B = []
    for i, b in enumerate(B):
        new_B.append(b+s-(N+1)*C[i])
    new_B.append(a+s)
    B = new_B

delta = max(min(B) - N, 0)
count += delta*N
for i in range(N):
    B[i] -= delta

while max(B) > N-1:
    tmp = -1
    for i in range(N):
        if B[i] > tmp:
            tmp = B[i]
            ind = i
    for i in range(N):
        if i == ind:
            B[i] -= N
        else:
            B[i] += 1
    count += 1

print(count)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))

ans = 0
while True:
    tmp = 0
    for i in range(n):
        k = -(-(a[i]+1)//n)-1
        tmp += k
        a[i] -= k*n
        a[i] -= k
    for i in range(n):
        a[i] += tmp
    if not tmp:
        break
    ans += tmp

print(ans)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))

a.sort()
ans = 0
while a[-1] >= n:
    k = -(-(a[-1]+1)//n)-1
    ans += k
    for i in range(n-1):
        a[i] += k
    a[-1] -= n*k
    a.sort()

print(ans)

Solution (Python):
def main():
    import sys,math
    input = sys.stdin.readline

    n = int(input())
    a = list(map(int,input().split()))

    res = 0
    judge = True
    for e in a:
        if e >= n:
            judge = False
            break
    if judge:
        print(res)
        return
    
    x = 0
    while True:
        if a[x] <= n-1:
            x += 1
            if x == n:
                x = 0
            continue

        p = (a[x]//n)
        a[x] -= p*n
        res += p

        judge = True
        for i in range(n):
            if i == x:
                continue
            a[i] += p
            if a[i] >= n:
                judge = False
        if judge:
            print(res)
            return
        
        x += 1
        if x == n:
            x = 0

if __name__ == '__main__':
    main()

Solution (Python):


n = int( input() )
da=list(map(int, input(). split()))
li=[da[i]%(n+1) for i in range(n)]
su=0
ans=0
for i in range(n):
	su+=da[i]
for i in range(n):
	div=(n-1)-(da[i]%(n+1))
	now=0
	ju=True
	for j in range(n):
		if (n+1+div+da[j])%(n+1)==n:
			ju=False
			break
		now+=(n+1+div+da[j])%(n+1)
	if ju and su >= now:
		
		ans = max(ans,now)
print(su-ans)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
ans = 0
while True:
    S = 0
    for i, a in enumerate(A):
        if a>=N:
            res = (a-N+1)//(N+1)+1
            A[i]-=res*(N+1)
            S+=res
    A = list(map(lambda x:x+S, A))
    ans+=S
    if all(a<N for a in A):
        break
print(ans)

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
    N = int(input())
    A = list(map(int,input().split()))
    
    A = sorted(A)
    K = 0
    while A[-1] >= N:
        M = A[-1]
        p = M//N
        K += p
        for i in range(N-1):
            A[i] += p
        A[-1] -= p*N
        A = sorted(A)
        
    print(K)
    
    
main()

Solution (Python):
N = int(input())
A = list(map(int, input().split()))

ans = 0

while max(A) >= N:
    maxIndex = A.index(max(A))
    cnt = A[maxIndex] // N
    for i in range(N):
        if i == maxIndex:
            A[i] -= N * cnt
        else:
            A[i] += cnt
    ans += cnt

print(ans)

Solution (Python):
N = int(input())
A = [int(x) for x in input().split()]
A.sort(reverse = True)
D = [0] * N
pos = 1
D[0] = A[0] // N
A[0] = A[0] % N
while D[pos] != D[(pos - 1) % N]:
  pre = (pos - 1) % N
  a = D[pre] - D[pos]
  A[pos] += a
  D[pos] = D[pre] + A[pos] // N
  A[pos] %= N
  pos = (pos + 1) % N
print(D[pos])

Solution (Python):

n = int(input())
a = [int(item) for item in input().split()]

itr = 0
max_a = max(a)
while max_a >= n:
    for i in range(n):
        if a[i] >= n:
            num = a[i] // n
            a[i] %= n
            for j in range(n):
                if i == j:
                    continue
                a[j] += num
            itr += num
    max_a = max(a)
print(itr)

Solution (Python):
N = int(input())
a = list(map(int, input().split()))

ans = 0
while True:
    sub = [0] * N
    for i in range(N):
        sub[i] = (a[i] - (N - 1) - 1) // N + 1
    sum_sub = sum(sub)
    if sum_sub == 0:
        break
    ans += sum_sub
    for i in range(N):
        a[i] -= N * sub[i] - (sum_sub - sub[i])

print(ans)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
ans = 0
while max(a) >= n:
  t = sum(x//n for x in a)
  for i in range(n):
    g = a[i]//n
    a[i] += t - g - g*n
  ans += t
print(ans)

Solution (Python):
import heapq


n = int(input())
a = list(map(int, input().split()))

cnt = 0
while True:
    max_a = max(a)
    max_ind = a.index(max_a)
    if max_a <= n-1:
        break
    decrease = max_a // n
    max_a = max_a % n
    for i in range(n):
        if i == max_ind:
            a[i] = max_a
        else:
            a[i] += decrease
    cnt += decrease
    
    min_a = min(a)
    min_ind = a.index(min_a)    
    if min_a <= n-1:
        continue
    decrease = min_a - (n - 1)
    for i in range(n):
        a[i] -= decrease
    cnt += decrease * n

print(cnt)

Solution (Python):
n=int(input())
arr=list(map(int,input().split()))
cnt=0
while 1:
  tmp=[]
  flag=0
  for i in range(n):
    val=arr[i]
    if val<=n-1:
      flag+=1
    if (val+1)%(n+1):
      times=(val+1)//(n+1)
    else:
      times=(val+1)//(n+1)
    arr[i]-=times*n
    tmp.append(times)
  if flag==n:
    break
  sums=sum(tmp)
  cnt+=sums
  for i in range(n):
    arr[i]+=(sums-tmp[i])
print(cnt)

Solution (Python):


import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline 

n = int(input())
a = [int(i) for i in readline().split()]

s = sum(a)


def isOK(k): 
    res = 0
    for ai in a:
        c = ai + k - (n-1)
        res += (c+n)//(n+1)
    if res == k: return True
    else: return False

for i in range(max(s-5000,0),s+2,1):
    if isOK(i):
        s = i
        break


print(s)

Solution (Python):
def check(N, A, n):
    for i in range(min(n, N)+1):
        x = n - i
        cnt = 0
        for a in A:
            if a + x >= N:
                cnt += -(-(a + x - N + 1)//(N+1))
        if cnt <= x:
            return True
    return False


def main():
    N = int(input())
    A = list(map(int, input().split()))
    lo = 0
    hi = int(1e18)
    while lo < hi:
        mid = (lo + hi) // 2
        if check(N, A, mid):
            hi = mid
        else:
            lo = mid + 1
    print(lo)


if __name__ == "__main__":
    main()

Solution (Python):
from numpy import*;n,a=open(0);n,a=int(n),int64(a.split());c,s=0,1
while s:b=a//n;s=sum(b);c+=s;a+=s-b*-~n
print(c)

Solution (Python):
n,*a=map(int,open(0).read().split())
c=0
while 1:
  m=max(a)
  if m<n:break
  t,f=m//n,1
  c+=t
  for i,b in enumerate(a):
    if b==m and f:
      a[i]%=n
      f=0
    else:
      a[i]+=t
print(c)

Solution (Python):
n,*a=map(int,open(0).read().split())
c=0
while 1:
  m=max(a)
  if m<n:break
  t,f=m//n,1
  c+=t
  for i,b in enumerate(a):
    if b==m and f:
      a[i]%=n
      f=0
    else:
      a[i]+=t
print(c)

Solution (Python):

from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007


def A():
    n = I()

    return


def B():
    n = I()

    return


def C():
    n = I()

    return


def D():
    n = I()

    return

def E():
    n = I()
    a = LI()
    ans = 0
    s = 1
    while s:
        b = [a[i]//n for i in range(n)]
        s = sum(b)
        a = [(a[i]%n+s-b[i]) for i in range(n)]
        ans += s
        
    print(ans)
    return


def F():
    n = I()

    return


if __name__ == "__main__":
    E()

Solution (Python):
import sys
input = sys.stdin.readline

import numpy as np

N = int(input())
A = np.array(input().split(), dtype=np.int64)

def test(T):
    
    
    
    
    
    x = np.maximum(0, (A + T + 1) // (N + 1))
    return x.sum() <= T


left = -1
right = 10 ** 18
while right > left + 1:
    mid = (left + right) // 2
    if test(mid):
        right = mid
    else:
        left = mid

for x in range(max(0, right - 5000), right + 1):
    if test(x):
        answer = x
        break

print(answer)

Solution (Python):

N = int(input())
a = list(map(int, input().split()))

b = [x for x in a]
c = 0
while max(b) >= N:
    i = max(range(N), key=lambda i: b[i])
    c += b[i] // N
    for j in range(N):
        if j != i:
            b[j] += b[i] // N
    b[i] %= N

ans = c


print(ans)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
k = 0
while max(A) > N - 1:
    S = 0
    for i in range(N):
        S += A[i] // N

    for i in range(N):
        A[i] = A[i] - (A[i] // N) * N + S - A[i] // N

    k += S

print(k)

Solution (Python):
import sys


def bisect_left_callable(fn, x, lo, hi):
    """
    lo から hi-1 のうち、fn の結果が x 以上となる、最も左の値
    bisect.bisect_left と同じ
    https://docs.python.org/ja/3/library/bisect.html
    :param callable fn:
    :param x:
    :param int lo: 最小値
    :param int hi: 最大値 + 1
    :return: lo <= ret <= hi
    """
    while lo < hi:
        mid = (lo + hi) // 2
        if fn(mid) < x:
            lo = mid + 1
        else:
            hi = mid
    return lo


sys.setrecursionlimit(100000)
INF = float('inf')

N = int(sys.stdin.readline())
A = list(map(int, sys.stdin.readline().split()))

MAX = 10 ** 20


def check(k):
    """
    :param int k:
    :return: k 回操作して条件を満たせば 0、k が大きすぎたらプラス、小さすぎたらマイナス
    """
    
    
    li = [a + k for a in A]
    sub = 0
    for a in li:
        sub += (a + 1) // (N + 1)
    return k - sub



ans = bisect_left_callable(check, 0, 0, MAX)

ans = max(0, ans - 10000)
while check(ans) != 0:
    ans += 1
print(ans)

Solution (Python):
def solve(A, N):
    ret = 0
    while max(A) > N-1:
        for i in range(N):
            if A[i] > N-1:
                t = A[i]//N
                for j in range(N):
                    if j == i:
                        A[j] -= t*N
                        continue
                    A[j] += t
                ret += t
    return ret
N = int( input())
A = list( map( int, input().split()))
ans = 0
for i in range(N):
    if A[i] > N-1:
        t = A[i]//N
        for j in range(N):
            if j == i:
                A[j] -= t*N
                continue
            A[j] += t
        ans += t
for i in range(N):
    if A[i] > N+1:
        t = A[i]//(N+1)
        if A[i] - t*(N+1) <= N-1:
            t -= 1
        A[i] -= t*(N+1)
        ans += t*(N+1)
print( ans + solve(A,N))

Solution (Python):
def solve(A, N):
    ret = 0
    while max(A) > N-1:
        for i in range(N):
            if A[i] > N-1:
                t = A[i]//N
                for j in range(N):
                    if j == i:
                        A[j] -= t*N
                        continue
                    A[j] += t
                ret += t
    return ret
N = int( input())
A = list( map( int, input().split()))
ans = 0
for i in range(N):
    if A[i] > N-1:
        t = A[i]//N
        for j in range(N):
            if j == i:
                A[j] -= t*N
                continue
            A[j] += t
        ans += t
for i in range(N):
    if A[i] > N+1:
        t = A[i]//(N+1)
        if A[i] - t*(N+1) <= N-1:
            t -= 1
        A[i] -= t*(N+1)
        ans += t*(N+1)
print( ans + solve(A,N))

Solution (Python):



def solve(n, a):

    c = [ai // (n + 1) for ai in a]
    r = sum(c)
    b = [a[i] + r - (n + 1) * c[i] for i in range(n)]
    ans = r

    min_b = min(b)
    if n < min_b:
        r = min_b - n
        b = [bi - r for bi in b]
        ans += r * n

    while True:
        max_b = -1
        j = -1
        for i in range(n):
            if max_b < b[i]:
                max_b = b[i]
                j = i
        if max_b < n:
            break
        ans += 1
        b[j] -= n + 1
        for i in range(n):
            b[i] += 1

    return ans


def main():
    n = input()
    n = int(n)
    a = list(map(int, input().split()))

    print(solve(n, a))


if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
a = [int(x) for x in input().split()]
ans = 0
while max(a)>=n:
    b = sum([x//n for x in a])
    ans += b
    a = [x%n+b-x//n for x in a]
print(ans)

Solution (Python):
N=int(input())
a=list(map(int,input().split()))

ans=0
cnt=0
B=[0 for i in range(N)]
for x in range(10**6):
    for i in range(N):
        if a[i]>=N:
            cnt+=a[i]//N
            B[i]+=a[i]//N
            a[i]%=N
    if cnt==0:
        print(ans)
        exit()
    else:
        ans+=cnt
        for j in range(N):
            a[j]+=(cnt-B[j])
        cnt=0
        B=[0 for k in range(N)]
print(ans)

Solution (Python):
N = int(input())
As = list(map(int, input().split()))

ans = 0
while True:
    Bs = [A//N for A in As]
    if sum(Bs) == 0:
        print(ans)
        break
    sumB = sum(Bs)
    ans += sumB
    As = [A%N + sumB - B for A, B in zip(As, Bs)]

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
p = 0
q = 0
for x in a:
    q += x
p = max(0, q-n*(n-1))
for y in range(p, q+1):
    z = 0
    for x in a:
        z += (x+y+1)//(n+1)
    if y >= z:
        print(y)
        break

Solution (Python):



import array
from bisect import *
from collections import *
import fractions
import heapq 
from itertools import *
import math
import random
import re
import string
import sys

N = int(input())
As = list(map(int, input().split()))

ans = 0
while True:
    taken = [0] * N
    for i, a in enumerate(As):
        taken[i] = a // N
        As[i] -= taken[i] * N
    
    
    

    taken_sum = sum(taken)
    if taken_sum == 0:
        break
    ans += taken_sum
    for i in range(N):
        As[i] += (taken_sum - taken[i])
print(ans)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))

ans = 0
while any([a>N-1 for a in A]):
    for i in range(N):
        if A[i] <= N-1: continue
        d = A[i] - (N-1)
        k = -(-d//N)
        A[i] -= k*N
        ans += k
        for j in range(N):
            if j==i: continue
            A[j] += k
print(ans)

Solution (Python):
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

n = ni()
a = list(li())

def judge(a: list, n: int):
    for ai in a:
        if ai >= n:
            return True
        
    return False

ans = 0
while judge(a, n):
    amax = max(a)
    argmax = a.index(amax)
    x = -( -(amax-(n-1)) // n)
    ans += x
    
    nex = [ai + x if i != argmax else ai - n*x for (i,ai) in enumerate(a)]
    a = nex
    
print(ans)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))

ans = 0
while any([a>N-1 for a in A]):
    A.sort()
    d = A[-1] - (N-1)
    k = -(-d//N)
    ans += k
    A[-1] -= k*N
    for i in range(N-1):
        A[i] += k
print(ans)

Solution (Python):
N=int(input())
A=[int(i) for i in input().split()]
ans=0
while max(A)>N-1:
    A.sort()
    t=A[-1]//N
    A[-1]=A[-1]-t*N
    ans+=t
    for i in range(N-1):
        A[i]+=t
print(ans)

Solution (Python):
N=int(input())
a=[int(i) for i in input().split()]
ans=0
while True:
  f=0
  for i in range(N):
    if a[i]>=N:
      d=a[i]//N
      ans+=d
      a[i]%=N
      for j in range(N):
        if i!=j:
          a[j]+=d
    else:
      f+=1
  if f==N:
    break
print(ans)

Solution (Python):
def inpl(): return [int(i) for i in input().split()]

N = int(input())
a = inpl()
a1 = a
c = []
while c != a:
    c = a
    b = [i // N for i in a]
    B = sum(b)
    a = [i % N + B - i // N for i in a]
print(sum(a1)-sum(c))

Solution (Python):
n = int(input())
a = [int(i) for i in input().split()]
def f(x):
	k = 0
	for i in a: k+=(i+x-n)//(n+1)+1
	return k<=x
for i in range(max(0,sum(a)-n*(n-1)),51*(10**16)):
	if f(i): break
print(i)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
cnt = 0
f = True
while f:
    f = False
    for i in range(N):
        if A[i] >= N - cnt:
            q, m = divmod(A[i] + cnt + 1, N+1)
            A[i] = m - cnt - 1
            cnt += q
            f = True
print(cnt)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))

ans = 0
while True:
    if all(map(lambda x: x < N, A)):
        break

    temp = 0
    num = [0] * N

    for i in range(N):
        temp += A[i] // N
        num[i] = A[i] // N
        A[i] %= N

    ans += temp
    A = [a + temp - n for a, n in zip(A, num)]

print(ans)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
ans = 0

M = max(a)
m = min(a)
while M > n - 1:
	Mi = a.index(M)
	div = M // n
	for i in range(n):
		if i == Mi:
			a[i] -= n * div
		else:
			a[i] += div
	ans += div
	M = max(a)
	m = min(a)

print(ans)

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
ans = 0
while True:
  for i in range(n):
    b = a[i]
    c = b//n
    b = b%n
    a[i] = b
    for j in range(n):
      if j == i:
        continue
      a[j] += c
    ans += c
  if max(a) < n:
    break
print(ans)

Solution (Python):
from sys import exit, setrecursionlimit, stderr
from functools import reduce
from itertools import *
from collections import *
from bisect import bisect
 
def read():
  return int(input())
 
def reads():
  return [int(x) for x in input().split()]

N = read()
A = [a-(N-1) for a in reads()]

def possible(x): 
  return sum((a + x + N) // (N+1) for a in A if a + x > 0) <= x

l = -1; r = 10 ** 19
while r - l > 1:
  m = (l + r) // 2
  if possible(m):
    r = m
  else:
    l = m
ans = min(x for x in range(l - 10*N**2, l + 10*N**2) if possible(x))
print(ans)

Solution (Python):
from sys import exit, setrecursionlimit, stderr
from functools import reduce
from itertools import *
from collections import *
from bisect import bisect
 
def read():
  return int(input())
 
def reads():
  return [int(x) for x in input().split()]

N = read()
A = [a-(N-1) for a in reads()]

def possible(x): 
  return sum((a + x + N) // (N+1) for a in A if a + x > 0) <= x


l = -1; r = 10 ** 19
while r - l > 1:
  m = (l + r) // 2
  if possible(m):
    r = m
  else:
    l = m
ans = min(x for x in range(l - 10*N**2, l + 10*N**2) if possible(x))
print(ans)

Solution (Python):
n = int(input())
aaa = list(map(int, input().split()))
ans = 0
while True:
    flag = True
    for i, a in enumerate(aaa):
        if a >= n:
            flag = False
            k = (a - n) // n + 1
            ans += k
            for j in range(n):
                if i == j:
                    aaa[j] -= k * n
                else:
                    aaa[j] += k
    if flag:
        break
print(ans)

Solution (Python):
N = int(input())
a = list(map(int,input().split()))

ans = 0
while any(map(lambda x: x >= N, a)):
    a.sort(reverse=True)
    k = (a[0] - N)//N + 1
    ans += k
    a[0] -= k*N
    for i in range(1,N):
        a[i] += k

print(ans)

Solution (Python):
n=int(input())
a=map(int,raw_input().split(' '))
n=len(a)
ans=0
while 1 :
    k=0
    for i in xrange(1,n) :
        if a[i]>a[k] :
            k=i
    if a[k]<n :
        break
    t=a[k]/n
    ans+=t
    for i in xrange(n) :
        a[i]+=t
    a[k]-=t*(n+1)

print ans

Solution (Python):
def sim(n, a):
    ans = 0
    for i in range(n):
        div = a[i] / n
        ans += div
        for j in range(n):
            a[j] += div
            if i == j:
                a[j] -= div + div * n
    return ans, a

    

N = input()
A = map(int, raw_input().split())

final_ans = 0
ans, a = sim(N, A)
final_ans = ans
while True:
    ans, a = sim(N, a)
    if ans == 0:
        break
    else:
        final_ans += ans

print final_ans

Solution (Python):
n = int(input())
a = [int(i) for i in input().split()]

ans = 0
while True:
    c = 0
    exist = False
    for i in range(n):
        if a[i] > n-1:
            c = a[i] // n
            a[i] %= n
            for j in range(n):
                if j != i:
                    a[j] += c
            ans += c
            exist = True
    if not exist:
        break

print(ans)

Solution (Python):
import numpy as np
import math

N = int(input())
a = list(map(int, input().split()))
a = np.asarray(a)

ans = 0
while(1):
    if a.max() < N:
        break
    
    for i in range(a.size):
        if a[i] > (N-1):
            diff = math.ceil((a[i] - (N-1))/N)
            
            a[i] -= N * diff
            a[:i] += 1 * diff
            a[i+1:] += 1 * diff

            ans += diff
print(ans)

Solution (Python):
n,*A=map(int,open(0).read().split())
ans = 0
while 1:
    A.sort(reverse=1)
    if A[0] > A[-1]+n:
        p = (A[0] - A[-1]) // (n+1)
        for j in range(n):
            A[j] += p
        A[0] -= p*(n+1)
        ans += p
    else:
        break
if max(A) >= 2*n:
    ans += (max(A) - 2*n)*n
    ma = max(A) - 2*n
    for i in range(n):
        A[i] -= ma
while max(A) >= n:
    for i in range(n):
        A[i] += 1
    A[A.index(max(A))] -= n+1
    ans += 1
print(ans)

Solution (Python):
def check(_a):
    for i in range(0, len(_a)):
        if N <= _a[i]:
            return True
    else:
        return False


if __name__ == '__main__':
    N = int(input())
    a = list(map(int, input().split()))

    divide_num = [0 for i in range(N)]
    ans = 0
    while check(a):
        sum_num = 0
        for i in range(0, N):
            tmp = a[i] // N
            divide_num[i] = tmp
            sum_num += tmp
            ans += tmp

        a = [(a[i] % N + sum_num - divide_num[i]) for i in range(0, N)]

    print(ans)

Solution (Python):

import numpy as np
N = int(input())
A = np.array([int(i) for i in input().split()])

K = 0
while max(A) >= N:
    p = A // N
    S = sum(p)
    q = S - p
    A = A - p * N + q
    K += sum(p)

print(K)

Solution (Python):


'''
27 0 0 0 0 0 0 ->(3)
6 3 3 3 3 3 3
'''

N=int(input())

mozir=input()
hyo=mozir.split(' ')
a=[int(mono) for mono in hyo]

ans=0
tgt=0
while True:
    dist=a[tgt]-(N-1)
    kai=dist//N + (1 if dist%N != 0 else 0)
    ans+=kai

    for idx in range(0,N,1):
        if idx != tgt:
            a[idx]+=kai
        else:
            a[idx]=a[idx]%N

    if tgt==N-1:
        turuturup=True
        for idx in range(0,N,1):
            if a[idx]>=N:
                turuturup=False
                break

        if turuturup:
            break
    tgt=(tgt+1)%N

print(ans)

Solution (Python):
from heapq import*
n=int(input())
a=[-int(e)for e in input().split()]
r=0
heapify(a)
while 1:
	x=heappop(a)
	if x>-n:break
	c=-x//n
	x+=c*n
	for i in range(n-1):a[i]-=c
	heappush(a,x)
	r+=c
print(r)