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:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

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

N = input()

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
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(N)
    else:
        print(int(N[0]) - 1 + 9 * len(N2))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N=input()
Nl=list(N)

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

Submission status: Wrong Answer

Solution code:
s = input()
s_n = int(s)
dig = len(s)

if dig == 1:
	print(s_n)
	exit(0)

flg = 1
for i in range(1,len(s)):
	if s[i] != '9':
		flg = 0
		break
		
if flg == 1:
	print(s_n)
	exit(0)

if s[0] == '1':
	print(9*(dig-1))
else:
	print(9*(dig-1) + int(s[0]) - 1)

Submission status: Wrong Answer

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

    res = 0
    for i in range(len(N)):
        tmp = 0
        for j in range(len(N)):
            if j <= i-1:
                tmp += N[j]
            elif j==i:
                tmp += N[j]-1
            else:
                tmp += 9
        res = max(res, tmp)

    print(res)


if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
n = input()

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

Submission status: Wrong Answer

Solution code:
a=input()
m=int(a[len(a)-1])
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:
def solve():
    N = list(map(int, list(input())))
    print(N)

    res = 0
    for i in range(len(N)):
        tmp = 0
        for j in range(len(N)):
            if j <= i-1:
                tmp += N[j]
            elif j==i:
                tmp += N[j]-1
            else:
                tmp += 9
        res = max(res, tmp)

    print(res)


if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
# coding=utf-8

n = input()

l = len(n)-1

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

Submission status: Wrong Answer

Solution code:
n = input()

#print(n)
#print(n[0])
#n
maxt1=0
for i in range(len(n)-1):
    maxt1+= int(n[i])

#999
maxt2=(len(n)-1)*9+int(n[0])-1

print(max(maxt1,maxt2))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from sys import exit, 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 = input()

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

Submission status: Wrong Answer

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

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
tes =input()
a = [int(data) for data in tes] 
print(sum(a))

Submission status: Wrong Answer

Solution code:
# coding=utf-8

n = input()

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

Submission status: Wrong Answer

Solution code:
N = list(input())
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()
s_n = int(s)
dig = len(s)

if dig == 1:
	print(s_n)
	exit(0)

if s[0] == '1':
	print(9*(dig-1))
else:
	print(9*(dig-1) + int(s[0]) - 1)

Submission status: Wrong Answer

Solution code:
n=input()

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

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

print(ans)

Submission status: Wrong Answer

Solution code:
def A_Digit_Sum_2():
    a=input()
    b=str(a)
    c=0
    for i in range(len(b)):
        c +=int(b[i])
    print(c)

A_Digit_Sum_2()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from sys import exit, 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 = input()

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
a = input()

res = 0
tmp = 0

while a:
    res = max(res, sum(map(int, str(a))) + tmp)
    a /= 10
    if a == 0:continue
    a-=1
    tmp += 9

print res

Submission status: Wrong Answer

Solution code:
n=int(input())
ret = 0
i = 0
while (n>=9):
    ret += 9
    n //=10

ret += n-1
print(ret)

Submission status: Time Limit Exceeded

Solution code:
import itertools as it

N =  int(input())
n =list(map(str,range(10)))
ans = 0
for i in it.combinations_with_replacement(n,16):
  a = int("".join(i))
  if a <= N:
    b = sum(map(int,i))
    ans = max(ans,b)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
ss = int(input())
t = 0
i = 0
while t < ss:
    t += 9*10**i
    i += 1
tmp = 0
def sumup(x):
    res = 0
    for i in str(x):
        res += int(i)
    return res

while ss >= t-9*10**(i-1) :
    s = sumup(ss)
    if tmp < s:
        tmp = s
    else:
        pass
    ss -= 1

print(tmp)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

def retSum(num):
    str_num = str(num)
    ret = 0
    for n in str_num:
        ret+=int(n)
    return ret

ret_max = 0
for i in range(n , 0, -1):
    r = retSum(i)
    if r > ret_max:
        ret_max = r
print(ret_max)

Submission status: Time Limit Exceeded

Solution code:
def cal(N):
  strn = str(N)
  ans = 0
  for s in strn:
    ans += int(s)
  return ans
 
N = int(input())
mmax = 0
for i in range(1, N+1):
  mmax = max(cal(i), mmax)

