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: Runtime Error

Solution code:
def resolve():
    n = input()
    l = len(n)
    if n[:1] == "9" * l-1: #c9999...99のとき
        print(int(n[0]) + 9*(l-1))
    else:
        print((int(n[0])-1) + (9*(l-1)))
resolve()

Submission status: Runtime Error

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)

return mmax

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n = input()

def nine(s):
    st = ""
    for _ in range(s):
        st += "9"
    return st


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

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

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

if int(n[0]) == 4:
    if int(n[1:]) >= int(nine(len(n) - 1)):
        print(9*(len(n) -1) + 4)
        exit()
    else:
        print(9*(len(n) -1) + 3)
        exit()

if int(n[0]) == 5:
    if int(n[1:]) >= int(nine(len(n) - 1)):
        print(9*(len(n) -1) + 5)
        exit()
    else:
        print(9*(len(n) -1) + 4)
        exit()

if int(n[0]) == 6:
    if int(n[1:]) >= int(nine(len(n) - 1)):
        print(9*(len(n) -1) + 6)
        exit()
    else:
        print(9*(len(n) -1) + 5)
        exit()

if int(n[0]) == 7:
    if int(n[1:]) >= int(nine(len(n) - 1)):
        print(9*(len(n) -1) + 7)
        exit()
    else:
        print(9*(len(n) -1) + 6)
        exit()

if int(n[0]) == 8:
    if int(n[1:]) >= int(nine(len(n) - 1)):
        print(9*(len(n) -1) + 8)
        exit()
    else:
        print(9*(len(n) -1) + 7)
        exit()

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n = int(input())

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

Submission status: Runtime Error

Solution code:
def maxf(n):
    a = list(map(int, list(n)))
    for i, item in enumerate(a):
        if i == 0 and item != 9:
            a[i] -=1
            a[i+1:] = [9]*(len(a)-1)
            break
        elif item != 9:
            a[i-1] -= 1
            a[i:] = [9]*(len(a)-i)
            break
    return sum(a)

print(maxf(input())

Submission status: Runtime Error

Solution code:
N = int(input())

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

res = 0
for t in tmp:
    res += int(t)

print(res)

Submission status: Runtime Error

Solution code:
N = int(input())

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

res = 0
for t in tmp:
    res += int(t)

print(res)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
N = int(input())
n = len(str(N))
NS = str(N)
ans0 = 0
for i in range(NS):
    ans0 += int(i)
ans1 = int(NS[0])-1 + 9 * (n-1)
ans2 = 9 * (n-1)

print(max(ans0, ans1, ans2))

Submission status: Runtime Error

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

for i in range(l):
    n[i] = int(n[i])

score = 0

if l == 1:
    print(n[0])
else:
    if n.count(9) == l-1:
        if n[0] != 9:
            score += sum(n)
        else:
            score += 8 + 9*(l-1)
    else:
        if n[0] != 9:
            score += n[0]-1 + 9*(l-1)
        else:
            for j in range(n):
                if n[j] != 9:
                    n[j-1] = 8
                    score += 8 + 9*(l-1)
                    break
    print(score)

Submission status: Runtime Error

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

for i in range(l):
    n[i] = int(n[i])

score = 0

if l == 1:
    print(n[0])
else:
    if n.count(9) == l-1:
        if n[0] != 9:
            score += sum(n)
        else:
            score += 8 + 9*(l-1)
    else:
        if n[0] != 9:
            score += n[0]-1 + 9*(l-1)
        else:
            for j in range(n):
                if n[j] != 9:
                    n[j-1] = 8
                    score += 8 + 9*(n-1)
                    break
    print(score)

Submission status: Runtime Error

Solution code:
n = input()

s = str(n)
if len(s)==1:
  print(n)
elif s.count('9')==len(s):
  print(9*len(s))
else:
  for i in range(1,len(s)):
    if s[i]!='9':
      t = int(s[i-1])
      break  
  print(t-1 + (len(s)-1)*9 )

Submission status: Runtime Error

Solution code:
n = input()

s = str(n)
if len(s)==1:
  print(n)
else:
  for i in range(1,len(s)):
    if s[i]!='9':
      t = int(s[i-1])
      break  
  print(t-1 + (len(s)-1)*9 )

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n = int(input())
k = len(str(n))
a = 9 * (k - 1)
b = 0
for i in range(k):
  b += int(str(n)[i])
c = 0
if k > 2:
  m = str(int(n))[:k - 2] + str(int(int(str(n)[-2]) - 1)) + '9'
  for i in range(k):
    c += int(m[i])
ans = max(a, b, c)
print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
Num = str(input())
L = len(Num)
if Num[1:] == '9'*(l-1):
  val = int(Num[0]) + 9*(l-1)
else:
  val = int(Num[0])-1 + 9*(l-1)
print(val)

Submission status: Runtime Error

Solution code:
n = list(input())
mi = 9
index = 0
for i in range(len(n)):
  if int(n[i])<mi:
    mi = int(n[i])
    index = i
n[index-1] = int(n[index-1])-1 
for j in range(index,len(n)):
  n[j] = 9
print(sum(n))

Submission status: Runtime Error

Solution code:
N = int(input())

for i in range(17):
    if 10**i <=N <10**(i+1):
        m=i+1
        break

for j in range(1,10):
    if (10**(m-1))*j + 10**(m-1)-1 <=N< (j+1)*(10**(m-1))+10**(m-1)-1:
        a = j
        break

print(a+9*(m-1))

Submission status: Runtime Error

Solution code:
import sys
n = input()
intn = int(n)
s = int("1"*len(n))*9
if s==intn:
  print(len(n)*9)
  sys.exit()
ans=int("1"*(len(n)-1))*9
xx = int(n[0])-1
def split(aa):
  return [c for c in aa]
ans=split(str(ans))
c = 0
for i in range(len(ans)):
    ans[c] = int(ans[c])
    c+=1
print(sum(ans)+xx)

Submission status: Runtime Error

Solution code:
n = input()
intn = int(n)
s = int("1"*len(n))*9
if s==intn:
  print(len(n)*9)
  quit()
ans=int("1"*(len(n)-1))*9
xx = int(n[0])-1
def split(aa):
  return [c for c in aa]
ans=split(str(ans))
c = 0
for i in range(len(ans)):
    ans[c] = int(ans[c])
    c+=1
print(sum(ans)+xx)

Submission status: Runtime Error

Solution code:
n=list(input())
n_list=[int(i) for i in n]
if n_list[0]!=1 and set(n_list)!=[9]:
  ans=9*(len(n_list)-1)+n_list[0]-1
  print(ans)
elif n_list[0]!=1 and set(n_list)==[9]:
  ans=len(n_list)*9
  print(ans)
elif n_list[0]+n_list[1]<=9:
  ans=(len(n_list)-1)*9
  print(ans)
else:
  ans=n_list[0]+n_list[1]-1+9*(len(n_list)-2)
  print(ans)

Submission status: Runtime Error

Solution code:
n=list(input())
n_list=[int(i) for i in n]
if n_list[0]!=1 and set(n_list)!=[9]:
  ans=9*(len(n_list)-1)+n_list[0]-1
  print(ans)
elif n_list[0]!=1 and set(n_list)==[9]:
  print(9*len(n_list))
elif n_list[0]+n_list[1]<=9:
  ans=(len(n_list)-1)*9
  print(ans)
else:
  ans=n_list[0]+n_list[1]-1+9*(len(n_list)-2)
  print(ans)

Submission status: Runtime Error

Solution code:
n=list(input())
n_list=[int(i) for i in n]
if n_list[0]!=1 and set(n_list)!=[9]:
  ans=9*(len(n_list)-1)+n_list[0]-1
  print(ans)
elif n_list[0]!=1 and set(n_list)==[9]:
  print(9*len(n_list))
elif n_list[0]+n_list[1]<=9:
  ans=(len(n_list)-1)*9
else:
  ans=n_list[0]+n_list[1]-1+9*(len(n_list)-2)

Submission status: Runtime Error

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7
#from decimal import *

def ketawa(x):
	return sum(map(int, list(str(x))))

N = input()

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

else:
	pint(max(ketawa(N), int(N[0])-1 + 9*(len(N)-1)))

Submission status: Runtime Error

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, log
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
from decimal import Decimal
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7
from decimal import *

N = INT()

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

ans2 = sum([int(x) for x in list(str(N))])

ans3 = sum([int(x) for x in list(str(N//10*10-1))])

print(max(ans1, ans2, ans3))

Submission status: Runtime Error

Solution code:
x=int(input())
y=str(x)
n=len(y)
print(int(n[0])+9*n)

Submission status: Runtime Error

Solution code:
n=int(input())
t=list(str(n))
if n%10**len(t)==0:
  print(9*(len(t)-1))
elif: n%10**(len(t)-1)==0:
  print(t[0]+9*(len(t)-2))
else:
  print(t[0]-1+9*(len(t)-2))

Submission status: Runtime Error

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

import sys
import numpy as np
int1 = lambda x: int(x) - 1
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

N = int(readline())
N_chr = str(N)
c = N_chr[0]

acc = int(c) + 9 *(len(N_chr)-1)
if N == acc:
    print(acc)
else:
    print(acc-1)

Submission status: Runtime Error

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

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
#from math import gcd
import bisect
from collections import defaultdict
from collections import deque
from collections import Counter
from functools import lru_cache


#############
# Constants #
#############

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

#############
# Functions #
#############

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

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

#####Shorten#####
def DD(arg): return defaultdict(arg)


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

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

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

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

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

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

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

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

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

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

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

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

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

#############
# Main Code #
#############
N = I()
NN = N+0

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

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

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

Submission status: Runtime Error

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

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

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

print(ans)

Submission status: Runtime Error

Solution code:
n = input()
n = list(n)
#5行目のエラーを防ぐためにstrからlistに変換
if n[-2] != '9' and n[-1] != '9':
  n[-2] = '9'
  n[-1] = str(int(n[-1])+1)

N = [int(i) for i in n]
#listをstrからintに変換
print(sum(N))

Submission status: Runtime Error

Solution code:
n = input()

if n[-2] != '9' and n[-1] != '9':
  n[-2] = '9'
  n[-1] = str(int(n[-1])+1)

print(sum(n))

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n = input()
int_n = int(n)
len_n = len(n)
ans = 0

if n[0] == '1' and n[1:] == '0'*(len_n-1):
    print(9 * (len_n - 1))
    exit()

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

Submission status: Runtime Error

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

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

Submission status: Runtime Error

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

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

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:
N = input()

l = len(N)

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

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:
N = input()

l = len(N)
j = "9"*(l-1)

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

Submission status: Runtime Error

Solution code:
n =input()

#最上位の桁をc,桁数をkとすると、
#c以外が9の場合(ex.49999)はc,9*(k-1)となる
#それ以外の場合、c,9*(k-2),8となる(ex.9995:9989)

c = int(n[0])
k = len(n)

if int(n[1:])==int('9'*(k-1)):
    ans = c+9*(k-1)
else:
    ans = c+9*(k-1)-1

print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
maxnum = 0

if (N%(int("1" + (len(str(N)) - 1)*"0"))) == int((len(str(N)) - 1) * "9"):
    tmp = list(str(N))
    tmp = [int(item) for item in tmp]
    print(sum(tmp))
else:
    tmp = list(str(N - (N%(int("1" + (len(str(N)) - 1)*"0"))) - 1))
    tmp = [int(item) for item in tmp]
    print(sum(tmp))

Submission status: Runtime Error

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

# A - Digit Sum 2
def digit_sum(str_num):
	sum = 0	
	
	for i in range(len(str_num)):
		sum += int(str_num[i])
	
	return sum


N = input()
patterns = [N]

for i in range(len(N)-1):
	if N[i] != '0':
		left, right = '', ''  
				
		if i > 0:
			left = N[:i]
		
		n = int(N[i]) - 1
		
		if i < len(N) - 1:
			right = '9' * (len(N)-i-1) 
		
		patterns.append(left + str(n) + right)

ans = 0

for p in patterns:
	ans = max(ans, digit_sum(p))
	
print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
N = input()
Dig = len(N)
if Dig == 1:
    print(N)
else:
    for i in range(Dig):
        if N[i] != 9:
            num = i
            break
        else:
            continue
    list_num = list(N)
    target = int(list_num[num]) -1
    ans = 9*(num -1) + target + 9*(DIg)
    print(ans)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import sys
import math

def main():
    N = int(input())
    digit = math.floor(math.log10(N))
    ans = [9 * digit]
    ans.append(sum([int(s) for s in str(N)]))
    
    for i in range(1, digit+1):
        tmp = [int(s) for s in str(N//(10**i) * (10**i) - 1)]
        ans.append(sum(tmp))
    print(max(ans))


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import sys
import math

def main():
    N = int(input())
    digit = math.floor(math.log10(N))
    ans = [9 * digit]
    ans.append(sum([int(s) for s in str(N)]))
    
    for i in range(1, digit+1):
        tmp = [int(s) for s in str(N//(10**i) * 10**i - 1)]
        ans.append(sum(tmp))
    print(max(ans))


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
def myAnswer(N:int)->int:
   K = len(str(N)) #桁数
   c = N // 10**(K-1) #最上位の桁の数
   if((c*10**(K-1) + int("9"*(K-1)))<= N):
      return c + 9 *(K - 1)
   else:
      return c + 9 *(K - 1) -1 


def modelAnswer():
   tmp=1
def main():
   N = int(input())
   print(myAnswer(N))

if __name__ == '__main__':
   main()

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))
a.sort(reverse=True)

ans = sum(a[1:2*(n+1):2])
print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))
a.sort(reverse=True)

ans = sum(a[1:2*n+1:2])
print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
sys.setrecursionlimit(10 ** 5 + 10)
def input(): return sys.stdin.readline().strip()

def resolve():
    N = input()
    M = [int(c) for c in N]

    lm=len(M)
    cnt=0
    if set(M[1:])=={9}:
        for i in M:
            cnt+=i
        print(cnt)
    else:
        print(9*(lm-1)+M[0]-1)

resolve()

Submission status: Runtime Error

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

ans = max(9*(n-1), sum(list(map(int, str(N)))), sum(list(map(int, str(N - N%10**(n-2) -1)))))
print(ans)

Submission status: Runtime Error

Solution code:
-*- coding: utf-8 -*-
N = input()

if len(N) == 1:
  # 1桁の数字
  print(N)
elif all(map(lambda x: x == '9', N[1:])):
  # 下位が全部9の場合
  print(sum(int(n) for n in N))
else:
  # 下位を全部9にする例
    print(9 * (len(N) - 1) + int(N[0]) - 1)

Submission status: Runtime Error

Solution code:
def main():
    n = int(input())
    l = len(str(n))
    v = int(str(n)[0]) + 9 * (l - 1)
    if str(n)[1:] == '9' * (l - 1):
        print(ans)
    else:
        print(v-1)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
N=input()
M=len(str(N))
K=int(N[0])
ans=0
for i in range (M):
	ans+=N[i]

print(max(9*(M-1)+(K-1)),ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
# coding: utf-8
N=int(input())
ans=0
n=0
sn=str(N)
w=[int(i) for i in sn]
wa=sum(w)

for i in range(N):
    if 10**i>N:
        n=i
        break
n0=int(str(N)[0])
n1=int(str(N)[1])
if n==1 or n==0:
    print(N)
elif n==2:
    ans=max(wa,9+n0)
    print(ans)
else:
    ans=9*(n-1)
    #print(n0,n1)
    if n1==0:
        ans=ans+n0-1
        print(ans)
    else:
        ans=max(ans,ans-9+n1-1+n0,ans+n0-1)
        print(ans)

Submission status: Runtime Error

Solution code:
# coding: utf-8
N=int(input())
ans=0
for i in range(N):
    if 10**i>N:
        n=i
        break
#print(n)
if n==1:
    print(N)
else:
    ans=9*(n-1)
    n0=int(str(N)[0])
    n1=int(str(N)[1])
    #print(n0,n1)
    if n1==0:
        print(ans)
    else:
        ans=max(ans,ans-9+n1-1+n0,ans+n0-1)
        print(ans)

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!
N=int(input())

if len(str(N))==1:
    ans=N
else:
    
    if 1==len(set(list(str(N))[1:])) and int(str(N)[2])==9:
        ans=9*(len(str(N))-1) + int(str(N)[0])
    else:
        ans=9*(len(str(N))-1) + int(str(N)[0])-1

print(ans)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**6)
from math import floor,ceil,sqrt,factorial,log
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from operator import itemgetter
from fractions import gcd
mod = 10 ** 9 + 7
 
#整数input
def ii(): return int(sys.stdin.readline().rstrip()) #int(input())
def mii(): return map(int,sys.stdin.readline().rstrip().split())
def limii(): return list(mii()) #list(map(int,input().split()))
def lin(n:int): return [ii() for _ in range(n)]
def llint(n: int): return [limii() for _ in range(n)]
#文字列input
def ss(): return sys.stdin.readline().rstrip() #input()
def mss(): return sys.stdin.readline().rstrip().split()
def limss(): return list(mss()) #list(input().split())
def lst(n:int): return [ss() for _ in range(n)]
def llstr(n: int): return [limss() for _ in range(n)]

#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？

n=ii()
ns=list(str(n))
if int("".join(ns[1:])) == "".join([str(9)]*(len(ns)-1)):
    print(int(ns[0])+9*(len(ns)-1))
else:
    print(int(ns[0])+9*(len(ns)-1)-1)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
import os
import itertools
import string
import heapq
import _collections
from collections import Counter
from collections import defaultdict
from functools import lru_cache
import bisect
import re
import queue


class Scanner():
    @staticmethod
    def int():
        return int(sys.stdin.readline().rstrip())

    @staticmethod
    def string():
        return sys.stdin.readline().rstrip()

    @staticmethod
    def map_int():
        return [int(x) for x in Scanner.string().split()]

    @staticmethod
    def string_list(n):
        return [input() for i in range(n)]

    @staticmethod
    def int_list_list(n):
        return [Scanner.map_int() for i in range(n)]

    @staticmethod
    def int_cols_list(n):
        return [int(input()) for i in range(n)]


class Math():
    @staticmethod
    def gcd(a, b):
        if b == 0:
            return a
        return Math.gcd(b, a % b)

    @staticmethod
    def lcm(a, b):
        return (a * b) // Math.gcd(a, b)

    @staticmethod
    def roundUp(a, b):
        return -(-a // b)

    @staticmethod
    def toUpperMultiple(a, x):
        return Math.roundUp(a, x) * x

    @staticmethod
    def toLowerMultiple(a, x):
        return (a // x) * x

    @staticmethod
    def nearPow2(n):
        if n <= 0:
            return 0
        if n & (n - 1) == 0:
            return n
        ret = 1
        while(n > 0):
            ret <<= 1
            n >>= 1
        return ret

    @staticmethod
    def sign(n):
        if n == 0:
            return 0
        if n < 0:
            return -1
        return 1

    @staticmethod
    def isPrime(n):
        if n < 2:
            return False
        if n == 2:
            return True
        if n % 2 == 0:
            return False
        d = int(n ** 0.5) + 1
        for i in range(3, d + 1, 2):
            if n % i == 0:
                return False
        return True


class PriorityQueue:
    def __init__(self, l=[]):
        self.__q = l
        heapq.heapify(self.__q)
        return

    def push(self, n):
        heapq.heappush(self.__q, n)
        return

    def pop(self):
        return heapq.heappop(self.__q)


MOD = int(1e09) + 7
INF = int(1e15)


def calc(N):
    return sum(int(x) for x in str(N))


def main():
    # sys.stdin = open("sample.txt")
    N = Scanner.int()

    ans = calc(N)
    ans = max(ans, calc(int(str(int(str(N)[0])-1) + "9" * (len(str(N)) - 1))))
    ans = max(ans, calc(N - N % 10 - 1))
    print(ans)
    return


if __name__ == "__main__":
    main()

Submission status: Runtime Error

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

m = str(int(s[0]) - 1) + (len(s) - 1) * "9" 
ans1 = 0
for i in range(len(m)):
    ans1 += int(m[i])

if s[-1] != "9":
    n1 = n - (int(s[-1]) + 1)
    s = str(n1)

ans2 = 0
for i in range(len(s)):
    ans2 += int(s[i])

print(max(ans1, ans2))

Submission status: Runtime Error

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

m = str(int(s[0]) - 1) + (len(s) - 1) * "9" 
ans1 = 0
for i in range(len(m)):
    ans1 += int(m[i])

if s[-1] != "9":
    n = n - (int(s[-1]) + 1)
    s = str(n)

ans2 = 0
for i in range(len(s)):
    ans2 += int(s[i])

print(max(ans1, ans2))

Submission status: Runtime Error

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

m = str(int(s[0]) - 1) + (len(s) - 1) * "9" 
ans1 = 0
for i in range(len(m)):
    ans1 += int(m[i])

if s[-1] != "9":
    n -= int(s[-1]) +1
    s = str(n)

ans2 = 0
for i in range(len(s)):
    ans2 += int(s[i])

print(max(ans1, ans2))

Submission status: Runtime Error

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

if s[-1] != "9":
    n1 = n - int(s[-1]) +1
    s1 = str(n1)

ans1 = 0
for i in range(len(s1)):
    ans1 += int(s[i])

m = str(int(s[0]) - 1) + (len(s) - 1) * "9" 
ans2 = 0
for i in range(len(m)):
    ans2 += int(m[i])

print(max(ans1, ans2))

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10000000)
INF = 1<<32


def solve(N: int):
    if N < 10:
        print(n)
        exit()

    n = len(list(str(N)))
    t = sum(map(int, str(N)[0]+'9'*(n-1)))

    al = [t]
    for i in range(max(0, N-10), N+1):
        x = sum(map(int, list(str(i))))
        # print(i, x)
        al.append(x)

    print(max(al)-1)

    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    solve(N)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
N = list(i for i in input())
L = len(N)

if L == 1:
  answer = int(N)
else:
  if N[0] == '9' and len(set(N)) == 1:
    answer = 9*L
  else:
    if N[0] != '9' and '9' in N and len(set(N)) == 2:
      answer = 9*(L-1) + int(N[0])
    else:
      answer = 9*(L-1) + int(N[0]) - 1

print(answer)