Advertisement
nq1s788

27 (макс сумма не кратная k)

Jun 1st, 2024 (edited)
625
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.62 KB | None | 0 0
  1. #https://inf-ege.sdamgia.ru/problem?id=27424
  2. def gen_pairs(previous, pair):
  3.     ret = []
  4.     for lst in previous:
  5.         if lst == 0:
  6.             continue
  7.         for cur in pair:
  8.             ret.append(lst + cur)
  9.     return ret
  10.  
  11.  
  12. data = open('27.txt').readlines()
  13. n = int(data[0])
  14. k = 3 #!!!!!!!!!!!!!!
  15. a = data[1:]
  16. for i in range(n):
  17.     a[i] = list(map(int, a[i].split()))
  18. dp = [[0 for i in range(k)] for j in range(n)]
  19. for x in a[0]:
  20.     dp[0][x % k] = max(dp[0][x % k], x)
  21. for i in range(1, n):
  22.     for sum in gen_pairs(dp[i - 1], a[i]):
  23.         dp[i][sum % k] = max(dp[i][sum % k], sum)
  24. print(max(dp[-1][1:]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement