Skip to content

Instantly share code, notes, and snippets.

@whatalnk
Created January 22, 2017 17:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whatalnk/b8a9db77f4deff4fbcbdcec6dc56c511 to your computer and use it in GitHub Desktop.
Save whatalnk/b8a9db77f4deff4fbcbdcec6dc56c511 to your computer and use it in GitHub Desktop.
AtCoder AGC #009
n = gets.chomp.to_i
aa = []
bb = []
n.times do
a, b = gets.chomp.split(" ").map(&:to_i)
aa << a
bb << b
end
ret = 0
(n-1).downto(0).each do |i|
if bb[i] == 1 || aa[i] == 0 then
c = 0
elsif aa[i] >= bb[i] && aa[i] % bb[i] == 0
c = 0
elsif aa[i] > bb[i] then
c = (aa[i] / bb[i] + 1) * bb[i] - aa[i]
else
c = bb[i] - aa[i]
end
ret += c
aa[i] += c
aa[i-1] += ret if i > 0
end
puts ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment