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()
if len(N)==1:
    print(int(N))
else:
    if all(N[i]=='9' for i in range(1,len(N))):
        print(sum(int(i) for i in N))
    else:
        print(int(N[0])-1 + 9*(len(N)-1))

Submission status: Accepted

Solution code:
s = input()

n = len(s)
ok = False
ans = 9
if n == 1:
    print(s)
    exit()

for i in range(n-1):
    if ok:
        ans += 9
        continue
    if s[i+1] == '9':
        ans += ord(s[i]) - ord('0')
        continue
    x = ord(s[i]) - ord('0')
    ans += x-1
    ok = True

print(ans)

Submission status: Accepted

Solution code:
import math
n = int(input())
k = int(math.log10(n))

a = n // (10 ** k)
b = n % (10 ** k)

if k == 0:
  ans = n
elif n % 10 == 9:
  ans = k * 9 + a
else:
  ans = k * 9 + a -1

print(ans)

Submission status: Accepted

Solution code:
N=input()
l=len(N)-1
h=int(N[0])
N=int(N)
if N==(h+1)*10**l-1:
  print(h+9*l)
else:
  print(h+9*l-1)

Submission status: Accepted

Solution code:
n = int(input())
L = len(str(n))

def sumdigit(i):
    return sum(map(int, list(str(i))))

print(max([sumdigit(n - (n%(10**i) + 1)) for i in range(1, L)] + [sumdigit(n)]))

Submission status: Accepted

Solution code:
N=list(input())
if len(N) == 1:
    print(int(N[0]))
    exit()
all9 = True
for i in range(1,len(N)):
    if N[i] != '9':
        all9 = False
        break

c = int(N[0])
if not all9:
    c -= 1
print(c + 9 * (len(N)-1))

Submission status: Accepted

Solution code:
import bisect,collections,copy,itertools,math,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def main():
    n = I()
    ans = 0
    if n<10:
        ans = n
    else:
        keta = len(str(n))
        top = int(str(n)[0])
        down = int(str(n)[1:])
        if top == 1:
            if down == 10**(keta-1)-1:
                ans = 1+(keta-1)*9
            else:
                ans = (keta-1)*9
        else:
            if down == 10**(keta-1)-1:
                ans = top+(keta-1)*9
            else:
                ans = top-1+(keta-1)*9
    
    print(ans)

main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np
from functools import reduce

rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: sys.stdin.readline().split()
ri = lambda: int(sys.stdin.readline())
rm = lambda: map(int, sys.stdin.readline().split())
rf = lambda: map(float, sys.stdin.readline().split())
rl = lambda: list(map(int, sys.stdin.readline().split()))
inf = float('inf')
mod = 10**9 + 7

s = rr()
lens = len(s)
a = int(s[0])
b = s[1:]
t = '9'*(lens-1)
if b == t:
    print((lens-1)*9+a)
else:
    print((lens-1)*9+a-1)

Submission status: Accepted

Solution code:
n = list(input())
n = [int(i) for i in n]
digit = len(n)


if n[1:] == [9]*(digit-1):
    ans = sum(n)
else:
    ans = (n[0]-1)+9*(digit-1)
print(ans)

Submission status: Accepted

Solution code:
N = input()
N_row = len(N)
while True:
  if N_row == 1:
    print(int(N))
    break
  else:
    N = list(N)
    a = int(N[0])
    b = int(N[-1])
    for i in range(1, N_row):
      if not int(N[i]) == 9:
        sum = (a-1) + 9*(N_row-1) 
        print(sum)
        break
    else:
      sum = a + 9*(N_row-1)
      print(sum)
      break
  break

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = input()
N_list = list(N)
N = int(N)
K = len(N_list)

ans = int(N_list[0])+(K-1)*9
ans_list = [N_list[0]] + ['9' for _ in range(K-1)]
ans_cand = int(''.join(ans_list))
if N < ans_cand:
    print(ans - 1)
else:
    print(ans)

Submission status: Accepted

Solution code:
n = input()

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

print(ans)

Submission status: Accepted

Solution code:
#AtCoder Grand Contest 021 a
n=input()
keta=len(n)
c=n[0]
ans=int(c)+9*(keta-1)
if n[1:]!=("9"*(keta-1)):
    ans-=1
print(ans)

Submission status: Accepted

Solution code:
n=input()

l=len(n)
if int(n)>9:
  ans=[9]
else:
  print(int(n))
  exit()
tmp=n[l-1]
for i in range(2,l+1):
  #print(tmp, int(n[l-i]))
  if int(tmp) != 9:
    tmp=int(n[l-i])-1
    #print("tmp:",tmp)
  else:
    tmp=int(n[l-i])
  if i==l:
    ans.append(int(tmp))
  else:
    ans.append(9)
  #print(ans)
print(sum(ans))

Submission status: Accepted

Solution code:
n = list(input())
n = [int(i) for i in (n)]
m = len(n)
if m == 1:
    print(n[0])
else:
    flg = False
    for i in range(1,m):
        if n[i] != 9:
            flg = True
            break
    if flg:
        print(n[0] - 1 + 9 * (m - 1))
    else:
        print(sum(n))

Submission status: Accepted

Solution code:
n = list(input())
n = [int(i) for i in (n)]
m = len(n)
if m == 1:
    print(n[0])
else:
    flg = False
    for i in range(1,m):
        if n[i] != 9:
            flg = True
            break
    if flg:
        n[0] -= 1
        for i in range(1,m):
            n[i] = 9
    print(sum(n))

Submission status: Accepted

Solution code:
import collections


def main():
    n = input()
    if n[0] == "9":
        if n[1:] == "9" * len(n[1:]):
            print(9 * len(n))
        else:
            print(9 * len(n) - 1)
    else:
        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)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = input()
if N[0] == "9":
    if N[1:] == "9" * len(N[1:]):
        print(9 * len(N))
    else:
        print(9 * len(N) - 1)
else:
    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 N[0] == "9":
    if N[1:] == "9" * len(N[1:]):
        print(9 * len(N))
    else:
        print(9 * len(N) - 1)
else:
    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:
def main():
    N = int(input())
    
    if N % 10 == 0:
        N -= 1
    else:
        first = 0
        M = N
        while True:
            d = M % 10
            M //= 10
            if M == 0:
                first = d
                break

        if N != (first + 1) * (10 ** (len(str(N)) - 1)) - 1:
            N = (first) * (10 ** (len(str(N)) - 1)) - 1

    ans = 0
    N = str(N)
    for d in N:
        ans += int(d)

    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = input()
if N[0] == "9":
    if N[1:] == "9"*len(N[1:]):
        print(9*len(N))
    else:
        print(9*len(N)-1)
else:
    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:
s = input()
cnt = len(s)

if s[0] == "9":
    if s[1:] == "9"*(cnt-1):
        print(9*cnt)
    else:
        print(8+9*(cnt-1))
else:
    if s[1:] == "9"*(cnt-1):
        print(int(s[0]) + 9*(cnt-1))
    else:
        print((int(s[0])-1) + 9*(cnt-1))

Submission status: Accepted

Solution code:
n = input()
keta = len(n)
if n[0] == '9':
    # 9999
    if n[1:] == '9'*(keta-1):
        print(9*keta)
    # 9899
    else:
        print(9*keta-1)
else:
    # 49999
    if n[1:] == '9'*(keta-1):
        print(int(n[0]) + 9*(keta-1))
    # 234
    else:
        print(int(n[0])-1 + 9*(keta-1))

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
read = sys.stdin.buffer.read
sys.setrecursionlimit(10 ** 7)
INF = float('inf')

from functools import lru_cache

def F(N):
    if N < 10:
        return N
    q, r = divmod(N, 10)
    x1 = F(q) + r
    x2 = F(q-1) + 9
    return max(x1, x2)
ans = F(int(input()))
print(ans)

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
read = sys.stdin.buffer.read
sys.setrecursionlimit(10 ** 7)
INF = float('inf')

N = input()

sN = set(N)
if len(N) == 1:
    ans = int(N)
elif len(N) > 1 and N[1:] == "9" * (len(N) - 1):
    ans = sum(map(int, list(N)))
else:
    ans = (len(N) - 1) * 9
    fst = int(N[0]) - 1
    ans += fst
print(ans)

Submission status: Accepted

Solution code:
n = input()

tmp = len(n)-1

if int(n[0] + tmp*'9') == int(n):
    print(9*tmp + int(n[0]))
else:
    print(9*tmp + int(n[0])-1)

Submission status: Accepted

Solution code:
N=input()

S=[-1]*len(N)
for i in range(len(N)):
    S[i]=int(N[i])

if sum(S[1:])== 9*(len(S)-1):
    print(sum(S))
else:
    print(9*(len(S)-1) + (S[0]-1))

Submission status: Accepted

Solution code:
n = input()

if len(list(n)) == 1:
    print(int(n))
    exit()

if set(list(n)) == {'9'}:
    print(len(list(n))*9)
    exit()

tmp = ''
for i in range(1,len(n)):
    if n[i] == '9':
        tmp += n[i-1]
    else:
        tmp += str(int(n[i-1])-1) + '9'*(len(n)-i)
        break
else:
    tmp += '9'

ans=0
for i in range(len(tmp)):
    ans += int(tmp[i]) 

print(ans)

Submission status: Accepted

Solution code:
N=input()

k=len(N)
A='9'*(k-1)

if N[0]==1:
    if N[1:]==A:
        print(k*9-8)
    else:
        print((k-1)*9)

else:
    if N[1:]==A:
        print(int(N[0])+(k-1)*9)
    else:
        print(int(N[0])+(k-1)*9-1)

Submission status: Accepted

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

ans = int(s[0]) + 9 * (n - 1)

if n != 1:
  if s[1:] != '9' * (n-1):
    ans -= 1

print(ans)

Submission status: Accepted

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

ans = 0
if s[1:] == '9' * (n - 1):
  ans = int(s[0]) + 9 * (n - 1)
elif n != 1:
  ans = int(s[0]) + 9 * (n - 1) - 1
else:
  ans = int(s[0])

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

p = N[-1]
q = sum(N)

ans = 0
for i in range(len(N)-1):
    if N[i] != 9:
        for j in range(i,len(N)):
            if N[j] != 0:
                N[j] -= 1
                N[i] = 9
            else:
                N[j] = 9

N[-1] = p-1
print(max(sum(N),q))

Submission status: Accepted

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

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])-1) + 9 * (len(n)-1))

Submission status: Accepted

Solution code:
n = input()

ans = None
for i in n[1:]:
    if int(i) != 9:
        ans = int(n[0]) - 1 + sum([9]*(len(n)-1))
        break

if ans is None:
    ans = sum([ int(i) for i in list(n)  ])
       
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

n = str(N)

res = 0

if len(n) == 1:
    res = N

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

    if nine:
        res = int(n[0]) + 9 * (len(n) - 1)
    else:
        res = (int(n[0]) - 1) + 9 * (len(n) - 1)

print(res)

Submission status: Accepted

Solution code:
import collections
import sys
import numpy as np
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
MOD = 10**9+7
import itertools
import math

n = int(input())
s = str(n)
k = len(s)
c = int(s[0])
if s == str(c) + "9"*(k-1):
    print(c + 9*(k-1))
else:
    print(c+9*(k-1)-1)

Submission status: Accepted

Solution code:
def resolve():
    N = int(input())
    N += 1

    ans = 0
    for i, n in enumerate(range(len(str(N)))):
        if i == 0:
            continue
        ans += int(9)
    ans += int(str(N)[0])-1
    print(ans)
    return


resolve()

Submission status: Accepted

Solution code:
'''
自宅用PCでの解答
'''
import math
#import numpy as np
import itertools
import queue
import bisect
from collections import deque,defaultdict
import heapq as hpq
from sys import stdin,setrecursionlimit
#from scipy.sparse.csgraph import dijkstra
#from scipy.sparse import csr_matrix
ipt = stdin.readline
setrecursionlimit(10**7)
mod = 10**9+7
alp = "abcdefghijklmnopqrstuvwxyz"

def main():
    n = input()
    ln = len(n)
    ma = sum(map(int,n))
    ma2 = int(n[0])-1+9*(ln-1)
    if ma < ma2:
        print(ma2)
    else:
        print(ma)


    return None

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def resolve():
    n = input()
    l = len(n)
    if n[1:] == "9" * (l-1): #c9999...99のとき
        print(int(n[0]) + 9*(l-1))
    else:
        print((int(n[0])-1) + (9*(l-1)))
resolve()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
X = list(map(int,input()))
sn = []
for i in range(len(X)):
  sn.append(sum(X[0:i+1]))
ssn = []
for i in range(len(X)):
  if (sn[i] != 0):
    ssn.append(sn[i]-1 + 9*(len(X)-1-i))
ssn.append(sum(X))
print(max(ssn))

Submission status: Accepted

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

ans = 0
for i in range(n_len+1):
    curr_sum = 0
    for j in range(n_len):
        num = int(n[j])
        if j < i:
            curr_sum += num
        elif j == i:
            if num == 0: curr_sum = -10000000000
            curr_sum += (num-1)
        else:
            curr_sum += 9
    ans = max(ans, curr_sum)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
S = input()
N = int(S)
ans = sum(map(int,S))

for i in range(1,len(S)):
    n = str(int(S[:i]) - 1)
    t = sum(map(int,n)) + 9*(len(S)-i)
    ans = max(ans,t)
print(ans)

Submission status: Accepted

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

if N[1:] == "9"*(n-1):
  ans = int(N[0])+9*(n-1)
else:
  ans = int(N[0])-1 + 9*(n-1)

print(ans)

Submission status: Accepted

Solution code:
n =input()

#最上位の桁をc,桁数をkとすると、
#c以外が9の場合(ex.49999)はc,9*(k-1)となる
#それ以外の場合、c,9*(k-2),8となる(ex.9995:9989)

c = int(n[0])
k = len(n)
if int(n)<10:
    print(n);exit()
if int(n[1:])==int('9'*(k-1)):
    ans = c+9*(k-1)
else:
    ans = c+9*(k-1)-1

print(ans)

Submission status: Accepted

Solution code:
n = int(input())

s = str(n)
l = len(s)
sum1 = 0

s = str(n)
for i in range(l):
    sum1 +=int(s[i])

ans = 0
for i in range(l):
    if i == 0:
        sum2 = (int(s[i])-1) + 9*(l-1) 
    else:
        sum2 = int(s[0]) + 9*(l-2) + int(s[i])-1

    ans = max(ans,max(sum1,sum2))

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
k=len(str(n))
c=n//10**(k-1)
print(c+9*(k-1) if n==(c+1)*10**(k-1)-1 else c+9*(k-1)-1)

Submission status: Accepted

Solution code:
N = int(input())
keta = len(str(N))
SN = str(N)
if N <= 9:
    print(N)
elif SN.count('9') == keta:
    print(9*keta)
elif SN.count('9') == keta-1 and SN[0] != '9':
    print((keta - 1) * 9 + int(SN[0]))
else:
    print((keta - 1) * 9 + int(SN[0])-1)

Submission status: Accepted

Solution code:
N = int(input())
K = len(str(N))
c = N // 10**(K-1)

print(c+9*(K-1) if N ==(c+1)*10**(K-1)-1 else c+9*(K-1)-1)

Submission status: Accepted

Solution code:
dd = list(map(int, open(0).read().rstrip()))
n = len(dd)

ans = sum(dd)
tmp = 0

for i in range(n):
    if dd[i] > 0:
        tmp += dd[i]
        ans = max(ans, tmp - 1 + (n-i-1)*9)

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
k=len(str(n))
c=n//10**(k-1)
print(c+9*(k-1) if n==(c+1)*10**(k-1)-1 else c+9*(k-1)-1)

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()
print(max(sum([int(i) for i in N]),(len(N)-1)*9+int(N[0])-1))

Submission status: Accepted

Solution code:
s =input()
s2 = int(s)
n= int(s)%9
mn = 0
while s2:
    mn+=s2%10
    s2//=10
print(max(mn,(len(s)-1)*9+(int(s[0])-1)))

Submission status: Accepted

Solution code:
s=[s for s in input()]
n=len(s)
print(max(ord(s[0])-ord('0')-1+9*(n-1),int(eval('+'.join(s)))))

Submission status: Accepted

Solution code:
n = input()

def nine(s):
    st = ""
    for _ in range(s):
        st += "9"
    return st

if len(n) == 1:
    print(n)
    exit()

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

if int(n[0]) == 2:
    if int(n[1:]) >= int(nine(len(n) - 1)):
        print(9*(len(n) -1) + 2)
        exit()
    else:
        print(9*(len(n) -1) + 1)
        exit()

if int(n[0]) == 3:
    if int(n[1:]) >= int(nine(len(n) - 1)):
        print(9*(len(n) -1) + 3)
        exit()
    else:
        print(9*(len(n) -1) + 2)
        exit()

if int(n[0]) == 4:
    if int(n[1:]) >= int(nine(len(n) - 1)):
        print(9*(len(n) -1) + 4)
        exit()
    else:
        print(9*(len(n) -1) + 3)
        exit()

if int(n[0]) == 5:
    if int(n[1:]) >= int(nine(len(n) - 1)):
        print(9*(len(n) -1) + 5)
        exit()
    else:
        print(9*(len(n) -1) + 4)
        exit()

if int(n[0]) == 6:
    if int(n[1:]) >= int(nine(len(n) - 1)):
        print(9*(len(n) -1) + 6)
        exit()
    else:
        print(9*(len(n) -1) + 5)
        exit()

if int(n[0]) == 7:
    if int(n[1:]) >= int(nine(len(n) - 1)):
        print(9*(len(n) -1) + 7)
        exit()
    else:
        print(9*(len(n) -1) + 6)
        exit()

if int(n[0]) == 8:
    if int(n[1:]) >= int(nine(len(n) - 1)):
        print(9*(len(n) -1) + 8)
        exit()
    else:
        print(9*(len(n) -1) + 7)
        exit()

if int(n[0]) == 9:
    if int(n[1:]) >= int(nine(len(n) - 1)):
        print(9*(len(n) -1) + 9)
        exit()
    else:
        print(9*(len(n) -1) + 8)
        exit()

Submission status: Accepted

Solution code:
n = list(input())

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

Submission status: Accepted

Solution code:
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    n = input()
    l = len(n)

    ans = 0
    x = int(n)
    while x > 0:
        ans += x % 10
        x //= 10

    else:
        for i in range(l):
            score = 0
            for j in range(i):
                score += int(n[j])
            score += int(n[i]) - 1
            score += 9 * (l - (i + 1))
            ans = max(score, ans)

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = input()

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

if len(n) == 1:
    print(n)
else:
    print(sum if set(n[1:]) == {"9"} else sum - 1)

Submission status: Accepted

Solution code:
import sys
import heapq
import math
import fractions
import bisect
import itertools
from collections import Counter
from collections import deque
from operator import itemgetter
def input(): return sys.stdin.readline().strip()
def mp(): return map(int,input().split())
def lmp(): return list(map(int,input().split()))

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

Submission status: Accepted

Solution code:
def maxf(n):
    a = list(map(int, list(n)))
    for i, item in enumerate(a):
        if i == 0 and item != 9:
            if a[i+1:] == [9]*(len(a)-1):break
            a[i] -=1
            a[i+1:] = [9]*(len(a)-1)
            break
        elif item != 9:
            a[i-1] -= 1
            a[i:] = [9]*(len(a)-i)
            break
    return sum(a)

print(maxf(input()))

Submission status: Accepted

Solution code:
N = int(input())

if N < 10:
    print(N)
    exit()

n = N - int((len(str(N)) - 1) * '9')
tmp = (len(str(N)) - 1) * '9'
if len(str(n)) > (len(str(N)) - 1):
    tmp = str(n)[0] + tmp

res = 0
for t in tmp:
    res += int(t)

print(res)

Submission status: Accepted

Solution code:
#from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from fractions import gcd
from itertools import combinations,permutations,accumulate # (string,3) 3回
#from collections import deque
from collections import deque,defaultdict,Counter
import decimal
import re
#import bisect
#
#    d = m - k[i] - k[j]
#    if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#
# my_round_int = lambda x:np.round((x*2 + 1)//2)
# 四捨五入
import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7
#mod = 9982443453
def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())
n = input()
if len(n) == 1:
    print(n)
    exit()
allnine = True
for i in range(1, len(n)):
    if n[i] == '9':
        pass
    else:
        allnine = False
if n[0] == '9' and allnine:
    print(9 * len(n))
    exit()
if n[0] == '1' and allnine:
    print(1 + (len(n)-1) * 9)
    exit()
if n[0] != '1' and allnine:
    print(int(n[0]) + (len(n)-1) * 9)
    exit()
print(int(n[0]) + (len(n)-1) * 9 - 1)

Submission status: Accepted

Solution code:
N = int(input())
a = 0
b = 0
while N > 0:
    if N < 10:
        c = N
        break
    if N % 10 != 9:
        b = 1
    N //= 10
    a += 1
if b == 1:
    c -= 1
ans = c + a * 9
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
S = input()
flag = 0
for dig in S[1:]:
    if dig != "9":
        flag = 1
 
ans = int(S[0]) + 9 * (len(S)-1)
 
if flag == 1:
    ans -= 1
 
print(ans)

Submission status: Accepted

Solution code:
N = input().strip()
k = len(N)
cmax = 0
for i in range(k):
    cmax += int(N[i])
if k>1:
    cnt = (int(N[0])-1)+9*(k-1)
    cmax = max(cmax,cnt)
print(cmax)

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 8)
ini = lambda: int(sys.stdin.readline())
inm = lambda: map(int, sys.stdin.readline().split())
inl = lambda: list(inm())
ins = lambda: sys.stdin.readline().rstrip()
debug = lambda *a, **kw: print("\033[33m", *a, "\033[0m", **dict(file=sys.stderr, **kw))

X = ins()


def solve():
    ans = sum([int(d) for d in X])

    ans = max(ans, int(X[0]) - 1 + 9 * (len(X) - 1))

    return ans


print(solve())

Submission status: Accepted

Solution code:
N = int(input())
n = len(str(N))
NS = str(N)
ans0 = 0
for i in NS:
    ans0 += int(i)
ans1 = int(NS[0])-1 + 9 * (n-1)
ans2 = 9 * (n-1)

print(max(ans0, ans1, ans2))

Submission status: Accepted

Solution code:
s=list(input())
n=len(s)
for i in range(n):
  s[i]=int(s[i])
dec_flg=0
for i in range(n):
  if i==0:continue
  if dec_flg==0:
    if 0<=s[i]<=8 and s[i-1]>0:
      s[i-1]-=1
      s[i]=9
      dec_flg=1
  else:
    if 0<=s[i]<=8:
      s[i]=9
ans=0
for ss in s:
  ans+=ss
print(ans)

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 8)
ini = lambda: int(sys.stdin.readline())
inm = lambda: map(int, sys.stdin.readline().split())
inl = lambda: list(inm())
ins = lambda: sys.stdin.readline().rstrip()
debug = lambda *a, **kw: print("\033[33m", *a, "\033[0m", **dict(file=sys.stderr, **kw))

s = ins()


def solve():
    ans = 9 * (len(s) - 1)
    x = [int(d) for d in s]
    sx = sum(x)
    ans = max(ans, sx)
    ans = max(ans, (x[0] - 1) + 9 * (len(s) - 1))
    return ans


print(solve())

Submission status: Accepted

Solution code:
N = input().strip()
k = len(N)
a = int(N[0])
if k==1:
    cmax = a
else:
    cnt = 0
    for i in range(k):
        cnt += int(N[i])
    cmax = max((a-1)+9*(k-1),cnt)
print(cmax)

Submission status: Accepted

Solution code:
N = input()
ans = sum(int(c) for c in N)

for i in range(1,len(N)):
    t = sum(int(c) for c in N[:i]) - 1 + 9 * (len(N) - i)
    ans = max(ans, t)
print(ans)

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N = [int(i) for i in list(input())]
length = len(N)

for l in N[1:]:
    if not l==9:
        ans = (length-1)*9+(N[0]-1)
        print(ans)
        break
else:
    print(sum(N))

Submission status: Accepted

Solution code:
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


n = list(map(int, input()))
ans = sum(n)
s = 0
for i,num in enumerate(n):
    if num>=1:
        ans = max(ans, s + (num-1) + 9*(len(n)-i-1))
    s += num
print(ans)

Submission status: Accepted

Solution code:
N = input()
res = sum(map(int, N))
res = max(res, max(0, int(N[0]) - 1 + 9 * (len(N) - 1)))
for i in range(1, len(N) - 1):
    if int(N[i]) > 0:
        t = 0
        for j in range(i):
            t += int(N[j])
        t += int(N[i]) - 1 + 9 * (len(N) - i - 1)
        res = max(res, t)
print(res)

Submission status: Accepted

Solution code:
n = int(input())

di = len(str(n))
ans = sum([int(w)for w in str(n)])

for i in range(di):
    digits = [int(w)for w in str(n)]
    if digits[i] == 0:
        continue
    t = sum(digits[0:i])

    t += digits[i]-1
    t += 9 * (di - i - 1)
    ans = max(ans, t)

print(ans)

Submission status: Accepted

Solution code:
n=input()
nn=len(n)
if nn==1:
    print(n)
    exit()
flg=True
for i in range(1,nn):
    if n[i]!="9":
        flg=False
ans=0
if flg:
    for i in range(nn):
        ans+=int(n[i])
    print(ans)
    exit()
if flg==False:
    if n[0]!="1":
        print(int(n[0])-1+9*(nn-1))
    if n[0]=="1":
        print(9*(nn-1))

Submission status: Accepted

Solution code:
def f(n):
    n = list(map(int, str(n)))
    if len(n) == 1:
        print(n[0])
    else:
        if sum([x - 9 for x in n]) != 0:
            mx = 9 * (len(n)-1) + n[0] - 1
        else:
            mx = 9 * len(n)
        print(max(mx, sum(n)))

n = int(input())
f(n)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
n = input()
print((len(n) - 1) * 9 + int(n[0]) - 1 * (n[1:] != "9" * (len(n)-1)))

Submission status: Accepted

Solution code:
S=str(int(input())+1)
N=[]
for s in S:
    N.append(int(s))
L=len(S)

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

Submission status: Accepted

Solution code:
# n, m, l = map(int, input().split())

# list_n = list(map(int, input().split()))

# n = input()
# list = [input() for i in range(N)

# list = [[i for i in range(N)] for _ in range(M)]

import math
import sys
input = sys.stdin.readline

N = int(input())
ans = 0
k = int(math.log10(N)) + 1
A = N//(10**(k-1))
if (A + 1) * (10 ** (k - 1)) - 1 <= N:
    ans = A + 9*(k-1)
else:
    ans = A-1+9*(k-1)
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import deque, defaultdict
from math import sqrt, factorial
# def input(): return sys.stdin.readline()[:-1] # warning not \n
# def input(): return sys.stdin.buffer.readline().strip() # warning bytes
# def input(): return sys.stdin.buffer.readline().decode('utf-8')


def solve():
    s = [x for x in input()]
    n = int(''.join(s))
    k = len(s)
    a = s[0] + '9' * (k - 1)
    b = int(a)
    a = [int(x) for x in a]
    if b > n:
        a[0] -= 1
    print(sum(a))


t = 1
# t = int(input())
for case in range(1,t+1):
    ans = solve()


"""


"""

Submission status: Accepted

Solution code:
n=input()
c=0
for i in range(1,int(len(n))):
    if n[i]=="9":
        c+=1
if c==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:
def digits_sum(N):
	sum=0
	while N/10!=0:
		sum+=N%10
		N//=10
	return sum
	
N=input()
N=list(N)
for k in range(len(N)):
	N[k]=int(N[k])
ans=0
pos=0
for i in range(1,len(N)):
	if N[i]!=9:
		N[i-1]=N[i-1]-1
		pos=i
		break
if pos==0:
	print(sum(N))
else:
	for j in range(pos):
		ans+=N[j]
	ans+=9*(len(N)-pos)
	print(ans)

Submission status: Accepted

Solution code:
N = input()

ans1 = sum([int(c) for c in N]) 
ans2 = int(N[0]) - 1 + 9 * (len(N) - 1)
print(max(ans1, ans2))

Submission status: Accepted

Solution code:
a=list(input());x,y=len(a)-1,int(a[0]);print(9*x+y if all(i=='9'for i in a[1:])else y-1+9*x)

Submission status: Accepted

Solution code:
a=list(input());x,y=len(a)-1,int(a[0]);print(9*x+y if all(i=='9' for i in a[1:]) else y-1+9*x)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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