• + 0 comments

    f# solution

    open System
    
    let binarySearchMin predicate (arr: int64[]) =
        let rec loop a b lastFound =
            match a, b with
            | a, b when a = b && lastFound > 0 -> lastFound
            | a, b when a = b -> -1
            | _ ->
                let mid = (b - a + 1) / 2
                let checkAt = a + mid
    
                match predicate arr.[checkAt] with
                | false -> loop (a + mid) b lastFound
                | true -> loop a (b - mid) checkAt
    
        loop 0 ((Array.length arr) - 1) (-1)
    
    let search s arr = (binarySearchMin (fun x -> x >= s) arr)
    
    let N = Console.ReadLine() |> int
    
    let arr =
        Console.ReadLine()
        |> (fun s -> s.Split(" "))
        |> Array.map int
        |> Array.sortDescending
        |> Array.scan (fun acc curr -> acc + (int64 curr)) 0L
    
    let t = Console.ReadLine() |> int
    
    [ 1..t ]
    |> Seq.map (fun _ -> Console.ReadLine() |> int64)
    |> Seq.map (fun s -> search s arr)
    |> Seq.map string
    |> Seq.iter Console.WriteLine