You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

B.py 727B

123456789101112131415161718192021222324252627282930313233343536373839
  1. from __future__ import print_function
  2. import sys
  3. s, e = map(int, sys.stdin.readline().split())
  4. es = map(int, sys.stdin)
  5. OK = [ [0]*(s+1) for i in xrange(e+1) ]
  6. OK[0][0] = 1
  7. for i in xrange(e):
  8. for j in xrange(s+1):
  9. if OK[i][j]: OK[i+1][j] = 1
  10. if es[i] > j: continue
  11. if OK[i][j-es[i]]: OK[i+1][j] = 2
  12. ns = []; i, j = e, s
  13. while i > 0 and j >= 0:
  14. x = OK[i][j]
  15. if x == 2:
  16. ns.append(i-1)
  17. i -= 1
  18. j -= es[i]
  19. else:
  20. i -= 1
  21. # print(ns)
  22. # import pprint; pprint.pprint(OK)
  23. print(e+1)
  24. k, l = 1, 3
  25. for n in ns[:-1]:
  26. print(k, l, es[n])
  27. k = l; l += 1
  28. print(k, 2, es[ns[-1]])
  29. ns = set(ns); k = 2
  30. for i,j in enumerate(es):
  31. if i not in ns:
  32. print(k, l, j)
  33. k = l; l += 1