-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmango.ml
43 lines (40 loc) · 1.04 KB
/
mango.ml
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
let rec bisect friends m =
let len = Array.length friends in
let aux = Array.make len 0 in
let rec happy k m =
for i = 0 to len - 1 do
aux.(i) <- (function (a, h) -> a + (k - 1) * h) friends.(i)
done;
Array.fast_sort (-) aux;
let rec fold_k i acc =
if i >= k then acc
else
let acc' = acc + aux.(i) in
fold_k (i + 1) acc'
in
(fold_k 0 0) <= m
in
let rec loop lo hi =
if lo <= hi then
let mid = lo + (hi - lo) / 2 in
if not (happy mid m) then
loop lo (mid - 1)
else
loop (mid + 1) hi
else lo - 1
in
loop 1 len
and read_int_list times =
if times = 0 then []
else
let x = Scanf.scanf " %d" (function x -> x) in
x :: (read_int_list (times - 1))
and main () =
let (n, m) = Scanf.scanf "%d %d\n" (fun x y -> (x, y)) in
let a = 2500000000000001 :: (read_int_list n) in
let h = 1 :: (read_int_list n) in
let friends = Array.of_list (List.combine a h) in
let idx = bisect friends m in
print_int idx;
print_char '\n'
let () = main ()