Problem:


Score : 300 points

Problem Statement

Find the maximum possible sum of the digits (in base 10) of a positive integer not greater than N.

Constraints

1\leq N \leq 10^{16}

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the maximum possible sum of the digits (in base 10) of a positive integer not greater than N.

Sample Input 1

100

Sample Output 1

18

For example, the sum of the digits in 99 is 18, which turns out to be the maximum value.

Sample Input 2

9995

Sample Output 2

35

For example, the sum of the digits in 9989 is 35, which turns out to be the maximum value.

Sample Input 3

3141592653589793

Sample Output 3

137



Sample input:
100


Sample output:
18

Submission status: Accepted

Solution code:
N=input()
n=len(N)

if len(N)>1:
    #print(">=10")
    for i in range(n-1):
        now=N[n-1-i]
        #print("i,now",i,now)
        if now!='9':
            #print("not (9 or 8)")
            tosub=int(now)+1
            left=int(N[:n-1-i+1])-tosub
            right=N[n-1-i+1:]
            #print("tosub,left,right",tosub,left,right)
            N=str(left)+str(right)
            N.zfill(n)
            #print("new N",N)

num=0
for i in N:
    num+=int(i)
print(num)

Submission status: Accepted

Solution code:
# 9or8で埋めたやつが強い あるいはオリジナル
# 桁を一つ落として（1なら9999...）残りを9で埋める
# オリジナルはたとえば199とかのとき19が最大になる

s = input()
tmp = 0
for i in s:
    tmp += int(i)
if s[0] == "1":
    print(max(9 * (len(s) - 1), tmp))
else:
    print(max(int(s[0]) - 1 + 9 * (len(s) - 1), tmp))

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(100000000)
def input():
    return sys.stdin.readline()[:-1]
from bisect import *
from collections import *
from heapq import *
from math import *
from copy import *

N = input()
l = len(N)
if l == 1:
    print(N)
    exit()
x = int(N[0])*int(pow(10, l-1))+int('9'*(l-1))
if int(N) < x:
    x -= 1
ans = 0
while x > 0:
    ans += x%10
    x //= 10
print(ans)

Submission status: Accepted

Solution code:
a = list(input())
if len(a) == 1:
    print(a[0])
else:
    c = 0
    for i in range(len(a)):
        b = ""
        d = 0
        for j in range(len(a)):
            if i == j:
                b += str(max(int(a[i]) - 1, 0))
                d += max(int(a[i]) - 1, 0)
            else:
                b += "9"
                d += 9
        if int(b) <= int("".join(a)):
            c = max(c, d)
    c = max(c, sum(map(int, a)))
    print(c)

Submission status: Accepted

Solution code:
N = input()
lenN = len(N)
allnine = 1 #オロナインではない
sum = int(N[0])
for i in range(1, lenN):
    if N[i] != "9":
        allnine = 0
        break
    sum += int(N[i])
if allnine == 1: #Nは一桁 or Nは二桁以上で二桁目以降が全て9
    print(str(sum))
else:
    print(str(int(N[0]) + 9*(lenN-1) - 1))

Submission status: Accepted

Solution code:
from sys import stdin
N = (stdin.readline().rstrip())
ans = 0
if len(N) == 1:
    print(N)

elif set(N) == {"9"}:
    ans = len(N) * 9
    print(ans)
    
elif set(N[1:]) == {"9"}:
    ans = (len(N)-1) * 9 + int(N[0])
    print(ans)

else:
    ans += (int(N[0]))
    ans += ((len(N)-1)*9)
    ans -= 1
    print(ans)

Submission status: Accepted

Solution code:
# AGC021A - Digit Sum 2
def main():
    N = input().rstrip()
    f = int(N[0])  # first digit
    ds1 = sum(map(int, str(N)))  # ds: digit sum
    ds2 = (f - 1) + 9 * (len(N) - 1)
    print(max(ds1, ds2))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# AGC021A - Digit Sum 2
def main():
    N = input().rstrip()
    l = len(N)
    if l == 1:
        print(N)
        return
    f = int(N[0])  # first digit
    ans = f + 9 * (l - 1)
    flg = set(N[1:]) == {"9"}
    print(ans if flg else ans - 1)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n=input()
k=len(n)
if k==1:print(int(n));exit()
flag=all([i=="9" for i in n[1:]])
k1=int(n[0])
if flag:print(k1+9*(k-1))
else:print((k1-1)+9*(k-1))

Submission status: Accepted

Solution code:
import math
import fractions
import copy
import numpy as np
def j(q):
    if q==1: print("Yes")
    elif q == 0:print("No")
    exit(0)
rem = pow(10,9)+7
"""
def ct(x,y):
    if (x>y):print("+")
    elif (x<y): print("-")
    else: print("?")
"""

def ip():
    return int(input())
def iprow():
    return [int(i) for i in input().split()]
