Advertisement
nq1s788

26(3)

Apr 10th, 2024
882
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.67 KB | None | 0 0
  1. data = open('file.txt').readlines()
  2. n, k = map(int, data[0].split())
  3. arr = data[1:]
  4. for i in range(n):
  5.     arr[i] = arr[i].split()
  6.     arr[i] = (int(arr[i][0]), arr[i][1])
  7. arr.sort()
  8. cur_sum = 0
  9. x = 0
  10. y = 0
  11. a = []
  12. b = []
  13. for i in range(n):
  14.     if arr[i][1] == 'A':
  15.         a.append(arr[i][0])
  16.     else:
  17.         b.append(arr[i][0])
  18.     if cur_sum + arr[i][0] <= k:
  19.         cur_sum += arr[i][0]
  20.         if arr[i][1] == 'A':
  21.             x += 1
  22.         else:
  23.             y += 1
  24. while y > 0 and x < len(a):
  25.     if cur_sum + (a[x] - b[y - 1]) <= k:
  26.         cur_sum += a[x] - b[y - 1]
  27.         x += 1
  28.         y -= 1
  29.     else:
  30.         break
  31. print(x, k - cur_sum)
  32.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement