Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #https://inf-ege.sdamgia.ru/problem?id=38960
- data = open('26 (2).txt').readlines()
- n, S = map(int, data[0].split())
- a = []
- for e in data[1:]:
- c, t = e.split()
- c = int(c)
- a.append((c, t))
- a.sort()
- s = 0 #текущая сумма
- cnt = 0 #кол-во взятых
- cnt_a = 0
- for e in a:
- if s + e[0] <= S:
- s += e[0]
- cnt += 1
- if e[1] == 'A':
- cnt_a += 1
- l = cnt - 1
- r = cnt
- while 1:
- while l >= 0 and a[l][1] == 'A':
- l -= 1
- while r < n and a[r][1] == 'B':
- r += 1
- if l < 0 or r >= n:
- break
- if s - a[l][0] + a[r][0] <= S:
- s = s - a[l][0] + a[r][0]
- cnt_a += 1
- l -= 1
- r += 1
- else:
- break
- print(cnt_a, S - s)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement