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



Sample input:
100


Sample output:
18

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

# AtCoder Grand Contest
# Problem A

import math


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


def count_digit(number: int):
    return int(math.log10(number)) + 1


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

    # See: https://img.atcoder.jp/agc021/editorial.pdf
    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)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

# AtCoder Grand Contest
# Problem A

import math


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


def count_digit(number: int):
    return int(math.log10(number)) + 1


def generate_candidate_value(most_significant_digit, digit_count):
    filled_with_9 = 10 ** (digit_count - 1) - 1
    candidate_value = str(most_significant_digit) + str(filled_with_9)
    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())

    # See: https://img.atcoder.jp/agc021/editorial.pdf
    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)

Submission status: Wrong Answer

Solution code:
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

Submission status: Wrong Answer

Solution code:
import sys

if __name__ == '__main__':
    # n = 100
    # n = 9995
    # n = 3141592653589793
    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()

    # 真ん中はすべて9にする
    ary[1:-1] = [9 for i in range(0, len(ary[1:-1]))]

    # すでに9が並んでたら上位を減らさない
    if ary[1:-1].count(9) != len(ary[1:-1]):
        ary[0] = ary[0] - 1

    # 下位が99 or 98でなかったら下位を89にする
    if sum(ary[-2:]) < 18:
        ary[-2] = 8
        ary[-1] = 9

    print(ary)
    print(sum(ary))

Submission status: Wrong Answer

Solution code:
s=input()
a=int(s[0])

print(a+9*(len(s)-1)-1)

Submission status: Wrong Answer

Solution code:
num=input()
s = list(num)
if int(num) == 10**len(s)-1:
    print(9*len(s))
else:
    print(int(s[0])-1+9*(len(s)-1))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = [int(n) for n in list(input())]
S = [9 for _ in N]
S[0] = N[0] - 1

print(sum(S))

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# hello worldと表示する
n =input()
N = list(n)
if 1!=len(N):
    if int(N[1]) !=9:
        print(int(N[0])-1+9*(len(N)-1))
    elif int(N[1])==9 and n!=str(10**len(N)-1):
        print(int(N[0])-1+9*(len(N)-1))
    else:   
        print(9*len(N))
else:
    print(N[0])

Submission status: Wrong Answer

Solution code:
#encoding=utf-8
def solve(n):
    k = len(str(n))
    c = int(str(n)[0])
    if str(n)[1:(k+1)] == '9' * k:
        return c + 9 * (k - 1)
    if k == 1:
        return c
    return c + 9 * (k - 1)  - 1

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

Submission status: Wrong Answer

Solution code:
if __name__=='__main__':
    N = int(input())

    N_list = list(map(int, list(str(N))))
    if 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))

Submission status: Wrong Answer

Solution code:
N = int(input())
if N < 10 :
  print(N)
else :
  L = str(N)
  print(9 * (len(L) - 1) + int(L[0]) - 1)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# hello worldと表示する
n =input()
N = list(n)
if 1!=len(N):
    if int(N[1]) !=9:
        print(int(N[0])-1+9*(len(N)-1))
    elif int(N[1])==9 and n!=str(len(N)*10-1):
        print(int(N[0])+9*(len(N)-1))
    else:   
        print(9*len(N))
else:
    print(N[0])

Submission status: Wrong Answer

Solution code:
N = list(input())
if 1!=len(N):
    if N[1]!=9:
        print(int(N[0])-1+9*(len(N)-1))
    elif N[i+1]==9:
        print(int(N[0])+9*(len(N)-1))
else:
    print(N[0])

Submission status: Wrong Answer

Solution code:
N = list(input())
print(N)
if 1!=len(N):
    if N[1]!=9:
        print(int(N[0])-1+9*(len(N)-1))
    elif N[i+1]==9:
        print(int(N[0])+9*(len(N)-1))
else:
    print(N[0])

Submission status: Wrong Answer

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

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
print (sum)

Submission status: Wrong Answer

Solution code:
def digit_sum(_num):
    _sum = 0
    while _num:
        _sum += _num % 10
        _num //= 10
    return _sum


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 = [digit_sum(10 ** (_digit - 1) - 1)]
if _topdig >= 2:
    _sumlist.append(_topdig - 1 + 9 * (_digit - 1))
elif _num == 2 * (10 ** (_digit - 1)) - 1:
    _sumlist.append(1 + 9 * (_digit - 1))
else:
    pass
print(max(_sumlist))

Submission status: Wrong Answer

