Scanf.scanf "%d" (fun n ->
    let a = Array.init n (fun _ -> Scanf.scanf " %d" (fun a -> a)) in

    let rec loop acc =
        let rec loop2 i =
            if i = n then acc else
                if a.(i) < n then loop2 (i + 1) else (
                    let c = a.(i) / n in
                    for j = 0 to n - 1 do
                        if j = i then a.(j) <- a.(j) - c * n else
                                      a.(j) <- a.(j) + c
                    done;
                    loop (acc + c)
                )
        in
        loop2 0
    in
    loop 0 |> Printf.printf "%d\n"
)