Problem:
Score : 600 points

Problem Statement

We have a sequence of length N consisting of non-negative integers. Consider performing the following operation on this sequence until the largest element in this sequence becomes N-1 or smaller. (The operation is the same as the one in Problem D.)

Determine the largest element in the sequence (if there is more than one, choose one). Decrease the value of this element by N, and increase each of the other elements by 1.

It can be proved that the largest element in the sequence becomes N-1 or smaller after a finite number of operations.

You are given the sequence a_i. Find the number of times we will perform the above operation.

Constraints

2 ≤ N ≤ 50

0 ≤ a_i ≤ 10^{16} + 1000

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the number of times the operation will be performed.

Sample Input 1

4
3 3 3 3

Sample Output 1

0

Sample Input 2

3
1 0 3

Sample Output 2

1

Sample Input 3

2
2 2

Sample Output 3

2

Sample Input 4

7
27 0 0 0 0 0 0

Sample Output 4

3

Sample Input 5

10
1000 193 256 777 0 1 1192 1234567891011 48 425

Sample Output 5

1234567894848

Solution in C# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;

class Program
{
    static void Main(string[] args)
    {
        var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
        Console.SetOut(sw);
        Solve();
        Console.Out.Flush();
    }
    static void Solve()
    {
        var N = sc.ReadInt();
        var A = sc.ReadLongArray(N);
        var seg = SegmentTreeLazy.CreateRangeAddSum(N);
        for (int i = 0; i < N; i++)
        {
            seg.Set(i, A[i]);
        }
        seg.Build();

        var ans = 0L;
        while (true)
        {
            var ok = true;

            for (int i = 0; i < N; i++)
            {
                // 減らす
                var a = seg.Query(i, i + 1);
                var k = a / N;
                if (k == 0) continue;
                ans += k;
                seg.Update(i, i + 1, -N * k);
                // 増やす
                if (i > 0) seg.Update(0, i, k);
                if (i < N - 1) seg.Update(i + 1, N, k);
                ok = false;
            }

            if (ok) break;
        }
        Console.WriteLine(ans);
    }

    class SegmentTreeLazy
    {
        private int N;
        private long[] Node;
        private long[] Lazy;
        private Func<long, long, long> Fx;
        private Func<long, long, long> Fm;
        private Func<long, long, long> Fa;
        private long Ex;
        private long Em;

        public SegmentTreeLazy(int n, Func<long, long, long> fx, long ex, Func<long, long, long> fm, long em, Func<long, long, long> fa)
        {
            Fx = fx;
            Ex = ex;
            Fm = fm;
            Em = em;
            Fa = fa;
            N = 1;
            while (N < n) N <<= 1;
            Node = new long[2 * N - 1];
            Lazy = new long[2 * N - 1];
        }
        public void Set(int i, long value)
        {
            Node[i + N - 1] = value;
        }
        public void Build()
        {
            for (int i = 0; i < Node.Length; i++)
            {
                Lazy[i] = Em;
            }
            for (int k = N - 2; k >= 0; k--)
            {
                Node[k] = Fx(Node[2 * k + 1], Node[2 * k + 2]);
            }
        }
        private void Eval(int k)
        {
            if (Lazy[k] == Em) return;
            if (k < N - 1)
            {
                Lazy[k * 2 + 1] = Fm(Lazy[k * 2 + 1], Lazy[k]);
                Lazy[k * 2 + 2] = Fm(Lazy[k * 2 + 2], Lazy[k]);
            }
            // 自身を更新
            Node[k] = Fa(Node[k], Lazy[k]);
            Lazy[k] = Em;
        }
        private void Update(int a, int b, long value, int k, int l, int r)
        {
            Eval(k);
            if (a <= l && r <= b)
            {  // 完全に内側の時
                Lazy[k] = Fm(Lazy[k], value);
                Eval(k);
            }
            else if (a < r && l < b)
            {
                Update(a, b, value, k * 2 + 1, l, (l + r) / 2);  // 左の子
                Update(a, b, value, k * 2 + 2, (l + r) / 2, r);  // 右の子
                Node[k] = Fx(Node[k * 2 + 1], Node[k * 2 + 2]);
            }
        }
        public void Update(int l, int r, long value)
        {
            Update(l, r, value, 0, 0, N);
        }
        public long Query(int l, int r)
        {
            return Query(l, r, 0, 0, N);
        }
        private long Query(int a, int b, int k, int l, int r)
        {
            Eval(k);
            if (r <= a || b <= l) return Ex;
            else if (a <= l && r <= b) return Node[k];
            else
            {
                var vl = Query(a, b, k * 2 + 1, l, (l + r) / 2);
                var vr = Query(a, b, k * 2 + 2, (l + r) / 2, r);
                return Fx(vl, vr);
            }
        }
        // Create ...
        public static SegmentTreeLazy CreateRangeUpdateMin(int n)
        {
            // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DSL_2_A&lang=ja
            Func<long, long, long> fx = (x1, x2) => Math.Min(x1, x2); // max
            var ex = 100100100100100;
            Func<long, long, long> fm = (m1, m2) => m2; // update
            var em = 100100100100100;
            Func<long, long, long> fa = (x, m) => m; // update
            return new SegmentTreeLazy(n, fx, ex, fm, em, fa);
        }
        public static SegmentTreeLazy CreateRangeUpdateMax(int n)
        {
            Func<long, long, long> fx = (v1, v2) => Math.Max(v1, v2); // max
            var ex = -100100100100100;
            Func<long, long, long> fm = (m1, m2) => m2; // update
            var em = -100100100100100;
            Func<long, long, long> fa = (x, m) => m; // update
            return new SegmentTreeLazy(n, fx, ex, fm, em, fa);
        }
        public static SegmentTreeLazy CreateRangeAddSum(int n)
        {
            // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DSL_2_B
            Func<long, long, long> fx = (x1, x2) => x1 + x2; // sum
            var ex = 0L;
            Func<long, long, long> fm = (m1, m2) => m1 + m2; // add
            var em = 0L;
            Func<long, long, long> fa = (x, m) => x + m; // add
            return new SegmentTreeLazy(n, fx, ex, fm, em, fa);
        }
        public static SegmentTreeLazy CreateRangeUpdateSum(int n)
        {
            // https://atcoder.jp/contests/arc079/tasks/arc079_c
            Func<long, long, long> fx = (x1, x2) => x1 + x2; // sum
            var ex = 0L;
            Func<long, long, long> fm = (m1, m2) => m2; // update
            var em = 0L;
            Func<long, long, long> fa = (x, m) => m; // update
            return new SegmentTreeLazy(n, fx, ex, fm, em, fa);
        }
    }
    static Scanner sc = new Scanner();
}
class Scanner
{
    string[] S = new string[0];
    int Index = 0;
    char[] Separators = new char[] { ' ' };

