Created
January 22, 2017 17:10
-
-
Save whatalnk/b8a9db77f4deff4fbcbdcec6dc56c511 to your computer and use it in GitHub Desktop.
AtCoder AGC #009
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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