Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.55 KB | None | 0 0
  1. k = int(input())
  2. n = int(input())
  3. a = []
  4. for i in range(n):
  5.     a.append(int(input()))
  6. ans = 0
  7. last = n - 1
  8. while last >= 0:
  9.     full = a[last] // k
  10.     ans += full * 2 * (last + 1)
  11.     a[last] %= k
  12.     if a[last] == 0:
  13.         last -= 1
  14.         continue
  15.     cur = a[last]
  16.     a[last] = 0
  17.     ans += 2 * (last + 1)
  18.     while cur < k and last >= 0:
  19.         last -= 1
  20.         if last == -1:
  21.             break
  22.         take = min(k - cur, a[last])
  23.         cur += take
  24.         a[last] -= take
  25.         if cur == k:
  26.             break
  27. print(ans)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement