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

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

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

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

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

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

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

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

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

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

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

Solution (Python):



from fractions import gcd
from itertools import combinations,permutations,accumulate 

from collections import deque,defaultdict,Counter
import decimal
import re












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

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)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Solution (Python):

n = input()
print((len(n) - 1) * 9 + int(n[0]) - 1 * (n[1:] != "9" * (len(n)-1)))

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

Solution (Python):









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)

Solution (Python):

import sys
from collections import deque, defaultdict
from math import sqrt, factorial





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

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


"""


"""

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

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

Solution (Python):
N = input()

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

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

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

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

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

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

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

for i in range(l):
    n[i] = int(n[i])

score = 0

if l == 1:
    print(n[0])
else:
    if n.count(9) == l:
        score = sum(n)
    elif n.count(9) == l-1:
        if n[0] != 9:
            score += sum(n)
        else:
            score += 8 + 9*(l-1)
    else:
        if n[0] != 9:
            score += n[0]-1 + 9*(l-1)
        else:
            for j in range(l):
                if n[j] != 9:
                    n[j-1] = 8
                    score += 8 + 9*(l-1)
                    break
    print(score)

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

if __name__ == '__main__':
    main()

Solution (Python):
import sys
input = sys.stdin.readline

def linput(ty=int, cvt=list):
	return cvt(map(ty,input().split()))

def gcd(a: int, b: int):
	while b: a, b = b, a%b
	return a

def lcm(a: int, b: int):
	return a * b // gcd(a, b)

def dist(x1,y1,x2,y2):
	return abs(x1-x2)+abs(y1-y2)

def main():
	
	S = input().rstrip()
	
	res = 0
	l,r = 0,0
	for s in S:
		d = int(s)
		l += 9 if r else d-1
		r += d
	
	res = max(l,r)
	print(res)
	
	
	

if __name__ == "__main__":
	main()

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

if __name__ == '__main__':
    main()

Solution (Python):
n = int(input());
ans = 0;
all9 = 1;
while n//10 > 0 :
    ans += 9;
    if all9 == 1 and (n - n//10*10) < 9 :
        all9 = 0;
    n = n//10;

if all9 == 1 :
    ans = ans + n;
else :
    ans = ans + n - 1;

print(ans);

Solution (Python):
N=int(input())
cnt=1
flag=True
while N>=10:
  if N%10!=9:
    flag=False
  N//=10
  cnt+=1
if flag==True:
  ans=N+9*(cnt-1)
else:
  ans=N+9*(cnt-1)-1
print(ans)

Solution (Python):
def main():
    a = 0
    b = False
    for i,v in enumerate(input()):
        if i == 0:
            a += int(v)
        else:
            b = int(v) < 9
            a += 9
    print(a-b)

if __name__ == "__main__":
    main()

Solution (Python):
def main():
    n = input()[::-1]
    a = 0
    borrow = False
    for i in enumerate(n):
        if i[0] == len(n)-1:
            if borrow:
                a += int(i[1])-1
            else:
                a += int(i[1])
        else:
            if int(i[1]) < 9:
                borrow = True
            a += 9
    print(a)
        
    
if __name__ == "__main__":
    main()

Solution (Python):
n = [int(i) for i in list(input())]
l = len(n)

ans =  max((n[0]-1) + 9*(l-1),sum(n))
print(ans)

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

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

n=str(N)
Length=len(n)

l=list(n)

if l[1:]==['9']*(Length-1):
    ans=int(n[0])+9*(Length-1)
else:
    ans=int(n[0])-1+9*(Length-1)

print(ans)

Solution (Python):
n = input()
k = len(n)
a = 9 * (k - 1) + int(n[0]) - 1
b = 0
for i in range(k):
  b += int(n[i])
c = 0
m = ''
if k > 2 and n[-2] != '0':
  m = str(int(n))[:k - 2] + str(int(n[-2]) - 1) + '9'
elif k > 2 and n[-2] == '0':
  m = str(int(n) - (int(n[-1]) + 1))
for i in range(len(m)):
  c += int(m[i])
ans = max(a, b, c)
print(ans)

Solution (Python):
N = list(input())
num = len(N)
if N.count('9') == num:
    print(9*num)
elif N.count('9') == num-1 and N[0] != '9':
    print(int(N[0])+9*(num-1))
else:
    if num <= 1:
        print(''.join(N))
    else:
        ans = int(N[0])-1
        ans += 9*(num-1)
        print(ans)

Solution (Python):
N = list(input())
num = len(N)
if N.count('9') == num:
    print(9*num)
elif N.count('9') == num-1 and N[0] != '9':
    print(int(N[0])+9*(num-1))
else:
    if num <= 1:
        print(''.join(N))
    else:
        ans = int(N[0])-1
        ans += 9*(num-1)
        print(ans)

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

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

Solution (Python):
N = input()

c = int(N[0])
K = len(N)
ans = c + 9 * (K - 1) - 1
for i in N[1:]:
    if i != "9":
        break
else:
    ans += 1

print(ans)

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

for i in range(17):
    if 10**i <=N <10**(i+1):
        m=i+1
        break

for j in range(0,10):
    if (10**(m-1))*j + 10**(m-1)-1 <=N< (j+1)*(10**(m-1))+10**(m-1)-1:
        a = j
        break

print(a+9*(m-1))

Solution (Python):
n = list(map(int, input()))
l = [sum(n)]
for i in range(1, len(n)):
    n[-i] = 9
    n[-i-1] -= 1
    l.append(sum(n)) 
print(max(l))

Solution (Python):
N = list(map(int, input()))
nines = []
total = sum(N)
nines.append(total)

for i in range(1, len(N)):
    N[-i] = 9
    N[-i-1] -= 1
    nines.append(sum(N))

print(max(nines))

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

Solution (Python):
N = int(input())
topdigi = int(str(N)[0])
keta = len(str(N))

candidate = int(str(topdigi) + "9" * (keta - 1))

if candidate > N:
    ans = (topdigi - 1) + 9 * (keta - 1)
else:
    ans = topdigi + 9 * (keta - 1)
print(ans)

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

Solution (Python):
N = input()
ans = 0

if N[1:] == "9" * (len(N) - 1):
    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 = 0

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

print(ans)

Solution (Python):
def resolve():
    N = int(input())
    NList = list(str(N))
    maxA = eval("+".join(NList))
    maxA = max(maxA, eval(
        "+".join(list(str(int(NList[0]) - 1) + "9" * (len(NList) - 1)))))
    print(maxA)


resolve()

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

Solution (Python):
N = input()


K = len(N)


c = int(N[0])


d = N[1:]

if "9" * len(d) == d:
    print(c + 9 * (K - 1))
else:
    print(c + 9 * (K - 1) - 1)

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_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:
	print(max(ketawa(N), int(N[0])-1 + 9*(len(N)-1)))

Solution (Python):
N = int(input())
x = 9
order = 0
for i in range(16):
    if x > N:
        break
    x = x*10+9
    order = i+1
ans = x
for i in range(10):
    ans -= 10**order
    if  ans <= N:
        break
ret = 0
while True:
    if ans == 0:
        break
    ret += ans % 10
    ans = ans//10
print(ret)

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

Solution (Python):
s=input()
k=10**(len(s)-1)

print(sum(map(int,str((int(s)+1)//k*k-1))) if len(s)!=1 else s)

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

Solution (Python):


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

def main():
    n = input()
    digit=len(n)
    nine_numbers=n.find('9')
    limit=0

    if int(n)<=9:
        print(n)

    elif digit==nine_numbers:
        print(9*int(nine_numbers))

    else:
        max_ans = digit_sum(n)
        head=int(n[0])-1
        limit=str(head)+'9'*(digit-1) 
        ans = max(max_ans,digit_sum(limit))
        print(ans)

if __name__=="__main__":
    main()

Solution (Python):
import sys
input = sys.stdin.readline



def main():
    N = input().strip()
    ans = sum([int(n) for n in N])

    for i in range(len(N)-1):
        if N[i] == 0:
            continue
        else:
            ans = max(ans, sum([int(n) for n in N[:i]]) +
                      int(N[i])-1 + 9*(len(N)-i-1))

    return ans


if __name__ == "__main__":
    print(main())

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

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, log
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
from decimal import Decimal
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
from decimal import *

N = input()

if len(N) == 1:
	print(N)
else:
	ans1 = 9*(len(N)-1) + int(N[0])-1
	ans2 = sum([int(x) for x in list(N)])
	print(max(ans1, ans2))

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, log
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
from decimal import Decimal
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
from decimal import *

N = INT()

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

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

ans2 = sum([int(x) for x in list(str(N))])

ans3 = sum([int(x) for x in list(str(N//10*10-1))])

print(max(ans1, ans2, ans3))

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

Solution (Python):
import math

N = int(input())

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

    start = [str(4) , str(5)]
    start += [str(9) for _ in range(ketasu - 2)]
    start = int(''.join(map(str, start)))

    if start > N:
        start = [str(9) for _ in range(ketasu-1)]
        start = int(''.join(map(str, start)))

    

    for i in range(start,N+1,10**(ketasu - 2)):
        temp_ans = 0
        for j in list(str(i)):
            temp_ans += int(j)

        if temp_ans > ans:
            ans = temp_ans

    print(ans)

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

Solution (Python):
N = input()
ndig = len(N)
ans1 = N[0] + "9" * (ndig - 1)
ans2 = str(int(N[0]) - 1) + "9" * (ndig - 1)
if int(ans1) <= int(N):
    print(sum(int(d) for d in ans1))
else:
    print(sum(int(d) for d in ans2))

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

N2 =str(N)[0]+(len(str(N))-1)*"9"
ans = 0
for n in N2:
    ans+=int(n)
if int(N2) > N:
    ans-=1
print(ans)

Solution (Python):
n=list(input())
le=len(n)
if le==1:
  print(int(n[0]))
  exit()
x=list(n)
x.remove(x[0])
if len(set(x))==1 and x[0]=='9':
  key=int(n[0])
  print(9*(le-1)+key)
  exit()
m=list(n)
m.remove(m[-1])
if len(set(m))==1 and m[0]=='9':
  print(9*(le-1)+8)
else:
  key=int(n[0])
  print(9*(le-1)+key-1)

Solution (Python):
x=int(input())
y=str(x)
n=len(y)

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

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

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

Solution (Python):
import bisect
N = int(input())
if len(str(N)) == 1:
    print(N)
    exit()
L = len(list(str(N)))-1
A = [10**(L)*i+10**(L)-1 for i in range(10)]  
n = bisect.bisect_left(A,N)-1
if N in A:
    n += 1
ans = [int(list(str(A[n]))[i]) for i in range(len(list(str(A[n]))))]
print(sum(ans))

Solution (Python):
n=list(input())
x=[]
for i in n:
  x.append(int(i))
out=int(n[0])-1+(len(n)-1)*9

if x[1:]==[9]*(len(n)-1) or len(n)==1:
  out=sum(x)
print(out)

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

f=True
c=int(n[0])
if len(n)==1:
	x=N
else:
	for i in range(1, len(n)):
		if int(n[i]) != 9:
			f=False
	
	if f:
		x=c+9*(len(n)-1)
	else:
		x=c+9*(len(n)-1)-1

print(x)

Solution (Python):
s = input()

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

Solution (Python):
n = input()

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

if all(x == "9" for x in n[1:]):
  ans = n
else:
  ok = False
  ans = ""
  for i, c in enumerate(n):
    x = int(c)
    if x == 9:
      ans += "9"
    else:
      if ok:
        ans += "9"
      else:
        if i == len(n)-1:
          ans = ans[:-1]
          ans += str(int(n[i-1])-1)
          ans += "9"
        else:
          ans += str(x-1)
      ok = True
t = 0
for c in ans:
  x = int(c)
  t += x

print(t)

Solution (Python):
n = input()

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

if all(x == "9" for x in n[1:]):
  ans = n
else:
  ok = False
  ans = ""
  for i, c in enumerate(n):
    x = int(c)
    if x == 9:
      ans += "9"
    else:
      if ok:
        ans += "9"
      else:
        if i == len(n)-1:
          ans = ans[:-1]
          ans += str(int(n[i-1])-1)
          ans += "9"
        else:
          ans += str(x-1)
      ok = True
t = 0
for c in ans:
  x = int(c)
  t += x

print(t)

Solution (Python):


import sys
import numpy as np
int1 = lambda x: int(x) - 1
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

c, *N = str(int(readline())+1)
print(int(c) + 9 *len(N)- 1)

Solution (Python):
import sys
import math
import itertools
def I():return int(sys.stdin.readline().replace("\n",""))
def I2():return map(int,sys.stdin.readline().replace("\n","").split())
def S():return str(sys.stdin.readline().replace("\n",""))
def L():return list(sys.stdin.readline().replace("\n",""))
def Intl():return [int(k) for k in sys.stdin.readline().replace("\n","").split()]
def Lx(k):return list(map(lambda x:int(x)*-k,sys.stdin.readline().replace("\n","").split()))

n = L()
n = [int(k) for k in n]
if len(n) == 1:
    print(n[0])
else:
    print(max(sum(n),9*(len(n)-1)+n[0]-1))