def ips():
    return (int(i) for i in input().split())
def printrow(a):
    for i in a:
        print(i)
"""
#decomment when needed
def combinations(n,r):
    if n<r:return 0
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
def permutations(n,r):
    if n<r:return 0
    return math.factorial(n) // math.factorial(n - r)
def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)
"""
n = ip()
b = str(n)
a = [int(i) for i in b]
if n <10:
    print(n)
    exit(0)
backup = copy.copy(a)
ans = sum(a)
for i in range(len(a)-1):
    if a[i] != 9:
        a[i]-=1
        for k in range(i+1,len(a)):
            a[k] = 9
ans = max(ans,sum(a))
#print(a,sum(a))
l = len(a)
if a[l-1] != 9:
    a[l-1] = 9
    a[l-2] -=1
#print(a,sum(a))
ans = max(ans,sum(a))
a = copy.copy(backup)
for i in range(1,len(a)-1):
    if a[i] != 9:
        a[i-1]-=1
        for k in range(i,len(a)):
            a[k] = 9
ans = max(ans,sum(a))
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
N += 1
N = str(N)
ans = 0
for i in range(len(N)):
  if i == 0:
    ans += int(N[i])-1
  else:
    ans += 9
print(ans)

Submission status: Accepted

Solution code:
N = input()
S = len(N)
C = 0

if S == 1:
  print(N)
elif all (i == "9" for i in N[1:]):
  for i in N:
    C += int(i)
  print(C)
else:
  print(int(N[0]) - 10 + S * 9)

Submission status: Accepted

Solution code:
N=int(input())
l=len(str(N))

if N<=9:
    ans=N
elif N%10**(l-1)==10**(l-1)-1:
    ans=N//10**(l-1) + 9*(l-1)
else:
    ans= (N//10**(l-1) - 1) + 9*(l-1)

print(ans)

Submission status: Accepted

Solution code:
N = int(input())

ret = ''

while True:
    f = False
    for i in range(1, 10)[::-1]:
        if int(str(i) + ret) <= N:
            ret = str(i) + ret
            f = True
            break
    if not f:
        break

print(sum(map(int, list(ret))))

Submission status: Accepted

Solution code:
n = list(input())

a = n[1:]

if all(x == "9" for x in a):
    ans = int(n[0])
    for i in a:
        ans +=9
    print(ans)
else:
    ans = int(n[0])-1
    for i in a:
        ans +=9
    print(ans)

Submission status: Accepted

Solution code:
def sol(n):
    #n=input()
    if len(set(n[1:]))<=1 and (n[-1]=='9' or len(n)==1):
      m=0
    else:
      m=1
    print(int(n[0])+9*len(n)-9-m)

n=input()
sol(str(n))

Submission status: Accepted

Solution code:
# 2019/07/17
# AtCoder Grand Contest 021 - A
# For the answer: 
# 1. 桁分割、桁数Count
# 2-1. 上位桁から9以外を探索
# 2-2. 9以外があれば、その1つ上桁を-1
# 2-3. ただし先頭の場合は自身を-1
# 3. (桁数-1)*9 + 上位桁-1

# Input
n = int(input())

# 1.
wn = n
ketalist = list()
defans = 0
while wn > 0:
    remn = wn % 10
    defans = defans + remn
    ketalist.append(remn)
    wn = int(wn / 10)

# 2. 
ketalist.reverse()
allflg = False
for i in range(len(ketalist)):
    if ketalist[i] < 9:
        if i == 0:
            tgtnum = ketalist[i] - 1
        else:
            tgtnum = ketalist[i-1] - 1
        break
    # Case all 9:
    if i == len(ketalist) - 1:
        allflg = True

# 3.
if len(ketalist) > 1 and allflg == False:
    if ketalist[0] == 1:
        ans = (len(ketalist) - 1) * 9
    else:
        ans = (len(ketalist) - 1) * 9 + tgtnum
elif len(ketalist) > 1 and allflg == True:
    ans = len(ketalist) * 9
else:
    ans = n

# Output
print(max(ans, defans))

Submission status: Accepted

Solution code:
#桁dp
N = input()
l = len(N)

ans = sum(map(int,N))
for i in range(l):
    if N[i]=="0":continue
    M=N[:i] + str(int(N[i])-1) + "9"*(l-i-1)
    S = sum(map(int,M))
    ans = max(ans,S)
print(ans)

Submission status: Accepted

Solution code:
N = list(input())
N = N[::-1]
n = len(N)
for i in range(n-1):
    if N[i]!="9":
        N[i+1] = str(int(N[i+1])-1)
        N[i] = "9"
N = N[::-1]
ans = 0
for i in N: ans += int(i)
print(ans)

Submission status: Accepted

Solution code:
n=input()
l=len(n)
if(l==1):
    print(n)
elif(str(int(n)+1)[0]!=n[0]):
    print(int(n[0])+(l-1)*9)
elif(len(str(int(n)+1))==l):
     print((int(n[0])-1)+(l-1)*9)

Submission status: Accepted

Solution code:
n=int(input())
if n<10:
    print(n)
else:
    n=list(map(int,list(str(n))))
    if set(n[1:])=={9}:
        print(sum(n))
    else:
        print(n[0]-1+9*(len(n)-1))

Submission status: Accepted

Solution code:
n=input()
ans=(len(n)-1)*9

p=0
for i in range(1,10):
    if i*10**(len(n)-1)+(10**(len(n)-1)-1)<=int(n):
        p=max(p,i)

print(ans+p)

Submission status: Accepted

Solution code:
Nin = input()
l = len(Nin)
fow = int(Nin[0])

all9 = True
for n in Nin[1:]:
    if n != '9':
        all9 = False
if all9:
    ans = fow+(l-1)*9
else:
    ans = fow-1+(l-1)*9
print(ans)

Submission status: Accepted

Solution code:
n=list(map(int,list(input())))
l=len(n)
s=0
for i in range(l-1,0,-1):
  if n[i]<9:
    s+=9
    if n[i-1]!=0:
      n[i-1]-=1
  else:
    s+=n[i]
print(s+n[0])

Submission status: Accepted

Solution code:
n = int(input())

def digit(a):
    return sum(map(int,list(str(a))))

if n < 10:
    print(n)
else:
    n2 = list(str(n))
    ln = len(n2)

    if n == 10**ln - 1:
        print(9*ln)
    elif n == int(n2[0])*(10**(ln-1))+ 10**(ln-1)-1:
        print(int(n2[0])+9*(ln-1))
    else:
        for i in range(ln):
            if i == 0:
                n2[0] = str(int(n2[0]) - 1)
                if n2[0] == "0":
                    n2[0] = ""
            else:
                n2[i] = "9"
        n2 = int("".join(n2))
        print(digit(n2))

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

arr = list(map(int, input()))
num = 0

if (arr.count(9) == len(arr)) or (arr[0] != 9 and arr.count(9) == len(arr) - 1):
    num = sum(arr)
else:
    num = arr[0] - 1 + (len(arr) - 1) * 9
print(num)

Submission status: Accepted

Solution code:
n=input()
if len(n)==1:print(n)
elif n=="9"*len(n):print(9*len(n))
elif n[1:]=="9"*(len(n)-1):print(int(n[0])+9*(len(n)-1))
else:print(9*(len(n)-1)+int(n[0])-1)

Submission status: Accepted

Solution code:
N = list(map(int, list(input())))
for i in range(1,len(N)):
    if N[i] != 9:
        ans = (N[0] - 1) + 9 * (len(N) - 1)
        print(ans)
        exit()
ans = N[0] + 9 * (len(N) - 1)
print(ans)

Submission status: Accepted

Solution code:
n = input()
s = 0
ans = 0
for i in range(len(n)):
    ans = max(int(n[i])-1 + (len(n)-i-1)*9 + s, ans)
    s+=int(n[i])
print(max(ans,s))

Submission status: Accepted

Solution code:
n=input()
lenn=len(n)
#print(lenn)

n1=n[0]
n2=n[1:]

if n2.count('9')==lenn-1:
    ans=int(n[0])+9*(lenn-1)
else:
    ans=int(n[0])-1+9*(lenn-1)
  
print(ans)

Submission status: Accepted

Solution code:
n = input()
l = len(n)

if l == 1:
    print(n)
elif set(list(n[1:])) == {"9"}:
    print(int(n[0])+9*(l-1))
else:
    print(int(n[0])-1+9*(l-1))

Submission status: Accepted

Solution code:
N = input()

if len(N) == 1:
    print(int(N))
    exit()

a = str(int(N) - int(N[-1]) - 1)
b = '9' * (len(N) - 1)

A = 0
B = 0
C = 0

for aa in a:
    A += int(aa)
for bb in b:
    B += int(bb)
if int(N[0]) > 1:
    B += int(N[0]) - 1
for n in N:
    C += int(n)


print(max(A, B, C))

Submission status: Accepted

Solution code:
N = input()
l = len(N)

if l == 1:
    print(int(N[0]))
    exit()

if all([c == '9' for c in N[1:]]):
    ans = 9 * (l - 1) + int(N[0])
else:
    ans = 9 * (l - 1) + int(N[0]) - 1

print(ans)

Submission status: Accepted

Solution code:
def main():
    N = input()
    if len(N) == 1:
        print(N)
        return
    for d in N[1:]:
        if d != "9":
            ans = int(N[0]) - 1 + 9 * (len(N)-1)
            print(ans)
            return
    ans = int(N[0]) + 9 * (len(N)-1)
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = input()
nine = ''.join(['9' for _ in range(len(n)-1)])
a = str(int(n[0]))
if int(a+nine) <= int(n):
    print(sum([int(i) for i in a+nine]))
else:
    a = str(int(n[0])-1)
    print(sum([int(i) for i in a+nine]))

Submission status: Accepted

Solution code:
s = input()
mx = 0
for i in s:
    mx += int(i)
tmp = int(s[0])-1
for i in s[1:]:
    tmp += 9
print(max(tmp, mx))

Submission status: Accepted

Solution code:
import sys
n = int(input())
i = -1
tmp = n
all9 = True
while True:
    if tmp % 10 != 9 and tmp // 10 != 0:
        all9 = False
    i += 1
    tmp //= 10
    if tmp == 0:
        break
if i == 0:
    print(n)
    sys.exit()
ans = n //(10 ** i)- 1
for j in range(i):
    ans += 9
if all9:
    ans += 1
print(ans)

Submission status: Accepted

Solution code:
N = input()
digits = len(N)
primal = int(N[0])
ans = 0
if set(N[1:]) == {'9'} or set(N[1:]) == set():
    ans = primal + 9*(digits - 1)
else:
    ans = primal - 1 + 9 * (digits - 1)
print(ans)

Submission status: Accepted

Solution code:
N = list(map(int, list(input())))

flag = False
cnt = N[0]
for n in N[1:]:
	if flag:
		cnt += 9
		continue
	
	if n == 9:
		cnt += 9
	else:
		cnt += -1 + 9
		flag = True

print(cnt)

Submission status: Accepted

Solution code:
from sys import stdin
N = stdin.readline().rstrip()
if set(N[1:]) == {'9'}:
  print(sum(int(c) for c in N))
elif int(N) < 10:
  print(int(N))
else:
  print(int(N[0]) - 1 + len(N[1:]) * 9)

Submission status: Accepted

Solution code:
def f(m):
  if m < 10:
    return m
  else:
    return f(m//10) + m%10

n = int(input())
m = f(n)
p = 1
while p <= n:
  m = max(m, f((n//p)*p-1))
  p *= 10
print(m)

Submission status: Accepted

Solution code:
def f(m):
  k = 0
  while m > 0:
    k += m%10
    m //= 10
  return k

n = int(input())

m = f(n)
p = 1
while p <= n:
  m = max(m, f((n//p)*p-1))
  p *= 10
  
print(m)

Submission status: Accepted

Solution code:
n=input()
deg=len(n)
if n.count('9')==len(n):
    print(deg*9)
elif deg==1:
    print(n)
else:
    if n[1:].count('9')==len(n)-1:
        print(int(n[0])+(deg-1)*9)
    else:
        print(int(n[0])-1+(deg-1)*9)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
A - Digit Sum 2
https://atcoder.jp/contests/agc021/tasks/agc021_a

"""
import sys


def calc_digit_sum(s):
    return sum([int(ch) for ch in s])

def solve(N):
    l = len(N)
    all_nine = True
    res = []
    for i, n in enumerate(N[::-1], start=1):
        if i != l:
            if n != '9':
                all_nine = False
            res.append('9')
        else:
            res.append(str(int(n)-1))

    if all_nine or l == 1:
        return calc_digit_sum(N)
    return calc_digit_sum(res)



def main(args):
    N = input()
    ans = solve(N)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Accepted

Solution code:
n = input()
print(max(sum(map(int, n)), int(n[0]) - 1 + (len(n) - 1) * 9))

Submission status: Accepted

Solution code:
n = input()
if set(n[1:]) == {"9"} or int(n) < 10:
    print(sum(map(int, n)))

else:
    print(int(n[0]) - 1 + (len(n) - 1) * 9)

Submission status: Accepted

Solution code:
s = input()

if int(s) < 10:
    print(int(s))
elif all([x == "9" for x in s[1:]]):
    print(int(s[0]) + 9 * (len(s) - 1))
else:
    print(int(s[0]) - 1 + 9 * (len(s) - 1))

Submission status: Accepted

Solution code:
N = int(input())
digit = []

while N > 0:
    digit.append(N % 10)
    N //= 10

ans = sum(digit)

for i in range(1, len(digit)):
    tmp = 9*i + sum(digit[i:])
    if digit[i] != 0:
        tmp -= 1
    
    if tmp > ans:
        ans = tmp

print(ans)

Submission status: Accepted

Solution code:
n = input()
num9 = n.count("9")
if num9 == len(n):
    print(sum([int(item) for item in n]))
elif num9 == len(n) - 1 and n[0] != "9":
    print(sum([int(item) for item in n]))
else:
    print(int(n[0]) - 1 + (len(n) - 1) * 9)

Submission status: Accepted

Solution code:
num = input()

for i in range(1,len(num)):
    if num[i] != '9':
        ans = (int(num[0]) - 1) + (9 * (len(num)-1))
        break
else:
    ans = (9 * (len(num) - 1)) + int(num[0])

print(ans)

Submission status: Accepted

Solution code:
S = input()
N = [int(c) for c in S]

if len(N) == 0:
    print(N[0])
    exit()

print(max(sum(N), 9 * (len(N) - 1) + N[0] - 1))

Submission status: Accepted

Solution code:
n=input()
l=len(n)
flag=0
for i in range(1,l):
  if n[i]!="9":
    flag=1
    break
ans=int(n[0])-1+9*(l-1)
if flag==1:
  print(ans)
else:
  print(ans+1)

Submission status: Accepted

Solution code:
S = list(input())
N = [str(int(S[0]) - 1)] + ["9"] * (len(S)-1)
print("{:.0f}".format(max(sum(map(int, N)), sum(map(int,S)))))

Submission status: Accepted

Solution code:
N=input()
if len(N) == 1:
    print(N)
else:
    dsum = len(N[1:])*9 + int(N[0])
    if not sum(9 - int(s) for s in N[1:]):
        print(dsum)
    else:
        print(dsum-1)

Submission status: Accepted

Solution code:
N = input()

r = []
r += [sum(map(int, list(str(N))))]

d = len(str(N))
r += [(int(list(str(N))[0]) - 1) + (9 * (d - 1))]

print(max(r))

Submission status: Accepted

Solution code:
s = str(input())
arr = list(s)
if len(arr) >1:
    if sum(map(int, arr))- int(arr[0])  != (len(arr)-1)*9:
        print(int(arr[0]) -1 + (9 * (len(arr)-1)))
    else:
        print(int(arr[0]) + (9 * (len(arr)-1)))
else:
    print(int(s))

Submission status: Accepted

Solution code:
s=int(input())
if s==1:print(1)
else:
    if sum(map(int,str(s)))==1:s-=1
    z=(10**(len(str(s))-1))
    y=(s+1)//z*z-1
    print(sum(map(int,str(y))))

Submission status: Accepted

Solution code:
N = input()

for i in range(1,len(N)):
    if N[i] != '9':
        ans = int(N[0])-1 + 9 * (len(N)-1)
        break
else:
    ans = 9 * (len(N)-1) + int(N[0])
    
print(ans)

Submission status: Accepted

Solution code:
n = input()
l = len(n)
if (l == 1):
    print(n)
elif all([i == '9' for i in n[1:]]):
    print(l * 9 + int(n[0]) - 9)
else:
    print(l * 9 + int(n[0]) - 10)

Submission status: Accepted

Solution code:

n = int(input())
K = 0
while(True):
    if n //10**K > 0:
        K += 1
    else:
        break
max_num = int(str(n//10**(K-1))+("9"*(K-1)))
if n == max_num:
    print(n//10**(K-1)+9*(K-1))
else:
    print(n//10**(K-1)+9*(K-1)-1)

Submission status: Accepted

Solution code:
def main():
    N = input()
    tmp = 10 ** (len(N) - 1) - 1
    res_ = int(tmp)
    if tmp == 0:
        print(N)
        return

    for i in range(1, 10):
        if int(str(i) + str(tmp)) <= int(N):
            res_ = int(str(i) + str(tmp))

    res = 0
    for x in str(res_):
        res += int(x)
    print(res)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = input()

if(len(N) == 0):
  print(int(N))
else:
  cand = str(int(N[0])-1) + '9' * (len(N) - 1)
  cand_int = eval("+".join(cand))
  N_int = eval("+".join(N))
  print( max(cand_int, N_int) )

Submission status: Accepted

Solution code:
N=int(input())
R=len(str(N))
def dsum(X):
	return(sum([ int(n) for n in list(str(X))]))
cand=[dsum(N)]
for i in range(R):
	a = (N//(10**i))*(10**i) + int('9'*i) if i>0 else 0
	b = (N//(10**i)-1)*(10**i) + int('9'*i) if i>0 else 0
	if a<=N:
		cand.append(dsum(a))
	else:
		cand.append(dsum(b))
print(max(cand))

Submission status: Accepted

Solution code:
N = input()
print(max((len(N) - 1) * 9 + int(N[0]) - 1, sum(int(n) for n in N)))

Submission status: Accepted

Solution code:
N = input()
sumn = sum([int(x) for x in N])
if(len(N)==1):
    print(N)
else:
    print(max((len(N)-1)*9 + int(N[0]) -1,sumn))

Submission status: Accepted

Solution code:
n=list(input())
for i in range(len(n)):
    n[i]=int(n[i])
num1=(n[0]-1)+(len(n)-1)*9
num2=sum(n)

print(max(num1,num2))

Submission status: Accepted

Solution code:
n = int(input())
N = str(n)

if n <= 9:
    print(n)
    exit()
ans = "9"*(len(N)-1)
for i in (9, 8, 7, 6, 5, 4, 3, 2, 1):
    if int(str(i)+ans) <= n:
        print(i+9*(len(N)-1))
        exit()
print(9*(len(N)-1))

Submission status: Accepted

Solution code:

def do_nothing():
    return 1

N = int(input())

cnt = 0
for i in range(20):
    for j in range(1,10):
        a = (j+1)*10**i-1
        if a <= N:
            do_nothing()
        else:
            print(cnt)
            exit()
        cnt += 1

Submission status: Accepted

Solution code:
n = input()
len_n = len(n)
if n == "1":
    ans = 1
elif int(n[0]) == 1:
    ans = 9 * (len_n - 1)
else:
    ans = (int(n[0]) - 1) + (9 * (len_n - 1))
if ans < sum([int(i) for i in list(n)]):
    ans = sum([int(i) for i in list(n)])
print(ans)

Submission status: Accepted

Solution code:
N = input()
t = 0
for i in N:
    t += int(i)
if N[0] == 1:
    print(max(t, (len(N) - 1) * 9))
else:
    print(max(t, int(N[0]) - 1 + (len(N) - 1) * 9))

Submission status: Accepted

Solution code:
n = input()

flag = True
for i in range(1,len(n)):
    if n[i]!='9':
        flag = False
        break

if flag:
    print(9*(len(n)-1)+int(n[0]))
else:
    print(9*(len(n)-1)+int(n[0])-1)

Submission status: Accepted

Solution code:
n = input()
l = len(n)
if l == 1:
    print(n)
elif set(n[1:]) == {'9'}:
    print(int(n[0])+9*(l-1))
else:
    print(9*(l-1)+int(n[0])-1)

Submission status: Accepted

Solution code:
N = input()



can = True
for i in range(len(N)-1):
  if N[i+1] != '9':
    can = False

if len(N) == 1:
  print(int(N))
elif can:
  ans = 0
  for i in N:
    ans += int(i)
  print(ans)
else:
  ans = int(N[0])-1 + (len(N)-1)*9
  print(ans)

Submission status: Accepted

Solution code:
N = int(input())
_N = N
c = 0
for i in range(17):
    _N //=10
    if _N>0:
        c +=1
    else:
        break
ma = 0
for i in range(11):
    v = i*10**c-1
    if v<0:
        continue
    if v>N:
        continue
    if ma<v:
        ma = v
_ma = ma
s = 0
for i in range(17):
    s+=_ma%10
    _ma //=10
print (s)

Submission status: Accepted

Solution code:
n = input()
if len(n) == 1:
    print(n)
else:
    n = str(int(n) + 1)
    print((int(n[0]) - 1) + (len(n) - 1) * 9)

Submission status: Accepted

Solution code:
n=int(input())
ans=int(str(n)[0])+9*(len(str(n))-1)
if str(n)[1:]=="9"*(len(str(n))-1):
  print(ans)
else:
  print(ans-1)

Submission status: Accepted

Solution code:
n=input()
if len(n)==1:
  print(int(n))
else:
  cnt=n[1:].count('9')
  if cnt==len(n)-1:
    print(int(n[0])+9*(len(n)-1))
  else:
    print(int(n[0])-1+9*(len(n)-1))

Submission status: Accepted

Solution code:
N = input()
N = [int(n) for n in N]

if all([n==9 for n in N[1:]]):
  print(sum(N))

else:
  print((N[0]-1) + 9*(len(N)-1))

Submission status: Accepted

Solution code:
N = int(input())

total1 = 0
for s in str(N):
    total1 += int(s)

total2 = int(str(N)[0])-1
for i in range(len(str(N))-1):
    total2 += 9

print(max(total1, total2))

Submission status: Accepted

Solution code:
n = int(input())
m = n
k = 0
while m >0:
    k += 1
    m = m//10
if (n+1) % (10**(k-1)) == 0:
    print(n//(10**(k-1))+9*(k-1))
else:
    print(n//(10**(k-1))-1+9*(k-1))

Submission status: Accepted

Solution code:
n = input().strip()

sumn = 0
ans = int(n[0]) + 9 * len(n[1:])
if not all(_n == '9' for _n in n[1:]):
  ans -= 1
print(ans)

Submission status: Accepted

Solution code:
n = input()

res = 0


if n[0] != "9":
    if len(n[1:]) == n.count("9"):
        res = int(n[0]) + 9*n.count("9")
    else:
        res = int(n[0])-1 + 9*(len(n)-1)
else:
    if len(n) == n.count("9"):
        res = 9*len(n)
    else:
        res = 9*(len(n)-1) + 8

print(res)

Submission status: Accepted

Solution code:
N = list(input())
ans1,ans2 = 0,0
for i in range(len(N)):
    if(i == 0): ans1 += int(N[i])-1
    else: ans1 += 9
    ans2 += int(N[i])   
 
ans = max(ans1,ans2)
print(ans)

Submission status: Accepted

Solution code:
N = input()
if len(N) == 1:
    print(int(N))
else:
    print(max(sum([int(i) for i in N]),(len(N)-1)*9+int(N[0])-1))

Submission status: Accepted

Solution code:
def dsum(n):
    ret = 0
    while n > 0:
        ret += n % 10
        n //= 10
    return ret

n = int(input())
s = "9" * len(str(n))
ans = 0
for i in range(len(str(n))):
    for x in range(10):
        a = s[:i] + str(x) + s[i+1:]
        if int(a) <= n:
            ans = max(ans, dsum(int(a)))
print(ans)

Submission status: Accepted

Solution code:
N = [int(x) for x in input()]
Nd = sum(N)
for i, d in enumerate(N):
    if i == 0:
        N[i] = d-1
    else:
        N[i] = 9
N9 = sum(N)

print(max(Nd, N9))

Submission status: Accepted

Solution code:
N = input()     

ans = sum([int(i) for i in N])

tmp = int(N[0])-1
for i in range(1,len(N)):
    tmp += 9
print(max(ans,tmp))

Submission status: Accepted

Solution code:
n = input()
if (all([i == '9' for i in n[1:]])):
    print(int(n[0])+9*(len(n)-1))
else:
    print(int(n[0])-1+9*(len(n)-1))

Submission status: Accepted

Solution code:
n = input()
ans = 9*(len(n)-1)
if n[1:]=='9'*(len(n)-1):
    print(ans+int(n[0]))
else:
    print(ans+int(n[0])-1)

Submission status: Accepted

Solution code:
N = input()
print(max(int(N[0]) - 1 + 9 * (len(N) - 1), sum(list(map(lambda x: int(x), N)))))

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
t = int(np.log10(n) + 1)
s = (n + 1) // (10**(t-1))
print(9*(t-1) + s-1)

Submission status: Accepted

Solution code:
N = int(input())

ans = 0
bN = N
while bN:
    ans += bN % 10
    bN //= 10

for i in range(17, 0, -1):
    p = N // (10**i)
    if p == 0:
        continue
    
    # tmp = (p-1)*10**i  + int("9"*i)    # 51231 i == 4 $B$G(B10000 $B$H$J$j!">&$O(B5
    tmp = 0
    rem = p-1
    while rem:
        tmp += rem % 10
        rem //= 10

    tmp += 9*i
    # print("i: ", i, "tmp: ", tmp)
    if tmp > ans:
        ans = tmp
print(ans)

Submission status: Accepted

Solution code:
N = input()
flag = True
if N[0] == '1':
  for i in range(1,len(N)):
    if N[i] != '9':
      flag = False
      break
  if not flag:
    print(9*(len(N)-1))
  else:
    print(1+9*(len(N)-1))
else:
  for i in range(1,len(N)):
    if N[i] != '9':
      flag = False
      break
  if not flag:
    print(9*(len(N)-1)+int(N[0])-1)
  else:
    print(9*(len(N)-1)+int(N[0]))

Submission status: Accepted

Solution code:
N = input()

K = len(N)
c = int(N[0])
rest_num = N[1:]

if rest_num == "9"*(K-1):
    ans = c + 9*(K-1)
else:
    ans = c-1 + 9*(K-1)

print(ans)

Submission status: Accepted

Solution code:
N = input()

K = len(N)
c = int(N[0])
rest_num = N[1:]

if K == 1 or set(rest_num) == {"9"}:
    ans = c + 9*(K-1)
else:
    ans = c-1 + 9*(K-1)

print(ans)

Submission status: Accepted

Solution code:


import sys
from collections import deque
import copy
import math
def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline


def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [N] = [long(input_raw[0])]
    dig = int(math.log(N, 10)) + 1
    max_d = N /(10 ** (dig - 1))
    X = max_d * (10 ** (dig - 1))
    for i in range(dig - 1):
        X += 9 * (10 ** (i ))
    if X <= N:
        print max_d + 9 * (dig - 1)
        return

    print (max_d - 1) + 9 * (dig - 1)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = input()
if n[1:] == "9"*(len(n)-1):
  print(int(n[0]) + 9*(len(n)-1))
else:
  print(int(n[0]) + 9*(len(n)-1) - 1)

Submission status: Accepted

Solution code:
n = input()


if len(n) == 1:
    print(int(n))
    exit()
flag = 0
for i in range(len(n)):
    if int(n[i]) != 9:
        flag = 1
if flag == 0:
    print(9 * len(n))
    exit()

flag = 0
for i in range(1, len(n)):
    if int(n[i]) != 9:
        flag = 1

if flag == 0:
    print(int(n[0]) + 9 * (len(n)-1))
    exit()

print(int(n[0]) - 1 + 9 * (len(n) - 1))

Submission status: Accepted

Solution code:
S = input()
ans = sum(int(c) for c in S)
for i in range(1,len(S)):
    tmp = sum(int(c) for c in S[:i]) - 1 + 9*(len(S)-i)
    ans = max(ans, tmp)
print(ans)

Submission status: Accepted

Solution code:
N = input()
if len(N) == 1:
    ans = N
else:
    tmp = list(set(N))
    if len(tmp) == 1 and tmp[0] == "9":
        ans = len(N) * 9
    elif N[-1] == "9":
        ans = sum(list(map(int, N)))
    else:
        ans = 9 * (len(N) - 1) + int(N[0]) - 1
print(ans)

Submission status: Accepted

Solution code:
N=list(input())
ans=int(N[0])-1
ans+=9*(len(N)-1)
ans2=0
for n in N:ans2+=int(n)
print(max(ans,ans2))

Submission status: Accepted

Solution code:
s,*n=str(int(input())+1);print(int(s)+9*len(n)-1)

Submission status: Accepted

Solution code:
s=str(int(input())+1);print(int(s[0])+9*len(s)-10)

Submission status: Accepted

Solution code:
s=input();print(max(int(s[0])-1+9*(len(s)-1),sum(map(int,list(s)))))

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    s = input()
    ans = len(s[1:]) * 9 + ord(s[0]) - ord('0')
    if s[1:].count('9') != len(s[1:]):
        ans -= 1
    print(ans)

Submission status: Accepted

Solution code:
S = input()

ans = 0
if len(S)==1:
    ans = S
else:
    ans += int(S[0]) - 1
    for i in range(len(S)-1):
        ans += 9
    sum_S = 0
    for i in range(len(S)):
        sum_S += int(S[i])
    if ans < int(sum_S):
        ans = sum_S
print(ans)

Submission status: Accepted

Solution code:
  
def solve():
  s = input()
  a = len(s)
  n = int(s)
  udge = '9'*a
  if a == 1:
    ans = n
  elif a > 1:
   if s == udge: ans = 9*a
   else:
     ans = 9*(a-1)+int(s[0])-1
     ans2 = 0
     for i in s:
      ans2 += int(i)
     ans = max(ans,ans2)
  return ans
  
  
if __name__ == '__main__':
  print(solve())

Submission status: Accepted

Solution code:
n = input()

lst = list(map(int, list(n)))
print(max(sum(lst), lst[0] - 1 + 9 * (len(lst) - 1)))

Submission status: Accepted

Solution code:
N=[int(i) for i in input()];print(max(sum(N),int(N[0])-1+9*(len(N)-1)))

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/agc021/tasks/agc021_a

import itertools
import collections
import bisect

def main():
    N = list(map(int, input()))
    if N[1:].count(9) == len(N) - 1:
        print(sum(N))
    else:
        print(N[0] - 1 + (len(N) - 1) * 9)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = input()

origin = 0
for i in range(len(N)):
    origin += int(N[i])
    
if N[0] == '1':
    tmp = '9' * (len(N) - 1)
    
else:
    tmp = str(int(N[0]) - 1)
    tmp = tmp + '9' * (len(N) - 1)
    
res = 0
for i in range(len(tmp)):
    res += int(tmp[i])
    
res = max(origin, res)
print(res)

Submission status: Accepted

Solution code:
N = input()
if len(N) == 1:
  print(N)
elif N[1:] == "9"*(len(N) -1):
  print(sum(list(map(int, list(N)))))
else:
  print(int(N[0]) - 1 + 9 * len(N[1:]))

Submission status: Accepted

Solution code:
N = [int(i) for i in input()]

L = list(set(N[1:]))
if L == [9] or L == []:
  print((len(N) - 1) * 9 + N[0])
else:
  print((len(N) - 1) * 9 + N[0] - 1)

Submission status: Accepted

Solution code:
import collections
import itertools
import numpy as np
import sys
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

s = S()

if int(s) >= 10:
    if len(set(s[1:])) == 1 and list(set(s[1:]))[0] == "9":
        print((len(s)-1)*9+int(s[0]))
    else:
        print((len(s)-1)*9+int(s[0])-1)
else:
    print(s)

Submission status: Accepted

Solution code:
n = input()
l = list(n)
m = int(n)
if l[1:]==["9"]*(len(n)-1):
    print(int(n[0])+9*(len(n)-1))
else:
    if l[0]==1:
        print(9*(len(n)-1))
    else:
        print(9*(len(n)-1)+int(n[0])-1)

Submission status: Accepted

Solution code:
N = input()
digit = len(N)
if(digit == 1):
    print(int(N))

elif(N[1:] == "9"*(digit-1)):
    print(9*(digit-1)+int(N[0]))
else:
    ans=[int(N[0])-1]+[9]*(digit-1)
    print(sum(ans))