Last active
February 25, 2018 06:34
-
-
Save whatalnk/376d22f49bb553c7b1cabad93e55972b to your computer and use it in GitHub Desktop.
AtCoder AGC #021 A. Digit Sum 2
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 | |
ndigit = n.length | |
n = n.to_i | |
ans = 0 | |
if n < 10000 then | |
(1..n).each do |m| | |
x = 0 | |
while m > 0 | |
x += m%10 | |
m /= 10 | |
end | |
ans = [ans, x].max | |
end | |
else | |
(0..9).each do |i| | |
(0..9).each do |j| | |
(0..9).each do |k| | |
(0..9).each do |l| | |
if "#{i}#{j}#{"9"*(ndigit - 4)}#{k}#{l}".to_i <= n | |
x = i + j + 9 * (ndigit - 4) + k + l | |
ans = [ans, x].max | |
end | |
end | |
end | |
end | |
end | |
end | |
puts ans |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment