-
Notifications
You must be signed in to change notification settings - Fork 0
/
beans.py
52 lines (41 loc) · 1.11 KB
/
beans.py
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
49
50
51
52
def find_k(arr, n, m):
rem = n
beans = 0
while rem > 0:
if rem < arr[0]:
print('NO')
return None
for i in range(1, m+1):
if arr[m-i] <= rem:
beans += int(rem/arr[m-i])
rem = rem % arr[m-i]
if rem == 0:
return beans
n, m = [int(t) for t in input().strip().split()]
arr = [int(i) for i in input().strip().split()]
arr.sort()
beans = find_k(arr, n, m)
if beans:
print(beans)
else:
print('NO')
q = int(input())
def minC(arr, m, n):
if (n == 0):
return 0
res = 100000
for i in range(m):
if arr[i] <= n:
sub_res = minC(arr, m, n-arr[i])
if sub_res != 100000 and sub_res < res-1:
res = sub_res + 1
return res
for j in range(q):
n, m = [int(t) for t in input().strip().split()]
arr = [int(i) for i in input().strip().split()]
arr = sorted(arr, reverse=True)
beans = minC(arr, m, n)
if beans:
print(beans)
else:
print('NO')