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 = int(input())

lst = list(map(int,str(n)))

if set(lst[1:]) == {9}:
    print(lst[0] + 9*(len(lst)-1))
else:
    print((lst[0]-1) + 9*(len(lst)-1))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# A
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(0,i-1):
  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: Wrong Answer

Solution code:
def main():
    N = input()
    lenN = len(N)
    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)
                print(ans)
                return
            else:
                ans += 9*(lenN - i) - 1
                print(ans)
                return

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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)
        
if N[-1] == 9:
    print(len(N)*9)
else:
    print(8+((len(N)-1)*9))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
print(n//10**(len(str(n))-1))
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: Wrong Answer

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

n = int(input())

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

ans = 0
while True:
    if n // 10 == 0:
        break
    ans += 9
    n //= 10
ans += (n - 1)

print(ans)

Submission status: Wrong Answer

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

if int(N) < 9:
    ans  = 0

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

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
n = input(); a = 0; b = 0; c = 0
for i in range(len(n)):
    if n[i] != "9":
        if a == 0: a += 1; b = i; c = int(n[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(9*len(n)-1)

Submission status: Wrong Answer

Solution code:
n = input(); a = 0; b = 0; c = 0
for i in range(len(n)):
    if n[i] != "9":
        if a == 0: a += 1; b = i; c = int(n[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: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import math
n = int(input())
k = int(math.log10(n))

a = n // (10 ** k)
b = n % (10 ** k)

if n % 10 == 9:
  ans = k * 9 + a
else:
  ans = k * 9 + a -1

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N=list(input())
if len(N) == 1:
    print(int(N[0]))
    exit()
all9 = True
for i in range(1,len(N)):
    if N[i] != 9:
        all9 = False
        break

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

Submission status: Wrong Answer

Solution code:
N = input()
N_row = len(N)
while True:
  if N_row == 1:
    print(int(N))
    break
  else:
    N = list(N)
    a = int(N[0])
    b = int(N[-1])
    for i in range(N_row):
      if not int(N[i]) == 9:
        sum = (a-1) + 9*(N_row-1)  
        print(sum)
        break
      else:
        sum = a + 9*(N_row-1)
        print(sum)
        break
    break

Submission status: Wrong Answer

Solution code:
#73
N = input()
N_row = len(N)
N = list(N)
a = int(N[0])
b = int(N[-1])
for i in range(N_row):
  if not int(N[i]) == 9:
    sum = (a-1) + 9*(N_row-1)  
    print(sum)
    break
else:
  sum = a + 9*(N_row-1)
  print(sum)

Submission status: Wrong Answer

Solution code:
n = int(input())
k = len(str(n))
c = int(str(n)[0])
if set(list(str(n%(10**(k-1))))) == {9}:
    print(c+9*(k-1))
else:
    print(c+9*(k-1)-1)

Submission status: Wrong Answer

Solution code:
N = input()
N_list = list(N)
N = int(N)
K = len(N_list)

ans = int(N_list[0])+(K-1)*9
ans_list = [N_list[0]] + ['9' for _ in range(K-1)]
ans_cand = int(''.join(ans_list))
print(ans_cand)
if N < ans_cand:
    print(ans - 1)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
N = input()
N_list = list(N)
N = int(N)
K = len(N_list)

ans = int(N_list[0])+(K-1)*9
if N > ans:
    print(ans - 1)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
n = input()

ans = 0
s = 0
for i in range(len(n)):
    ans = max(ans, s + int(n[i]) - 1 + 9 * (len(n) - i - 1))

print(ans)

Submission status: Wrong Answer

Solution code:
n=input()

l=len(n)
if int(n)>9:
  ans=[9]
else:
  print(int(n))
  exit()
tmp=n[l-1]
for i in range(2,l+1):
  #print(tmp, int(n[l-i]))
  if int(tmp) != 9:
    tmp=int(n[l-i])-1
    #print("tmp:",tmp)
  if i==l:
    ans.append(int(tmp))
  else:
    ans.append(9)
  #print(ans)
print(sum(ans))

Submission status: Wrong Answer

Solution code:
n = list(input())
n = [int(i) for i in (n)]
m = len(n)
if m == 1:
    print(n[0])
else:
    flg = False
    for i in range(m):
        if n[i] != 9:
            flg = True
            break
    if flg:
        n[0] -= 1
        for i in range(1,m):
            n[i] = 9
    print(sum(n))

Submission status: Wrong Answer

Solution code:
import collections


def main():
    n = list(map(lambda x: int(x), list(input())))

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

    if collections.Counter(n[1: len(n)])[9] == len(n) - 1:
        print(sum(n))

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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = input()
if N.startswith("10"):
    print(9*(len(N)-1))
elif set(list(N)) == set([9]):
    print(len(N)*9)
else:
    print(int(N[0])-1+9*(len(N)-1))

Submission status: Wrong Answer

Solution code:
import sys
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
read = sys.stdin.buffer.read
sys.setrecursionlimit(10 ** 7)
INF = float('inf')

N = input()

sN = set(N)
if len(N) > 1 and N[1:] == "9" * (len(N) - 1):
    ans = sum(map(int, list(N)))
else:
    ans = (len(N) - 1) * 9
    fst = int(N[0]) - 1
    ans += fst
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
read = sys.stdin.buffer.read
sys.setrecursionlimit(10 ** 7)
INF = float('inf')

N = input()

sN = set(N)
if len(sN) == 1 and "9" in sN:
    ans = len(N) * 9
elif len(N) > 1 and N[1:] == "9" * (len(N) - 1):
    ans = sum(map(int, list(N)))
else:
    ans = (len(N) - 1) * 9
    fst = int(N[0]) - 1
    if fst:
        ans += fst
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
read = sys.stdin.buffer.read
sys.setrecursionlimit(10 ** 7)
INF = float('inf')

N = input()

sN = set(N)
if len(sN) == 1 and "9" in sN:
    ans = len(N) * 9
else:
    ans = (len(N) - 1) * 9
    fst = int(N[0]) - 1
    if fst:
        ans += fst
print(ans)

Submission status: Wrong Answer

Solution code:
n = input()

tmp = len(n)-1

if int(n[0] + tmp*'9') < int(n):
    print(9*tmp + int(n[0]))
else:
    print(9*tmp + int(n[0])-1)

Submission status: Wrong Answer

Solution code:
n = input()

tmp = len(n)-1

print(9*tmp + int(n[0])-1)

Submission status: Wrong Answer

Solution code:
n = input()

ans = 0

tmp = len(n)-1

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

Submission status: Wrong Answer

Solution code:
n = input()

if set(list(n)) == {'9'}:
    print(len(list(n))*9)
    exit()

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

ans=0
for i in range(len(tmp)):
    ans += int(tmp[i]) 

print(ans)

Submission status: Wrong Answer

Solution code:
n = input()

if set(list(n)) == {'9'}:
    print(len(list(n))*9)
    exit()

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

ans=0
for i in range(len(tmp)):
    ans += int(tmp[i]) 

print(ans)

Submission status: Wrong Answer

Solution code:
n = input()

if set(list(n)) == {'9'}:
    print(len(list(n))*9)
    exit()

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

ans=0
for i in range(len(tmp)):
    ans += int(tmp[i]) 

print(ans)

Submission status: Wrong Answer

Solution code:
n = input()

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

ans=0
for i in range(len(tmp)):
    ans += int(tmp[i]) 

print(ans)

Submission status: Wrong Answer

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

ans = 0
if s[0] != '9' and s[1:] == '9' * (n - 1):
  ans = int(s[0]) + 9 * (n - 1)
elif n != 1:
  ans = int(s[0]) + 9 * (n - 1) - 1
else:
  ans = int(s[0])

print(ans)

Submission status: Wrong Answer

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

ans = 0
if s[0] != '9' and s[1:] == '9' * (n - 1):
  ans = int(s[0]) + 9 * (n - 1)
elif n != 0:
  ans = int(s[0]) + 9 * (n - 1) - 1
else:
  ans = int(s[0])

print(ans)

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

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

ans = 0
for c in s:
  if c != '9':
    break
else:
  print(n * 9)
  exit(0)

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

Submission status: Wrong Answer

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

ans = 0
for c in s:
  if c != '9':
    break
else:
  print(n * 9)
  exit(0)

print(int(s[0])-1 + 9 * (n - 1))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
n = input()

ans = None
for i in n[1:]:
    if int(i) != 9:
        ans = int(n[0]) - 1 + sum([9]*(len(n)-1))
        break

if ans is None:
    ans = sum([ 1 for i in list(n)  ])
       
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())

n = str(N)

res = 0

if len(n) == 1:
    res = N

else:
    nine = True
    for i in range(len(n)):
        if n[i] != "9":
            nine = False
            break

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

print(res)

Submission status: Wrong Answer

Solution code:
N = int(input())

n = str(N)

res = 0

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

else:
    if n[0] == "1":
        nine = True
        for i in range(1, len(n)):
            if n[i] != "9":
                nine = False
                break

        if nine:
            res = 1 + 9 * (len(n) - 1)
        else:
            res = 9 * (len(n) - 1)

    else:
        nine = True
        for i in range(len(n)):
            if n[i] != "9":
                nine = False
                break

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

print(res)

Submission status: Wrong Answer

Solution code:
N = int(input())

n = str(N)

res = 0

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

else:
    if n[0] == 1:
        nine = True
        for i in range(1, len(n)):
            if n[i] != "9":
                nine = False
                break

        if nine:
            res = 1 + 9 * (len(n) - 1)
        else:
            res = 9 * (len(n) - 1)

    else:
        nine = True
        for i in range(len(n)):
            if n[i] != "9":
                nine = False
                break

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

print(res)

Submission status: Wrong Answer

Solution code:
N = int(input())

n = str(N)

res = 0

if n[0] == 1:
    nine = True
    for i in range(1, len(n)):
        if n[i] != "9":
            nine = False
            break

    if nine:
        res = 1 + 9 * (len(n) - 1)
    else:
        res = 9 * (len(n) - 1)

else:
    nine = True
    for i in range(len(n)):
        if n[i] != "9":
            nine = False
            break

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

print(res)

Submission status: Wrong Answer

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

Solution code:
def resolve():
    n = input()
    l = len(n)
    if len(set(n)) == 1 and list(set(n))[0] == "9":
      print(int(n[0])*l)
      quit()
    if n[0] == "1":
        print(9 * (l-1))
    else:
        print((int(n[0])-1) + (9*(l-1)))
resolve()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
X = list(map(int,input()))
sn = []
for i in range(len(X)):
  sn.append(sum(X[0:i+1]))
ssn = []
for i in range(len(X)):
  ssn.append(sn[i]-1 + 9*(len(X)-1-i))
print(max(ssn))

Submission status: Wrong Answer

Solution code:
X = list(map(int,input()))
print(X)
sn = []
for i in range(len(X)):
  sn.append(sum(X[0:i+1]))
ssn = []
for i in range(len(X)):
  ssn.append(sn[i]-1 + 9*(len(X)-1-i))
print(max(ssn))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
keta = len(str(N))
SN = str(N)
if N <= 9:
    print(N)
elif SN.count('9') == keta:
    print(N)
elif SN.count('9') == keta-1 and SN[0] != '9':
    print((keta - 1) * 9 + int(SN[0]))
else:
    print((keta - 1) * 9 + int(SN[0])-1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

S = input()
N = int(S)

flg = True
for x in S:
    if x != '9':
        flg = False
        break
if flg:
    ans = len(S) * 9
else:
    ans = (len(S) - 1) * 9 + int(S[0]) - 1
print(ans)

Submission status: Wrong Answer

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

S = input()
N = int(S)

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

Submission status: Wrong Answer

Solution code:
N = input()
if N.count("9") == len(N):
    print(len(N)*9)
    exit()
M = N.lstrip("9")
if N != M and len(M) == 1:
    print(len(N)*9-1)
elif 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: Wrong Answer

Solution code:
n=input()
ans=0
for i in n:
    ans+=int(i)
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = list(input())

if len(n) == 1:
    print(n[0])
elif n[0] == '1':
    print(9 * (len(n) - 1))
elif n.count('9') == len(n):
    print(9 * len(n))
elif n.count('9') == len(n) - 1:
    if n[0] != 9:
        print((int(n[0])) + (9 * (len(n) - 1)))
    else:
        print(9 * (len(n) - 1) + 8)
else:
    print((int(n[0]) - 1 ) + (9 * (len(n) - 1)))

Submission status: Wrong Answer

Solution code:
n = list(input())

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

Submission status: Wrong Answer

Solution code:
n = list(input())

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

Submission status: Wrong Answer

Solution code:
n = list(input())

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

Submission status: Wrong Answer

Solution code:
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    n = input()
    l = len(n)
    ans = 0

    if int(n) < 10:
        ans = int(n)
    else:
        for i in range(l):
            score = 0
            for j in range(i):
                score += int(n[j])
            score += int(n[i]) - 1
            score += 9 * (l - (i + 1))
            ans = max(score, ans)

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n = input()

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

Submission status: Wrong Answer

Solution code:

def maxf(n):
    a = list(map(int, list(n)))
    for i, item in enumerate(a):
        if i == 0 and item != 9:
            if a[i+1:] == [9]*(len(a)-1):break
            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 a, sum(a)

print(maxf(input()))

Submission status: Wrong Answer

Solution code:
n = input()

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
from collections import defaultdict, deque, Counter
import math
 
# import copy
from bisect import bisect_left, bisect_right
# import heapq
 
# sys.setrecursionlimit(1000000)
 
# input aliases
input = sys.stdin.readline
getS = lambda: input().strip()
getN = lambda: int(input())
getList = lambda: list(map(int, input().split()))
getZList = lambda: [int(x) - 1 for x in input().split()]
 
INF = 10 ** 20
MOD = 10**9 + 7
divide = lambda x: pow(x, MOD-2, MOD)
 
def search(pros,tgt,g,d):
    tmp = 0
    cnt = 0
    rest = []
    for i in range(d):
        if tgt % 2 == 1:
            tmp += 100 * (i+1) * pros[i][0] + pros[i][1]
            cnt += pros[i][0]
        else:
            rest.append((i+1,pros[i][0]-1))
        tgt //= 2
    # print(tmp)
    if tmp >= g:
        return cnt
    for ap, ac in rest[::-1]:
        if tmp + 100 * ap * ac >= g:
            cnt += math.ceil((g - tmp)/(100 * ap))
            return cnt
        else:
            cnt += ac
            tmp += 100 * ac * ap

    return INF

def main():
    a = getS()
    # a = "3141592653589793"9995
    l = len(a)
    if a == "9" * l:
        print(9 * l)
        return 
    ans = 0
    for c in a:
        ans += int(c)
    #    print(max(9 * (l - 1), ans))
    print((9 * (l - 1)) + int(a[0]) - 1)

 
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

print(max(ans1, ans2))

Submission status: Wrong Answer

Solution code:
N = input().strip()
k = len(N)
a = int(N[0])
if k==1:
    cmax = a
else:
    cmax = (a-1)+9*(k-1)
print(cmax)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N = [int(i) for i in list(input())]
length = len(N)

for l in N[:1]:
    if not l==9:
        ans = (length-1)*9+(N[0]-1)
        print(ans)
        break
else:
    print(sum(N))

Submission status: Wrong Answer

Solution code:
n = int(input())

di = len(str(n))
ans = 0

digits = [int(w)for w in str(n)]
for i in range(di):
    if digits[i] == 0:
        continue
    digits[i] -= 1

    t = sum(digits[0:i])
    t += digits[i]
    t += 9 * (di - i - 1)
    ans = max(ans, t)

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
n = input()
print((len(n) - 1) * 9 + int(n[0]) - 1 * (n != "9" * len(n)))

Submission status: Wrong Answer

Solution code:
S=input()
N=[]
for s in S:
    N.append(int(s))
L=len(S)

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

Submission status: Wrong Answer

Solution code:
a=list(input())
p=print
x=len(a)
y=int(a[0])
print(9*x-9+y if all(i=='9' for i in a[1:]) else y+9*(x-1))

Submission status: Wrong Answer

Solution code:
a = list(input())
if all(i=='9' for i in a):
    print(9*len(a))
elif len(a)==1:
    print(a[0])
else:
    print(max(int(a[0])-1, 0)+9*(len(a)-1))

Submission status: Wrong Answer

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

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:
        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(l):
                if n[j] != 9:
                    n[j-1] = 8
                    score += 8 + 9*(l-1)
                    break
    print(score)