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):


import sys, re
from collections import deque, defaultdict, Counter
from math import sqrt, hypot, factorial, pi, sin, cos, radians
if sys.version_info.minor >= 5: from math import gcd
else: from fractions import gcd 
from heapq import heappop, heappush, heapify, heappushpop
from bisect import bisect_left, bisect_right
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from functools import reduce, partial
from fractions import Fraction
from string import ascii_lowercase, ascii_uppercase, digits

def input(): return sys.stdin.readline().strip()
def ceil(a, b=1): return int(-(-a // b))
def round(x): return int((x*2+1) // 2)
def fermat(x, y, MOD): return x * pow(y, MOD-2, MOD) % MOD
def lcm(x, y): return (x * y) // gcd(x, y)
def lcm_list(nums): return reduce(lcm, nums, initial=1)
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N = INT()

def digit_sum(i):
    sm = 0
    for s in str(i):
        sm += int(s)
    return sm

suff = '9999999999999'
ans = 0 

for i in range(12):
    
    for j in range(999, 99, -1):
        j = int(str(j) + suff)
        if j > N:
            continue
        ans = max(digit_sum(j), ans)
    suff = suff[:-1]

for i in range(9999, 0, -1):
        if i > N:
            continue
        ans = max(digit_sum(i), ans)
print(ans)

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

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

Solution (Python):
import sys
n = input()
tmpn = list(n[1:])
for i in tmpn:
    if i != '9':
        print(int(n[0]) - 1 + len(tmpn) * 9)
        sys.exit(0)
print(sum(map(int, list(n))))

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

Solution (Python):
S = input()
N = len(S)


def digitSum(n):
    s = str(n)
    array = list(map(int, list(s)))
    return sum(array)

ans = digitSum(S)
candidate = (int(S[0]) - 1) * pow(10, N-1) + (pow(10, N-1) - 1)
candidate = digitSum(candidate)

if ans > candidate:
    print(ans)
else:
    print(candidate)

Solution (Python):
a = input()
b = int(a)
c = list(a)
t = int(a[0])
r = t+(len(a)-1)*9

if t != 9  and a.count("9") == len(a)-1:
    r = t+(len(a)-1)*9
elif a.count("9") != len(a):
    t -= 1
    r = t+(len(a)-1)*9
if b<9:
    r = b
elif b == 9:
    r = 9
    
print(r)

Solution (Python):


N = input()

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

print(ans)

Solution (Python):
arr=[int(i) for i in input()]
csum=[arr[0]]
for i in range(1,len(arr)):
  csum.append(csum[-1]+arr[i])
maxsum= csum[-1]

print(max(maxsum,arr[0]-1+9*(len(arr)-1)))

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

Solution (Python):



def main():
    n = list(map(int, input()))
    c = n[0]

    if len(n) > 1:
        k = set(n[1:])

        if len(k) == 1 and k == {9}:
            print(c + 9 * (len(n) - 1))
        else:
            print(c + 9 * (len(n) - 1) - 1)
    else:
        print(c)


if __name__ == '__main__':
    main()

Solution (Python):
N=int(input())
keta=len(str(N))
num=10**keta-1
for i in range(10):
	if num<=N:
		ls=list(str(num))
		ans=0
		for j in range(len(ls)):
			ans+=int(ls[j])
		print(ans)
		exit(0)
	else:
		num-=10**(keta-1)

Solution (Python):
N=input()

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

a=0
for i in range(len(N)):
    a+=int(N[i])

print(max(ans,a))

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

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

Solution (Python):
N = int(input())
tmp = N
c = 0
while tmp//10!=0:
    tmp = tmp//10
    c += 1
ans = c*9+(tmp-1) if (N+1)%10**c!=0 else c*9+tmp
print(ans)

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

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

A = []
tmp = N

while tmp>0:
    A.append(tmp%10)
    tmp = (tmp - tmp%10)//10

ans1 = sum(A)

for i in range(len(A)-1):
    A[i] = 9
    A[i+1] = A[i+1] -1

print(max(ans1, sum(A)))

Solution (Python):
N=input()
m=len(N)
ans=0
for i in range(m-1):
  if N[i+1]=="9":
    ans+=int(N[i])
  else:
    ans+=int(N[i])-1
    ans+=9*(m-2-i)
    break
if m!=1:
  ans+=9
else:
  ans+=int(N)
print(ans)

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

if len(N) == 1:
    print(N[0])
else:
    if not all(map(lambda n: n == 9, N[1:])):
        for i in range(len(N)):
            if i == 0:
                N[i] -= 1
            else:
                N[i] = 9

    print(sum(N))

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

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

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

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

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

Solution (Python):
import math
N = int(input())
L = int(math.log10(N))
print(N // 10 ** L + 9 * L - ((N + 1) % 10 ** L > 0))

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

Solution (Python):
def listToString(_list, split=""):
    maped_list = map(str, _list)  
    mojiretu = split.join(maped_list)
    return mojiretu


n = int(input())
a = [int(i) for i in str(n)]
count = 0
for i in range(len(str(n))):
    
    now = a[-i-1:n]
    nowSum = 0
    for j in range(len(now)):
        nowSum += now[j]
    
    if a[-i-1] > 0:
        if nowSum < now[0] + 9 * (len(now)-1):
            a[-i-1] -= 1
            for k in range(1, len(now)):
                a[-i-1 + k] = 9
    
nowSum = 0
for i in range(len(a)):
    nowSum += a[i]
print(nowSum)

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

Solution (Python):
import sys


def next_str() -> str:
    result = ""
    while True:
        tmp = sys.stdin.read(1)
        if tmp.strip() != "":
            result += tmp
        elif tmp != '\r':
            break
    return result


def next_int() -> int:
    return int(next_str())


def main() -> None:
    n = next_int()
    flg = False
        
    for i, v in enumerate(str(n)):
        if i != 0 and v != '9':
            flg = True

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


if __name__ == '__main__':
    main()

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

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

if n<=9:
  print(n)
else:
  x=10**(l-1)
  y=int('9'*(l-1))
  sum=9*(l-1)
  sum+=(n-y)//x
  print(sum)

Solution (Python):
N = list(input())
N = [int(e) for e in N]
v1 = sum(N)
v2 = N[0] - 1 + 9 * (len(N) - 1)
print(max(v1,v2))

Solution (Python):
import math

N=list(input())
ans=0

for i in range(len(N)-1,-1,-1):
    if i!= 0 and int(N[i]) < 9:
        N[i-1]=str(int(N[i-1])-1)
        ans+=int(9)
    else:
        ans+=int(N[i])

print(ans)

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

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

p = 0
for i in range(len(L)) :
    p += int(L[i])


if len(L) == 1 :
    ans = int(L[0])
else :
    if L.count("9") == len(L) :
        ans = 9*len(L)
    elif L[0] == "1" :
        ans = 9*(len(L)-1)
    else :
        ans = (int(L[0])-1) + 9*(len(L)-1)
print(max(p,ans))

Solution (Python):
N = int(input())
N2 = N
logN = 0
while N2 // 10 > 0:
    logN += 1
    N2 //= 10

Ans1, Ans2 = 0, 0
for i in range(logN):
    Ans1 += 9*(10**i)
    Ans2 += 9*(10**i)
Ans1 += N2 * (10**logN)
Ans2 += (N2-1) * (10**logN)
if Ans1 <= N:
    print(N2 + 9 * logN)
else:
    print(N2 - 1 + 9 * logN)

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

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

if N[1:].count('9') == len(N)-1:
    print(sum([int(i) for i in N]))
    exit()

if N[0]!='9' or any(map(lambda x:x!='9',N)):
    n = chr(ord(N[0])-1) + N[1:]



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


if int(N) >= ans:
    print(sum([int(i) for i in str(ans)]))
else:
    print(sum([int(i) for i in str(ans)]) -1)

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

Solution (Python):
a = input()
a = list(a)
l = len(a)
for i in range(l):
    a[i] = int(a[i])
m = sum(a)
for i in range(l):
    n = a[i]-1 + 9*(l-i-1)
    if n > m:
        m = n
print(m)

Solution (Python):
N = input()

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

if len(N) -1 == N.count("9"):
    if N[0] == str(9):
        print(9 * (len(N)-1) + 8)
        exit()
        
    sd = 0
    N = int(N)
    while N > 0:
        sd += N % 10
        N //= 10
    print(sd)
    exit()

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

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

Solution (Python):
s = input()
a = int(s[0])
k = len(s)
n = int(s)

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

if (n + 1) % 10**(k-1) == 0:
    print(a + 9*(k-1))
    exit()
else:
    print(a - 1 + 9*(k-1))

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

Solution (Python):
n=int(input())
j=True
x=0
for i in range(len(str(n))):
    if str(n)[i]!='9':
        j=False
        x=i
if x==0:
    print(int(str(n)[0])+9*(len(str(n))-1))
else:
    print(int(str(n)[0])+9*(len(str(n))-1)-1)

Solution (Python):

N = raw_input()

a=int(N[0])
b=len(N)

flag=True
for i in range(1,len(N)):
    if int(N[i])!=9:
        flag=False

if flag:
    print a+9*(b-1)
else:
    print a+9*(b-1)-1

Solution (Python):
n=input()
if n<10:
	print n
	quit(0)
if str(n)[1:].count('9')==len(str(n)[1:]):
	print sum([ord(ch)-ord('0') for ch in str(n)])
else:
	print ord(str(n)[0])-ord('0')-1+9*len(str(n)[1:])

Solution (Python):
n = int(input())
strs = [int(x) for x in str(n)]
kijyun = [9]*(len(strs)-1)
if strs[1:] == kijyun:
    print((len(strs)-1)*9+strs[0])
else:
    print((len(strs)-1)*9+strs[0]-1)

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

Solution (Python):
listN = list(map(int, list(str(input()))))

index=0
while index < len(listN):
    if index ==0 and listN[index]!=9:
        index+=1
    else:
        if listN[index]!=9:
            listN[index-1]-=1
            index2 = index
            while index2 < len(listN):
                listN[index2]=9
                index2+=1
            index3=0
            count=0
            while index3 < len(listN):
                count+=listN[index3]
                index3+=1
            print(count)
            break
        else:
            index+=1
if index == len(listN):
    count=0
    index4=0
    while index4 < len(listN):
        count+=listN[index4]
        index4+=1
    print(count)

Solution (Python):
import math
n = int(input())
p = math.floor(math.log10(n))
ans = (n // (10**p)) * (10**p) - 1
if (n + 1) % (10**p) == 0:
    ans += 10**p
list_ans = list(str(ans))
count = 0
for i in list_ans:
    count += int(i)
print(count)

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

Solution (Python):
N = int(input())
digit = len(str(N))

s = sum([int(i) for i in list(str(N))])
print(max(9 * (digit - 1) + int(str(N)[0]) - 1, s))

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

for i in range(19):
    if 10**i <= n and 10**(i+1) > n:
        r = i 
w = n //(10**r)


if r == 0:
    print(n)
else:
    if n == (w+1)*(10**r)-1:
        print(w+r*9)
    else:
        print(w-1+r*9)

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

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

Solution (Python):
N = input()

ans = 0
all_nine_flg = True

for i in range(1,len(N)):
  if N[i] == "9":
    continue
  else:
    all_nine_flg = False
    break

sub = 0 if all_nine_flg else 1
print( int(N[0]) + 9*(len(N)-1) - sub)

Solution (Python):
s=input()
c=s.count("9")
if c==len(s):
    print(c*9)
elif len(s)==1:
    print(s)
elif s[1:].count("9")==len(s)-1:
    print(s[1:].count("9")*9+int(s[0]))
else:
    print((len(s)-1)*9+int(s[0])-1)

Solution (Python):
n=input()

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

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

Solution (Python):
def a_digit_sum_2(N):
    n_lead = int(str(N)[0])  
    n_later = str(N)[1:]  
    n_length = len(str(N))  
    if n_later == '9' * (n_length - 1):  
        ans = n_lead + 9 * (n_length - 1)  
    else:
        ans = (n_lead - 1) + 9 * (n_length - 1)  
    return ans 

N = int(input())
print(a_digit_sum_2(N))

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

ans = -1
if int(n) < 10:
    ans = int(n)
else:
    flg = True
    for i in range(1, n_length):
        if n[i] != '9':
            flg = False
            break
    if flg:
        ans = sum(map(int, list(n)))
    else:
        ans = 9 * (n_length - 1) + int(n[0]) - 1
print(ans)

Solution (Python):
N = input()
n = N[1:]
for c in n:
    if int(c) != 9:
        print((int(N[0])-1)+9*len(n))
        quit()
    else:
        pass
print(int(N[0])+9*len(n))

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

Solution (Python):
def digit_sum(num:int, base:int) -> int:
    if num < base:
        return num
    else:
        return digit_sum(int(num/base), base) + (num % base)

def judge(n: int) -> int:
    if n < 10:
        return n
    
    else:
        
        if str(n)[0:1] == '1':
            
            cand_str = "9" * (len(str(n)) - 1)
            cand = int(cand_str)
            
            return max(digit_sum(n,10), digit_sum(cand,10))
        
        
        else:
            top = n // 10**(len(str(n)) - 1)
            cand_str = str(top-1) + "9" * (len(str(n))-1)
            cand = int(cand_str)
            
            return max(digit_sum(n,10), digit_sum(cand,10))

n = int(input())

ans = judge(n)
print(ans)

Solution (Python):
n=int(input())
if n<10:
  print(n)
else:
  s=str(n)
  t=len(s)
  f=lambda x:sum(map(int,x))
  v=max(9*(t-1),f(s))
  for i in range(1,t):
    v=max(v,f(str(n-n%(10**i)-1)))
  print(v)

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

Solution (Python):
def digit_sum(n:int) -> int:
    res = n
    ans = 0
    while res > 0:
        ans = ans + res%10
        res //= 10
    
    return ans

def keta(n:int) -> int:
    res = n
    ans = 0
    while res > 0:
        ans += 1
        res //= 10
    return ans

n = int(input())
n_keta = keta(n)

ans = 0
if n % 10**(n_keta-1) == 10**(n_keta-1) - 1:
    ans = digit_sum(n)
    
else:
    ans = n // 10**(n_keta-1) - 1
    ans += 9 * (n_keta-1)
    
print(ans)

Solution (Python):
def digit_sum(n:int) -> int:
    res = n
    ans = 0
    while res > 0:
        ans = ans + res%10
        res //= 10
    
    return ans

def keta(n:int) -> int:
    res = n
    ans = 0
    while res > 0:
        ans += 1
        res //= 10
    return ans

n = int(input())
n_keta = keta(n)

ans = 0
if n >= 2*10**(n_keta-1):
    ans = n // 10**(n_keta-1) - 1
    ans += 9 * (n_keta-1)
    
elif n == 2*10**(n_keta-1) - 1:
    ans = digit_sum(n)
    
else:
    ans = 9 * (n_keta-1)
    
ans = max(ans, digit_sum(n))
print(ans)

Solution (Python):
import math

def main():
    N = int(input())
    p = 10**math.floor(math.log(N, 10))
    a = (N+1) // p
    a = p * a - 1
    r = 0
    while a > 0:
        r += (a%10)
        a //= 10
    print(r)
main()

Solution (Python):
n = map(int,list(raw_input()))

for i in range(1,len(n)):
	if n[i] != 9:
		print n[0] + (9*(len(n)-1)) - 1
		quit()

print sum(n)

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

Solution (Python):
n = input()
flag = False
ans = 0
for ele in n[-1:0:-1]:
  if ele!="9":
    flag = True
  ans+=9
if flag:
  ans += int(n[0])-1
else:
  ans += int(n[0])
print(ans)

Solution (Python):
a=input()
x=0
y=0
for c in a:
    x+=int(c)
    y+=9
y+=int(a[0])-10
print(max(x,y))

Solution (Python):
def f(s):
    p=0
    for i in str(s):
        p+=int(i)
    return p
N=int(input())
i=1
ans=f(N)
while N>=10**i:
    p=f((N//10**i)*(10**i)-1)
    ans=max(ans,p)
    i+=1
print(ans)

Solution (Python):
N = input()
newN = N[1:]
p = N[0]
nines = newN.count("9")
if nines == len(newN):
    ans = 9*(len(N)-1) + (int(p))
else:
    ans = 9*(len(N)-1) + (int(p)-1)

print(ans)

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

Solution (Python):

def ds(n):
    t = 0
    while n > 0: 
        t+=n %10
        n//=10
    return t

n = input()
print(max(ds(int(n)), len(n) * 9 - 9 + int(n[0])-1))

Solution (Python):

def main():
    n = input()
    m = len(n)
    ans = 0
    tmp = 0
    for i, v in enumerate(n):
        num = int(v)
        if num > 0:
            ans = max(ans, tmp + (num - 1) + (m-1-i)*9)
        tmp += num
    print(max(ans, tmp))


if __name__ == '__main__':
    main()

Solution (Python):
def sc(k):
	a = 0
	for c in str(k):
		a += int(c)
	return a

def mx(k):
	n = len(k);
	if k[0] == '1':
		return '9' * (n - 1)
	return chr(ord(k[0]) - 1) + '9' * (n - 1)

n = input()


print(max(sc(n), sc(mx(n))))

Solution (Python):
def main():
    s = input()
    
    k = len(s)
    if k == 1:
        ans = int(s)
    else:
        ans = 9 * (k-1) + int(s[0]) - 1
        
        for i in range(1, k):
            if s[i] != "9":
                break
                
        else:
            ans += 1
    
    print(ans)
    
    
if __name__ == "__main__":
    main()

Solution (Python):
N=list(input())
import sys
mark=0
for i in range (len(N)):
    if N[i]!='9':
        mark=i
        break
if mark==0 and N[mark+1:]==['9']*len(N[mark+1:]):
    count=0
    for l in range (len(N)):
        count+=int(N[l])
    print(count)
    sys.exit()
N[0]=str(int(N[0])-1)
for j in range (1,len(N)):
    N[j]='9'
count=0
for k in range (len(N)):
    count+=int(N[k])
print(count)

Solution (Python):
from math import log10

N = int(input())
d = int(log10(N))+1
ans = (d-1)*9 + max(0, N//10**(d-1)-1)
if (N+1)%10**(d-1) == 0:
    ans = max(ans, N//10**(d-1)+(d-1)*9)
print(ans)

Solution (Python):

def inpl(): return map(int, input().split())
N = input()
ans = 0

while N:
    if N[0] == "9":
        ans += 9
        N = N[1:]
    else:
        break

if all([n=="9" for n in list(N[1:])]) and len(N) >= 2:
    print(ans + int(N[0]) + len(N[1:])*9)
elif ans == 0:
    if len(N) == 1:
        ans = int(N)
    else:
        ans += int(N[0]) - 1 + len(N[1:])*9
    print(ans)
elif len(N)==0:
    print(ans)
else:
    print(ans - 1 + len(N)*9)

Solution (Python):
N = input()

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

Solution (Python):
n = input()
ans = int(n[0])
all_nine = False
for i in n[1:]:
    i = int(i)
    if(all_nine):
        ans += 9
    elif(i != 9):
        ans += 8
        all_nine = True
    else:
        ans += 9
print(ans)

Solution (Python):
N = input()

ans = int(N[0])
nine_only = False
for n in N[1:]:
    n = int(n)
    if nine_only:
        ans += 9
    elif n<=8:
        ans += 8
        nine_only=True
    else:
        ans += n
print(ans)

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

Solution (Python):
a= [int(x) for x in list(str(input()))]
b=a[0]
a[0]=9
c=0
d=0
for i in a:
    c+=1 
    if i != 9:
        d=1
print(b+9*(c-1)-d)

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

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

Solution (Python):
import sys

n = list(input())
order10 = len(n)

if len(n)==1:
    print(n[0])
    sys.exit()

if n.count('9')==len(n):
    ans = 9*len(n)
    print(ans)
else:
    n2 = []
    for i in range(1,len(n)):
        n2.append(n[i])

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

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

Solution (Python):
n_str = input()
n = int(n_str)
o_n = len(n_str)



ans_str = '9'*(o_n-1)
     
for i in [9,8,7,6,5,4,2,1,0]:
    test_str = str(i)+ans_str
    if int(test_str) <= n:
        ans_str = test_str
        break
 
ans_array = [int(c) for c in ans_str]

ans = sum(ans_array)
 
print(ans)

Solution (Python):
a =input()

b = [int(x) for x in list(a)]
if len(a) == 1:
    print(int(a))
elif len(a) == a[1:].count('9') + 1:
    print(sum(b))
else:
    print(int(b[0])-1+(len(a)-1)*9)

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

Solution (Python):
def solve():
    N = input()
    n = int(N)

    if N[1:].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)

solve()

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

Solution (Python):


import sys, math, copy




HUGE = 2147483647
HUGEL = 9223372036854775807
ABC = "abcdefghijklmnopqrstuvwxyz"

def main():
    def digsum(s):
        su = 0
        for c in s:
            su += int(c)
        return su

    def break_ith_dig(s, i1):
        assert 2 <= i1 <= dig
        if s[-i1] == "0":
            return s
        s = s[:-i1] + str(int(s[-i1]) - 1) + "9" * (i1 - 1)
        return s

    ns = input()
    dig = len(ns)
    ma = digsum(ns)
    for i in range(2, dig + 1):
        ns = break_ith_dig(ns, i)
        ma = max(ma, digsum(ns))

    print(ma)

main()

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

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

Solution (Python):
N = int(input())
len = len(str(N))
a = list(str(N))
b =0
for i in range(1,len)[::-1]:
  if a[i] == -1:
    a[i] == '9'
  if a[i] != '9':
    b += (int(a[i])+1) * (10**(len-1-i))
    a[i-1] = str(int(a[i-1])-1)
a = list(str(N-b))
b = 0
for i in a:
  b += int(i)
print(b)

Solution (Python):



num = int(raw_input())
length = len(str(num))
sum_of_num = sum(list(map(int,list(str(num)))))
max_of_num = int(str(num)[0])-1+(length-1)*9
print max(sum_of_num,max_of_num)

Solution (Python):
N = list(input())
lenN = len(N)
retN = []
toNext = 0
for n in reversed(N):
    n = int(n) + toNext
    lenN = lenN - 1
    toNext = 0
    if n < 9 and lenN != 0:
        toNext = -1
        retN.append(9)
        continue
    retN.append(n) 

print(sum(retN))

Solution (Python):
N=input()
num=0
if int(N)<10:
    print(N)
else:
    for i in range(len(N)-2):
        num+=9
    x=max(int(N[0])+int(N[len(N)-1]),(int(N[0])-1)+9)
    print(num+x)

Solution (Python):
N=input()
num=0
if int(N)<10:
    print(N)
else:
    for i in range(len(N)-2):
        num+=9
    x=max(int(N[0])+int(N[len(N)-1]),(int(N[0])-1)+9)
    print(num+x)

Solution (Python):
n = int(input())
x = len(str(n))-1
answer = 9*x
for i in range(9,0,-1):
	if n>=int(str(i)+"9"*x):
		answer = 9*x+i
		break
print(answer)

Solution (Python):
n = int(input())
low = '9'*(len(str(n))-1)
pos=''
for i in range(1, 10):
    if int(str(i)+low)<=n:
        pos=str(i)
if pos=='':
    pos='0'
print(len(low)*9 + int(pos))

Solution (Python):
N = list(str(input()))
mx = sum(map(lambda x: ord(x)-ord('0'),N))
for i in xrange(1,len(N)):
    N[i]='0'
N = int("".join(N))
mx = max(mx,sum(map(lambda x: ord(x)-ord('0'),str(N-1))))
print mx

Solution (Python):

n = int(raw_input())

def digit_sum(num):
    ret = 0
    while num > 0:
        ret = ret + num % 10
        num = num // 10
    return ret

ans = digit_sum(n)
buff = n
d = 10
while n > 0:
    n = n // 10
    buff = (buff // d) * d - 1
    d = d * 10
    if buff < 0 : break
    ans = max([ans,digit_sum(buff)])

print ans

Solution (Python):
s = input()

res = 0

i = True
p = 1
for c in s:
    if p == 1:
        p = 0
    elif c != '9':
        i = False
        break

if len(s) == 1:
    res = int(s)
elif i:
    res += int(s[0])
else:
    res += int(s[0]) - 1

res += (len(s) - 1) * 9
print(res)

Solution (Python):
n = input().rstrip()
if len(n) == 1:
  print(n)
else:
  flg = True
  for i in range(1,len(n)):
    if n[i] != '9':
      flg = False
  if flg:
    print(9*(len(n)-1)+int(n[0]))
  else:
    print(9*(len(n)-1)+int(n[0])-1)

Solution (Python):
import sys

a = int(input())
start = 0
power = 1
ans = 0
while True:
    for i in range(9):
        ans += 1
        start += power
        if start-1 >= a:
            print(ans-1)
            sys.exit()
    power *= 10

Solution (Python):
n=int(input())
if n<10:
  print(n)
else:
  L=list(map(int, list(str(n))))
  if(L.count(9)==len(L)) or (L[0]!=9 and L.count(9)==len(L) -1):
    print(sum(L))
  else :
    print(9*(len(L)-1)+int(L[0])-1)

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

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

Solution (Python):
N = input()
le = len(N)

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

if all([N[i] == "9" for i in range(1, le)]):
    ans = sum([int(N[i]) for i in range(le)])
else:
    tmp = int(N[0]) * (10 ** (le - 1)) - 1
    ans = sum([int(t) for t in str(tmp)])
print(ans)

Solution (Python):

"""
  problem:
    https://agc021.contest.atcoder.jp/tasks/agc021_a
"""

from __future__ import print_function
import sys

def solve(_input):
    """
    main function.

    Arguments:
    _input -- string of the format "n p", where n and p are positive integer, separated by space " ".
    """

    
    _li = list(_input.strip())
    try:
        _digits = map(int,_li)
    except (ValueError, IndexError) as ex:
        print ('[ERROR] Please input a whole number', ex)
        return -1

    
    _ans = sum(_digits)

    for _i in range(0,len(_digits)-1):

        if ( _digits[_i] == 0 ):
            continue

        _r = 0
        
        for _j in range(0,_i - 1):
            _r += _digits[_j]
        
        _r += _digits[_i] - 1

        _r += (len(_digits) - _i - 1) * 9

        _ans = max(_ans, _r)

    return _ans


print (solve(sys.stdin.readline()))

Solution (Python):
N = input()
L = len(N)

ans = sum(map(int, N))
num = 0
for i in range(L):
    d = N[i]
    if d == '0':
        continue
    num += int(d)
    ans = max(num - 1 + 9*(L-i-1), ans)
print(ans)

Solution (Python):
import math

N = int(input())
digit_num = math.ceil(math.log10(N))
msd =int(N/(10**(digit_num-1)))

if (N+1)%10 == 0:
    print(msd + 9*(digit_num-1))
elif N-10<0:
    print(N)
else:
    print(msd + 9*(digit_num-1) - 1)

Solution (Python):
import math

n = int(input())
digit = len(str(n))
digit -= 1

m = n - math.pow(10, digit) + 1
print(int(digit * 9 + (m // math.pow(10, digit))))

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

for i in range(1, K):
    if N[i] != '9':
        c -= 1
        break

ret = c + 9 * (K - 1)

print(ret)

Solution (Python):
N = input()
if len(N) == 1:
    print(int(N))
    exit()

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

Solution (Python):


n=int(raw_input())
res=0
n+=1
while n>9:
 res+=9
 n/=10
print res+n-1

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

Solution (Python):

s=list(map(int,list(input())))
mx=sum(s)
for i in range(len(s)):
    mx=max(sum(s[0:i+1])-1+9*(len(s)-i-1),mx)
print(mx)

Solution (Python):
if __name__ == '__main__':
    N = input()
    if len(N) == 1:
        print(N)
    else:
        cand_1 = sum([int(_) for _ in N])
        cand_2 = 9 * (len(N) - 1) + int(N[0]) - 1

        if cand_1 > cand_2:
            print(cand_1)
        else:
            print(cand_2)

Solution (Python):


n = input()
d = len(n)

res = 0
if d >= 2:
    res = 9*(d-1) + int(n[0]) - 1

res = max(res,sum(map(int,n)))


print(res)

Solution (Python):
N = input()

num_list = [sum(map(int, N))]
l = len(N)
head = 0

for i, num in enumerate(N):
    if num == 0:
        continue
    num_list += [head + int(num) - 1 + 9 * (l - i - 1)]
    head += int(num)

print(max(num_list))

Solution (Python):
a = input()
b = str()
l = len(a) 
for i in range(1, l):
    b += (a[i])
p = str(9) * (l-1)
if b == p:
    ans = 9 * (l-1) + int(a[0])
else:
    ans = 9 * (l-1) + int(a[0]) - 1
print(ans)