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
input = sys.stdin.readline

import math

import bisect
from collections import defaultdict
from collections import deque
from collections import Counter
from functools import lru_cache






MOD = 10**9+7
INF = float('inf')






def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]


def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()


def DD(arg): return defaultdict(arg)



def inv(n): return pow(n, MOD-2, MOD)


kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret


def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1:
    arr.append([temp, 1])
  if arr==[]:
    arr.append([n, 1])
  return arr


def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors


def make_primes(N):
  max = int(math.sqrt(N))
  seachList = [i for i in range(2,N+1)]
  primeNum = []
  while seachList[0] <= max:
    primeNum.append(seachList[0])
    tmp = seachList[0]
    seachList = [i for i in seachList if i % tmp != 0]
  primeNum.extend(seachList)
  return primeNum


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


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


def count_bit(n):
  count = 0
  while n:
    n &= n -1
    count += 1
  return count


def base_10_to_n(X, n):
  if X//n:
    return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i-1])*n**i for i in range(len(str(X))))


def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count




N = I()
NN = N+0

L = int_log(N,10)
if N == pow(10,L+1)-1:
  print(L*9+9)
  exit()

count_9 = 0
while (NN+1)%10==0:
  count_9 += 1
  NN = int(str(NN)[:-1])

if count_9 == len(str(N))-1:
  print(sum(map(int,list(str(N)))))
else:
  L = int_log(N,10)
  print((count_9+L)*9+(int(str(N)[0])+9)%10)

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

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 8)

input = sys.stdin.readline


def main():
    S = input().strip()

    ans = 0
    f = False
    for i, s in enumerate(S):
        if f:
            ans += 9
        elif i == len(S) - 1:
            ans += int(S[i])
        else:
            if S[i + 1] != "9":
                f = True
                ans += int(S[i]) - 1
            else:
                ans += int(S[i])

    print(ans)

    
    

if __name__ == '__main__':
    main()

Solution (Python):
n = input()
digit = len(n)
n = int(n)
ans = (digit - 1) * 9 + n // (10 ** (digit - 1)) - 1
tmp = n - n // (10 ** (digit - 1)) * (10 ** (digit - 1))
if tmp == (10 ** (digit - 1)) - 1:
    ans += 1
print(ans)

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

Solution (Python):
N = int(input())
if N < 10:
    print(N)
else:
    str_N = str(N)
    
    for n in str_N[1:]:
        if n != '9':
            
            print(int(str_N[0]) - 1 + (len(str_N) - 1) * 9)
            break
    else:
        print(int(str_N[0]) + (len(str_N) - 1) * 9)

Solution (Python):
X = input()

if X[0] == 1:
	X2 = [9]*(len(X) - 1)
elif X[1:].count("9") == len(X[1:]):
	X2 = X
else:
	tmp = int(X[0])-1
	X2 = [str(tmp)] + ["9"]*(len(X)-1)


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

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

ans = sum(N)

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

print(max(ans, sum(N)))

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

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
    N = input().strip()
    L = len(N)
    N = int(N)
    s = ''
    ans = 0
    for _ in range(L):
        t = 9
        while int(str(t) + s) > N:
            t -= 1
        s = str(t) + s
        ans += t
    print(ans)    

if __name__ == '__main__':
    main()

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

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

Solution (Python):
n=str(input())
cn=sum(map(int,list(n)))

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

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

Solution (Python):
n = list(map(int,input()))
cnt = 0
b = tuple(n)
c = b

for i in n:
    cnt += 1
    if i == 9:
        continue
    else:
        if cnt == len(n):
            cnt -= 1
            n[cnt-1] -= 1
            break
        else:
            if i == 0:
                n[cnt-2] -= 1
                cnt -= 1
                break
            else:
                n[cnt-1] -= 1
                break

for i in range(cnt,len(n)):
    n[i] = 9

if len(n) == 1:
    n = b

if sum(n) >= sum(b):
    print(sum(n))
else:
    print(sum(b))

Solution (Python):
S = input()
N = []
for s in S:
  N.append(int(s))

N.reverse()

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

print(ans)

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

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

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):
N = input()
if N[1:] == (len(N)-1)*'9':
    print(int(N[0]) + (len(N)-1)*9)
else:
    print((int(N[0])-1) + (len(N)-1)*9)

Solution (Python):
import sys

input = sys.stdin.readline


def main():
    N = int(input())

    L = len(str(N))
    ans = sum(map(int, str(N)))

    for d in range(L):
        if str(N)[d] == "0":
            continue
        m = sum(map(int, str(N)[:d + 1])) - 1 + 9 * (L - (d + 1))
        if m > ans:
            ans = m

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
N=int(input())
a=len(str(N))
if a==1:
    print(N)
    exit()
else:
    ans=9*(a-1)


    
    for i in range(a):
        y=0
        if i==0:
            for j in range(a):
                y+=int(str(N)[j])
            
        else:
            x=str(int(str(N)[:a-i])-1)+"9"*i
            if len(x)!=a:
                x="0"*(a-len(x))+x
            for j in range(a):
                y+=int(x[j])
           
        if ans<y:
            ans=y
print(ans)

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

Solution (Python):
s = input()
g = s[1:]
a = g.replace('9','')
if a == '':
  print(int(s[0])+(len(s)-1)*9)
elif len(s) == 1:
  print(s)
else:
  print(int(s[0])-1+9*(len(s)-1))

Solution (Python):
n = input()
if int(n) < 10:
	print(int(n))
	exit()
flag = n[1:] != '9' * (len(n) - 1)
print(int(n[0]) - flag + 9*(len(n) - 1))

Solution (Python):
m = int(input())
m = str(m + 1)
n = list(m)

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

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

Solution (Python):
n = list(input())
s = 0
for a in n:
    s += int(a)

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

Solution (Python):
def digitsum(n):
    return eval("+".join(str(n)))

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

Solution (Python):
n_=input()
n=int(n_)
l=len(n_)
for i in range(1,17):
    if n+1==(int(n_[0])+1)*10**(l-1):
        ans=0
        while n>0:
            ans+=n%10
            n//=10
        print(ans)
        exit()

n = int(n_[0])*10**(l-1)-1
ans=0
while n>0:
    ans+=n%10
    n//=10
print(ans)

Solution (Python):
n=int(input())
s=str(n)
a=len(s)
b=(int(s[0])+1)*10**(a-1)-1
ans=9*(a-1)+int(s[0])
if n==b:
  print(ans)
else:
  print(ans-1)

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

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

Solution (Python):
N = input()

f = int(N[0])
res = (len(N) - 1) * 9 + f - 1
s = 0
for i in N:
    s += int(i)

print(max(res, s))

Solution (Python):
n = int(input())
for i in range(9 * len(str(n)), 0, -1):
    if i <= 9:
        i_min = i
    else:
        i_min = int(str(i % 9) + '9' * (i // 9))
    if i_min <= n:
        print(i)
        exit()

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

Solution (Python):
d = input()

f = int(d[0])
res = (len(d)-1)*9 + f-1
s = 0
for i in d:
    s += int(i)

print(max(res, s))

Solution (Python):
n = input()

a = 9*(len(n)-1)

b = 0
for i in n:
	b += int(i)

c = 0
for i in range(len(n)):
	if i == 0:
		c += int(n[i])-1
	else:
		c += 9
print (max(a,b,c))

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

Solution (Python):
import sys
INF = 1 << 60
MOD = 10**9 + 7 
sys.setrecursionlimit(2147483647)
input = lambda:sys.stdin.readline().rstrip()
def resolve():
    n = int(input())
    k = len(str(n))
    dp = [0, -INF]

    for c in str(n):
        c = int(c)
        ndp = [-INF, -INF]
        for d in range(10):
            for lt in range(2):
                if not lt and c < d:
                    continue
                nlt = max(lt, c > d)
                ndp[nlt] = max(ndp[nlt], dp[lt] + d)
        dp = ndp

    print(max(dp))
resolve()

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

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

Solution (Python):











import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,sqrt,factorial,hypot,log 
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from fractions import gcd
from random import randint
def ceil(a,b): return (a+b-1)//b
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def f(S):
    res = 0
    for s in S:
        res += ord(s)-ord("0")
    return res
def main():
    S=ST()
    print(max(f(S),int(S[0])-1 + 9*(len(S)-1)))
if __name__ == '__main__':
    main()

Solution (Python):
lst_N = [int(c) for c in input()]
set_N = set(lst_N)
first = lst_N[0]
left = lst_N[1:]
if len(lst_N) == 1: 
    print(lst_N[0])
elif len(set_N) == 1 and lst_N[0] == 9: 
    print(sum(lst_N))
elif first != 9 and len(set(left)) == 1 and left[0] == 9: 
    all_nine = "9" * (len(left))
    lst_all_nine = [int(c) for c in all_nine]
    print(first + sum(lst_all_nine))
else:
    all_nine = "9" * (len(lst_N) - 1)
    lst_all_nine = [int(c) for c in all_nine]
    print((first-1) + sum(lst_all_nine))

Solution (Python):
import sys

input = sys.stdin.readline

def main():
    ans = 0
    N = input().rstrip('\n')
    c = int(N[0])
    ans = c + 9*(len(N)-1)
    if N[1:]!='9'*(len(N)-1):
        ans -= 1
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
import sys
N = input()
if len(N) == 1:
    print(N)
    sys.exit()

s = 0
for x in N:
    s += int(x)

if N[0] != '9' and '9'*(len(N)-1) == N[1:]:
    print(s)
elif N[0] != '9':
    print((int(N[0])-1) + 9*(len(N)-1))
elif '9'*len(N) == N:
    print(s)
else:
    print((int(N[0])-1) + 9*(len(N)-1))

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

Solution (Python):
X = int(input())
Y = list(map(int, str(X)))
if X >= 10:
  ans = max(sum(Y), Y[0]-1+9*(len(Y)-1))
else:
  ans = X
print(ans)

Solution (Python):
import sys
import heapq
import re
from itertools import permutations
from bisect import bisect_left, bisect_right
from collections import Counter, deque
from fractions import gcd
from math import factorial, sqrt, ceil
from functools import lru_cache, reduce
INF = 1 << 60
MOD = 1000000007
sys.setrecursionlimit(10 ** 7)


class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

def is_prime(n):
    if n == 1:
        return False
    for i in range(2,int(n**0.5)+1):
        if n % i == 0:
            return False
    return True




def warshall_floyd(d, n):
    
    for k in range(n):
        for i in range(n):
            for j in range(n):
                d[i][j] = min(d[i][j],d[i][k] + d[k][j])
    return d



def dijkstra_heap(s, edge, n):
    
    d = [10**20] * n
    used = [True] * n 
    d[s] = 0
    used[s] = False
    edgelist = []
    for a,b in edge[s]:
        heapq.heappush(edgelist,a*(10**6)+b)
    while len(edgelist):
        minedge = heapq.heappop(edgelist)
        
        if not used[minedge%(10**6)]:
            continue
        v = minedge%(10**6)
        d[v] = minedge//(10**6)
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                heapq.heappush(edgelist,(e[0]+d[v])*(10**6)+e[1])
    return d


def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])

    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])

    return arr


def lcm(x, y):
    return (x * y) // gcd(x, y)


def lcm_list(numbers):
    return reduce(lcm, numbers, 1)


def gcd_list(numbers):
    return reduce(gcd, numbers)






def eratosthenes(limit):
    A = [i for i in range(2, limit+1)]
    P = []

    while True:
        prime = min(A)
        
        if prime > sqrt(limit):
            break
            
        P.append(prime)
            
        i = 0
        while i < len(A):
            if A[i] % prime == 0:
                A.pop(i)
                continue
            i += 1
            
    for a in A:
        P.append(a)
            
    return P


def permutation_with_duplicates(L):

    if L == []:
        return [[]]

    else:
        ret = []

        
        S = sorted(set(L))

        for i in S:

            data = L[:]
            data.remove(i)

            for j in permutation_with_duplicates(data):
                ret.append([i] + j)

        return ret



n = input()
m = len(n)
ans1 = 9 * (m - 1)
ans2 = sum([int(i) for i in n])
ans3 = 0
ok = False
for i in range(m):
    x = int(n[i])
    if i == 0:
        if x >= 2:
            ok = True
            ans3 += x - 1
        continue
    if ok:
        ans3 += 9
    else:
        if x >= 1:
            ok = True
            ans3 += x - 1
ans = max(ans1, ans2, ans3)



print(ans)

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

Solution (Python):
n = list(input())
ans = 0

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

if a < sum(map(int, n)):
    ans = sum(map(int, n))
else:
    ans = a

print(ans)

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

Solution (Python):
N = input()

cnt = sum(list(map(int, N)))
kyuu = (int(len(N))-1) * 9 + int(N[0]) -1
if cnt > kyuu:
    print(cnt)
else:
    print(kyuu)

Solution (Python):
import sys
def S(): return sys.stdin.readline().rstrip()
N = S()
if all([n=='9' for n in N[1:]]):
    print(int(N[0])+9*(len(N)-1))
else:
    print(int(N[0])-1+9*(len(N)-1))

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

Solution (Python):
n = list(map(int, list(input().strip())))

total = sum(n)

for i in range(len(n)):
    subtotal = 0
    for j in range(i - 1):
        subtotal += n[j]
    subtotal += n[i] - 1
    for k in range(i + 1, len(n)):
        subtotal += 9

    total = max(total, subtotal)

print(total)

Solution (Python):
n = int(input())
d = len(str(n))
n_sum = 0
for i in range(d):
    n_sum += int(str(n)[i])
def judge(n):
    for i in range(1, len(n)):
        if n[i] != "9":
            return True
    return False
print(n // (10 ** (d - 1)) - 1 + 9 * (d - 1) if judge(str(n))  else n_sum)

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

Solution (Python):
N = int(input())
S = str(N)
if(len(S) == 1): print(S); exit(0)
if(len(S) == 2): print(max(N // 10 + N % 10, N // 10 - 1 + 9)); exit(0)
for i in range(1, len(S)):
    if(int(S[i]) != 9):
        print(int(S[0]) - 1 + (len(S) - 1) * 9)
        exit(0)
print(int(S[0]) + (len(S) - 1) * 9)

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

Solution (Python):


N = input()
digit = len(N)
ans = 0

for i in range(digit):
    ans += int(N[i])
    if i<digit-1 and N[i+1]!='9':
        ans += 9*(digit-1-i)-1
        break

print(ans)

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

Solution (Python):
n = input()
ans = 0
for i in range(len(n)):
    tmp = [9]*len(n)
    for j in range(9, -1, -1):
        tmp[i] = j
        s = int(''.join(map(str, tmp)))
        if s <= int(n):
            ans = max(ans, sum(map(int, str(s))))
            break
print(ans)

Solution (Python):
N = input()

Nl = len(N)
tmp = N[0] + '9'*(Nl-1)

if int(N) >= int(tmp):
    answer = int(N[0]) + 9*(Nl-1)
else:
    answer = int(N[0])-1 + 9*(Nl-1)
    
print(answer)

Solution (Python):
A= input()
if len(A)== 1:
	print(A)
	exit()
length=len(A)
B = "9" * (length-1)
A=int(A)
C=A-int(B)
B=str(int(C/10**(length-1)))+B
B=int(B)
B=list(str(B))
B=list(map(int,B))
print(sum(B))

Solution (Python):

import re
import math
from collections import defaultdict
from collections import deque
import itertools
from copy import deepcopy
import random
import time
import os
import queue
import sys
import datetime
from functools import lru_cache

readline=sys.stdin.readline
sys.setrecursionlimit(2000000)

alphabet="abcdefghijklmnopqrstuvwxyz"
mod=int(10**9+7)
inf=int(10**20)
def yn(b):
    if b:
        print("yes")
    else:
        print("no")
def Yn(b):
    if b:
        print("Yes")
    else:
        print("No")
def YN(b):
    if b:
        print("YES")
    else:
        print("NO")
class union_find():
    def __init__(self,n):
        self.n=n
        self.P=[a for a in range(N)]
        self.rank=[0]*n
 
    def find(self,x):
        if(x!=self.P[x]):self.P[x]=self.find(self.P[x])
        return self.P[x]
 
    def same(self,x,y):
        return self.find(x)==self.find(y)
 
    def link(self,x,y):
        if self.rank[x]<self.rank[y]:
            self.P[x]=y
        elif self.rank[y]<self.rank[x]:
            self.P[y]=x
        else:
            self.P[x]=y
            self.rank[y]+=1
 
    def unite(self,x,y):
        self.link(self.find(x),self.find(y))
 
    def size(self):
        S=set()
        for a in range(self.n):
            S.add(self.find(a))
        return len(S)
def is_pow(a,b):
    now=b
    while now<a:
        now*=b
    if now==a:return True
    else:return False
def bin_(num,size):
    A=[0]*size
    for a in range(size):
        if (num>>(size-a-1))&1==1:
            A[a]=1
        else:
            A[a]=0
    return A
def get_facs(n,mod_=0):
    A=[1]*(n+1)
    for a in range(2,len(A)):
        A[a]=A[a-1]*a
        if(mod_>0):A[a]%=mod_
    return A
def comb(n,r,mod,fac):
    if(n-r<0):return 0
    return (fac[n]*pow(fac[n-r],mod-2,mod)*pow(fac[r],mod-2,mod))%mod
def next_comb(num,size):
    x=num&(-num)
    y=num+x
    z=num&(~y)
    z//=x
    z=z>>1
    num=(y|z)
    if(num>=(1<<size)):return False
    else:return num
def get_primes(n,type="int"):
    if n==0:
        if type=="int":return []
        else:return [False]
    A=[True]*(n+1)
    A[0]=False
    A[1]=False
    for a in range(2,n+1):
        if A[a]:
            for b in range(a*2,n+1,a):
                A[b]=False
    if(type=="bool"):return A
    B=[]
    for a in range(n+1):
        if(A[a]):B.append(a)
    return B
def is_prime(num):
    if(num<=1):return False
    i=2
    while i*i<=num:
       if(num%i==0):return False
       i+=1
    return True
def ifelse(a,b,c):
    if a:return b
    else:return  c
def join(A,c=""):
    n=len(A)
    A=list(map(str,A))
    s=""
    for a in range(n):
        s+=A[a]
        if(a<n-1):s+=c
    return s
def factorize(n,type_="dict"):
    b = 2
    list_ = []
    while b * b <= n:
        while n % b == 0:
            n //= b
            list_.append(b)
        b+=1
    if n > 1:list_.append(n)
    if type_=="dict":
        dic={}
        for a in list_:
            if a in dic:
                dic[a]+=1
            else:
                dic[a]=1
        return dic
    elif type_=="list":
        return list_
    else:
        return None
def floor_(n,x=1):
    return x*(n//x)
def ceil_(n,x=1):
    return x*((n+x-1)//x)
    return ret
def seifu(x):
    return x//abs(x)

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

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

nup = sorted(N)

if N[1:len(N)+1] == ["9"] * (len(N)-1):
    sum1 = 0
    for i in range(len(N)):
        sum1 += int(N[i])
    print(sum1)

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

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

Solution (Python):


import sys
input= lambda: sys.stdin.readline().rstrip()
sys.setrecursionlimit(10**9)
def pin(type=int):return map(type,input().split())
def tupin(t=int):return tuple(pin(t))
def lispin(t=int):return list(pin(t))

from collections import Counter
def resolve():
    N=list(input())
    a=sum(map(int,N))
    for n in range(1,len(N)):
        t=(N[:n]+[9]*(len(N)-n))
        a=max(a,sum(map(int,t))-1)
    print(a)
        

resolve()

Solution (Python):


n = input()
if int(n) >= int(n[0] + '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()
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):
s=list(input())

memo = []

for i in s:
    memo.append(int(i))

ans = max(sum(memo),(memo[0]-1)+(len(memo)-1)*9)

print(ans)

Solution (Python):
N = input()
K = len(N)
c = N[0]
a = 0
for i in range(1,K):
  if N[i] != "9":
    a = 1
if a == 0:
  print(int(c) + 9*(K-1))
else:
  print(int(c) + 9*(K-1)-1)

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

Solution (Python):
n = input()

s = 0
for each in n:
  s += int(each)

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

print(max(s, l))

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

Solution (Python):






N = int(input())





k = len(str(N))
if N%(10**(k-1)) == 10**(k-1) - 1:
    print(int(N/(10**(k-1))) + 9*(k-1))
else:
    print(int(N/(10**(k-1))) + 9*(k-1) - 1)

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

main()

Solution (Python):
import sys
def input(): return sys.stdin.readline().strip()

def resolve():
    n=input()
    keta=len(n)
    saileft=int(n[0])
    if n[1:].count('9')==keta-1:
        print(saileft+(keta-1)*9)
    else:
        print(saileft-1+9*(keta-1))
resolve()

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

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

Solution (Python):
n = input()
int_n = int(n)
len_n = len(n)
ans = sum([int(i) for i in n])

for i in range(len_n):
    target = int(n[:i+1][-1]) -1
    if target < 0:
        continue
    tmp_n = n[:i] + str(target) + '9'* (len_n - i - 1)
    tmp_n_ans = sum([int(i) for i in tmp_n])
    ans = max(tmp_n_ans, ans)
print(ans)

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

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

Solution (Python):
S = list(input())
S = list(map(lambda x: int(x),S))
ans = 0

for i in range(len(S) - 1,0,-1):
  
  if S[i] == 9:
    tmp = sum(S)
  else:
    S[i] = 9
    S[i - 1] -= 1
    tmp = sum(S)
  
  ans = max(tmp,ans)

if len(S) == 1:
  ans = S[0]

print(ans)

Solution (Python):
str_n = input()
digits = [int(c) for c in str_n]

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

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

Solution (Python):
n = 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):
S = input()
N = len(S)

sum_s = 0
for s in S:
  sum_s += int(s)

print(max(sum_s, (N-1)*9 + int(S[0])-1))

Solution (Python):
s = input()
n = len(s)
ans = 0

for c in s:
  ans += int(c)

for i in range(n):
  res =0 
  d = int(s[i])
  if d==0:
    continue
  else:
    d -= 1
  for c in s[:i]:
    res += int(c)
  res += d
  res += (n-i-1)*9
  ans = max(ans, res)
print(ans)

Solution (Python):
n = input()
ans = 0
accu = 0
for i, j in enumerate(n):
    j = int(j)
    if j>0:
        ans = max(ans, accu+j-1+9*(len(n)-i-1))
    accu += j
ans = max(ans, accu)
print(ans)

Solution (Python):
import sys

input = sys.stdin.readline
MOD = 1000000007

N  = input()[:-1]

if len(N)== 1:
    print(int(N))
elif len(N)  == 2:
    if N[-1] == '9':
        print(int(int(N)/10) + 9)
    else:
        print(int(int(N)/10)-1 + 9)

else:
    res = 0
    if N[0] == '9':
        check = True
        for i in range(0,len(N)):
            if N[i] != '9':
                check = False
        if check:
            res = 9*(len(N))
        else:
            res = 9*(len(N)-1)+8
    else:
        check = True
        for i in range(1,len(N)):
            if N[i] != '9':
                check = False
            if check:
                res = 9*(len(N)-1)+int(N[0])
            else:
                res = 9*(len(N)-1)+int(N[0])-1
    print(res)

Solution (Python):
n = input()
if int(n) == (int(n[0])+1) * 10 ** (len(n)-1) - 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()
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):
n = input()

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