Advertisement
nq1s788

Untitled

Jan 29th, 2025
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.65 KB | None | 0 0
  1. data = open("27-B (2).txt").readlines()
  2. n = int(data[0])
  3. a = list(map(int, data[1:]))
  4. q = [[], [], []]
  5. for e in a:
  6.     q[e % 3].append(e)
  7. for i in range(3):
  8.     q[i].sort()
  9. answ = 100000000
  10. for x in range(3):
  11.     for y in range(x, 3):
  12.         for z in range(y, 3):
  13.             if (x + y + z) % 3 != 0:
  14.                 continue
  15.             s = 0
  16.             for i in range(3):
  17.                 cnt = (x, y, z).count(i)
  18.                 if len(q[i]) >= cnt:
  19.                     s += sum(q[i][:cnt])
  20.                 else:
  21.                     s = -1
  22.                     break
  23.             if s != -1:
  24.                 answ = min(answ, s)
  25. print(answ)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement