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:
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:
        score += 8 + 9*(l-1)
    else:
        score += n[0] - 1
        score += 9*(l-1)
    print(score)

Submission status: Wrong Answer

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

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

score = 0
p = True

if l == 1:
    print(n[0])
else:
    while p == True:
        for j in range(l):
            if n[j] != 9:
                if j == l-1:
                    score += 8
                else:
                    score += n[j] - 1
                    score += 9*(l-j-1)
                break
            else:
                score += 9
        p = False
    print(score)

Submission status: Wrong Answer

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

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

score = 0
p = True

if l == 1:
    print(n[0])
else:
    while p == True:
        for j in range(l):
            if n[j] != 9:
                p = False
                score += n[j] - 1
                score += 9*(l-j-1)
                break
            else:
                score += 9
        p = False
    print(score)

Submission status: Wrong Answer

Solution code:
N = int(input())
a = 0
ans = 0
b = 0
while N > 0:
    if N % 10 != 9:
        b += 1
    if N <= 10:
        ans += N - 1
        break
    N //= 10
    a += 1
if b == 0:
    print(a * 9)
else:
    ans += 9 * a
    print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
a = 0
ans = 0
b = 0
while N > 0:
    if N <= 10:
        ans += N - 1
        break
    if N % 10 != 9:
        b += 1
    N //= 10
    a += 1
if b == 0:
    print(a * 9)
else:
    ans += 9 * a
    print(ans)

Submission status: Wrong Answer

Solution code:
N = input()

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

Submission status: Wrong Answer

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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

print(ans+n-1);

Submission status: Wrong Answer

Solution code:
n = [int(i) for i in list(input())]
l = len(n)

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = list(input())
num = len(N)
if N.count('9') == num:
    print(N*num)
else:
    if num <= 1:
        print(''.join(N))
    else:
        ans = int(N[0])-1
        ans += 9*(num-1)
        print(ans)

Submission status: Wrong Answer

Solution code:
import sys
N = list(input())
ans, count = 0, 0
num = len(N)

if len(N) == 1:
    print(int(N[0]))
    sys.exit()
    
for i in range(num):
    a = int(N[i])
    if a != 9 and count == 0:
        if i == 0:
            N[i] = str(a-1)
            ans += int(N[i])
        else:
            ans += 8
            N[i-1] = '8'
            N[i] = '9'
        count = 1
    elif a == 9:
        ans += a
    elif a != 9:
        if count == 0:
            ans += 8
            N[i-1] = '8'
            N[i] = '9'
            count = 1
        else:
            ans += 9
            N[i] = '9'
print(ans)

Submission status: Wrong Answer

Solution code:
N = list(input())
ans, count = 0, 0
num = len(N)

for i in range(num):
    a = int(N[i])
    if a != 9 and count == 0:
        if i == 0:
            N[i] = str(a-1)
            ans += int(N[i])
        else:
            ans += 8
            N[i-1] = '8'
            N[i] = '9'
        count = 1
    elif a == 9:
        ans += a
    elif a != 9:
        if count == 0:
            ans += 8
            N[i-1] = '8'
            N[i] = '9'
            count = 1
        else:
            ans += 9
            N[i] = '9'
print(ans)

Submission status: Wrong Answer

Solution code:
N = input()

c = int(N[0])
K = len(N)
ans = c + 9 * (K - 1) - 1
for i in N:
    if i != "9":
        break
else:
    ans += 1

print(ans)

Submission status: Wrong Answer

Solution code:
N = input()

c = int(N[0])
K = len(N)
print(c + 9 * (K - 1) - 1)

Submission status: Wrong Answer

Solution code:
n = list(input())
mi = 10
index = 0
for i in range(len(n)):
  if int(n[i])<mi:
    mi = int(n[i])
    index = i
if index==0:
  print(int(n[index])-1+9*(len(n)-1))
  exit()
  
n[index-1] = int(n[index-1])-1

for j in range(index,len(n)):
  n[j] = 9
n = [int(i) for i in n]
print(sum(n))

Submission status: Wrong Answer

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
n = [int(i) for i in n]
print(sum(n))

Submission status: Wrong Answer

Solution code:
N = input()

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

ans = (b-1) + 9 * a

if int(str(b) + "9"*a) < int(N):
    print(ans)
else:
    ans = b + 9 * a
    print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = input()

l = len(N) - 1
s = int(N[0]) - 1

if int(N) <= 9:
    print(int(N))
else:
    print(9*l+s)

Submission status: Wrong Answer

Solution code:
N = input()

l = len(N) - 1
s = int(N[0]) - 1

print(9*l+s)

Submission status: Wrong Answer

Solution code:
N = int(input())
x = 9
order = 0
for i in range(16):
    if x > N:
        break
    x = x*10+9
    order = i+1
ans = x
for i in range(10):
    ans -= 10**order
    print(ans)
    if  ans <= N:
        break
ret = 0
while True:
    if ans == 0:
        break
    ret += ans % 10
    ans = ans//10
print(ret)

Submission status: Wrong Answer

Solution code:
N = int(input())
x = 9
order = 0
for i in range(16):
    if x > N:
        break
    x = x*10+9
    order = i+1
ans = x
for i in range(10):
    ans -= 10**order
    if  ans < N:
        break
ret = 0
while True:
    if ans == 0:
        break
    ret += ans % 10
    ans = ans//10
print(ret)

Submission status: Wrong Answer

Solution code:
N = int(input())
x = 9
order = 0
for i in range(16):
    if x > N:
        break
    x = x*10+9
    order = i+1
ans = x
for i in range(10):
    ans -= 10**order
    print(ans)
    if  ans < N:
        break
ret = 0
while True:
    if ans == 0:
        break
    ret += ans % 10
    ans = ans//10
print(ret)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
s=input()
k=10**(len(s)-1)
#print(s,str(int(s)//k*k-1))
print(sum(map(int,str(int(s)//k*k-1))) if len(s)!=1 else s)

Submission status: Wrong Answer

Solution code:
s=input()
k=10**(len(s)-1)
print(sum(map(int,str(int(s)//k))) if len(s)!=1 else s)

Submission status: Wrong Answer

Solution code:
s=input()
k=10**(len(s)-1)
print(sum(map(int,str(int(s)//k*k-1))))

Submission status: Wrong Answer

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

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

def main():
    n = input()
    digit=len(n)
    nine_numbers=n.find('9')
    limit=0

    if int(n)<=9:
        print(n)

    elif digit==nine_numbers:
        print(9*int(nine_numbers))

    else:
        head=int(n[0])-1
        limit=str(head)+'9'*(digit-1)
        ans = digit_sum(limit)
        print(ans)

if __name__=="__main__":
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n=list(input())
le=len(n)
if le==1:
  print(int(n[0]))
  exit()
x=list(n)
x.remove(x[0])
if len(set(x))==1 and x[0]=='9':
  key=int(n[0])
  print(9*(le-1)+key-1)
  exit()
m=list(n)
m.remove(m[-1])
if len(set(m))==1 and m[0]=='9':
  print(9*(le-1)+8)
else:
  key=int(n[0])
  print(9*(le-1)+key-1)

Submission status: Wrong Answer

Solution code:
n=list(input())
le=len(n)
m=list(n)
m.remove(m[-1])
if len(set(m))==1 and m[0]=='9':
  print(9*(le-1)+8)
else:
  key=int(n[0])
  print(9*(le-1)+key-1)

Submission status: Wrong Answer

Solution code:
x=int(input())
y=str(x)
n=len(y)

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
import itertools
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
import math
import collections
import copy
import decimal


if __name__ == "__main__":
    n = int(input())
    if len(str(n)) == 1:
        print(n)
        sys.exit()
    n = list(str(n))
    if n[0] == "1":
        c = "9"*(len(n)-1)
    else:
        c = str(int(n[0])-1)+ "9"*(len(n)-1)
    ans = 0
    for i in c:
        ans += int(i)
    print(ans)

Submission status: Wrong Answer

Solution code:
import bisect
N = int(input())
if len(str(N)) == 1:
    print(N)
    exit()
L = len(list(str(N)))-1
A = [10**(L)*i+10**(L)-1 for i in range(10)]  
n = bisect.bisect_left(A,N)-1
if N in A:
    n += 1
ans = [int(list(str(A[n]))[i]) for i in range(len(list(str(A[n]))))]
print(sum(ans))
print(A)

Submission status: Wrong Answer

Solution code:
import bisect
N = int(input())
if len(str(N)) == 1:
    print(N)
    exit()
L = len(list(str(N)))-1
A = [10**(L)*i+10**(L)-1 for i in range(10)]
n = bisect.bisect_left(A,N)-1
ans = [int(list(str(A[n]))[i]) for i in range(len(list(str(A[n]))))]
print(sum(ans))

Submission status: Wrong Answer

Solution code:
N = int(input())
n = [int(list(str(N))[i]) for i in range(len(list(str(N))))]
L = len(list(str(N)))-1
a = (N-10**L+1)//(10**L+1)
print(max(L*9+a,sum(n)))

Submission status: Wrong Answer

Solution code:
N = int(input())
n = [int(list(str(N))[i]) for i in range(len(list(str(N))))]
L = len(list(str(N)))-1
a = (N-10**L+1)//(10**L+1)
print(L*9+a)

Submission status: Wrong Answer

Solution code:
n=list(input())
x=[]
for i in n:
  x.append(int(i))
out=int(n[0])-1+(len(n)-1)*9
sample=[1]
sample.extend([9]*(len(n)-1))
if [9]*len(n)==x or sample==x or len(n)==1:
  out=sum(x)
print(out)

Submission status: Wrong Answer

Solution code:
n=list(input())
x=[]
for i in n:
  x.append(int(i))
out=int(n[0])-1+(len(n)-1)*9
sample=[1]
sample.extend([9]*(len(n)-1))
if [9]*len(n)==x or sample==x:
  out=sum(x)
if len(n)==1:
  out=n[0]
print(out)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = input()

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

ok = False
ans = ""
for i, c in enumerate(n):
  x = int(c)
  if x == 9:
    ans += "9"
  else:
    if ok:
      ans += "9"
    else:
      if i == len(n)-1:
        ans = ans[:-1]
        ans += str(int(n[i-1])-1)
        ans += "9"
      else:
        ans += str(x-1)
    ok = True
    
t = 0
for c in ans:
  x = int(c)
  t += x
print(t)

Submission status: Wrong Answer

Solution code:
n = input()

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

ok = False
ans = ""
for i, c in enumerate(n):
  x = int(c)
  if x == 9:
    ans += "9"
  else:
    if ok:
      ans += "9"
    else:
      if i == len(n)-1:
        ans = ans[:-1]
        ans += str(int(n[i-1])-1)
        ans += "9"
      else:
        ans += str(x-1)
    ok = True

print(ans)
t = 0
for c in ans:
  x = int(c)
  t += x
print(t)

Submission status: Wrong Answer

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: Wrong Answer

Solution code:
import sys
import math
import itertools
def I():return int(sys.stdin.readline().replace("\n",""))
def I2():return map(int,sys.stdin.readline().replace("\n","").split())
def S():return str(sys.stdin.readline().replace("\n",""))
def L():return list(sys.stdin.readline().replace("\n",""))
def Intl():return [int(k) for k in sys.stdin.readline().replace("\n","").split()]
def Lx(k):return list(map(lambda x:int(x)*-k,sys.stdin.readline().replace("\n","").split()))

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

Submission status: Wrong Answer

Solution code:
import sys
import math
import itertools
def I():return int(sys.stdin.readline().replace("\n",""))
def I2():return map(int,sys.stdin.readline().replace("\n","").split())
def S():return str(sys.stdin.readline().replace("\n",""))
def L():return list(sys.stdin.readline().replace("\n",""))
def Intl():return [int(k) for k in sys.stdin.readline().replace("\n","").split()]
def Lx(k):return list(map(lambda x:int(x)*-k,sys.stdin.readline().replace("\n","").split()))

n = L()
n = [int(k) for k in n]
n[0] -= 1
print(9*(len(n)-1)+n[0])

Submission status: Wrong Answer

Solution code:
from itertools import product
import math


def deg2int(digits):
    return sum(d * 10**p for p, d in enumerate(digits))


N = input()
N0 = int(N[0])
N = int(N)
m = 0
m_dig = [0]
k = math.floor(math.log10(N))
for i in (k, k + 1):
    for digits in product([8, 9], repeat=i):
        n = deg2int(digits)
        s = sum(digits)
        if n <= N and s > m:
            m = s
            m_dig = digits

if len(m_dig) == k:
    m += N0 - 1
print(m)

Submission status: Wrong Answer

Solution code:
N = int(input())
if N < 10:
    print(N)
else:
    str_N = str(N)
    ### 最上位以外すべて9 の場合はその値が最大値
    for n in str_N:
        if n != '9':
            ### それ以外は最上位の桁を一つ下げてあと全部9 が一番最大
            print(int(str_N[0]) - 1 + (len(str_N) - 1) * 9)
            break
    else:
        print(int(str_N[0]) + (len(str_N) - 1) * 9)

Submission status: Wrong Answer

Solution code:
N = int(input())
if N < 10:
    print(N)
else:
    ### 2桁以上は最上位の桁を一つ下げてあと全部9 が一番最大
    str_N = str(N)
    print(int(str_N[0]) - 1 + (len(str_N) - 1) * 9)

Submission status: Wrong Answer

Solution code:
N = int(input())

### 最上位の桁を一つ下げてあと全部9 が一番最大
str_N = str(N)
print(int(str_N[0]) - 1 + (len(str_N) - 1) * 9)

Submission status: Wrong Answer

Solution code:
X = input()

if X[0] == 1:
	X2 = [9]*(len(X) - 1)
elif X.count("9") == len(X):
	X2 = X
else:
	tmp = int(X[0])-1
	X2 = [str(tmp)] + ["9"]*(len(X)-1)


print(sum(list(map(int, X2))))

Submission status: Wrong Answer

Solution code:
X = input()

if X[0] == 1:
	X2 = [9]*(len(X) - 1)
else:
	tmp = int(X[0])-1
	X2 = [str(tmp)] + ["9"]*(len(X)-1)


print(sum(list(map(int, X2))))

Submission status: Wrong Answer

Solution code:
n = list(input())

head = int(''.join(n[:2]))

for k in range(0,10):
  tmp = str(head)
  tmp = list(tmp)
#  print(tmp)
  
  if len(tmp) == 1:
    break
  elif int(tmp[1]) == 9:
    if n.count("9") == len(n):
      break
    elif k == 0:
      tmp[0] = int(tmp[0])-1
      break
    else:
      break
  elif int(tmp[1]) <= 7:
    head -= 1
    continue
  else:
    break  

#print("tmp:",tmp)

for i in range(len(n)-2):
  tmp.append(9)

#print(tmp)  
  
ans = 0  
  
for j in range(len(tmp)):
  ans += int(tmp[j])

print(ans)

Submission status: Wrong Answer

Solution code:
n = list(input())

head = int(''.join(n[:2]))

for k in range(0,10):
  tmp = str(head)
  tmp = list(tmp)
#  print(tmp)
  
  if len(tmp) == 1:
    break
  elif int(tmp[1]) == 9:
    if k == 0:
      tmp[0] = int(tmp[0])-1
      break
    else:
      break
  elif int(tmp[1]) <= 7:
    head -= 1
    continue
  else:
    break  

#print("tmp:",tmp)

for i in range(len(n)-2):
  tmp.append(9)

#print(tmp)  
  
ans = 0  
  
for j in range(len(tmp)):
  ans += int(tmp[j])

print(ans)

Submission status: Wrong Answer

Solution code:
n = list(input())

for k in range(0,10):
  head = int(''.join(n[:2]))-k
  
  tmp = str(head)
  tmp = list(tmp)
  
  if len(tmp) == 1:
    break
  elif int(tmp[1]) == 9:
    tmp[0] = int(tmp[0])-1
    break
  elif int(tmp[1]) <= 7:
    continue
  else:
    break

#print(tmp)

for i in range(len(n)-2):
  tmp.append(9)

#print(tmp)  
  
ans = 0  
  
for j in range(len(tmp)):
  ans += int(tmp[j])

print(ans)

Submission status: Wrong Answer

Solution code:
n = list(input())

for k in range(0,10):
  head = int(''.join(n[:2]))-k
  
  tmp = str(head)
  tmp = list(tmp)
  
  if len(tmp) == 1:
    break
  elif head == 99:
    continue
  elif int(tmp[1]) <= 7:
    continue
  else:
    break

#print(tmp)

for i in range(len(n)-2):
  tmp.append(9)

#print(tmp)  
  
ans = 0  
  
for j in range(len(tmp)):
  ans += int(tmp[j])

print(ans)

Submission status: Wrong Answer

Solution code:
n = list(input())

for k in range(0,10):
  head = int(''.join(n[:2]))-k
  
  tmp = str(head)
  tmp = list(tmp)
  
  if len(tmp) == 1:
    break
  elif int(tmp[1]) <= 8:
    continue
  else:
    break

#print(tmp)

if n[:2] == tmp:
  tmp.append(8)
  for i in range(len(n)-3):
    tmp.append(9)
else:  
  for i in range(len(n)-2):
    tmp.append(9)

#print(tmp)  
  
ans = 0  
  
for j in range(len(tmp)):
  ans += int(tmp[j])
  
print(ans)

Submission status: Wrong Answer

Solution code:
n = list(input())

for k in range(0,10):
  head = int(''.join(n[:2]))-k
  
  tmp = str(head)
  tmp = list(tmp)
  
  if len(tmp) == 1:
    break
  elif int(tmp[1]) <= 8:
    continue
  else:
    break

#print(tmp)

for i in range(len(n)-2):
  tmp.append(9)

#print(tmp)  
  
ans = 0  
  
for j in range(len(tmp)):
  ans += int(tmp[j])
  
print(ans)

Submission status: Wrong Answer

Solution code:
n = list(input())

for k in range(0,10):
  head = int(''.join(n[:2]))-k
  
  tmp = str(head)
  tmp = list(tmp)
  
  if len(tmp) == 1:
    break
  elif int(tmp[1]) <= 7:
    continue
  else:
    break

#print(tmp)

for i in range(len(n)-2):
  tmp.append(9)

#print(tmp)  
  
ans = 0  
  
for j in range(len(tmp)):
  ans += int(tmp[j])
  
print(ans)

Submission status: Wrong Answer

Solution code:
n = list(input())

for k in range(1,3):
  head = int(''.join(n[:2]))-k
  
  tmp = str(head)
  tmp = list(tmp)
  
  if len(tmp) == 1:
    break
  elif tmp[1] == "0":
    continue
  else:
    break

#print(tmp)

for i in range(len(n)-2):
  tmp.append(9)

#print(tmp)  
  
ans = 0  
  
for j in range(len(tmp)):
  ans += int(tmp[j])
  
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = list(map(int,input()))
cnt = 0
b = tuple(n)
c = b

for i in n:
    cnt += 1
    if i == 9:
        continue
    else:
        if cnt == len(n):
            cnt -= 1
            n[cnt-1] -= 1
            break
        else:
            if i == 0:
                n[cnt-2] -= 1
                cnt -= 1
                break
            else:
                n[cnt-1] -= 1
                break

for i in range(cnt,len(n)):
    n[i] = 9

if len(n) == 1:
    n = b

print(n)
print(b)

if sum(n) >= sum(b):
    print(sum(n))
else:
    print(sum(b))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = list(map(int,input()))
cnt = 0
ans = []
a = n[0]

for i in n:
    cnt += 1
    if i == 9:
        continue
    else:
        if cnt == len(n):
            cnt -= 1
            n[cnt-1] -= 1
            break
        else:
            if i == 0:
                n[cnt-2] -= 1
                cnt -= 1
                break
            else:
                n[cnt-1] -= 1
                break

for i in range(cnt,len(n)):
    n[i] = 9

if len(n) == 1:
    print(a)
else:
    print(sum(n))

Submission status: Wrong Answer

Solution code:
n = list(map(int,input()))
cnt = 0
ans = []

for i in n:
    cnt += 1
    if i == 9:
        continue
    else:
        if cnt == len(n):
            cnt -= 1
            n[cnt-1] -= 1
            break
        else:
            if i == 0:
                n[cnt-2] -= 1
                cnt -= 1
                break
            else:
                n[cnt-1] -= 1
                break

for i in range(cnt,len(n)):
    n[i] = 9


print(sum(n))

Submission status: Wrong Answer

Solution code:
n = list(map(int,input()))
cnt = 0
ans = []

for i in n:
    cnt += 1
    if i == 9:
        continue
    else:
        if cnt == len(n):
            cnt -= 1
            n[cnt-1] -= 1
            break
        else:
            if i == 0:
                n[cnt-2] -= 1
                cnt -= 1
                break
            else:
                n[cnt-1] -= 1
                break

for i in range(cnt,len(n)):
    n[i] = 9

print(n)

print(sum(n))

Submission status: Wrong Answer

Solution code:
n = list(map(int,input()))
cnt = 0
ans = []

for i in n:
    cnt += 1
    if i == 9:
        continue
    else:
        if cnt == len(n):
            cnt -= 1
            n[cnt-1] -= 1
            break
        else:
            n[cnt-1] -= 1
            break

for i in range(cnt,len(n)):
    n[i] = 9


print(sum(n))

Submission status: Wrong Answer

Solution code:
n = list(map(int,input()))
cnt = 0
ans = []

for i in n:
    cnt += 1
    if i == 9:
        continue
    else:
        if cnt == len(n):
            break
        else:
            n[cnt-1] -= 1
            break

for i in range(cnt,len(n)):
    n[i] = 9

print(sum(n))

Submission status: Wrong Answer

Solution code:
n = list(map(int,input()))
cnt = 0
ans = []

for i in n:
    cnt += 1
    if i == 9:
        continue
    else:
        if cnt == len(n):
            break
        else:
            n[cnt-1] -= 1
            break

for i in range(cnt,len(n)):
    n[i] = 9
print(n)

print(sum(n))

Submission status: Wrong Answer

Solution code:
n = list(map(int,input()))
cnt = 0
ans = []

for i in n:
    cnt += 1
    if n == 9:
        continue
    else:
        n[cnt-1] -= 1
        break

for i in range(cnt,len(n)):
    n[i] = 9


print(sum(n))

Submission status: Wrong Answer

Solution code:
s = input()
a = s.replace('9','')
if a == '':
  print(len(s)*9)
elif len(s) == 1:
  print(s)
else:
  print(int(s[0])-1+9*(len(s)-1))

Submission status: Wrong Answer

Solution code:
s = input()
a = s.replace('9','')
if a == '':
  print(len(s)*9)
else:
  print(int(s[0])-1+9*(len(s)-1))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
m = int(input())
m = str(m + 1)
n = list(m)

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def digit(n):
    s = str(n)
    array = list(map(int, s))
    return array


num_int = int(input())
N = digit(num_int)
for i, v in enumerate(N):
    if i == 0:
        N[i] = v - 1
    else:
        N[i] = 9

print(sum(N))

Submission status: Wrong Answer

Solution code:
n_=input()
n=int(n_)
l=len(n_)
for i in range(1,17):
    if n+1==int(n_[0])*10**(l-1):
        ans=0
        while n>0:
            ans+=n%10
            n//=10
        exit()

n = int(n_[0])*10**(l-1)-1
ans=0
while n>0:
    ans+=n%10
    n//=10
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())

if n < 10:
    print(n)
else:
    a = []
    n -= n%10 + 1
    n = str(n)
    lgh = len(n)
    b = str(int(n[0])-1) + '9'*(lgh-1)
    b = map(int, b)
    ans2 = sum(b)
    for i in range(lgh):
        a.append(n[i])
    a = map(int,a)
    ans1 = sum(a)

    print(max(ans1, ans2))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
d = input()

f = int(d[0])
res = (len(d)-1)*9 + f-1
print(res)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,sqrt,factorial,hypot,log #log2ないｙｐ
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 fractions import gcd
from random import randint
def ceil(a,b): return (a+b-1)//b
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    S=ST()
    print(int(S[0])-1 + 9*(len(S)-1))
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
lst_N = [int(c) for c in input()]
set_N = set(lst_N)
first = lst_N[0]
left = lst_N[1:]
if len(lst_N) == 1:
    print(lst_N[0])
elif len(set_N) == 1 and lst_N[0] == 9:
    print(sum(lst_N))
elif first != 9 and len(left) == 1 and lst_N[1] == 9:
    all_nine = "9" * (len(lst_N) - 1)
    lst_all_nine = [int(c) for c in all_nine]
    print(first + sum(lst_all_nine))
else:
    all_nine = "9" * (len(lst_N) - 1)
    lst_all_nine = [int(c) for c in all_nine]
    print((first-1) + sum(lst_all_nine))

Submission status: Wrong Answer

Solution code:
lst_N = [int(c) for c in input()]

first = lst_N[0]
left = lst_N[1:]
if len(lst_N) == 1:
    print(lst_N[0])
elif set(lst_N) == 9:
    print(sum(lst_N))
elif first != 9 and set(left) == 9:
    all_nine = "9" * (len(lst_N) - 1)
    lst_all_nine = [int(c) for c in all_nine]
    print(first + sum(lst_all_nine))
else:
    all_nine = "9" * (len(lst_N) - 1)
    lst_all_nine = [int(c) for c in all_nine]
    print((first-1) + sum(lst_all_nine))

Submission status: Wrong Answer

Solution code:
lst_N = [int(c) for c in input()]

first = lst_N[0]
left = lst_N[1:]
if len(lst_N) == 1:
    print(lst_N[0])
elif set(left) == 9:
    print(sum(lst_N))
else:
    all_nine = "9" * (len(lst_N) - 1)
    lst_all_nine = [int(c) for c in all_nine]
    print((first-1) + sum(lst_all_nine))

Submission status: Wrong Answer

Solution code:
lst_N = [int(c) for c in input()]

first = lst_N[0]
left = lst_N[1:]
if set(left) == 9:
    print(sum(lst_N))
else:
    all_nine = "9" * (len(lst_N) - 1)
    lst_all_nine = [int(c) for c in all_nine]
    print((first-1) + sum(lst_all_nine))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
X = int(input())
Y = list(map(int, str(X)))
if X >= 10:
  ans = max(X, Y[0]-1+9*(len(Y)-1))
else:
  ans = X
print(ans)

Submission status: Wrong Answer

Solution code:
X = int(input())
Y = list(map(int, str(X)))
if X >= 10:
  ans = Y[0]-1+9*(len(Y)-1)
else:
  ans = X
print(ans)