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

Solution (Python):
N=input()
n=int(N)
N3=[int(x) for x in list(N)]

if n<10:
    print(n)
    exit()
else:
    ans=sum(N3)
    for i in range(len(N)-1):
        N2=int(N[:i+1])-1
        
        N1=[int(x) for x in list(str(N2))]
        ans=max(ans,sum(N1)+9*(len(N)-i-1))
    print(ans)

Solution (Python):
import sys

def IS(): return sys.stdin.readline().rstrip()
def II(): return int(IS())
def MII(): return list(map(int, IS().split()))
def MIIZ(): return list(map(lambda x: x-1, MII()))

def DD2(d1,d2,init=0): return [[init]*d2 for _ in range(d1)]
def DD3(d1,d2,d3,init=0): return [DD2(d2,d3,init) for _ in range(d1)]

def to_bin(x: int) -> str: return format(x, 'b') 
def to_oct(x: int) -> str: return format(x, 'o') 
def to_hex(x: int) -> str: return format(x, 'x') 
MOD=10**9+7
def divc(x,y) -> int: return -(-x//y)
def divf(x,y) -> int: return x//y
def gcd(x,y):
    while y: x,y = y,x%y
    return x
def lcm(x,y): return x*y//gcd(x,y)
def enumerate_divs(n):
    """Return a tuple list of divisor of n"""
    return [(i,n//i) for i in range(1,int(n**0.5)+1) if n%i==0]
def get_primes(MAX_NUM=10**3):
    """Return a list of prime numbers n or less"""
    is_prime = [True]*(MAX_NUM+1)
    is_prime[0] = is_prime[1] = False
    for i in range(2, int(MAX_NUM**0.5)+1):
        if not is_prime[i]: continue
        for j in range(i*2, MAX_NUM+1, i):
            is_prime[j] = False
    return [i for i in range(MAX_NUM+1) if is_prime[i]]

from itertools import accumulate as acc
from collections import deque, Counter
from heapq import heapify, heappop, heappush
from bisect import bisect_left


def main():
    n = II()
    s = str(n)
    d = len(s)
    maxv = 0
    sumv = int(s[0])+(d-1)*9
    sumc = s[0]+'9'*(d-1)
    if int(sumc) > n:
        sumv -= 1
    print(sumv)

if __name__ == '__main__':
    main()

Solution (Python):
n =input()





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)

Solution (Python):
n = int(input())

def get_first(n):
	return int(str(n)[0])

def check(n):
	
	s = str(n)
	for i in range(1, len(str(n))):
		if str(s[i]) != '9':
			return False
	return True

if check(n):
	res = get_first(n) + 9 * (len(str(n)) - 1)
else:
	res = (get_first(n) - 1) + 9 * (len(str(n)) - 1)

print(res)

Solution (Python):
n = int(input())
s= str(n)

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

ans = 0

for i in range(1,len(s)):
    if int(s[i])!=9:
        for j in range(i):
            ans += int(s[j])
        ans-=1
        ans += 9*(len(s)-i)
        print(ans)
        exit()

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

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

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

Solution (Python):
n = input()
m = int(n)
c = n[0]
k = len(n)
tmp = int(c + "9"*(k-1))
ans = int(c) + (k-1)*9
if tmp > m:
    ans = (int(c)-1) + (k-1)*9
print(ans)

Solution (Python):
N = input()
c = N[0]
K = len(N)

temp = int(c + "9"*(K-1))
temp_s = sum(int(a) for a in list(str(temp)))

if temp <= int(N):
    print(temp_s)
else:
    print(temp_s - 1)

Solution (Python):
N_str = input()
N = int(N_str)
ans = 0
for i in range(len(N_str)):
  cand = 0
  tmp = '9'*i + N_str[i] + '9'*(len(N_str)-i-1)
  if int(tmp) <= N:
    cand = int(tmp)
  else:
    if int(N_str[i]) >= 1:
      tmp = '9'*i + str(int(N_str[i])-1) + '9'*(len(N_str)-i-1)
    if  int(tmp) <= N:
      cand = int(tmp)
  ans = max(ans, sum([int(l) for l in str(cand)]))
print(ans)

Solution (Python):
N = int(input())

if N > 9:
    if (N%(int("1" + (len(str(N)) - 1)*"0"))) == int((len(str(N)) - 1) * "9"):
        tmp = list(str(N))
        tmp = [int(item) for item in tmp]
        print(sum(tmp))
    else:
        tmp = list(str(N - (N%(int("1" + (len(str(N)) - 1)*"0"))) - 1))
        tmp = [int(item) for item in tmp]
        print(sum(tmp))
else:
    print(N)

Solution (Python):
N = input()
ans = int(N[0])+9*(len(N)-1)
if not all([N[i] == '9' for i in range(1,len(N))]):
    ans -= 1
print(ans)

Solution (Python):
N=input()
L=len(N)
dp=[[0,0] for _ in range(L+1)]




dp[1]=[int(N[0]),int(N[0])-1]
for i in range(1,L):
    for j in range(2):
        for d in range(10 if j else int(N[i])+1):
            dp[i+1][int(j or d<int(N[i]))]=max(dp[i][j]+d,dp[i+1][int(j or d<int(N[i]))])
print(max(dp[-1]))

Solution (Python):
S=input()
dp=[[0 for i in range(len(S))] for j in range(2)]
if len(S)==1:
    print(S)
    exit()
dp[0][0]=int(S[0])
dp[1][0]=int(S[0])-1
for i in range(len(S)-1):
    dp[0][i+1] = dp[0][i]+int(S[i+1])
    dp[1][i+1] = max(dp[0][i]+int(S[i+1])-1,dp[1][i]+9)
print(max(dp[1][len(S)-1],dp[0][len(S)-1]))

Solution (Python):










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

if len(N) == 1:
    print(int(N))
    exit()
ans = 0
if ok:
    for i in range(len(N)):
        ans += int(N[i])
else:
    if N[0] == '1':
        for i in range(len(N)-1):
            ans += 9
    else:
        ans += int(N[0]) - 1
        for i in range(len(N)-1):
            ans += 9

print(ans)

Solution (Python):



S = (input())

if S.count("9") == len(S):
    print(len(S)*9)

elif len(S) == 1:
    print(int(S))

elif S.count("9") == (len(S)-1) and (S[0]!="9"):
    print((int(S[0])) + (len(S)-1)*9)

elif S[0] == "9":
    print(8 + ((len(S)-1)*9))
else:
    print((int(S[0])-1) + (len(S)-1)*9)

Solution (Python):
import sys
import time
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7

def main():
    N = input()
    L = len(N)
    if L == 1:
        ans = int(N)
    elif set(N[1:]) == {'9'}:
        ans = int(N[0]) + 9*(L-1)
    else:
        ans = (int(N[0])-1) + 9*(L-1)
    print(ans)
if __name__ == '__main__':
    main()

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def digit_sum(n):
    res = 0
    while n > 0:
        res += n % 10
        n //= 10
    return res


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

    res = 0
    for i in range(l):
        if i == 0:
            res += int(n[i]) - 1
        else:
            res += 9

    res = max(res, digit_sum(int(n)))
    print(res)


if __name__ == '__main__':
    resolve()

Solution (Python):

s = input()
out = True
for c in s[1:]:
    if c != '9':
        out = False
        break

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

Solution (Python):
n = input()
A = []
sum = 0
for i in n:
    A.append(i)
    sum += int(i)

if A[0] == '1':
    print(max(sum, (len(A) - 1) * 9))
else:
    print(max(sum, (len(A) - 1) * 9 + int(A[0]) - 1))

Solution (Python):
from statistics import median


from fractions import gcd
from itertools import combinations 
from collections import deque
from collections import defaultdict
import bisect










import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7

def readInts():
  return list(map(int,input().split()))
def main():
    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)

if __name__ == '__main__':
  main()

Solution (Python):
N=int(input())
s=str(N)
ans=0
ans=9*(len(s)-1)
ans+=(int(s[0])-1)

val=0
for i in range(len(s)):
    val+=int(s[i])

ans=max(ans,val)
print(ans)

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

Solution (Python):
def myAnswer(N:int)->int:
   length = len(str(N))
   if(length == 1): return N
   l = list(str(N))
   tmp = 0
   for n in l[1:]:
      tmp += int(n)
   total = length * 9
   if(tmp == total - 9):
      return tmp + int(l[0])
   else:
      total += -9 + int(l[0]) - 1
      return total
def modelAnswer():
   tmp=1
def main():
   N = int(input())
   print(myAnswer(N))

if __name__ == '__main__':
   main()

Solution (Python):
import sys



def digit_sum(str_num):
	sum = 0	
	
	for i in range(len(str_num)):
		sum += int(str_num[i])
	
	return sum


N = input()
patterns = [N]

for i in range(len(N)-1):
	if N[i] != '0':
		left, right = '', ''  
				
		if i > 0:
			left = N[:i]
		
		n = int(N[i]) - 1
		
		if i < len(N) - 1:
			right = '9' * (len(N)-i-1) 
		
		patterns.append(left + str(n) + right)

ans = 0

for p in patterns:
	ans = max(ans, digit_sum(p))
	
print(ans)

Solution (Python):
n = int(input())
str_n = str(n)
n_1 = str_n[0]
tmp = int(n_1+"9"*(len(str_n)-1))
ans = int(n_1) + 9*(len(str_n)-1)

if n == tmp:
    print(ans)
else:
    print(ans-1)

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

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

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

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    
    from itertools import combinations, permutations, accumulate, groupby, product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    from math import floor, ceil
    

    
    

    n = input().rstrip()
    def digsum(n):
        res = 0
        while n:
            res += n%10
            n //= 10
        return res
    if len(n)==1:
        print(n)
    else:
        if n.count('9')==len(n):
            print(9*len(n))
        elif n.count('9')==len(n)-1 and n[0]!='9':
            print(digsum(int(n)))
        else:
            print(9*(len(n)-1)+int(n[0])-1)

if __name__ == '__main__':
    main()

Solution (Python):
f, *n = str(int(input())+1)
print(int(f) + 9*len(n) - 1)

Solution (Python):
N = int(input())
if set(list(str(N))) == {"9"}:
    print(9 * len(str(N)))
elif N < 10:
    print(N)
elif set(list(str(N)[1:])) == {"9"}:
    print(int(str(N)[0]) + 9 * len(str(N)[1:]))
else:
    print(9 * (len(str(N)) - 1) + int(str(N)[0]) - 1)

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

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

Solution (Python):
N = input()
Dig = len(N)
num = None
if Dig == 1:
    print(N)
else:
    for i in range(Dig-1):
        if N[i+1] != str(9):
            num = i
            break
        else:
            continue
    list_num = list(N)
    if not num == None:
        target = int(list_num[num]) -1
        array = list(map(int,N))
        ans = sum(array[:num]) + target + 9*(Dig-num-1)
        print(ans)
    else:
        array = list(map(int,N))
        print(sum(array))

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

intN = int("".join(list(map(str, N))))
sumN = sum(N)

for x in range(len(N)):
    if x == 0:
        N[x] = N[x] - 1
    else:
        N[x] = 9
intN2 = int("".join(list(map(str, N))))
sumN2 = sum(N)
print(max(sumN, sumN2))

Solution (Python):
n = str(input())

o = len(n)-1

if o == 0:
    print(n)
elif n[1:] == "9" * o:
    print(int(n[0]) + 9*o)
else:
    print(int(n[0]) - 1 + 9*o)

Solution (Python):

import sys
import math

def main():
    N = int(input())
    digit = math.floor(math.log10(N))
    c = N // 10**digit
    if N == (1 + c) * 10 ** digit - 1:
        print(9 * digit + c)
    else:
        print(9 * digit + c -1)


if __name__ == '__main__':
    main()

Solution (Python):
import sys
import math

def main():
    N = int(input())
    digit = int(math.log10(N))
    c = N // (10**digit)
    if N < int(str(c) + '9'*digit):
        print(9 * digit + c -1)
    else:
        print(9 * digit + c)

if __name__ == '__main__':
    main()

Solution (Python):
















N = input()

if int(N)>=10:
    size = len(N)
    top = int(N[0])
    ans = (size-1)*9 + top-1
    num = sum(map(int,list(N)))
    ans = max(ans,num)
else:
    ans = int(N)
    
print(ans)

Solution (Python):

import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

def main():
    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)
            break
    else:
        ans=N[0]+9*(len(N)-1)
    print(ans)
        

if __name__ == '__main__':
    main()

Solution (Python):
n = str(int(input().rstrip())+1)
ans = (len(n)-1)*9
ans += (int(n[0])-1)
print(ans)

Solution (Python):
import math
n = int(input())
l = int(math.log10(n))
ma = n//10**l
ans = ma+l*9

if int(str(ma)+'9'*l) > n:
    print(ans-1)
else:
    print(ans)

Solution (Python):

def myAnswer(N:int)->int:
   K = len(str(N)) 
   if(K == 1):return N
   c = N // 10**(K-1) 
   if((c*10**(K-1) + int("9"*(K-1)))<= N):
      return c + 9 *(K - 1)
   else:
      return c + 9 *(K - 1) -1 


def modelAnswer():
   tmp=1
def main():
   N = int(input())
   print(myAnswer(N))

if __name__ == '__main__':
   main()

Solution (Python):
x = int(input())
x += 1

ans = 9 * (len(str(x))-1) + int(str(x)[0])-1
print(ans)

Solution (Python):
n = str(int(input().rstrip())+1)
ans = (len(n)-1)*9
ans += (int(n[0])-1)
print(ans)

Solution (Python):
N = input()

c = N[0]
ans = int(c) + 9 * (len(N) - 1)
if int(N) < int(c + '9' * (len(N) - 1)):
    ans -= 1
print(ans)

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

Solution (Python):
import sys
import fractions

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = ir()
NS = list(str(N))

q, mod = divmod(N, 10)
i = -1
while q:
    if mod < 8:
        NS[i:] = ['9' for i in range(-i)]
        NS[i-1] = str(int(NS[i-1]) - 1)
    i -= 1
    q, mod = divmod(q, 10)

print(sum(map(int, NS)))

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 5 + 10)
def input(): return sys.stdin.readline().strip()

def resolve():
    N = input()
    M = [int(c) for c in N]

    lm=len(M)
    cnt=0
    if lm==1:
        print(M[0])
    elif set(M[1:])=={9}:
        for i in M:
            cnt+=i
        print(cnt)
    else:
        print(9*(lm-1)+M[0]-1)

resolve()

Solution (Python):
s = list(input())

for i in range(1,len(s)):
    if s[i] != "9":
        print(int(s[0])-1+9*(len(s)-1))
        exit()
print(int(s[0])+9*(len(s)-1))

Solution (Python):
n=input()
kouho1="9"*(len(n)-1)

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

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

Solution (Python):
n = input()
for i in n[1:]:
    if i != '9':
        print((len(n) - 1) * 9 - 1 + int(n[0]))
        exit()
ans = 0
for i in n:
    ans += int(i)
print(ans)

Solution (Python):
N = int(input())
original = N

cnt = 0
while N >= 10:
    N //= 10
    cnt += 1
    
temp = (N+1) * 10 ** cnt - 1
temp2 = N * 10 ** cnt - 1


if temp == original:
    print(sum(list(map(int, list(str(temp))))))
else:
    print(sum(list(map(int, list(str(temp2))))))

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

Solution (Python):
n=int(input())
digit=len(str(n))
def su(k):
  t=[int(str(k)[i]) for i in range(len(str(k)))]
  return sum(t)
m=su(n)
for i in range(1,digit):
  m=max(m,su(pow(10,i)*(n//pow(10,i))-1))
print(m)

Solution (Python):

import sys
sys.setrecursionlimit(10 ** 5 + 10)
def input(): return sys.stdin.readline().strip()

def resolve():
    N = input()
    M = [int(c) for c in N]
    lm=len(M)
    NN=int(N)
    if lm==1:
        print(NN)
    else:
        if set(M[1:])=={9}:
            print(M[0]+(lm-1)*9)
        else:

            print(M[0]-1+(lm-1)*9)



resolve()

Solution (Python):



import sys
input = sys.stdin.readline

def main():
    N = str(int(input()))
    K = len(N)
    c = int(N[0])
    x = sum([int(i) for i in N])
    print(max(c-1+(K-1)*9, x))

if __name__ == '__main__':
    main()

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

Solution (Python):
n = input()

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

print(ans)

Solution (Python):
n = input()

def sumdigits(num):
  cc = 0
  for c in num:
    cc += int(c)
  return cc

if all([s == '9' for s in n]):
  print(9 * len(n))
else:
  print(max(9 * (len(n) - 1) + int(n[0]) - 1, sumdigits(n)))

Solution (Python):
N = input()

def digsum(n):
    total = 0
    while n > 0:
        total += n%10
        n /= 10
        n = int(n)
    return total

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

if N[1:].count('9') == len(N)-1:
    print(digsum(int(N)))
    exit()

ans = int(str(int(N[0])-1) + '9'*(len(N)-1))
print(digsum(ans))

Solution (Python):
N = input()

def digsum(n):
    total = 0
    while n > 0:
        total += n%10
        n /= 10
        n = int(n)
    return total

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

if N[1:].count('9') == len(N)-1:
    print(digsum(int(N)))
    exit()

ans = int(str(int(N[0])-1) + '9'*(len(N)-1))
print(digsum(ans))

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

Solution (Python):

r = input()

ans = sum(list(map(int, r)))

if r[0] == 1:
    print(max(ans, (len(r)-1)*9))
else:
    print(max(ans, int(r[0])-1+(len(r)-1)*9))

Solution (Python):
import sys
r = input()

ans = sum(list(map(int, r)))

if len(r) == 1:
    print(max(ans, int(r)))
    sys.exit()
if len(str(int(r)+1)) == len(r)+1:
    print(max(ans, len(r)*9))
    sys.exit()
if r[0] == 1:
    print(max(ans, (len(r)-1)*9))
else:
    print(max(ans, int(r[0])-1+(len(r)-1)*9))

Solution (Python):
import heapq
from collections import defaultdict, deque
from math import ceil, factorial
from fractions import gcd
import sys

sys.setrecursionlimit(10 ** 7)
INF = float("inf")
MOD = 10 ** 9 + 7

si = lambda: input().strip()
ii = lambda: int(input())
mii = lambda: map(int, input().split())
lmii = lambda: list(map(int, input().split()))
smii = lambda: sorted(map(int, input().split()))


S = si()

digit = len(S)

head, remaining = S[0], S[1:]

ans = int(head) + 9 * (digit-1)
if remaining != '9' * (digit-1):
    ans -= 1

print(ans)

Solution (Python):
S = input()
for s in S[1:]:
    if s != '9':
        print(int(S[0]) - 1 + 9 * (len(S) - 1))
        break
else:
    print(sum([int(s) for s in S]))

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

def ketawa(x):
	return sum(map(int, list(str(x))))

N = input()

if len(N) == 1:
	print(N)
else:
	if N[0] == 1:
		print(max(ketawa(N),9*(len(N)-1)))
	else:
		print(max(ketawa(N), int(N[0])-1+9*(len(N)-1)))

Solution (Python):
N = input()
ans = 0
ans1 = int(N[0])-1
for i in range(len(N)):
    ans += int(N[i])

for i in range(1, len(N)):
    ans1 += 9
print(max(ans, ans1))

Solution (Python):
n = int(input())
ans = -1
for i in range(18):
    for j in range(2, 11):
        m = (10 ** i) * j - 1
        ans += 1
        if m > n:
            print(ans)
            exit()

Solution (Python):
n = int(input())
if n <= 8:
    print(n)

else:
    
    sn = str(n)
    num = len(sn)
    if sn[1:] == "9" * (num - 1):
        print(int(sn[0]) + 9 *(num - 1))
    else:
        print((int(sn[0]) - 1) + 9 * (num - 1))

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

Solution (Python):
N = input()
if len(N) == 1:
  
  print(N)
elif all(map(lambda x: x == '9', N[1:])):
  
  print(sum(int(n) for n in N))
else:
  
    print(9 * (len(N) - 1) + int(N[0]) - 1)

Solution (Python):
def digitSum(n):
    digitSum = 0
    while n > 0:
        digitSum += n%10
        n //= 10
    return digitSum

n = input()
length = len(n)

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

Solution (Python):
def digitSum(n):
    digitSum = 0
    while n > 0:
        digitSum += n%10
        n //= 10
    return digitSum

n = int(input())
n_str = str(n)

left = 0
right = 0

if n_str[0] == 1:
    left = int("9" * (len(n_str) - 1))
    right = int("1" + left)
else:
    left = int(str(int(n_str[0])-1) + "9" * (len(n_str) - 1))
    right = int(n_str[0] + "9" * (len(n_str) - 1))

if left <= n < right:
    print(digitSum(left))
elif n == right:
    print(digitSum(right))

Solution (Python):
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))

Solution (Python):
N = int(input())
ans = int(str(N)[0])-1
ans += (len(str(N))-1)*9
if int(str(N)[0]+str('9'*(len(str(N))-1))) == N:
    ans += 1
print(ans)

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

Solution (Python):
N = input()
lenN = len(N)
print((int(N) - (int(10**(lenN-1))-1))//(10**(lenN-1)) + 9 * (lenN -1))

Solution (Python):

def inputlist(): return [int(j) for j in input().split()]


N = input()
n = len(N)
dp = [[0,0] for _ in range(len(N)+1)]
dp[1][0] = int(N[0])-1
dp[1][1] = int(N[0])
for i in range(2,len(N)+1):
    if int(N[i-1]) != 0:
        dp[i][0] = max(dp[i-1][0]+9,dp[i-1][1]+int(N[i-1])-1)
        dp[i][1] = dp[i-1][1] + int(N[i-1])
    if int(N[i-1]) == 0:
        dp[i][1] = dp[i-1][1]
        dp[i][0] = dp[i-1][0]+9
print(max(dp[n][0],dp[n][1]))

Solution (Python):
n = input()
t = n[0] + '0'*(len(n)-1)
t = str(int(t)-1)
r1 = sum(list(int(d) for d in t))
r2 = sum(list(int(d) for d in n))
print(max(r1,r2))

Solution (Python):
import math
import fractions
import bisect
import collections
import itertools
import heapq
import string
import sys
import copy
from decimal import *
from collections import deque
sys.setrecursionlimit(10**7)
MOD = 10**9+7
INF = float('inf') 
def gcd(a,b):return fractions.gcd(a,b) 
def lcm(a,b):return (a*b) // fractions.gcd(a,b) 
def iin(): return int(sys.stdin.readline()) 
def ifn(): return float(sys.stdin.readline()) 
def isn(): return sys.stdin.readline().split() 
def imn(): return map(int, sys.stdin.readline().split()) 
def imnn(): return map(lambda x:int(x)-1, sys.stdin.readline().split()) 
def fmn(): return map(float, sys.stdin.readline().split()) 
def iln(): return list(map(int, sys.stdin.readline().split())) 
def iln_s(): return sorted(iln()) 
def iln_r(): return sorted(iln(), reverse=True) 
def fln(): return list(map(float, sys.stdin.readline().split())) 
def join(l, s=''): return s.join(l) 
def perm(l, n): return itertools.permutations(l, n) 
def perm_count(n, r): return math.factorial(n) // math.factorial(n-r) 
def comb(l, n): return itertools.combinations(l, n) 
def comb_count(n, r): return math.factorial(n) // (math.factorial(n-r) * math.factorial(r)) 
def two_distance(a, b, c, d): return ((c-a)**2 + (d-b)**2)**.5 
def m_add(a,b): return (a+b) % MOD
def print_list(l): print(*l, sep='\n')

def sieves_of_e(n):
    is_prime = [True] * (n+1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, int(n**0.5)+1):
        if not is_prime[i]: continue
        for j in range(i * 2, n+1, i): is_prime[j] = False
    return is_prime

N = str(iin())
sum_N = 0
for n in N:
    sum_N += int(n)

ans = 0
if N[0] != 1: ans = int(N[0])-1
ans += (len(N)-1)*9
print(max(sum_N, ans))

Solution (Python):
N = input()
tmp = 0
for i in N:
    tmp += int(i)
M = len(N)
ans = int(N[0])-1 + 9*(M-1)
print(max(ans,tmp))

Solution (Python):
def main():
    n = int(input())
    l = len(str(n))
    v = int(str(n)[0]) + 9 * (l - 1)
    if str(n)[1:] == '9' * (l - 1):
        print(v)
    else:
        print(v-1)

if __name__ == '__main__':
    main()

Solution (Python):
N=int(input())
S=str(N)
flag=False 

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

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

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

Solution (Python):
N=str(input())
L=len(N)
dp=[[0]*2 for _ in range(L)]
dp[0][0]=int(N[0])
dp[0][1]=int(N[0])-1
for i in range(1,L):
  dp[i][1]=max(dp[i-1][0]+max(0,int(N[i])-1),dp[i-1][1]+9)
  dp[i][0]+=(dp[i-1][0]+int(N[i]))
print(max(dp[L-1][0],dp[L-1][1]))

Solution (Python):

N = input()
if int(N) < 10:
    print(N)

elif all(map(lambda x: x == '9', N[1:])):
    print(sum(int(n) for n in N))

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

Solution (Python):
ans = 0
a = input()
num_len = len(a)
num = [k for k in a]
for i in range(1,num_len):
  if num[i] != "9":
    num[i-1]= chr(ord(num[i-1])-1)
    for j in range(i,num_len):
      num[j] = "9"
    break
for w in num:
  ans = ans + int(w)
print(ans)

Solution (Python):

import sys


def solve(N: int):
    S = str(N)
    keta = len(S)
    if S[1:].count("9") == keta-1:
        print(9*(keta-1)+int(S[0]))
        return
    
    if keta == 1:
        print(N)
        return

    print(int(S[0])-1+9*(keta-1))

    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    solve(N)

if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
n_str = str(n)
num = len(n_str)
hikaku = int(str(n_str[0]) + str(9)*(num-1))
atai = int(int(n_str[0]) + int(9)*(num-1))
if n >= hikaku:
    print(atai)
else:
    print(atai-1)

Solution (Python):
def main():

    n = int(input())
    list_rev = []
    temp = list(str(n))
    for i in range(len(temp)):
        list_rev.append(int(temp[-i-1]))
    ans = list_rev[0]
    for i in range(1,len(temp)):
        a = list_rev[i] + ans
        b = list_rev[i] - 1 + 9 * i
        ans = max(a,b)
    print(ans)


def digit_int(num, dig):
    str_num = str(num)
    list_num = list(str_num)
    return list_num[-dig]
if __name__ == '__main__':
    main()

Solution (Python):
def get_digit_sum(n):
    result = 0
    for d in n:
        result += int(d)
    return result


def main():
    n = input()
    answer = str(int(n[0]) - 1) + "9" * (len(n) - 1)
    print(max(get_digit_sum(answer), get_digit_sum(n)))


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    N = input()
    t = 0
    l = []
    for i in N:
        t += int(i)
        l.append(t)
    n = len(N)
    for i, v in enumerate(N):
        if int(v) > 0:
            t = max(l[i] - 1 + 9*(n - i - 1), t)
    return t
print(main())

Solution (Python):
if __name__ == "__main__":
    n = input()

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

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

    ans = 0
    for s in tmp:
        ans += int(s)

    print(ans)

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

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

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

Solution (Python):
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
 
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()







    

S = str(N)
su = 0



wa = 0
wa = 9 * (len(S) - 1) 





if S[1:] == (len(S)-1) * "9":
    ans = 9 * (len(S)-1) + int(S[0])
    print(ans)


else:
    ans = wa + int(S[0]) - 1
    
    
    print(ans)

Solution (Python):
def main():
    S = [int(i) for i in input()]
    print(max(sum(S), S[0]-1 + (len(S)-1)*9))


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    S = input()
    N = len(S)
    if N == 1:
        print(S)
    elif all("9" == s for s in S):
        print(N*9)
    elif all("9" == s for s in S[1:]):
        print(int(S[0]) + 9*(N-1))
    elif S[0] == "1":
        print(9*(N-1))
    else:
        print((int(S[0])-1) + 9*(N-1))


if __name__ == '__main__':
    main()

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