SaveText.Ru

Без имени
  1. f = open('27-A_demo.txt', 'r')
  2. txt = f.read().splitlines()
  3. f.close()
  4.  
  5. txt.pop(0)
  6.  
  7. def pair(s):
  8.     return list(map(int, s.split(' ')))
  9.  
  10. txt = list(map(pair, txt))
  11.  
  12. mindiff = 10000
  13. s = 0
  14. for line in txt:
  15.     a, b = line[0], line[1]
  16.     if mindiff > abs(a - b) and abs(a - b) % 3 != 0:
  17.         mindiff = abs(a - b)
  18.     s += min(line)
  19.  
  20. if s % 3 == 0:
  21.     s += mindiff
  22.  
  23. print(s)

Share with your friends:

Print