Skip to content

Instantly share code, notes, and snippets.

@whatalnk
Last active February 25, 2018 06:34
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/376d22f49bb553c7b1cabad93e55972b to your computer and use it in GitHub Desktop.
Save whatalnk/376d22f49bb553c7b1cabad93e55972b to your computer and use it in GitHub Desktop.
AtCoder AGC #021 A. Digit Sum 2
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