fork download
  1. import sys
  2.  
  3. T = int(sys.stdin.readline())
  4. for Ca in range(T):
  5. N = int(sys.stdin.readline())
  6. W = [int(i) for i in sys.stdin.readline().split(" ")]
  7. D = [-1 for i in range(141)]
  8. D[0] = 0 # D[i] stores the minimal weight of a tower of height i using only the first ants
  9. mh = 0
  10. for w in W: # adding an ant
  11. # finding the largest tower made of that ant could carry
  12. # by monotonicity it then can carry also all smaller towers
  13. h = mh
  14. while(D[h] > 6 * w): #this stops as D[0]=0
  15. h -= 1
  16. if h == mh:
  17. mh += 1
  18. # If it can carry: It might be possible to create a less heavy tower of height h2 + 1
  19. for h2 in range(h, -1, -1):
  20. if D[h2 + 1] == -1 or D[h2 + 1] > D[h2] + w:
  21. D[h2 + 1] = D[h2] + w
  22. print("Case #%i: %i" % (Ca+1, mh))
Success #stdin #stdout 0.04s 9584KB
stdin
3
2
9 1
3
8 4 100
9
10 10 10 10 10 10 10 10 100
stdout
Case #1: 1
Case #2: 3
Case #3: 8