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
input = sys.stdin.readline
N = int(input())
a = list(map(int, input().split()))

def check(x):
  b = a[: ]
  for i in range(N):
    b[i] += x
  c = 0
  for i in range(N):
    t = max(0, b[i] - (N - 1))
    c += -(-t // (N + 1))
  return c <= x

ok = sum(a)
ng = -1
while ok - ng > 1:
  m = (ok + ng) // 2
  isok = 0
  for i in range(N):
    if check(m + i):
      isok = 1
      break
  if isok: ok = m
  else: ng = m
for i in range(N):
  if check(ok + i):
    print(ok + i)
    break

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
ans = 0
flag = True
while flag:
    flag = False
    for i in range(N):
        if A[i] >= N:
            count = A[i] // N
            ans += count
            for j in range(N):
                A[j] += count
            A[i] -= count
            A[i] %= N
            flag = True

print(ans)

Submission status: Accepted

Solution code:
# https://pitsbuffersolution.com/compro/atcoder/arc079e.php

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

    tot = sum(a)

    def is_ok(k):
        b = (x + k - (N - 1) for x in a)

        cnt = 0
        for x in b:
            cnt += (x + N) // (N + 1)

        return cnt <= k

    ret = 0

    k = max(0, tot - N * (N - 1))
    while k <= tot:
        if is_ok(k):
            ret = k
            break
        k += 1

    print(ret)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import heapq
import math
N = int(input())
a = list(map(int, input().split()))


hq = [-x for x in a]
heapq.heapify(hq)
ans = 0
while(hq[0] < 1-N):
    x = heapq.heappop(hq)
    x = -x
    k = x // N
    x %= N
    ans += k

    for i in range(N-1):
        hq[i] -= k
    heapq.heappush(hq, -x)

print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

N = int(input())
A = list(map(int, input().split()))
lb, ub = max(0, sum(A) - N * (N-1)), sum(A)
for S in range(lb, ub+1):
    s = 0
    for a in A:
        x = (a + S) // (N + 1)
        if 0 <= a - (N + 1) * x + S <= N - 1:
            s += x
        else:
            break
    else:
        if S == s:
            print(S)
            break

Submission status: Accepted

Solution code:
from itertools import permutations
import sys

sys.setrecursionlimit(10 ** 6)
from bisect import *
from collections import *
from heapq import *

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

def main():
    def ok(m):
        cnt=0
        for a in aa:
            if a+m<=n-1:continue
            cnt+=(a+m+1)//(n+1)
            if cnt>m:return False
        return True

    n=II()
    aa=LI()
    mx=max(aa)
    cur=0
    b=0
    while 1:
        cur=0
        for i in range(n):
            c=(aa[i]+b+1)//(n+1)
            c=max(c,0)
            aa[i]-=(n+1)*c
            cur+=c
        if cur==0:break
        b+=cur
    for m in range(b,b+10**5):
        if ok(m):break
    print(m)

main()

Submission status: Accepted

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

x = sum(a)

a.sort(reverse = True)

D = [0] * N

ans = 0
flag = True
while flag:
  k = 0
  for i in range(N):
    k += a[i] // N
  for i in range(N):
    D[i] = a[i] % N + (k - a[i] // N)
  flag = False
  ans += k
  for i in range(N):
    a[i] = D[i]
    if D[i] >= N:
      flag = True

print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
a = list(map(int, input().split()))
a.sort()
def check(x):
  b = [0] * N
  for i in range(N):
    b[i] = a[i] + x
  t = x
  for i in range(N):
    if t * (N + 1) >= b[i] - (N - 1):
      t -= -(-(b[i] - (N - 1)) // (N + 1))
    else:
      return False
  return True


ok = sum(a)
ng = -1

while ok - ng > 1:
  m = (ok + ng) // 2
  isok = 0
  for i in range(50):
    if check(m + i):
      isok = 1
      break
  if isok: ok = m
  else: ng = m


for i in range(50):
  if check(ok + i):
    print(ok + i)
    break

Submission status: Accepted

Solution code:
N, *A = map(int, open(0).read().split())
ans = 10**30
for i in range(N):
  l = -1
  r = 10**30
  while l+1<r:
    s = (l+r)//2
    x = s*N+i
    m = sum((c+x-N+1+N)//(N+1) for c in A)
    if m>x:
      l = s
    else:
      r = s
  x = r*N+i
  m = sum((c+x-N+1+N)//(N+1) for c in A)
  if m==x:
    ans = min(ans,x)
print(ans)

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin
sys.setrecursionlimit(10**9)
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

import numpy as np

n = ni()
a = np.array(na())

ans = 0

while max(a) >= n:
	for i in range(n):
		if a[i] >= n:
			b = a[i]//n
			a[i] = a[i]-b*n-b
			a+=b
			ans+=b

print(ans)

Submission status: Accepted

Solution code:
printn = lambda x: print(x,end='')
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True # and False
BIG = 10**18
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)

n = inn()
a = inl()

cnt = 0
while max(a)>n-1:
    sm = 0
    for i in range(n):
        j = a[i]//n
        a[i] -= j*(n+1)
        sm += j
    a = [x+sm for x in a]
    cnt += sm
#
print(cnt)
#ddprint(foo(0))
#ddprint(foo(1))

Submission status: Accepted

Solution code:
def main():
    N,*A=map(int, open(0).read().split())
    ans = 0
    while any(a>=N for a in A):
        x = sum(a//N for a in A)
        A = [x-a//N+a%N for a in A]
        ans += x
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
cnt=0
while max(a)>=n:
  s=0
  for i in range(n):
    s+=a[i]//n
  for i in range(n):
    a[i]+=(s-a[i]//n)-(a[i]//n)*n
  cnt+=s
print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
a.sort(reverse=True)
cnt = [0]*n
ans = 0
for i in range(100000):
  j = i%n
  if a[j]<n:
    continue
  x = a[j]//n
  y = a[j]%n
  a[j] = y
  ans += x
  for k in range(n):
    if j == k:
      continue
    a[k] += x
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(a) for a in input().split()]
ans = 0
while max(A) >= N:
    for i in range(N):
        if A[i] >= N:
            t = A[i] // N
            A = [A[j] - t * N if j==i else A[j] + t for j in range(N)]
            ans += t

print(ans)

Submission status: Accepted

Solution code:
#23:50
n = int(input())
a = list(map(int,input().split()))
ans = 0
while True:
  now = 0
  cnt = [0 for _ in range(n)]
  for i in range(n):
    #(a[i] - (n-1) - 1) // n + 1
    tmp = a[i]//n
    cnt[i] += tmp
    now += tmp
    a[i] -= tmp * n
  for i in range(n):
    a[i] += now - cnt[i]
  ans += now
  if now == 0:
    print(ans)
    break

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N = INT()
A = LIST()

a = max(A)
cnt = 0
while a >= N:
    d, m = divmod(a, N)
    mxidx = A.index(a)
    for i in range(N):
        if i == mxidx:
            A[i] = m
        else:
            A[i] += d
    cnt += d
    a = max(A)
print(cnt)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
a = list(map(int, input().split()))
if max(a) < N:
  print(0)
  exit(0)
ng = 0
ok = max(a) * N
def check(x):
  c = 0
  for i in range(N):
    c += max(-(-(a[i] + x - N + 1) // (N + 1)), 0)
  return x >= c

while ok - ng > 1:
  m = (ok + ng) // 2
  f = 1
  for i in range(N):
    if check(m + i):
      ok = m
      f = 0
      break
  if f: ng = m
for i in range(N):
  if check(ok + i):
    print(ok + i)
    break

Submission status: Accepted

Solution code:
def examC():
    ans = 0
    print(ans)
    return

def examD():
    ans = 0
    print(ans)
    return

def examE():
    N = I()
    A = LI()
    ans = 0
    while(True):
        cur = 0
        for i in range(N):
            cur += A[i]//N
        for i in range(N):
            A[i] = A[i]%N + cur-A[i]//N
        ans += cur
        if cur==0:
            break
    print(ans)
    return

def examF():
    ans = 0
    print(ans)
    return

import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LFI(): return list(map(float,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
alphabet = [chr(ord('a') + i) for i in range(26)]

if __name__ == '__main__':
    examE()

"""

"""

Submission status: Accepted

Solution code:
import sys
N = int(input())
As = list(map(int, input().split()))
def f(K):
    c = 0
    for a in As:
        c += (a+K+1)//(N+1)
#    print(K, K-c)
    return c<=K
def f2(K):
    c = 0
    for a in As:
        c += (a+K+1)//(N+1)
#    print(K, K-c)
#    print(K, c)
    return c<=K
for a in As:
    if a>=N:
        break
else:
    print(0)
    sys.exit()
mn, mx = 0, 10**18
idx = mx//2
while mx-mn>1:
    if f(idx):
        mx, idx = idx, (idx+mn)//2
        continue
    mn, idx = idx, (mx+idx)//2
#print(idx+1)
mn = max(1, idx-10**3)
for i in range(mn, mn+10**3+2):
    if f2(i):
        print(i)
        break

Submission status: Accepted

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

sys.setrecursionlimit(2000000)


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

Solution code:
N = int(input())
A = [int(a) for a in input().split()]

ans = 0
A.sort(reverse=True)
while A[0] >= N:
    num = A[0] // N
    ans += num
    A[0] %= N
    for i in range(1, N):
        A[i] += num
    A.sort(reverse=True)

print(ans)

Submission status: Accepted

Solution code:

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

        #print (kai,num)
        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 (l,r)

print (want(r))

Submission status: Accepted

Solution code:
import sys
import numpy as np

stdin = sys.stdin

ri = lambda: int(rs())
rl = lambda: list(map(int, stdin.readline().split()))  # applies to only numbers
rs = lambda: stdin.readline().rstrip()  # ignores trailing space

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)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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
        # print(ps,sumk,ans,aa)
        ps = sumk
    print(ans)

main()

Submission status: Accepted

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

Submission status: Accepted

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

    #a,bには下限と上限、valueにはOKな範囲のときはTrue、そうでないときにFalseを返す関数を入れる
    #limは下限を知りたいときはlower、上限を知りたいときはupperを入れる
    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()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#import numpy as np
N = int(input())
#a = np.array(list(map(int,input().split()))).astype(np.int64)
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)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
##create with UTF-8

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:
		#print(now)
		ans = max(ans,now)
print(su-ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

#A
def A():
    n = I()

    return

#B
def B():
    n = I()

    return

#C
def C():
    n = I()

    return

#D
def D():
    n = I()

    return
#E
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

#F
def F():
    n = I()

    return

#Solve
if __name__ == "__main__":
    E()

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

import numpy as np

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

def test(T):
    # T回操作をやるとして上手くいくか
    # ひとつひとつの数は、何もしないとa + T になってしまう
    # 1回やるとそこから N+1 減る。最低回数が分かる。
    # (a + T - N + 1) / (N+1) の切り上げ
    # (a + T + 1) // (N+1)
    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)

Submission status: Accepted

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

Submission status: Accepted

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