Problem:
 Score : 300 points    Problem Statement Find the maximum possible sum of the digits (in base 10 ) of a positive integer not greater than N .      Constraints  1\leq N \leq 10^{16}  N is an integer.         Input Input is given from Standard Input in the following format:  N       Output Print the maximum possible sum of the digits (in base 10 ) of a positive integer not greater than N .        Sample Input 1 100      Sample Output 1 18  For example, the sum of the digits in 99 is 18 , which turns out to be the maximum value.       Sample Input 2 9995      Sample Output 2 35  For example, the sum of the digits in 9989 is 35 , which turns out to be the maximum value.       Sample Input 3 3141592653589793      Sample Output 3 137

Solution (Python):
n = input()

a = n[0]

if len(n) > 1:
    b = n[1:]
    if (int(b)+1) % (10 ** len(b)) == 0:
        flag = True
    else:
        flag = False
else:
    b = ''
    flag = True

if flag:
    ans = int(a) + 9 * len(b)
else:
    ans = int(a) - 1 + 9 * len(b)

print(ans)

Solution (Python):
s=input();n=len(s);a=0;b=0
for i in range(n):
    a+=int(s[i])
for i in range(n):
    if i!=0:
        b+=9
    else:
        b+=(int(s[0])-1)
print(max(a,b))

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

Solution (Python):
n = int(input())
print(sum([int(j) for j in str(int(str(n+1)[0])-1) + "9"*(len(str(n+1))-1)]))

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

def digit_sum_simple(n):
    
    max_sum = -1

    for i in range(10**(len(str(n))-1)-1, n+1):
        
        i_parts = []
        for i_part in str(i):
            i_parts.append(int(i_part))

        this_sum = sum(i_parts)
        
        if max_sum < this_sum:
            max_sum = this_sum
            max_sum_i = i

    return max_sum, max_sum_i

def digit_sum_temp(n):
    max_sum_i = int(str(int(str(n+1)[0])-1) + "9"*(len(str(n+1))-1))
    max_sum = sum([int(j) for j in str(max_sum_i)])

    return max_sum

print(digit_sum_temp(n))

Solution (Python):
n = list(map(int, list(str(int(input())))))
l = [sum(n)]
ln = len(n) - 1

def d(i):
    if i > len(n) - 1: return
    d(i+1)
    v2 = n[i] - 1 if n[i] != 0 else 9
    l.append(v2 + (ln - i) * 9)

d(0)
print(max(l))

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

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

max = 0
max2 = 0
if n < 10:
    print(n)
else:
    while m > 0:
        max += int(m % 10)
        m = m/10
    m=n
    while True:
        if m >= 10:
            max2 += int(9)
        else:
            max2 += int(m % 10 - 1)
            break
        m = m / 10
    if max > max2:
        print(max)
    else:
        print(max2)

Solution (Python):
n = int(input())
 
for i in range(10)[::-1]:
	if n >= int(str(i) + '9'*(len(str(n))-1)):
		print(i + 9*(len(str(n))-1))
		break

Solution (Python):

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

res = 0
for i, j in enumerate(N):
    if j != "9":
        break

if i >= 2:
    res += 9*(i-1)
    N = N[i-1:]

A = 10**(len(N)-1)

res += max(sum(map(int, N)), sum(map(int, str(int(N)//A*A-1))))
print(res)

Solution (Python):






def get_most_significant_digit(number: int):
    separated_number = list(map(int, str(number)))
    return separated_number[0]


def count_digit(number: int):
    return len(str(number))


def generate_candidate_value(most_significant_digit, digit_count):
    filled_with_9 = 10 ** (digit_count - 1) - 1

    if digit_count > 1:
        candidate_value = str(most_significant_digit) + str(filled_with_9)
    else:
        candidate_value = str(most_significant_digit)
    return int(candidate_value)


def sum_digit(number: int, candidate_value: int):
    result = sum(list(map(int, str(candidate_value))))

    if number >= candidate_value:
        return result
    else:
        return result - 1


if __name__ == '__main__':
    number = int(input())

    
    most_significant_digit = get_most_significant_digit(number)
    digit_count = count_digit(number)
    candidate_value = generate_candidate_value(most_significant_digit,
                                               digit_count)

    result = sum_digit(number, candidate_value)
    print(result)

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

for i in range(10)[::-1]:
	if n >= int(str(i) + '9'*(len(str(n))-1)):
		print(i + 9*(len(str(n))-1))
		break

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

    ary = list(map(int, list(str(n))))

    
    m = -1
    middle = [9 for i in range(0, len(ary[1:-2]))] 
    for i in range(0, 10):
        for j in range(0, 10):
            for k in range(0, 10):
                if m < sum([i] + middle + [j, k]) and int(''.join(map(str, [i] + middle + [j, k]))) <= int(n):
                    m = sum([i] + middle + [j, k])
    print(m)

Solution (Python):
import sys

if __name__ == '__main__':
    
    
    
    n = input()

    ary = list(map(int, list(str(n))))

    
    if len(ary) == 1:
       print(sum(ary))
       sys.exit()

    
    if len(ary) == 2:
        if sum(ary) <= 9:
            print(9)
            sys.exit()
        else:
            print(sum(ary))
            sys.exit()

    
    if ary[1:-1].count(9) != len(ary[1:-1]):
        ary[0] = ary[0] - 1
        ary[-2] = 9
        ary[-1] = 9

    
    ary[1:-1] = [9 for i in range(0, len(ary[1:-1]))]

    
    if sum(ary[-2:]) < 18:
        ary[-2] = 8
        ary[-1] = 9

    
    print(sum(ary))

Solution (Python):
s=input()
a=int(s[0])
b=sum([int(s[i]) for i in range(len(s))])
for i in range(1,len(s)):
 if s[i]!="9":
    b=a+9*(len(s)-1)-1
    break

print(b)

Solution (Python):
import math
n=int(input())
s = str(n)
array = list(map(int, list(s)))
a1=sum(array)
size = int (math.log10(n))
a2=int(n/(10**size))-1+size*9
print(max(a1,a2))

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

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):
if __name__=='__main__':
    N = int(input())

    N_list = list(map(int, list(str(N))))
    if len(N_list) == 1:
        print(N_list[0])
    elif set(N_list[1:]) == {9}:
        print(sum(N_list))
    else:
        N_list[0] -= 1
        for i in range(1, len(N_list)):
            N_list[i] = 9
        print(sum(N_list))

Solution (Python):
def main():
    n = input()
    
    if len(n) == 1:
        print(n)
        return

    if set(n[1:]) == {'9'}:
        print(int(n[0]) + 9 * (len(n) - 1))
        return

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

    
    
    
    
    
    


if __name__ == '__main__':
    main()

Solution (Python):

def solve(n):
    ans_a = calc_digit_sum(n)
    ans_b = calc_digit_sum(n - n % 10 - 1)
    ans_c = calc_digit_sum(10 ** (len(str(n)) - 1) - 1 + 10 ** (len(str(n)) - 1) * (int(str(n)[0]) - 1) )
    return max(ans_a, ans_b, ans_c)

def calc_digit_sum(n):
    if n <= 0:
        return 0
    str_n = str(n)
    ans = 0
    for x in range(len(str_n)):
        ans = ans + int(str_n[x])
    return ans

if __name__ == "__main__":
    n = int(input())
    print(solve(n))

Solution (Python):
def digit_num(_num):
    _digit = 0
    while _num:
        _digit += 1
        _num //= 10
    return _digit

def ACGC021_A():
    _num = int(input())
    _digit = digit_num(_num)
    _topdig = _num // 10 ** (_digit - 1)
    if (_num - _topdig * 10 ** (_digit - 1)) == 10 ** (_digit - 1) - 1:
        print(_topdig + 9 * (_digit - 1))
    else:
        print(_topdig - 1 + 9 * (_digit - 1))

ACGC021_A()

Solution (Python):
N_list = [int (i) for i in list(input())]
digit = 0
dflag = False
max = 0
for i in N_list:
    max += i

for i in N_list:
    if i == 9:
        dflag = True
    else:
        break

    digit += 1

if dflag:
    N_list[digit-1] -=1
    for i in range(digit, len(N_list)):
        N_list[i] = 9

else:
    N_list[digit] -=1
    for i in range(digit+1, len(N_list)):
        N_list[i] = 9

sum = 0

for i in N_list:
    sum += i

if sum > max:
    max = sum
print (max)

Solution (Python):
def digit_num(_num):
    _digit = 0
    while _num:
        _digit += 1
        _num //= 10
    return _digit


_num = int(input())
_digit = digit_num(_num)
_topdig = _num // 10 ** (_digit - 1)
_sumlist = [9 * (_digit - 1)]
if (_num - _topdig * 10 ** (_digit - 1)) == 10 ** (_digit - 1) - 1:
    _sumlist.append(_topdig + 9 * (_digit - 1))
else:
    _sumlist.append(_topdig - 1 + 9 * (_digit - 1))

print(max(_sumlist))

Solution (Python):
n = input()
list = [10**i for i in range(1,20)]

if int(n)>=10:
    ans = 9*(len(n)-1)+int(n[0])-1
    if (int(n[1:])+1) in list: ans+=1
else: ans = int(n)

print(ans)

Solution (Python):
import sys

n = int(input())
s = len(str(n))

_max = ""
for i in range(1, s+1):
    _max += "9"
if str(n).count("9") == s:
    print(s*9)
else:
    for j in range(1, 10):
        for k in range(0, s):
            if int(_max) - j*10**k <= n:
                tmp = str(int(_max) - j*10**k)
                amari = tmp.replace("9", "")
                if len(amari) != 0:
                    print(tmp.count("9")*9+int(amari))
                else:
                    print(tmp.count("9")*9)
                sys.exit()

Solution (Python):
N = input()

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

Solution (Python):
def is_9(arr):
    s = []
    for i in range(len(arr)):
        if arr[i] != '9':
            return False
        s.append(arr[i])
    return True


n = list(input())
fd = int(n.pop(0))
if is_9(n):
    print(fd + int(9 * len(n)))
else:
    print(fd - 1 + int(9 * len(n)))

Solution (Python):
dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]

INF = float('inf')
MOD = 1000000000 + 7

def LI(): return [int(x) for x in input().split(" ")]
def LS(): return [str(x) for x in input().split(" ")]
def ItoS(L): return [str(x) for x in L]
def StoI(L): return [int(c) for c in L]

def check(n):
    l = len(str(n))
    for ele in list(str(n)):
        if ele != "9":
            return False

    return True

def Sum(n):
    L = list(str(n))
    sum = 0
    for ele in L:
        sum += int(ele)

    return sum

def solve():
    while 1:
        try:
            n = int(input())
            if 1 <= n <= 9:
                print(n)
                raise EndLoop

            l = len(str(n))

            tmp = str(n)[0]

            if check(n):
                print(9 * l)
            else:
                print(max(Sum(n), 9 * (l - 1) + int(tmp) - 1))
            break
        except:
            break

solve()

Solution (Python):
N = int(input())
num = str(N)
for i in range(1,len(num)):
    if num[-i] != "9" and 0 < N - (int(num[-i]) + 1) * 10**(i-1):
        N -= (int(num[-i]) + 1) * 10**(i-1)
        num = str(N)

D = 0
for digit in str(N):
    D += int(digit)

print(D)

Solution (Python):
N = int(input())
def calc(N):
    if len(str(N)) == 1:
        return N
    if len(list(set(str(N)[1:]))) == 1 and '9' in  list(set(str(N)[1:])):
        return int(str(N)[0])+9*len(str(N)[1:])
    ret = 0
    f = 0
    for idx,c in enumerate(str(N)):
        tmp = int(c)
        if f == 1:
            ret += 9
            continue
        if tmp != 9 and idx == 0:
            ret += tmp-1
            f = 1
            continue
        elif tmp != 9:
            ret -= 1
            f = 1
            ret += 9
            continue
        else:
            ret += 9
            continue
    return ret
print(calc(N))

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

if N <= 9:
	print(N)
	exit(0)

ans=0
b = False
for i in range( len(S) ):
	if i==0:
		ans += int(S[i])
		continue
	
	j = int(S[i])
	
	
	if b:
		ans += 9
	else:
		if j==9:
			ans += j
		if j!=9:
			b = True
			ans -= 1
			ans += 9

print(ans)

Solution (Python):
def A_Digit_Sum_2():
    a=input()
    count=0
    c=0
    for i in range(len(a)-1):
        if a[i+1]=='9':
            count+=1
    
    if count==(len(a)-1):
        for i in range(len(a)):
            c+=int(a[i])
    else:
        c=(len(a)-1)*9+int(a[0])-1
    print(c)

A_Digit_Sum_2()

Solution (Python):
N=int(input())
num_digits=0
for i in range(17):
    if N//(10**i)>0: num_digits=i
max_digits=N//(10**num_digits)
nines=9
if num_digits==0: print(max_digits)
else:
    for i in range(1,num_digits):
        nines+=9*10**i
    temp=N-max_digits*10**num_digits
    if temp/nines==1: print(max_digits+num_digits*9)
    else: print((max_digits-1)+num_digits*9)

Solution (Python):
N = list(input())
if len(N) == 1:
    print(int(N[0]))
elif N.count('9') == len(N):
    print(len(N) * 9)
else:
    for i in range(len(N)):
        if N[i] != '9':
            idx = i
            break
    if idx == len(N) - 1:
        print(sum(map(int, N[:-1])) + 8)
    else:
        if N[idx] == '0':
            print(len(N) * 9 - 1)
        elif N[idx + 1:].count("9") == len(N[idx + 1:]):
            print(sum(map(int, N)))
        else:
            print(sum(map(int, N[:idx + 1])) + (len(N) - idx - 1) * 9 - 1)

Solution (Python):
from collections import Counter
n = input()
c = Counter(n)
n9 = c['9']
if n9 == len(n):
    ans = 9*len(n)
elif n9 == len(n)-1 and n[0] != '9':
    ans = int(n[0]) + 9*(len(n)-1)
else:
    ans = int(n[0])-1 + 9*(len(n)-1)
        
print(ans)

Solution (Python):
import sys

if __name__ == "__main__":
    N = int(input())
    digit = len(str(N))
    digit_list = [int(x) for x in list(str(N))]
    if digit == 1:
        print(N)
        sys.exit()
    buf = 0
    for i in range(digit):
        buf += (9 * pow(10, i))
    if N == buf:
        print(9 * digit)
        sys.exit()
    buf = 0
    for i in range(digit - 1):
        buf += (9 * pow(10, i))
    if (N - digit_list[0] * pow(10, digit - 1)) == buf:
        cand = 0
        for ele in digit_list:
            cand += ele
        print(cand)
        sys.exit()
    cand = 0
    if digit_list[0] == 1:
        cand = 9 * (digit - 1)
    else:
        cand = digit_list[0] - 1 + 9 * (digit - 1)
    print(cand)

Solution (Python):
import math
n=int(input())
s = str(n)
array = list(map(int, list(s)))
a1=sum(array)
size = int (math.log10(n))
a2=int(n/(10**size))-1+size*9
print(max(a1,a2))

Solution (Python):
def num_sum(n):
	b = 0
	for i in range(16):
		a = n % 10
		b = b + a
		n = n // 10
	return b

x = int(input())
f = 0
for i in range(17):
	c = 10**(16-i)*(x//(10**(16-i))) -1
	if c <= 0:
		c = 0
	d = max(num_sum(x), num_sum(c), f)
	f = d
print(f)

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

Solution (Python):
def solve():
    N_str = raw_input()
    num_digits = len(N_str)
    N = int(N_str)

    ans = 0
    test = 10 ** num_digits - 1
    while True:
        base = 10 ** (num_digits - 1)
        for _ in xrange(10):
            if test <= N:
                ans = test
                break
            test -= base
        if ans:
            break

    print sum([int(d) for d in str(ans)])


if __name__ == "__main__":
    solve()

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

sum = 0

while N != []:
    if N[-1] != '9' and len(N) != 1:
        sum += 9
        del N[-1]
        if N[-1] == '0':
            N = list(str(int(''.join(N)) - 1))
        else:
            N[-1] = str(int(N[-1]) - 1)
    elif len(N) == 1:
        sum += int(N[0])
        del N[0]
    else:
        sum += 9
        del N[-1]

print(sum)

Solution (Python):
num = input()

counter = 0
lol = True

for i in range(1, len(num)):
    if int(num[i]) != 9:
        lol = False

if lol:
    counter += int(num[0])
else:
    counter += int(num[0]) - 1

counter += 9 * (len(num)-1)
print(counter)

Solution (Python):
N=int(input())
keta=len(str(N))
keta2=len(str(N+1))
alpha=int(str(N)[0])-1
beta=int(str(N+1)[0])-1
a=(keta-1)*9
if keta==1:
    print(N)
elif alpha==beta:
    print(a+alpha)
elif keta==keta2:
    print(a+beta)
else:
    print(keta*9)

Solution (Python):
n = input()
ls = list(map(int,[ _ for _ in n ]))
if int(n) <= 9:
    print(int(n))
elif sum(ls[1:]) / (len(ls)-1) == 9:
    print(sum(ls))
else:
    print(ls[0]-1 + 9*(len(ls)-1))

Solution (Python):
import random
import numpy
import sys
import copy

N=int(input())
m = 0
l = [9 for i in range(len(str(N)))]
for i in range(0,len(l)):
  s = copy.deepcopy(l)
  for j in range(9,-1,-1):
    s[i] = j
    num = int(''.join(map(str,s)))
    if num<=N:
      m=sum(s)
      print(m)
      exit()

Solution (Python):
import math
N = input()
n = int(N)
lst = list(N)
if len(lst)==1:
    print(int(lst[0]))
elif (n+1)%(10**(len(lst)-1)) == 0:
    print(int(lst[0])+9*(len(lst)-1))
else:
    print((int(lst[0])-1) + 9*(len(lst)-1))

Solution (Python):

n = int(input())
num = len(str(n))
tmp = pow(10, num)-1
s = 0
for i in range(num+1):
    if i == num:
        s = sum(list(map(int, str(tmp-9*pow(10, num-1)+(int(str(n)[0])-1)*pow(10, num-1)))))
        break
    elif tmp-pow(10, i) <= n:
        s = sum(list(map(int, str(tmp-pow(10, i)))))
        break
if sum(list(map(int, str(n)))) > s:
    print(sum(list(map(int, str(n)))))
else:
    print(s)

Solution (Python):

N = int(input())

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

a=[]

a.append(Sum(N))

N_str = str(N)
N_str = list(map(int, list(N_str)))

"右端の数字を1下げたときの最大"
N_str = str(N)
N_str = list(map(int, list(N_str)))
N_str[0] = N_str[0] - 1
for i in range(1,len(N_str)):
    N_str[i] = 9
a.append(sum(N_str))

print(max(a))

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

if all(N[i]=="9" for i in range(len(N))):
	ans=9*(len(N))

elif N[0]!="9" and all(N[i]=="9" for i in range(1,len(N))):
	for i in range(len(N)):
		ans+=int(N[i])

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

print(ans)

Solution (Python):
s = input()
if len(s) == 1:
    print(s)
else:
    t = set(s)
    if len(t) == 1 and "9" in t:
        print(len(s) * 9)
    elif len(t) == 2 and "9" in t and "0" not in t:
        if s[0] != "9":
            k = sorted(list(t))
            print((len(s) - 1) * 9 + int(k[0]))
        else:
            print((len(s) - 1) * 9 + 8)
    else:
        print(int(s[0]) + (len(s) - 1) * 9 - 1)

Solution (Python):

n = input()
result = sum(map(int, n))
acc = 0
for i, c in enumerate(n):
    if int(c):
        result = max(result, acc + (int(c) - 1) + (len(n) - i - 1) * 9)
    acc += int(c)
print(result)

Solution (Python):








N=int(raw_input())

dig=[]
rem = N

while rem>0:
    dig.append( rem % 10)
    rem = rem / 10

dig.reverse()



for i in range(1, len(dig)):
    if dig[i] < 9:
        dig[i-1] = dig[i-1] - 1
        dig[i] = 9
        for j in range(i+1, len(dig)):
            dig[j] = 9
        break



sum=0

for d in dig:
    sum = sum + d

print sum

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

num = 0
for x in range(16):
    if n>=10**x and n<10**(x+1):
        num=x
        break
    else:
        num=16
out=0

if (n+1)//(10**(num)) == n//(10**(num)):
    out = n//10**(num) -1 + 9*num
else:
    out = n//10**(num) + 9*num
print(out)

Solution (Python):
import sys,collections;sys.setrecursionlimit(10**7)
def Is(): return [int(x) for x in sys.stdin.readline().split()]
def Ss(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

s = S()
n1 = sum([int(x) for x in s])
n2 = int(s[0])-1+9*(len(s)-1)
print(max(n1,n2))

Solution (Python):


N = int(input())

def kazu(x):
    if x == 0:
        return 0,0
    else:
        a = x
        top = 0
        keta = 0
        while(a!=0):
            top = a
            a = a//10
            keta += 1
        return keta,top


def digsum(x):

    xketa, xtop = kazu(x)
 
    if x == 0:
        return 0
    else:
        xdown = x - (10**(xketa-1))*xtop
  
        return max((xtop -1) + 9*(xketa-1), xtop + digsum(xdown))
print(digsum(N))

Solution (Python):
N = int(input())
S = str(N)
L = list(S)

for x in range(len(L)):
    L[x] = int(L[x])

for x in range(1, len(L)):
    if L[x] != 9:
        L[x-1] -= 1

        for y in range(x, len(L)):
            L[y] = 9

res = 0

for x in L:
    res += x

print(res)

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

len_N=len(N)
sum_result=0

not_list_nine=[i for i in N if i != 9]
num_nine=len_N - len(not_list_nine)

N_first=N[0]

if N_first==9:
  if num_nine==len_N: sum_result=sum(N)
  else: sum_result=(len_N-1)*9+8
else:
  if num_nine==len_N-1: sum_result=sum(N)
  else: sum_result=(N_first-1)+(len_N-1)*9

print sum_result

Solution (Python):
def solve():
    inputstr = input()
    N = int(inputstr)
    L = len(inputstr)
    list = [dsum(mfloor(N,k)) for k in range(1,L)]
    list.append(dsum(N))
    ans = max(list)
    print(str(ans))

def dsum(n):
    ans = 0
    for d in str(n):
        ans += int(d)
    return ans

def mfloor(n,k):
    import math
    ans = math.floor(n/(10**k))*(10**k) - 1
    return ans

solve()

Solution (Python):
n = input()

if len(n) == 1:
    print(int(n))
else:
    if set(n[1:]) == {'9'}:
        print(sum(map(int, list(n))))
    else:
        print((int(n[0]) - 1) + (len(n) - 1) * 9)

Solution (Python):
string = input()
num = int(string)
n = list(map(int,list(string)))
total = sum(n)
if(len(n)==1):
    print(num)
else:
    digit_n = int(num/(10**(len(n)-1)))
    result = ((digit_n-1)+(len(n)-1)*9)
    print(result if result>total else total)

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

top = 0
div = 10**16
digi = 17
while div >= 1:
  if n // div >= 1:
    top = n // div
    break
  div //= 10
  digi -= 1

res = 0
nine = div - 1
if n % div == nine:
  res = 9 * (digi - 1) + top
else:
  res = 9 * (digi - 1) + top - 1

print(res)

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

Solution (Python):
N= list(str(input()))
import numpy as np
nn = np.array(N).astype(int)
ans = 0
for i in range(1,len(nn))[::-1]:
    if(nn[i] == -1):
        nn[i - 1] -= 1
        nn[i] = 9
    if(9-nn[i] > 0):
        nn[i-1] -= 1
        nn[i] = 9
        ans += 9
    else:
        ans += nn[i]

ans += nn[0]
print(ans)

Solution (Python):
def main():
    n = input()
    if len(n) == 1:
        print(n)
        return
    if set(n[1:]) == {'9'}:
        print(int(n[0]) + 9 * (len(n) - 1))
        return

    for i, c in enumerate(n):
        if c != '9':
            print(9 * (len(n) - 1) + int(n[0]) - 1)
            return
    else:
        print(9 * len(n))


if __name__ == '__main__':
    main()

Solution (Python):
N = long(raw_input())

if all(map(lambda x: x == '9', str(N)[1:])):
    print(sum(map(int, str(N))))
else:
    print((int(str(N)[0]) - 1) + (len(str(N))-1) * 9)

Solution (Python):
N=raw_input()
S=list(N)
init_sum=0
for i in S:
	init_sum+=int(i)

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

if init_sum>n:
	print init_sum
else:
	print n

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

T=sum(S)

if int(N)<10:
    print(N)
elif 9*(len(S)-1)+S[0]-1 >= T:
    print(9*(len(S)-1)+S[0]-1)
elif S[-1]==9: print(T)
elif S[0]==9: print(9*len(S))

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

Solution (Python):
N = input()

sum1 = sum(map(int, N))
sum2 = int(N[0]) - 1 + 9 * (len(N) - 1)

print(max(sum1, sum2))

Solution (Python):
n=int(input())
li=list(map(int,list(str(n))))
if(n<10):
    print(n)
else:
    ma=sum(li)
    for i in range(0,len(li)-1):
        if(li[-(i+1)]!=9):
            li[-(i+1)]=9
            li[-(i+2)]-=1
        ma=max([ma,sum(li)])
    print(ma)

Solution (Python):
tes =input()
a = [int(data) for data in tes] 
no_change = sum(a)
changes = a[0]-1 + (len(a)-1)*9
if changes > no_change:
	print(changes)
else:
	print(no_change)

Solution (Python):
N=list(input())
cnt=0
size=len(N)-1
if size==0:
    print(N[0])
    exit()
N.reverse()

for i in range(size):
    if N[i]!="9":
        N[i]="9"
        N[i+1]=str(int(N[i+1])-1)
for i in range(size+1):
    cnt+=int(N[i])
print(cnt)

Solution (Python):


n = raw_input()
a = len(n)

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

if not int(n) == (int(n[0])+1)*(10**(a-1))-1:
    ans -= 1

print ans

Solution (Python):
import sys
import random

def rd():
        return sys.stdin.readline().rstrip()

def rdi():
        return [ int(x) for x in rd().split() ]

n = rdi()[0]

def f(n):
        s = 0
        a = n
        b = 0
        t = ""
        while a >= 10:
                b = a % 10
                a = a / 10
                if b <= 8 and a >= 1:
                        s = s + 9
                        a = a - 1
                        b = 9
                else:
                        s = s + b
                t = str(b) + t
        s += a
        t = str(a) + t
        
        
        return int(t)


t = f(n)

s = sum([ int(a) for a in str(t) ])
print s

Solution (Python):
s = input()
if int(s) < 10: print(s); exit()

ans = -1
for i in range(len(s)):
    t = s[:i]
    NINE = (len(s)-len(t))
    SUB = 0
    t+= '9' * NINE
    if int(s) < int(t): SUB = int('1' + '0' * NINE)
    t = max(int(t) - SUB, 0)
    ans = max(ans, sum(int(c) for c in str(t)))
print(ans)

Solution (Python):
import math
n = int(input())
s = 10**(int(math.log(n,10))+1) - 1
while s>n:
    s -= 10**int(math.log(n,10))
print(sum(list(map(int,list(str(s))))))

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

nn = str(N)

Sum = []
s = 0
for n in nn:
    s += int(n)
Sum.append(s)

for i in range(len(nn)):
    s = 0
    for j,n in enumerate(nn):
        if j < i:
            s += int(n)
        elif j == i:
            s += int(n)-1
        else:
            s += 9
    Sum.append(s)

print(max(Sum))

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

t = len(str(N + 1)) - 1

s = int((N + 1) / (10 ** t)) - 1

print s + 9 * t

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

leng = len(nums)

for i in range(leng-1):
    index = -1 * (i+1)
    if nums[index] == 9:
        pass
    else:
        nums[index] = 9
        nums[index-1] = nums[index-1]-1

print(sum(nums))

Solution (Python):
N = int(input())
for i in range(18):
    a = (N + 1) // (10 ** i)
    if a:
        b = a
    elif a == 0:
        c = (i -1) * 9 + b - 1
        break
print(c)

Solution (Python):
n = input()
head = n[0]
tail = '0' * (len(n) - 1)
m = str(int(head + tail) - 1)
sumn = 0
summ = 0
for i in n:
    sumn += int(i)
for i in m:
    summ += int(i)
print(max(sumn, summ))

Solution (Python):

import math


N = input()
Nint = int(N)
Nlist = list(map(int,list(N)))
l = len(N)
ans = 0
for i in range(l-1, 0, -1):
    Nint -= ((int(N[i]) + 1) % 10) * 10**(l-i-1)
    N = str(Nint)


ans = sum(list(map(int,list(N))))
print(ans)

Solution (Python):
def poyo():
    ans = -1
    A = []
    for i in range(1, 100):
        t = sum([ord(x) - ord('0') for x in str(i)])
        ans = max(ans, t)
        A.append(ans)
        

    import numpy as np
    maxi, cnt = np.unique(A, return_counts=True)
    print(maxi)
    print(np.cumsum(cnt))
    for x, y in zip(maxi, np.cumsum(cnt)):
        print(x, y)


def naive(N):
    ans = -1
    for i in range(N + 1):
        t = sum([ord(x) - ord('0') for x in str(i)])
        ans = max(ans, t)
    return ans



import math


def solve(N):
    if N < 10:
        return (N)
    else:
        ans = (len(str(N + 1)) - 1) * 9
        ans += ord(str(N + 1)[0]) - ord('0') - 1
        return (ans)


def check():
    for i in range(10000):
        
        assert solve(i) == naive(i)


N = int(input())
print(solve(N))

Solution (Python):
n=input()
count=0

for i in range(len(n)):
    if n[i]=="9":
        count+=1

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

if len(n)==1:
    ans=int(n)


print(ans)

Solution (Python):
import sys
N1 = int(input())
temp = N1 -1
temp = list(map(int,list(str(temp))))
N2 = list(map(int,list(str(N1))))


if len(temp) != len(N2):
    print(sum(temp))
    sys.exit()
else:
    count = 0
    for i in range(1,len(N2)):
        if N2[i] == 9:
            count +=1
    if count == len(N2)-1:
        print(sum(N2)) 
        sys.exit()
    else:
        N2[0] = N2[0]-1
        for i in range(1,len(N2)):
            N2[i] = 9
        print(sum(N2))
        sys.exit()

Solution (Python):
import numpy as np
import scipy as sp
import scipy.sparse.csgraph as sg

N = [int(c) for c in input()]

for i in range(len(N)-1):
    if int(N[i]) + int(N[i+1]) < int(N[i]) - 1 + 9:
        N[i] -= 1
        for j in range(i+1, len(N)):
            N[j] = 9
num = sum(N)

print(num)

Solution (Python):
from sys import exit, stderr, setrecursionlimit
from functools import reduce
from itertools import *
from collections import defaultdict
from bisect import bisect

def read():
  return int(input())

def reads():
  return [int(x) for x in input().split()]

setrecursionlimit(1000000)

N = [int(c) for c in input()]
L = len(N)

if all(N[i] == 9 for i in range(1, L)):
  print(sum(N))
else:
  print((N[0]-1) + 9 * (L-1))

Solution (Python):

import re

n = input()
l = len(n)-1

def f(s):
    return sum(map(int, list(s)))

if l==0:
    print(n)
elif re.search("^9+$", n)!=None:
    print(9*(l+1))
else:
    r = max(f(n), 9*l + (int(n[0])-1))
    print(r)

Solution (Python):



import array
from bisect import *
from collections import *
import fractions
import heapq 
from itertools import *
import math
import random
import re
import string


def solve(N):
    N = str(N)
    ans = 0
    if N == len(N) * '9':
        ans = 9 * len(N)
    elif len(N) == 1:
        ans = int(N)
    elif N[1:] == (len(N)-1) * '9':
        ans = sum(map(int, list(N)))

    elif N[0] == '1':
        ans = 9 * (len(N) - 1)
    else:
        ans = int(N[0]) - 1 + 9 * (len(N)-1)
    return ans

N = input()
print(solve(N))

Solution (Python):
n = int(input())
a = 0
while 10 ** a <= n:
    a = a + 1
b = (n + 1) // (10 ** (a -1))
c = b * (10 ** (a - 1)) - 1
d = 0
for i in range(len(str(c))):
    d = d + int(str(c)[i])
print(d)

Solution (Python):
ns = input().strip()
d = len(ns)
n = int(ns)
ans = sum(map(int, str(n)))
for i in range(d):
    m = n - n % (10 ** i) - 1
    ans = max(ans, sum(map(int, str(m))))

print(ans)

Solution (Python):

N = input().rstrip()


ans = sum([int(x) for x in N])
for i in range(len(N)-1):
    A = str(int(N[:-(i+1)]) - 1)
    A += '9'*(i+1)
    
    ans = max(ans, sum([int(x) for x in A]))
print(ans)

Solution (Python):
n = input()

def solve():
    import math
    for i in range(len(n)):
        if i>0:
            if n[i] != '9':
                print(int(n[0])-1+9*(len(n)-1))
                return
    print(int(n[0])+9*(len(n)-1))
    return

solve()

Solution (Python):
import sys

N = sys.stdin.readline().strip()

if len(N) == 1:
    print(N)
else:
    N2 = N[1:]
    if all(map(lambda d: d == '9', N2)):
        print(sum(map(int, N)))
    else:
        print(int(N[0]) - 1 + 9 * len(N2))

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

ans = sum(map(int, n))
ans = max(ans, max(map(lambda i: sum(map(int, list(str(int(''.join(n[:i + 1])) - 1)))) + (l - i - 1) * 9, range(l))))
print(ans)