    public string Next()
    {
        if (this.Index < this.S.Length) return this.S[this.Index++];
        var line = "";
        while (line == "") line = Console.ReadLine();
        this.S = line.Split(this.Separators, StringSplitOptions.RemoveEmptyEntries);
        if (this.S.Length == 0) return this.Next();
        this.Index = 0;
        return this.S[this.Index++];
    }
    public string ReadStr()
    {
        return this.Next();
    }
    public char ReadChar()
    {
        return this.Next()[0];
    }
    public int ReadInt()
    {
        return int.Parse(this.Next());
    }
    public uint ReadUInt()
    {
        return uint.Parse(this.Next());
    }
    public long ReadLong()
    {
        return long.Parse(this.Next());
    }
    public double ReadDouble()
    {
        return double.Parse(this.Next());
    }
    public Tuple<int, int> ReadTup(int add = 0)
    {
        return Tuple.Create(this.ReadInt() + add, this.ReadInt() + add);
    }
    public Tuple<long, long> ReadTupLong(int add = 0)
    {
        return Tuple.Create(this.ReadLong() + add, this.ReadLong() + add);
    }
    public Tuple<int, int, int> ReadTup3(int add = 0)
    {
        return Tuple.Create(this.ReadInt() + add, this.ReadInt() + add, this.ReadInt() + add);
    }
    public Tuple<int, int, int, int> ReadTup4(int add = 0)
    {
        return Tuple.Create(this.ReadInt() + add, this.ReadInt() + add, this.ReadInt() + add, this.ReadInt() + add);
    }
    public int[] ReadIntArray(int n)
    {
        var array = new int[n];
        for (int i = 0; i < array.Length; i++)
        {
            array[i] = this.ReadInt();
        }
        return array;
    }
    public long[] ReadLongArray(int n)
    {
        var array = new long[n];
        for (int i = 0; i < array.Length; i++)
        {
            array[i] = this.ReadLong();
        }
        return array;
    }
    public double[] ReadDoubleArray(int n)
    {
        var array = new double[n];
        for (int i = 0; i < array.Length; i++)
        {
            array[i] = this.ReadDouble();
        }
        return array;
    }
    public char[] ReadCharArray(int n)
    {
        var array = new char[n];
        for (int i = 0; i < array.Length; i++)
        {
            array[i] = this.ReadChar();
        }
        return array;
    }
    public string[] ReadStrArray(int n)
    {
        var array = new string[n];
        for (int i = 0; i < array.Length; i++)
        {
            array[i] = this.ReadStr();
        }
        return array;
    }
    public Tuple<long, long>[] ReadTupLongArray(int n, int add = 0)
    {
        var array = new Tuple<long, long>[n];
        for (int i = 0; i < n; i++)
        {
            array[i] = this.ReadTupLong(add);
        }
        return array;
    }
    public Tuple<int, int>[] ReadTupArray(int n, int add = 0)
    {
        var array = new Tuple<int, int>[n];
        for (int i = 0; i < n; i++)
        {
            array[i] = this.ReadTup(add);
        }
        return array;
    }
    public Tuple<int, int, int>[] ReadTup3Array(int n, int add = 0)
    {
        var array = new Tuple<int, int, int>[n];
        for (int i = 0; i < n; i++)
        {
            array[i] = this.ReadTup3(add);
        }
        return array;
    }
    public Tuple<int, int, int, int>[] ReadTup4Array(int n, int add = 0)
    {
        var array = new Tuple<int, int, int, int>[n];
        for (int i = 0; i < n; i++)
        {
            array[i] = this.ReadTup4(add);
        }
        return array;
    }
}