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



Sample input:
4
3 3 3 3


Sample output:
0

Submission status: Accepted

Solution code:
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 が大きすぎたらプラス、小さすぎたらマイナス
    """
    # k を全体に足す
    # N + 1 をちょうど 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)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3


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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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)]

Submission status: Accepted

Solution code:
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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

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
    # print("===")
    # print(As)
    # print(taken)

    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)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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): # possible at exactly x-th turn
  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)

Submission status: Accepted

Solution code:
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): # possible in x turns exactly
  return sum((a + x + N) // (N+1) for a in A if a + x > 0) <= x
# possible(x) -> possible(x+N)

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)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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

Submission status: Accepted

Solution code:
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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:

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)

Submission status: Accepted

Solution code:
#coding=UTF-8

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/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)

Submission status: Accepted

Solution code:
import sys

# sys.stdin = open('e1.in')


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

Submission status: Accepted

Solution code:
# encoding:utf-8
import copy

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

	print(calc(a))
	
def calc(temp):
	i = 0
	# print(temp)
	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]
		#print(temp)

	return i

if __name__ == '__main__':
	main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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

Submission status: Accepted

Solution code:
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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
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
    # print(over)
    for j in range(N):
        As[j] += over
    # print(As)
    real_i = 0
    for j in range(N):
        diff = (As[j] // (N+1) - 1)
        As[j] -= diff * (N+1)
        real_i += diff
    # print(real_i, over)
    # print(As)
    for j in range(N):
        As[j] += (real_i - over)
    # print(As)
    i += real_i

i += solve_mini()

print(i)

Submission status: Accepted

Solution code:
# coding: utf-8
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()

Submission status: Accepted

Solution code:
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:
    #print(a)
    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)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
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)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8

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

    # a[i] >= N な項は少なくともa[i]//N 回選ばれる
    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())

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
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()

Submission status: Accepted

Solution code:
# E

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)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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

Submission status: Accepted

Solution code:
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())

Submission status: Runtime Error

Solution code:
N, *A = [int(_) for _ in open(0).read().split()]
sumB = max(0, sum(A) - N * (N - 1))
while True:
    B = [0 - (-(a + x + 1 - N)) // (N + 1) for a in A]
    if sumB == sum(B):
        break
    sumB += 1
print(sumB)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import sys


def count_linear_process(lis):
    length = len(lis)
    return [l // length for l in lis]


def linear_process(lis):
    length = len(lis)
    return [l % length for l in lis]


def check_end(lis):
    return max(lis) < len(lis)


def add_processed(lis, counts):
    sum_cnt = sum(counts)
    return [l + sum_cnt - cnt for l, cnt in zip(lis, counts)]


def update(lis, n_process=0):
    if check_end(lis):
        return n_process
    next_lis = linear_process(lis)
    counts = count_linear_process(lis)
    return update(
        add_processed(next_lis, counts), 
        n_process + sum(counts)
    )


def solve(N: int, a: "List[int]"):
    print(update(a))
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    a = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, a)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
from numpy import int64, divmod

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)

Submission status: Runtime Error

Solution code:
from numpy import int64, divmod

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

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

print(ans)

Submission status: Runtime Error

Solution code:
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 _ in range(n-1):a[i]-=c
	heappush(a,x)
	r+=c
print(r)

Submission status: Runtime Error

Solution code:
#include <cassert>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif
#pragma comment(linker, "/STACK:1024000000,1024000000") 
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define EPS 1e-10
#define set0(x) memset((x),0,sizeof(x))
#define setINF(x) memset((x),63,sizeof(x))
using namespace std;
int n;
long long a[55];
inline int mx(){
	int res=0;
	for(int i=1;i<n;i++){
		if(a[i]>a[res])res=i;
	}
	return res;
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
        cin>>n;
	for(int i=0;i<n;i++){
		cin>>a[i];
	}
	int t=0;
	long long ans=0;
	while(a[t=mx()]>=n){
		ans+=a[t]/n;
		for(int i=0;i<n;i++){
			if(i==t)continue;
			a[i]+=a[t]/n;
		}
		a[t]%=n;
	}
	cout<<ans<<endl;
	return 0;
}