fork download
  1. #Copy nói admin
  2. def knapsack(N, M, d):
  3. a = b = c = 0
  4. for m in range(1 << N):
  5. a1 = 0
  6. a2 = 0
  7. cnt = 0
  8. for i in range(N):
  9. if m & (1 << i):
  10. a1 += d[i][0]
  11. a2 += d[i][1]
  12. cnt += 1
  13. if a1 <= M and a2 > a:
  14. a = a2
  15. b = m
  16. c = cnt
  17. kq = []
  18. for i in range(N):
  19. if b & (1 << i):
  20. kq.append(i + 1)
  21.  
  22. return c, kq
  23. import sys
  24. input = sys.stdin.read
  25. data = input().splitlines()
  26. N, M = map(int, data[0].split())
  27. d = []
  28. for i in range(1, N + 1):
  29. W, V = map(int, data[i].split())
  30. d.append((W, V))
  31. cnt, kq = knapsack(N, M, d)
  32. print(cnt)
  33. print(' '.join(map(str, kq)))
Success #stdin #stdout 0.03s 10008KB
stdin
5 20
7 1
5 8
1 1
7 8
9 7
stdout
4
1 2 3 4