MakStashkevich

В. Коробки Решение задачи

Jun 17th, 2022 (edited)
396
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.12 KB | None | 0 0
  1. # 5
  2. # 6 2 3 5 1
  3. # 4 5 6 3
  4.  
  5. # count boxes
  6. n = str(input())
  7. if not n.isnumeric() or not (2 <= int(n) <= 1000):
  8.     raise Exception('Error condition n')
  9.    
  10. # count candies in boxes
  11. ai = str(input()).split(' ')
  12. if len(ai) < int(n):
  13.     raise Exception('Error condition ai count')
  14.    
  15. for a in ai:
  16.     if not str(a).isnumeric() or not (0 <= int(a) <= 1000):
  17.         raise Exception(f'Error condition a[{a}]')
  18.  
  19. # limit candies in boxes i-1
  20. bi = str(input()).split(' ')
  21. if len(bi) < int(int(n) - 1):
  22.     raise Exception('Error condition bi count')
  23.  
  24. i = 0
  25. act = 0
  26. for b in bi:
  27.     if not str(b).isnumeric() or not (0 <= int(b) <= 1000):
  28.         raise Exception(f'Error condition b[{b}]')
  29.    
  30.     a1 = int(ai[i])
  31.     a2 = int(ai[i + 1])
  32.    
  33.     clr = (a1 + a2) - int(b)
  34.     act += clr
  35.    
  36.     if a1 > a2:
  37.         if clr <= a1:
  38.             ai[i] = a1 - clr
  39.         else:
  40.             ai[i] = 0
  41.             ai[i + 1] = a2 - (clr - a1)
  42.     else:
  43.         if clr <= a2:
  44.             ai[i + 1] = a2 - clr
  45.         else:
  46.             ai[i + 1] = 0
  47.             ai[i] = a1 - (clr - a2)
  48.    
  49.     i += 1
  50.    
  51. print(act)
Add Comment
Please, Sign In to add comment