-
Notifications
You must be signed in to change notification settings - Fork 0
/
I.exs
44 lines (40 loc) · 820 Bytes
/
I.exs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
defmodule Main do
def readInt do
""
|> IO.gets
|> String.trim
|> String.split
|> Enum.map(&String.to_integer/1)
end
def can(list, val, n) do
oi = Enum.reduce(list, %{sum: 0, cnt: 1}, fn (at, acc) ->
if acc.sum + at > val do
%{sum: at, cnt: acc.cnt + 1}
else
%{sum: acc.sum + at, cnt: acc.cnt}
end
end)
oi.cnt <= n
end
def bb(lo, hi, list, n) do
m = div(lo + hi, 2)
cond do
lo == hi ->
hi
can(list, m, n) ->
bb(lo, m, list, n)
true ->
bb(m + 1, hi, list, n)
end
end
def main do
[n, r] = readInt()
if n != 0 and r != 0 do
lista = readInt()
ma = Enum.max(lista)
IO.inspect bb(ma, 1000 * r + 10, lista, n)
main()
end
end
end
Main.main()