Solution code:
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) in list: ans+=1
else: ans = int(n)

print(ans)

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

Solution code:
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)
        print(N)

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

print(D)

Submission status: Wrong Answer

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

    return True

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(9 * (l - 1) + int(tmp) - 1)
            break
        except:
            break

solve()

Submission status: Wrong Answer

Solution code:
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 1+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))

Submission status: Wrong Answer

Solution code:
N = input()

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

Submission status: Wrong Answer

Solution code:
3141592653589793

Submission status: Wrong Answer

Solution code:
N = input()

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

Submission status: Wrong Answer

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

if int(n)>=10:
    ans = 9*(l-1)+int(n[0])-1
    if n[0]=="9": ans+=1
else: ans = int(n)

print(ans)

Submission status: Wrong Answer

Solution code:
N = input()
ans = int(N[0]) - 1
ans += 9 * (len(N) - 1)
print(ans)

Submission status: Wrong Answer

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


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

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

Solution code:
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+1):
        nines+=9*10**i
    if N/nines==1: print(N)
    else: print((max_digits-1)+num_digits*9)

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())

b = []
for i in range(N):
    a = map(int, list(str(i)))
    b.append(sum(a))

print(max(b))

Submission status: Wrong Answer

Solution code:
N = int(input())

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
print(ret if len(str(N)) != 1 else N)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = list(input())
if len(N) == 1:
    print(int(N[0]))
elif N.count('9') == len(N):
    print(len(N) * 9)
elif N[0] == '1':
    print((len(N) - 1) * 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)

Submission status: Wrong Answer

Solution code:
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()
    cand = 0
    if digit_list[0] == 1:
        cand = 9 * (digit - 1)
    else:
        cand = digit_list[0] - 1 + 9 * (digit - 1)
    print(cand)

Submission status: Wrong Answer

Solution code:
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(16):
	c = 10*(x//(10**(i+1))) -1
	if c <= 0:
		c = 0
	d = max(num_sum(x), num_sum(c), f)
	f = d
print(f)

Submission status: Wrong Answer

Solution code:
import sys

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

Submission status: Wrong Answer

Solution code:
def A_Digit_Sum_2():
    a=input()
    a=str(a)
    c=(len(a)-1)*9+int(a[0])-1
    print(c)

A_Digit_Sum_2()

Submission status: Wrong Answer

Solution code:
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)
if num_digits==0: print(max_digits)
else: print((max_digits-1)+num_digits*9)

Submission status: Wrong Answer

Solution code:
def digit_sum(_num):
    _sum = 0
    while _num:
        _sum += _num % 10
        _num //= 10
    return _sum

_num = int(input())
_sumlist = []
_digit = 0
_sumlist.append(digit_sum(_num))
_sumlist.append(digit_sum((_num // 10) * 10 - 1))
while _num:
    _digit += 1
    _num //= 10
_sumlist.append(9 * (_digit - 1))
print(max(_sumlist))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = list(input())
if len(N) == 1:
    print(int(N[0]))
elif N.count('9') == len(N):
    print(len(N) * 9)
elif N[0] == '1':
    print((len(N) - 1) * 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 + 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)

Submission status: Wrong Answer

Solution code:
import scipy

Submission status: Wrong Answer

Solution code:
N = int(input())

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
print(ret)

Submission status: Wrong Answer

Solution code:
def Digit_Sum():
    _num = int(input())
    _sumlist = []
    _digit = 0
    _sumlist.append(digit_sum(_num))
    _sumlist.append(digit_sum((_num // 10) * 10 - 1))
    while _num:
        _digit += 1
        _num //= 10
    _sumlist.append(9 * (_digit - 1))
    print(max(_sumlist))


def digit_sum(_num):
    _sum = 0
    while _num:
        _sum += _num % 10
        _num //= 10
    return _sum

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())

ret = 0
f = 0
for idx,c in enumerate(str(N)):
    tmp = int(c)
    if f == 0 and tmp != 9:
        f = 1
        ret += (int(tmp) - 1)*(idx!=0 or tmp!=1) + 0
    else:
        ret += 9
print(ret)

Submission status: Wrong Answer

Solution code:
from collections import Counter

n = input()
c = Counter(n)
n9 = c['9']
if n9 == len(n):
    ans = int(n)
else:
    if n[0] == '1':
        ans = 9*(len(n)-1)
    else:
        ans = int(n[0])-1 + 9*(len(n)-1)
        
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())

ret = 0
f = 0
for idx,c in enumerate(str(N)):
    tmp = int(c)
    if f == 0 and tmp != 9:
        f = 1
        ret += (int(tmp) - 1)*(tmp!=1) + 0
    else:
        ret += 9
print(ret)

Submission status: Wrong Answer

Solution code:
n = input()
ls = list(map(int,[ _ for _ in n ]))
print(ls[0]-1 + 9*(len(ls)-1))

Submission status: Wrong Answer

Solution code:
import math
N = list(map(lambda x:int(x),input()))
print((N[0]-1) + 9*(len(N)-1))

Submission status: Wrong Answer

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

if all(N[i]=="9" for i in range(len(N))):
	ans=9*(1+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)

Submission status: Wrong Answer

Solution code:
N = int(input())

ret = 0
f = 0
for c in str(N):
    tmp = int(c)
    if f == 0 and tmp != 9:
        f = 1
        ret += int(tmp) - 1
    else:
        ret += 9
print(ret)

Submission status: Wrong Answer

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

if all(N[i]=="9" for i in range(len(N))):
	ans=9*(1+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)

Submission status: Wrong Answer

Solution code:
N=int(input())

r=0
for i in range(20):
    if N<10:
        r+=N
        break

    if N%10==9:
        # r+=9
        N=N//10
    else:
        r+=9
        N=N//10-1
print(r)

Submission status: Wrong Answer

Solution code:
s = list(map(int, input()))
if s[0] != 9:
    sumv = s[0]-1
else:
    sumv = 9
sumv += 9*(len(s)-1)
print(sumv)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
#coding:utf-8
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
print(s)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def main():
    n = input()
    if len(n) == 1:
        print(n)
        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()

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
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 num_nine==len_N or num_nine==len_N-1: sum_result=sum(N)
elif num_nine==len_N-2:
  if N_first != 9:
    sum_result=(N_first-1)+(len_N-1)*9
  else:
    sum_result=(len_N-1)*9+8

print sum_result

Submission status: Wrong Answer

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

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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
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)
print(cnt)

for i in range(size+1):
    cnt+=int(N[i])
print(cnt)

Submission status: Wrong Answer

Solution code:
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:
        k = sorted(list(t))
        print((len(s) - 1) * 9 + int(k[0]))
    else:
        print(int(s[0]) + (len(s) - 1) * 9 - 1)

Submission status: Wrong Answer

Solution code:
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:
        k = sorted(list(t))
        print((len(s) - 1) * 9 + int(k[0]))
    else:
        print(int(s[0]) + (len(s) - 1) * 9 - 1)

Submission status: Wrong Answer

Solution code:
N = list(input())
if len(N) == 1:
    print(int(N[0]))
else:
    for i, n in enumerate(N):
        if n != 9:
            # print(int(n) - 1 + 9 * (len(N) - i - 1))
            print(sum(list(map(int, N[:i + 1]))) - 1 + 9 * (len(N) - i - 1))
            break
        if i == len(N) - 1:
            print(9 * len(N))

Submission status: Wrong Answer

Solution code:
n = input()

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

print(ans)

Submission status: Wrong Answer

Solution code:
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(t)
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())

nn = str(N)

Sum = []
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))

Submission status: Wrong Answer

Solution code:
N=input()
S=[]
for i in range(len(N)):
    S.append(int(N[i]))
if int(N)<10:
    print(N)
elif int(N[0])!=9 and int(N[-1])!=9:
    print(9*(len(N)-1)+int(N[0])-1)
elif int(N[0])==9: print(9*len(N))
elif int(N[-1])==9: print(sum(S))

Submission status: Wrong Answer

Solution code:
s = input()
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)

Submission status: Wrong Answer

Solution code:
N = int(input())

def sum(n):
    s = 0
    while n != 0:
        s += n % 10
        n = n // 10
    return int(s)
        
import math
m = 0
for i in range(int(math.log10(N)) + 1):
    n = sum((N // 10**i) * 10**i - 1)
    if n > m:
        m = n

print(m)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n=input()
count=0

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

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

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


print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def main():
    n = input()
    sum = 0
    for i, c in enumerate(n):
        if c == '9':
            sum += 9
        elif i == len(n) - 1:
            sum += int(c)
        else:
            sum += int(c) - 1
            sum += 9 * (len(n) - i - 1)
            print(sum)
            return
    print(sum)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n=input()
count=0

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

if n[0]!="9":
    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)

Submission status: Wrong Answer

Solution code:
n=int(input())
li=list(map(int,list(str(n))))
print(li[0]-1+9*(len(li)-1))

Submission status: Wrong Answer

Solution code:
_num = int(input())
_sum = 0
while _num:
    _sum += _num % 10
    _num //= 10
print(_sum)

Submission status: Wrong Answer

Solution code:
#
N = input().rstrip()

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

Submission status: Wrong Answer

Solution code:
N = list(input())
if len(N) == 1:
    print(int(N[0]))
else:
    for i, n in enumerate(N):
        if n != 9:
            print(int(n) - 1 + 9 * (len(N) - i - 1))
            break
        if i == len(N) - 1:
            print(9 * len(N))

Submission status: Wrong Answer

Solution code:
s = input()
if len(s) == 1:
    print(s)
else:
    t = set(s)
    if len(t) == 1 and "9" in t:
        print(len(s) * 9)

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

Submission status: Wrong Answer

Solution code:
def poyo():
    ans = -1
    A = []
    for i in range(1, 10000):
        t = sum([ord(x) - ord('0') for x in str(i)])
        ans = max(ans, t)
        A.append(ans)
        # print(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)


import math
N = int(input())
if N < 10:
    print(N)
else:
    ans = (len(str(N)) - 1) * 9
    ans += ord(str(N)[0]) - ord('0') - 1
    print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N=raw_input()
S=list(N)
print int(S[0])-1+(len(S)-1)*9

Submission status: Wrong Answer

Solution code:
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):
    ans = sum(temp)
    print(ans)
    sys.exit()
else:
    N2[0] = N2[0]-1
    for i in range(1,len(N2)):
        N2[i] = 9
    print(sum(N2))
    sys.exit()

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
def poyo():
    ans = -1
    A = []
    for i in range(1, 10000):
        t = sum([ord(x) - ord('0') for x in str(i)])
        ans = max(ans, t)
        A.append(ans)
        # print(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)


import math
N = int(input())
ans = (len(str(N)) - 1) * 9
ans += ord(str(N)[0]) - ord('0') - 1
print(ans)

Submission status: Wrong Answer

Solution code:
from fractions import gcd
from math import factorial, ceil, sqrt, atan2, log, pi, e, asin,acos, cos, sin
from itertools import *
from fractions import Fraction
import string
import copy
import random
import bisect
from decimal import *
def id_generator(size=20, chars=string.digits):
    return ''.join(random.choice(chars) for _ in range(size))
 
def mp():
    return map(int,str(raw_input()).split())

n=input()
s=str(n)
ans=''
if s[0]!='9':
	print max(9*(len(s)-1)+(int(s[0])-1),sum(map(int,list(s))))
else:
	if s[1]=='0':
		print 9*(len(s)-1)+8
	else:
		i=0
		while i<len(s) and s[i]=='9':
			i+=1
		if i==len(s):
			print 9*len(s)
		else:
			if s[i]=='0':
				ans= 9*(len(s)-1)+8
			else:
				ans=9*(len(s)-1)+int(s[i])-1
			print ans

Submission status: Wrong Answer

Solution code:
N = input()
N_list = list(str(N))
sum = 0

for i in N_list:
    sum += int(i)

print (sum)

Submission status: Wrong Answer

Solution code:
import sys
import random

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

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

n = rdi()[0]

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
#print t
print s

Submission status: Wrong Answer

Solution code:
print((lambda N :len(N)*9+int(N[0])-10)(input()))

Submission status: Wrong Answer

Solution code:
x = int(input())
b = 0
for i in range(16):
  a = x % 10
  b = b + a
  x = x // 10
  
print(b)

Submission status: Wrong Answer

Solution code:
a=input()

m=sum([int(i) for i in list(a)])
for i in range(1,len(a)-1):
  t=str(int(a[:i])-1)
  h=a[i:]
  m=max(sum([int(i) for i in list(t)])+9*len(h),m)
print(m)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

import numpy as np

n = input()

a = np.zeros((int(len(n))))
for i in range(0,len(n)):
    a[i] = int(n[i])

print(int(sum(a)))

Submission status: Wrong Answer

Solution code:
import math

n = int(input())

a = round(math.log10(n))

amari = n //10**a
print((amari-1)+a*9)

Submission status: Wrong Answer

Solution code:
# coding=utf-8

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

if l==0:
    print(n)
elif (int(n)+1)%10==0:
    print(9*(l+1))
else:
    print(9*l + (int(n[0])-1))