fork(1) download
  1. def Solve(K, N, weights):
  2. weights.sort(reverse=True)
  3. aircrafts = 0
  4. i = 0
  5. while i < N:
  6. if weights[i] > K:
  7. return -1 # If a single person's weight exceeds K, return -1
  8. if i + 1 < N and weights[i] + weights[i + 1] > K:
  9. i += 1
  10. j = i + 1
  11. while j < N and weights[i] + weights[j] <= K:
  12. j += 1
  13. aircrafts += 1
  14. i = j
  15. return aircrafts
  16.  
  17. # Taking input from the user
  18. K = int(input())
  19. N = int(input())
  20. weights = list(map(int, input().split()))
  21.  
  22. # Output
  23. print(Solve(K, N, weights))
  24.  
Success #stdin #stdout 0.03s 9792KB
stdin
6
4
3 5 4 3
stdout
2