-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathMark and Toys.py
58 lines (47 loc) · 1.43 KB
/
Mark and Toys.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
53
54
55
56
57
58
# -*- coding: utf-8 -*-
"""
Mark and Jane are very happy after having their first kid. Their son is very fond of toys, so Mark wants to buy some.
There are N different toys lying in front of him, tagged with their prices, but he has only $K. He wants to maximize the
number of toys he buys with this money.
Now, you are Mark's best friend and have to help him buy as many toys as possible.
Input Format
The first line contains two integers, N and K, followed by a line containing N space separated integers indicating the
products' prices.
Output Format
An integer that denotes maximum number of toys Mark can buy for his son.
Constraints
1<=N<=105
1<=K<=109
1<=price of any toy<=109
A toy can't be bought multiple times.
"""
__author__ = 'Danyang'
class Solution(object):
def solve(self, cipher):
"""
1D binpacking
main solution function
:param cipher: the cipher
"""
n, K, A = cipher
A.sort()
cnt = 0
for elt in A:
K -= elt
if K >= 0:
cnt += 1
else:
break
return cnt
if __name__ == "__main__":
import sys
f = open("0.in", "r")
# f = sys.stdin
solution = Solution()
n, K = map(int, f.readline().strip().split(' '))
# construct cipher
A = map(int, f.readline().strip().split(' '))
cipher = n, K, A
# solve
s = "%s\n" % (solution.solve(cipher))
print s,