print(mmax)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
ans=0
for i in range(1,n+1):
  sa=list(str(i))
  nums=map(int,sa)
  sumj=sum(nums)
  
  ans=max(ans,sumj)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
ans=0
for i in range(1,n+1):
  sa=list(str(i))
  sumj=0
  for j in sa:
    sumj+=int(j)
  ans=max(ans,sumj)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
maxnum = 0
for i in range(N, int("1" + (len(str(N))-1)*"0") - 1-1, -1):
    tmp = list(str(i))
    tmp = [int(item) for item in tmp]
    maxnum = max(sum(tmp), maxnum)

print(maxnum)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
ans = 0
for i in range(1, n + 1):
    now = 0
    for c in str(i):
        now += int(c)
    ans = max(ans, now)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
m=0
for i in range(n+1):
  a=list(str(i))
  b=[int(j) for j in a]
  n=0
  for k in b:
    n+=k
  if m<n:
    m=n
print(m)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

max_number = 0
for i in range(1, n+1):
    numbers = map(int, list(str(i)))
    sum_digit_num = 0
    for n in numbers:
        sum_digit_num = sum_digit_num + n
    if sum_digit_num > max_number:
        max_number = sum_digit_num

print(max_number)

Submission status: Time Limit Exceeded

Solution code:
def digit_sum(n):
    result = 0
    while n != 0:
        result += n % 10
        n = n // 10
    return result

num = int(input())
print(max([digit_sum(n) for n in range(1, num + 1)]))

Submission status: Time Limit Exceeded

Solution code:

pattern = "simple"

n = int(input())

if pattern == "simple":
    
    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

    # print("max_sum  : {}\nmax_sum_i: {}".format(max_sum, max_sum_i))
    print(max_sum)

elif pattern == "temp":
    max_sum_i = int(str(n)[0]) * 10**(len(str(n))-1) -1
    max_sum = sum([int(i) for i in str(max_sum_i)])

    # print("max_sum  : {}\nmax_sum_i: {}".format(max_sum, max_sum_i))
    print(max_sum)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

max_sum = 0
for i in range(10**(len(str(n))-1)-1, n+1):
    this_sum = sum([int(n_part) for n_part in str(i)])
    if max_sum < this_sum:
        max_sum = this_sum

print(max_sum)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

max_sum = 0
for i in range(0,n+1):
    this_sum = sum([int(n_part) for n_part in str(i)])
    if max_sum < this_sum:
        max_sum = this_sum

print(max_sum)

Submission status: Time Limit Exceeded

Solution code:
def main():
    n = int(input())
    
    cnt = 0
    for i in range(1, n + 1):
        A = str(i)
        tmp = 0
        for a in A:
            tmp += int(a)
        cnt = max(cnt, tmp)
    print(cnt)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
def sum_digit(number: int):
    string = str(number)
    array = list(map(int, list(string)))
    return sum(array)


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

    sum_max = 0

    while number > 0:
        total_value = sum_digit(number)

        if total_value > sum_max:
            sum_max = total_value
        number -= 1
     
    print(sum_max)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
c = len(list(str(N))) - 1
b = []
if N%10 == 0:
    d = len(list(str(N-1))) - 1
    for i in range(10**d,N+1):
        a = map(int,list(str(i)))
        b.append(sum(a))

    print(max(b))

else:
    for i in range(10**c,N+1):
        a = map(int,list(str(i)))
        b.append(sum(a))

    print(max(b))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
n=0
num=[]
for i in range(N):
    s=list(str(N-i))
    for j in range(len(s)):
        n+=int(s[j])
    num.append(n)
    n=0
print(max(num))

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
N = int(input())
count = N
kakunin = 0
while 0 < count:
    kari = sum(map(int,str(N)))
    if kakunin < kari:
        kakunin = kari
    N -= 1
    count -= 1
print(kakunin)

Submission status: Time Limit Exceeded

