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

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
	a=[e-c for e in a]
	heappush(a,x)
	r+=c
print(r)

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
	a=[e-c for e in a]
	heappush(a,x)
	r+=c
print(r)

Solution (Python):
import sys




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


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


def read_str_list():
    return input().split()


def read_str():
    return input()


def ceil_frac(a, b):
    return (a + b - 1) // b


def solve():
    n = read_int()
    a = read_int_list()
    s = sum(a)
    res = 0
    x = s - n * n
    if x > 0:
        res += x
        for j in range(n):
            a[j] += x
        l = 0
        r = 10 ** 20
        while r - l > 1:
            m = (l + r) // 2
            decrements = 0
            for j in range(n):
                if a[j] >= m:
                    decrements += ceil_frac(a[j] - m, n + 1)
            possible = decrements <= x
            if possible:
                r = m
            else:
                l = m
        t = 0
        for j in range(n):
            if a[j] >= r:
                u = ceil_frac(a[j] - r, n + 1)
                a[j] -= u * (n + 1)
                t += u
        for _ in range(x - t):
            j0 = 0
            for j in range(n):
                if a[j] > a[j0]:
                    j0 = j
            a[j0] -= n + 1
    while max(a) >= n:
        for j in range(n):
            a[j] += 1
        j0 = 0
        for j in range(n):
            if a[j] > a[j0]:
                j0 = j
        a[j0] -= n + 1
        res += 1
    return res


def main():
    res = solve()
    print(res)


if __name__ == '__main__':
    main()

Solution (Python):

import copy

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

	print(calc(a))
	
