forked from EndlessCheng/codeforces-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1003D.go
48 lines (44 loc) · 769 Bytes
/
1003D.go
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
45
46
47
48
package main
import (
"bufio"
. "fmt"
"io"
"math/bits"
)
// github.com/EndlessCheng/codeforces-go
func CF1003D(_r io.Reader, _w io.Writer) {
in := bufio.NewReader(_r)
out := bufio.NewWriter(_w)
defer out.Flush()
coins := [31]int{}
var n, q int
var v uint
for Fscan(in, &n, &q); n > 0; n-- {
Fscan(in, &v)
coins[bits.TrailingZeros(v)]++
}
o:
for ; q > 0; q-- {
s, c := 0, coins
for Fscan(in, &v); v > 0; v &= v - 1 {
p := bits.TrailingZeros(v)
for cost := 1; ; cost <<= 1 {
if cost <= c[p] {
c[p] -= cost
s += cost
break
}
if p == 0 {
Fprintln(out, -1)
continue o
}
cost -= c[p]
s += c[p]
c[p] = 0
p--
}
}
Fprintln(out, s)
}
}
//func main() { CF1003D(os.Stdin, os.Stdout) }