Solution code:
_num = int(input())
_sumlist = []
for i in range(_num // 10, _num + 1):
    _sum = 0
    while i:
        _sum += i % 10
        i //= 10
    _sumlist.append(_sum)
print(max(_sumlist))

Submission status: Time Limit Exceeded

Solution code:
_num = int(input())
_sumlist = []
for i in range(1, _num + 1):
    _sum = 0
    while i:
        _sum += i % 10
        i //= 10
    _sumlist.append(_sum)
print(max(_sumlist))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
max = 0
for j in range(N+1):
    sum = 0
    N_list = list(str(j))
    for i in N_list:
        sum += int(i)
    if max < sum:
        max = sum

print (max)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

def sum(n):
    s = 0
    if n == 0:
        return 0
    else:
        while n != 0:
            s += n % 10
            n = n // 10
        return int(s)

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


print(m)

Submission status: Time Limit Exceeded

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 = sum(N)
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: Time Limit Exceeded

Solution code:
import math
n = int(input())
max = 0
s = 10**int(math.log(n,10)) - 1
for i in range(s,n+1):
    num = sum(list(map(int,list(str(i)))))
    max = num if num > max else max
print(max)

Submission status: Time Limit Exceeded

Solution code:
num = int(input())
max = 0
while num !=0:
    cnt = 0
    for i in str(num):
        cnt += int(i)
    if cnt > max:
        max = cnt
#     print(num,':',cnt)
    num -= 1
    
print(max)

Submission status: Accepted

Solution code:
s = input()
ch = 0
for i in range(len(s)-1):
    if s[i+1] != "9":
        ch = 1
        break
cnt = 9*(len(s)-1)
if ch == 1:
    print(cnt + int(s[0])-1)
else:
    print(cnt + int(s[0]))

Submission status: Accepted

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

ans = 0
for i, e in enumerate(n):
    if i == 0:
        ans += n[i]
        continue

    if n[i] != 9:
        ans = ans - 1 + 9 * (len(n) - i)
        break

    ans += 9

print(ans)

Submission status: Accepted

Solution code:
n = int(input())

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

Submission status: Accepted

Solution code:
def ketawa(x):
    l = list(str(x))
    wa = 0
    for a in l:
        wa += int(a)
    return wa

n = int(input())
lst = list(str(n))
keta = len(str(n))




nine = int("".join([lst[0]] + ["9"]*(keta-1)))

if nine <= n:
    print(ketawa(nine))
else:
    lst_head = str(int(lst[0]) - 1)
    nine = int("".join([lst_head] + ["9"]*(keta-1)))
    print(ketawa(nine))

Submission status: Accepted

Solution code:
#-*-coding:utf-8-*-
import sys
input=sys.stdin.readline

def main():
    n = list(map(int,input().rstrip()))
    digit=len(n)
    ans1=0
    ans2=0

    if digit==1:
        print(n[0])
        exit()
    else:
        ans1=sum(n)
        ans2=n[0]-1+(digit-1)*9
        print(max(ans1,ans2))

if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
N = input()

def rest_nine(x):
  for i in range(1, len(x)):
    if int(x[i]) != 9:
      return False
  return True

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

Submission status: Accepted

Solution code:
n = list(input())
if len(n) == 1:
    print(n[0])
    exit()
n = [int(n[i]) for i in range(len(n))]
m = set(n[1:])
if m == {9}:
    print(sum(n[1:]) + n[0])
    exit()
else:
    print(9*(len(n)-1) + n[0] -1)

Submission status: Accepted

Solution code:
N = input()
N_i = int(N)
N_s = list(N)


for i in range(0,17):
  if N_i < 10 ** i:
    break
flag = True
for j in range(1,i):
  if N_s[j] != "9":
    flag = False
    break

if flag == True:
  # 最上位を除いたすべての桁が9
   print(int(N_s[0])+9*(len(N_s)-1))
else:
   print(int(N_s[0])+9*(len(N_s)-1)-1)

Submission status: Accepted

Solution code:
# coding: utf-8


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

    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def main():
    N = input()
    lenN = len(N)
    listN = list(N)
    N2 = [int(i) for i in listN]
    bf = sum(N2)
    ans = 0
    for i, s in enumerate(N):            
        if s == '9':
           ans += 9
        else:
            if i == 0:
                ans += int(s)-1 + 9*(lenN - i - 1)
                break
            else:
                ans += 9*(lenN - i) - 1
                break
    ans = max(ans, bf)
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

N = N + 1

N_str = str(N)

first_N = int(N_str[0]) - 1
num = len(N_str)-1

print(num*9+first_N)

Submission status: Accepted

Solution code:
import sys
N = list(input())

if N.count("9") == len(N):
    print(len(N)*9)
    sys.exit(0)
if N.count("9") == len(N)-1:
    for n in range(len(N)):
        if N[n] != "9":
            if n == 0:
                i = int(N[n])
                print(i+((len(N)-1)*9))
                sys.exit(0)
            else:
                print(8+((len(N)-1)*9))
                sys.exit(0)

for n in range(len(N)-1):
    i = int(N[n])
    if i != 9:
        cnt1 = len(N[:n])
        cnt2 = len(N[n+1:])
        print((cnt1*9)+(i-1)+(cnt2*9))
        sys.exit(0)

Submission status: Accepted

Solution code:
import itertools as it

N = list(input())
l = len(N)
a = N[0]+"9"*(l-1) #N[0]+9...9

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

if int("".join(N))!=int(a):
  ans-=1

print(ans)

Submission status: Accepted

Solution code:
import sys, math
from functools import lru_cache
from collections import deque
sys.setrecursionlimit(10**9)
MOD = 10**9+7

def input():
    return sys.stdin.readline()[:-1]

def mi():
    return map(int, input().split())

def ii():
    return int(input())

def i2(n):
    tmp = [list(mi()) for i in range(n)]
    return [list(i) for i in zip(*tmp)]

N = ii()
m = 0
k = N
while k > 0:
    m += k%10
    k //= 10
print(max(m, int(str(N)[0])-1+9*(len(str(N))-1)))

Submission status: Accepted

Solution code:
n = int(input())
ans = n

if 8 < n:
    num = 8
    ans = 8
    add = 1
    flag = 0
    
    while 1:
        add *= 10
        
        for i in range(9):
            num += add
            ans += 1
            
            if n <= num:
                flag = 1
                break
                
        if flag == 1:
            break
            
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
if (n//10**(len(str(n))-1)+1)*10**(len(str(n))-1)-1 > n:
  mx = (n//10**(len(str(n))-1))*10**(len(str(n))-1)-1
else:
  mx = (n//10**(len(str(n))-1)+1)*10**(len(str(n))-1)-1
print(sum(list(map(int,str(mx)))))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

n = int(input())
str_n = str(n)

if n < 10:
    print(n)
    sys.exit(0)

ans = 0
while True:
    if n // 10 == 0:
        break
    ans += 9
    n //= 10

# 最上位から1桁下が全て9の場合、繰り下がりが不要
if set(str_n[1:]) == {'9'}:
    ans += n
else:
    ans += (n - 1)

print(ans)

Submission status: Accepted

Solution code:
N = input()
l = list(map(int,N))
i = 0
#ans = l[0] - 1 + (len(l)-1)*9 #暫定の答え

if int(N) < 9:
    ans  = int(N)

elif l.count(9) >= len(l)-1:
    if l[len(l)-1] != 9:
        spot = 8
    else:
        spot = min(l)
    ans = spot + (len(l)-1)*9

else:
    while True:
        if l[i] < 9 and i < len(l)-1:
            spot = l[i] - 1
            break
        else:
            i += 1 
    ans = spot + (len(l)-1)*9

print(ans)

Submission status: Accepted

Solution code:
n=input()
k=len(n)
if k==1:
    print(n);exit()
nn=int(n)
ans= nn//(10**(k-1))-1+9*(k-1)
print(max(ans, sum([int(i) for i in n]) ))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = input(); a = 0; b = 0; c = int(n[0])
for i in range(len(n)):
    if n[i] != "9":
        if a == 0: a += 1; b = i
        else: a += 1
if a == 0: print(9*len(n))
elif a == 1 and b == 0: print(c+9*(len(n)-1))
else: print(c-1+9*(len(n)-1))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N = list(input())
N_int = [int(n) for n in N]
cnt = 0

for i in range(len(N_int)-1):
    if N_int[i+1] != 9:
        N_int[i] -= 1
        for j in range(i+1, len(N_int)):
            N_int[j] = 9

for i in N_int:
    cnt += int(i)

print(cnt)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
S=str(N)
if len(S)==1:
  print(N)
  exit()
a=int(S[0])
b=len(S)
N=a+((b-1)*9)-1
if int(S[1:])==(10**(b-1))-1:
  N+=1
print(N)

Submission status: Accepted

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


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = list(input())
n = list(map(int,n))
m = len(n)
if m == 1:
    print(n[0])
elif len(set(n)) == 1 and n[0] == 9:
    print(9 * m)
elif len(set(n)) == 2 and n[0] != 9 and n[1] == 9:
    print(n[0] + (m - 1) * 9)
else:
    print(n[0] - 1 + (m - 1) * 9)

Submission status: Accepted

Solution code:
def solve():
    N = input()
    if len(N) == 1:
        print(int(N))
    elif all(i == '9' for i in N[1:]):
        print(int(N[0]) + 9 * (len(N) - 1))
    else:
        print((int(N[0]) - 1) + 9 * (len(N) - 1))

if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
N = input()
a = ''
kazu = int(N[0])
for i in range(len(N) - 1):
  a = a + '9'
if N[1:] == a:
  print(9 * (len(N) - 1) + kazu)
else:
  print(9 * (len(N) - 1) + kazu - 1)

Submission status: Accepted

Solution code:
# row = [int(x) for x in input().rstrip().split(" ")]
# n = int(input().rstrip())
# s = input().rstrip()

def resolve():
    import sys
    input = sys.stdin.readline

    num_list = [int(x) for x in input().rstrip()]

    ans = sum(num_list)

    for i in range(1, len(num_list)+1):
        tmp = sum([x for x in num_list[:i]]) - 1 + 9 * (len(num_list) - i)
        if tmp > ans:
            ans = tmp

    print(ans)


if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
def examA():
    N = SI()
    n = len(N)
    if n==1:
        print(N)
        return
    ans = (n-1)*9 + int(N[0]) - 1
    if int(N[1:])==int("9"*(n-1)):
        ans += 1
    print(ans)
    return

# 参考 https://atcoder.jp/contests/agc021/submissions/8392122
def examB():
    def norm2(vec):
        return math.sqrt(vec[0] ** 2 + vec[1] ** 2)
    # any 2 points must have different position.
    def ConvexHull(point_list):
        pos2idx = {point_list[i]: i for i in range(len(point_list))}
        y_val = defaultdict(list)
        x_list = sorted(list(set([p[0] for p in point_list])))
        for x, y in point_list:
            y_val[x].append(y)

        upper = [(x_list[0], max(y_val[x_list[0]]))]
        lower = [(x_list[0], min(y_val[x_list[0]]))]
        prev = float('inf')
        for xi in x_list[1:]:
            x0, y0 = upper[-1]
            x1, y1 = xi, max(y_val[xi])
            if (y1 - y0) / (x1 - x0) < prev:
                upper.append((x1, y1))
                prev = (y1 - y0) / (x1 - x0)
            else:
                while True:
                    x0, y0 = upper[-1]
                    if len(upper) == 1:
                        upper.append((x1, y1))
                        break
                    x00, y00 = upper[-2]
                    if (y1 - y0) / (x1 - x0) > (y1 - y00) / (x1 - x00):
                        upper.pop()
                    else:
                        prev = (y1 - y0) / (x1 - x0)
                        upper.append((x1, y1))
                        break

        prev = -float('inf')
        for xi in x_list[1:]:
            x0, y0 = lower[-1]
            x1, y1 = xi, min(y_val[xi])
            if (y1 - y0) / (x1 - x0) > prev:
                lower.append((x1, y1))
                prev = (y1 - y0) / (x1 - x0)
            else:
                while True:
                    x0, y0 = lower[-1]
                    if len(lower) == 1:
                        lower.append((x1, y1))
                        break
                    x00, y00 = lower[-2]
                    if (y1 - y0) / (x1 - x0) < (y1 - y00) / (x1 - x00):
                        lower.pop()
                    else:
                        prev = (y1 - y0) / (x1 - x0)
                        lower.append((x1, y1))
                        break

        # return upper, lower
        # return [pos2idx[xy] for xy in upper], [pos2idx[xy] for xy in lower]

        upper_idx, lower_idx = [pos2idx[xy] for xy in upper], [pos2idx[xy] for xy in lower]
        if upper_idx[-1] == lower_idx[-1]:
            upper_idx.pop()
        CH_idx = upper_idx
        CH_idx.extend(reversed(lower_idx))
        if CH_idx[0] == CH_idx[-1] and len(CH_idx) > 1:
            CH_idx.pop()
        return CH_idx

    N = I()
    P = [[]for _ in range(N)]
    D = defaultdict(int)
    for i in range(N):
        x,y = LI()
        P[i] = (x,y)
        D[(x,y)] = i
    C = ConvexHull(P)
    ans = [0]*N
    if len(C)==2:
        for c in C:
            ans[c] = 0.5
        for v in ans:
            print(v)
        return
    #print(C)
    for i,c in enumerate(C):
        s, t, u = C[i - 1], C[i], C[(i + 1) % len(C)]
        x0, y0 = P[s]
        x1, y1 = P[t]
        x2, y2 = P[u]
        vec0 = (y0 - y1, x1 - x0)
        vec1 = (y1 - y2, x2 - x1)
        ans[t] =  math.acos((vec0[0] * vec1[0] + vec0[1] * vec1[1]) / (norm2(vec0) * norm2(vec1))) / (2 * math.pi)
    for v in ans:
        print(v)
    return

def examC():
    ans = 0
    print(ans)
    return

def examD():
    S = SI()
    N = len(S)
    K = I()
    if K>150:
        K = 150
    dp = [[[0]*(K+1) for _ in range(N)] for i in range(N)]
    for i in range(N):
        for j in range(K+1):
            dp[i][i][j] = 1
    for i in range(2,N+1):
        for k in range(K+1):
            for l,r in enumerate(range(i-1,N)):
                if S[l]==S[r]:
                    dp[l][r][k] = dp[l+1][r-1][k] + 2
                else:
                    if k>0:
                        dp[l][r][k] = max(dp[l+1][r][k],dp[l][r-1][k],dp[l+1][r-1][k-1]+2)
                    else:
                        dp[l][r][k] = max(dp[l + 1][r][k], dp[l][r - 1][k])
    ans = max(dp[0][-1])
    #print(dp)
    print(ans)
    return

def examE():
    ans = 0
    print(ans)
    return

def examF():
    ans = 0
    print(ans)
    return

from decimal import Decimal as dec
import sys,bisect,itertools,heapq,math,random
from copy import deepcopy
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
def I(): return int(input())
def LI(): return list(map(int,sys.stdin.readline().split()))
def DI(): return dec(input())
def LDI(): return list(map(dec,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet,_ep
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
_ep = 10**(-12)
alphabet = [chr(ord('a') + i) for i in range(26)]

sys.setrecursionlimit(10**7)

if __name__ == '__main__':
    examA()

"""
142
12 9 1445 0 1
asd dfg hj o o
aidn
"""

Submission status: Accepted

Solution code:
S = input()
N = int(S)
K = len(S)
c = int(S[0])

if len(S) == 1:
  ans = c
elif (N+1)/(10**(K-1))-1 == c:
  ans = c + 9*(K-1)
else:
  ans = (c-1) + 9*(K-1)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = input()
tmp = len(N) - 1
NL = list(N)
if len(N) == 1:
    print(int(N))
elif all(x == "9" for x in NL[1:]):
    print(int(N[0]) + tmp * 9)
else:
    print(int(N[0]) - 1 + tmp * 9)

Submission status: Accepted

Solution code:
def solve(N):
    if len(N) > 1:
        tmp = N[0] + '9' * (len(N) - 1)
    else:
        print(int(N))
        exit()

    if int(tmp) > int(N):
        tmp = str(int(N[0]) - 1) + '9' * (len(N) - 1)

    ans = 0
    for s in tmp:
        ans += int(s)

    return ans


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

Submission status: Accepted

Solution code:
n = input()

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

print(num)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

def main():
    s = input()[:-1]
    n = len(s)
    dp = [[0, 0] for _ in range(n+1)]
    
    for i in range(n):
        key = int(s[n-1-i])
        dp[i+1][0] = dp[i][0] + key
        
        if key > 0:
            dp[i+1][0] = max(dp[i+1][0], dp[i][1] + key - 1)
        
        dp[i+1][1] = max(dp[i][0], dp[i][1]) + 9
    
    print(dp[n][0])
    
    
if __name__ == "__main__":
    main()

Submission status: Accepted

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