def calc(temp):
	i = 0
	
	n = len(temp)
	while max(temp) >= n:
		sum_count = sum([x // n for x in temp])
		i += sum_count
		temp = [x - (x // n) * n + sum_count - (x // n) for x in temp]
		

	return i

if __name__ == '__main__':
	main()

Solution (Python):
N = int(input())
A = [int(x) for x in input().split()]
ans = 0
while True:
    max_A = max(A)
    if max_A < N:
        break
    S = sum(a // N for a in A)
    ans += S
    for i in range(N):
        A[i] = A[i] - (A[i] // N) * N + (S - (A[i] // N))
print(ans)

Solution (Python):
import heapq
from math import ceil

n = int(input())
arr = list(map(int,input().split()))
arr = [-x for x in arr]
heapq.heapify(arr)
limit = -n+1

act = 0
while arr[0] < limit:
    x = arr[0]
    req = ceil((limit-x)/n)
    x+= req*(n+1)
    limit+= req
    act+= req
    heapq.heapreplace(arr, x)
print(act)

Solution (Python):
n = int(raw_input())
a = [int(x) for x in raw_input().split()]


def f(k):
    s = 0
    for ai in a:
        s += max(0, (ai - (n - 1 - k) + n) // (n + 1))
    return s <= k


lb = 0
ub = 500000000000000001

while lb + 1 < ub:
    mid = (lb + ub) // 2
    if f(mid):
        ub = mid
    else:
        lb = mid

m = lb + 1
i = m - 1
while i >= m - 50:
    if f(i):
        m = i
    i -= 1

print m

Solution (Python):
n = int(raw_input())
a = map(int, raw_input().split())
ans = 0
plus = [0] * n
while True:
    s = 0
    for i in xrange(n):
        ans += a[i] / n
        plus[i] = a[i] / n
        s += a[i] / n
        a[i] -= n * (a[i] / n)
    if s == 0:
        break
    for i in xrange(n):
        a[i] += s - plus[i]
print ans

Solution (Python):

import sys

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


def solve_mini():
    def argmax():
        Amax = max(As)
        for j, value in enumerate(As):
            if Amax == value:
                return j

    for i in range(10 ** 20):
        if max(As) < N:
            break
        arg = argmax()
        for j in range(N):
            if j == arg:
                As[j] -= N
            else:
                As[j] += 1
    return i


i = 0
if sum(As) > N * N:
    over = sum(As) - N * N
    
    for j in range(N):
        As[j] += over
    
    real_i = 0
    for j in range(N):
        diff = (As[j] // (N+1) - 1)
        As[j] -= diff * (N+1)
        real_i += diff
    
    
    for j in range(N):
        As[j] += (real_i - over)
    
    i += real_i

i += solve_mini()

print(i)

Solution (Python):

INF = 10 ** 20
MOD = 10 ** 9 + 7


def II(): return int(input())


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


def read():
    N = II()
    a = ILI()
    return (N, a)


def check_a(N, a):
    for _a in a:
        if _a >= N:
            return False
    return True


def solve(N, a):
    ans = 0
    while True:
        if check_a(N, a):
            break

        l_div, l_mod = [None] * N, [None] * N
        for i, _a in enumerate(a):
            l_div[i], l_mod[i] = divmod(_a, N)

        sum_div = sum(l_div)
        for i in range(N):
            a[i] = l_mod[i] + sum_div - l_div[i]

        ans += sum_div

    return ans


def main():
    params = read()
    print(solve(*params))


if __name__ == "__main__":
    main()

Solution (Python):
def spaceinput():
    s=input()
    if len(s)==0:
        return False

    return list(map(int,s.split(" ")))


N=int(input())
a=sorted(spaceinput(),reverse=True)

k=0
count=0
N=len(a)
while True:
    
    s=int(a[k]/N)
    a[k]-=N*s
    for j in range(N):
        if k!=j:
            a[j]+=s
    count+=s
    if max(a)<N:
        break
    k=(k+1)%N


print(count)

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

k = 0
amax = max(a)
while n - 1 < amax:
    for i in range(n):
        if n - 1 < a[i]:
            d = a[i] // n
            k += d
            for j in range(n):
                if j == i:
                    a[j] -= d * n
                else:
                    a[j] += d
    amax = max(a)

print(k)

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

def g(mid):
	for i in range(n):
		if f(mid * n + i):
			return True
	return False	

def f(mid):
	over = 0
	for x in a:
		over += (x + mid + 1) // (n + 1)
	return over <= mid

ok = 10 ** 30
ng = -1

while ok - ng > 1:
	mid = (ok + ng) // 2
	if g(mid):
		ok = mid
	else:
		ng = mid

for i in range(n):
	if f(ok * n + i):
		print(ok * n + i)
		break

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

step = 0
while max(*arr) >= n:
    arr = sorted(arr)
    cnt = arr[-1] // n
    arr[-1] -= cnt * n
    for i in range(n - 1):
        arr[i] += cnt
    step += cnt

print(step)

Solution (Python):

import numpy as np


N = int(input())
a = np.array([int(n) for n in input().split()])

total = 0

while a.max() >= N:
    add = (a//N).sum() - (a//N)
    total += (a//N).sum()
    a = a%N
    a += add

print(total)

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


def judge(k):
  cnt = sum(max((a+k+1)//(N+1),0) for a in A)
  return cnt <= k

if judge(0):
  print(0)
  exit(0)

low = 0
high = 10**18

while low + 1 < high:
  mid = (low+high)//2
  if judge(mid):
    high = mid
  else:
    low = mid

r = high

for i in range(max(r-N*N,0),r):
  if judge(i):
    r = i
    break

print(r)

Solution (Python):


def main():
    N = int(input())
    a = sorted(map(int, input().split()))

    
    tK = 0
    while max(a) >= N:
        K = 0
        for i in range(N):
            if a[i] >= N:
                k = a[i] // N
                a[i] -= (N + 1) * k
                K += k
        for i in range(N):
            a[i] += K
        tK += K
    return tK


print(main())

Solution (Python):


def main():
  N = int(raw_input())
  A = map(lambda s: int(s) - N + 1, raw_input().split())
  K = 0
  while True:
    c = []
    for i in xrange(N):
      if A[i] > 0:
        c.append(i)
    if not c:
      break
    s = 0
    for i in c:
      k = (A[i] + (N - 1)) / N
      A[i] -= k * (N + 1)
      s += k
    for i in xrange(N):
      A[i] += s
    K += s
  print K

main()

Solution (Python):


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

K = 0
R = 0

for i in range(N):
    q = a_list[i] // (N+1)
    if q >= 2:
        a_list[i] -= (q-1)*(N+1)
        R += (q-1)
        K += (q-1)

if R >= N:
    K += (R - N)*N
    R = N

for i in range(N):
    a_list[i] += R
    
while max(a_list) >= N:
    p = a_list.index(max(a_list))
    K += 1
    for i in range(N):
        if i == p:
            a_list[i] -= N
        else:
            a_list[i] += 1
    
print(K)

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


def check(lst):
    for i in range(N):
        if lst[i] >= N:
            return False
    return True

ans = 0
while not check(L):
    cnts = []
    for l in L:
        cnts.append(l // N)
    sum_cnts = sum(cnts)
    ans += sum_cnts
    for i in range(N):
        L[i] = L[i] - cnts[i] * N + sum_cnts - cnts[i]
print(ans)

Solution (Python):
n = int(raw_input())
a = map(int, raw_input().split())
ret = 0
while True:
	mp = 0
	for i in range(n):
		if a[i] > a[mp]:
			mp = i
	if a[mp] < n:
		break
	c = a[mp] // n
	ret += c
	a[mp] = a[mp] % n
	for i in range(n):
		if i != mp:
			a[i] += c
print ret

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
gosa = 1.0 / 10**9
mod = 10**9 + 7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()


def main():
    N = I()
    a = LI()
    r = 0
    f = True
    while f:
        f = False
        for i in range(N):
            if a[i] < N:
                continue
            f = True
            t = a[i] // N
            r += t
            for j in range(N):
                if i == j:
                    continue
                a[j] += t
            a[i] -= t * N
    return r



print(main())