Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- data = open('file.txt').readlines()
- n, k = map(int, data[0].split())
- arr = data[1:]
- for i in range(n):
- arr[i] = arr[i].split()
- arr[i] = (int(arr[i][0]), arr[i][1])
- arr.sort()
- cur_sum = 0
- x = 0
- y = 0
- a = []
- b = []
- for i in range(n):
- if arr[i][1] == 'A':
- a.append(arr[i][0])
- else:
- b.append(arr[i][0])
- if cur_sum + arr[i][0] <= k:
- cur_sum += arr[i][0]
- if arr[i][1] == 'A':
- x += 1
- else:
- y += 1
- while y > 0 and x < len(a):
- if cur_sum + (a[x] - b[y - 1]) <= k:
- cur_sum += a[x] - b[y - 1]
- x += 1
- y -= 1
- else:
- break
- print(x, k - cur_sum)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement