Advertisement
nq1s788

27(3) со словарем

Jun 19th, 2024
389
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.82 KB | None | 0 0
  1. data = open('27.txt').readlines()
  2. n = int(data[0])
  3. a = []
  4. for i in range(1, n + 1):
  5.     data[i] = list(map(int, data[i].split()))
  6.     if data[i][1] % 2 == 1:
  7.         a.append(data[i])
  8. n = len(a)
  9. a = list(map(sorted, a))
  10. dp = [{} for i in range(n)]
  11. sost = [(0, 0), (0, 1), (1, 0), (1, 1)]
  12. for i in range(n):
  13.     for ost in sost:
  14.         dp[i][ost] = 0
  15. dp[0][(a[0][0] % 2, a[0][1] % 2)] = sum(a[0])
  16. for i in range(1, n):
  17.     for ost in sost:
  18.         dp[i][ost] = dp[i - 1][ost]
  19.     x = a[i][0] % 2 #остаток первого в паре
  20.     y = a[i][1] % 2 #остаток второго в паре
  21.     sm = sum(a[i]) #сумма в паре
  22.     for ost in sost:
  23.         x1 = (ost[0] + x) % 2
  24.         y1 = (ost[1] + y) % 2
  25.         dp[i][(x1, y1)] = max(dp[i][(x1, y1)], dp[i - 1][ost] + sm)
  26. print(dp[-1][(1, 0)])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement