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

Solution code:
s=input()
d=len(s)
if s[1:].count('9')==d-1:
    print(sum(map(int,list(s))))
else:
    print(int(s[0])-1+9*(d-1))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = list(input())

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

j = 0
for i in N:
    j += int(i)
print(max(k,j))

Submission status: Accepted

Solution code:
n = int(input())
digit = len(str(n))
ans = int(str(n)[0])
if digit > 1:
    ans += 9 * (digit - 1)
    if int(str(n)[1:]) < (10 ** (digit-1) - 1):
        ans -= 1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
c = None
res = 0
for i in range(1, len(str(n))+1):
    c = '9' * i
    if int(c) <= n:
        continue
    else:
        for j in range(9, -1, -1):
            if int(str(j) + str(c)[1:]) <= n:
                c = str(j) + str(c)[1:]
                break
        break
for i in range(len(c)):
    res += int(c[i])
print(res)

Submission status: Accepted

Solution code:
s = input()
ints = [int(a) for a in s]
lens = len(s)

if s[1:].count('9') == lens-1:
    print(sum(ints))
else:
    print(ints[0] - 1 + 9*(lens-1))

Submission status: Accepted

Solution code:
s = input()
ints = [int(a) for a in s]

cnt = 0
for a in s[1:]:
    if a == '9':
        cnt += 1
        
if cnt == len(s)-1:
    print(sum(ints))
else:
    print(int(s[0])-1 + 9*(len(s)-1))

Submission status: Accepted

Solution code:
N = input()
if int(N) < 10:
    ans = int(N)
elif N[1:].count('9') == len(N) - 1:
    ans = sum(map(int, N))
else:
    ans =  int(N[0]) - 1+ (len(N) - 1) * 9
print(ans)

Submission status: Accepted

Solution code:
N=input()
ans1=sum(list(map(int,list(N))))
ans2=int(N[0])-1+9*(len(N)-1)
print(max(ans1,ans2))

Submission status: Accepted

Solution code:
from bisect import bisect_right

N = int(input())

l = [int(str(i%9+1)+"9"*(i//9)) for i in range(9*16)]

print(bisect_right(l, N))

Submission status: Accepted

Solution code:
s = [int(x) for x in input()]

if len(s) == 1:
    print(s[0])
else:
    ans = max(s[0] - 1 + 9 * (len(s)-1), sum(s))
    print(ans)

Submission status: Accepted

Solution code:
def solve(N):
    if N < 10:
        return N
    num = int("9" * (len(str(N)) - 1))
    k = int(str(N)[0] + str(num))
    if k <= N:
        return sum(int(c) for c in str(k))
    else:
        k = k - num - 1
        return sum(int(c) for c in str(k))


assert (solve(799) == 25)
assert (solve(999) == 27)
assert (solve(200) == 19)
assert (solve(100) == 18)
assert (solve(99) == 18)
assert (solve(89) == 17)
assert (solve(5) == 5)

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

Submission status: Accepted

Solution code:
def solve(N):
    if N < 10:
        return N
    num = 9
    while num < N:
        num *= 10
        num += 9
    num -= 9
    num //= 10

    k = int(str(N)[0] + str(num))
    if k <= N:
        return sum(int(c) for c in str(k))
    else:
        k = int(str(N)[0] + str(num)) - num - 1
        return sum(int(c) for c in str(k))


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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def solve(n: int):
    l = len(str(n))

    digits = [0]*l

    for i in reversed(range(l)):
        digits[i] = n % 10
        n //= 10

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


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

Submission status: Accepted

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

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

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

N = INT()

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

suff = '9999999999999'
ans = 0 
# 後ろに付ける9の数を減らして、10^16-1から10099までを作る
for i in range(12):
    # 999～100までループ
    for j in range(999, 99, -1):
        j = int(str(j) + suff)
        if j > N:
            continue
        ans = max(digit_sum(j), ans)
    suff = suff[:-1]
# 9999以下は普通に全部やる(大した大きさじゃないので)
for i in range(9999, 0, -1):
        if i > N:
            continue
        ans = max(digit_sum(i), ans)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

# 各桁の和
def digitSum(n):
    s = str(n)
    array = list(map(int, list(s)))
    return sum(array)

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

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

Submission status: Accepted

Solution code:
a = input()
b = int(a)
c = list(a)
t = int(a[0])
r = t+(len(a)-1)*9
#print(t,a.count("9"))
if t != 9  and a.count("9") == len(a)-1:
    r = t+(len(a)-1)*9
elif a.count("9") != len(a):
    t -= 1
    r = t+(len(a)-1)*9
if b<9:
    r = b
elif b == 9:
    r = 9
    
print(r)

Submission status: Accepted

Solution code:
# coding:utf-8

N = input()

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

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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


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

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

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=input()

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

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

print(max(ans,a))

Submission status: Accepted

Solution code:
N = str(input())

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

A = []
tmp = N

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

ans1 = sum(A)

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

    print(sum(N))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def listToString(_list, split=""):
    maped_list = map(str, _list)  # mapで要素すべてを文字列に
    mojiretu = split.join(maped_list)
    return mojiretu


n = int(input())
a = [int(i) for i in str(n)]
count = 0
for i in range(len(str(n))):
    # 下i桁目までで各位の和が最大になるものを算出
    now = a[-i-1:n]
    nowSum = 0
    for j in range(len(now)):
        nowSum += now[j]
    # abcdの時、4桁での最大値はa+(bcdのmax)かa-1 + 9 + 9 + 9
    if a[-i-1] > 0:
        if nowSum < now[0] + 9 * (len(now)-1):
            a[-i-1] -= 1
            for k in range(1, len(now)):
                a[-i-1 + k] = 9
    # print("i:{} a:{} now:{} nowSum:{}".format(i, listToString(a), listToString(now), nowSum))
nowSum = 0
for i in range(len(a)):
    nowSum += a[i]
print(nowSum)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys


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


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


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

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

N=list(input())
ans=0

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
L = list(input())

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


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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N = input()
n = N

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

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

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = input()

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:

N = raw_input()

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
S = input()
flag = 0
for dig in S[1:]:
    if dig != "9":
        flag = 1

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

if flag == 1:
    ans -= 1

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n = int(input())

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


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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = input()

ans = 0
all_nine_flg = True

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=input()

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def a_digit_sum_2(N):
    n_lead = int(str(N)[0])  # Nの先頭の数字
    n_later = str(N)[1:]  # Nの先頭以外の数字列
    n_length = len(str(N))  # Nの桁数
    if n_later == '9' * (n_length - 1):  # 先頭以外が9のみから構成された数だった
        ans = n_lead + 9 * (n_length - 1)  # c9...9の各桁をそのまま足す
    else:
        ans = (n_lead - 1) + 9 * (n_length - 1)  # 先頭は1減らさなければならない
    return ans 

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

def judge(n: int) -> int:
    if n < 10:
        return n
    
    else:
        # 先頭の位が1のとき、先頭の1を落として全部9にしたものが候補
        if str(n)[0:1] == '1':
            
            cand_str = "9" * (len(str(n)) - 1)
            cand = int(cand_str)
            
            return max(digit_sum(n,10), digit_sum(cand,10))
        
        # そうでないとき、先頭の位を1小さくする
        else:
            top = n // 10**(len(str(n)) - 1)
            cand_str = str(top-1) + "9" * (len(str(n))-1)
            cand = int(cand_str)
            
            return max(digit_sum(n,10), digit_sum(cand,10))

n = int(input())

ans = judge(n)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

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

Submission status: Accepted

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

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

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

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

Submission status: Accepted

Solution code:
import math

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

Submission status: Accepted

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

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

print sum(n)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:

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

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

Submission status: Accepted

Solution code:

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

n = input()


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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from math import log10

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
def inpl(): return map(int, input().split())
N = input()
ans = 0

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

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

Submission status: Accepted

Solution code:
N = input()

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = input()

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

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

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

#print(n,o_n)

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

Submission status: Accepted

Solution code:
a =input()

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

Submission status: Accepted

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

Submission status: Accepted

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

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

solve()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

import sys, math, copy
# import fractions, itertools
# import numpy as np
# import scipy

HUGE = 2147483647
HUGEL = 9223372036854775807
ABC = "abcdefghijklmnopqrstuvwxyz"

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

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

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

    print(ma)

main()

Submission status: Accepted

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