Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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