Advertisement
nq1s788

26 AB два указателя

Apr 16th, 2025
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.75 KB | None | 0 0
  1. #https://inf-ege.sdamgia.ru/problem?id=38960
  2. data = open('26 (2).txt').readlines()
  3. n, S = map(int, data[0].split())
  4. a = []
  5. for e in data[1:]:
  6.     c, t = e.split()
  7.     c = int(c)
  8.     a.append((c, t))
  9. a.sort()
  10. s = 0 #текущая сумма
  11. cnt = 0 #кол-во взятых
  12. cnt_a = 0
  13. for e in a:
  14.     if s + e[0] <= S:
  15.         s += e[0]
  16.         cnt += 1
  17.         if e[1] == 'A':
  18.             cnt_a += 1
  19. l = cnt - 1
  20. r = cnt
  21. while 1:
  22.     while l >= 0 and a[l][1] == 'A':
  23.         l -= 1
  24.     while r < n and a[r][1] == 'B':
  25.         r += 1
  26.     if l < 0 or r >= n:
  27.         break
  28.     if s - a[l][0] + a[r][0] <= S:
  29.         s = s - a[l][0] + a[r][0]
  30.         cnt_a += 1
  31.         l -= 1
  32.         r += 1
  33.     else:
  34.         break
  35. print(cnt_a, S - s)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement