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:
s = input()
l = len(s)
if l == 1:
	ans = int(s)
elif s.count("9") == l:
	ans = 9 * l
else:
	ans = (l-1)*9 + (int(s[0])-1)

print(ans)

Submission status: Wrong Answer

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

print(ans)

Submission status: Wrong Answer

Solution code:
s = input()

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

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = 1e10

#solve
def solve():
    n = II()
    if str(n + 1)[0] != str(n)[0]:
        print(sum(map(int, list(str(n)))))
    else:
        print(int(str(n)[0]) + (len(str(n)) - 1) * 9)
    return


#main
if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**6)
if sys.platform in (['ios','darwin','win32']):
	sys.stdin=open('Untitled.txt')
input = sys.stdin.readline
def INT(): return int(input())
def MAP(): return [int(s) for s in input().split()]

def main():
	N = INT()
	S = [int(s) for s in str(N)]
	# print(S)

	A = []
	smaller = False
	for i in range(len(S)-1):
		if smaller:
			A.append(9)
		else:
			if S[i] == 9 and S[i+1] == 9:
				A.append(9)
			elif S[i+1] == 9:
				A.append(S[i])
			else:
				smaller = True
				A.append(S[i]-1)

	if smaller: A.append(9)
	else: A.append(S[-1])

	print(A)
	print(sum(A))

if __name__ == '__main__':
	main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
s = str(n)
l = len(s)
s1 = ""
ans = 0
if 1 <= n <= 8:
    print(n)
    exit()
if s == "9"*l:
    s1 = s
elif 1<= int(s[0]) <= 8:
    s1 = str(int(s[0])-1) + "9"*(l-1)
elif s[0] == "9" and 0<= int(s[1]) <= 9:
    s1 = "8" +  "9"*(l-1)
elif s[0] == "9" and s[1] == "0":
    s1 = "89" + "9"*(l-2) 
elif s == "9"*(l-1) + any(("1","2","3","4","5","6","7","8")):
    si = "9"*(l-2) + "89"

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

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def dgsm(n):
    ans=0
    while n>0:
        ans+=n%10
        n//=10
    return(ans)
n=input()
if len(n)==1:
    print(n)
    exit()
ans=True
for i in range(len(n)):
    if n[i]!=9:
        ans=False
if ans:
    print(len(n)*9)
    exit()
print((len(n)-1)*9+int(n[0])-1)

Submission status: Wrong Answer

Solution code:
n = input().zfill(16)
INF = 10**18
memo_ls = [[0 for i in range(2)] for j in range(17)]
memo_ls[0][0] = 0
memo_ls[0][1] = -INF
for i in range(1, 17):
    s = int(n[i-1])
    memo_ls[i][0] = memo_ls[i-1][0] + s
    memo_ls[i][1] = memo_ls[i-1][1] + 9
    if s > 0:
        memo_ls[i][1] = max(memo_ls[i][1], memo_ls[i-1][0] + s - 1)
print(max(memo_ls[-1]))

Submission status: Wrong Answer

Solution code:
from sys import stdin

s = stdin.readline().rstrip()

if len(s) == 1:
    print(int(s))
    exit()

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

Submission status: Wrong Answer

Solution code:
from sys import stdin

s = stdin.readline().rstrip()

if len(s) == 1:
    print(int(s))
    exit()

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

Submission status: Wrong Answer

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

ans = 9 * (len(in_list) - 1) + in_list[0] - 1 # 先頭の位の数を一つ下げて、他が全部9が最大のはず
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
ans=0
if n<10:
  ans=n
else:
  k=1
  while n>9:
    if n%10!=9:
      k=0
    ans+=9
    n//=10
  if k:
    ans+=k
  else:
    ans+=n-1
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = list(i for i in input())
L = len(N)
 
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)

Submission status: Wrong Answer

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

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

print(answer)

Submission status: Wrong Answer

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

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

print(answer)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n=int(input())
k=list(str(n))
ans=0
for j in range(17):
    if n==10**j:
        ans=1
        break
for i in range(len(k)):
    k[i]=int(k[i])
if ans==1:
    print(9*(len(k)-1))
else:
    print(max(sum(k),9*len(k)-9+k[0]-1))

Submission status: Wrong Answer

Solution code:
n=int(input())
k=list(str(n))
ans=0
for j in range(17):
    if n==10**j:
        ans=1
        break
for i in range(len(k)):
    k[i]=int(k[i])
if ans==1:
    print(9*len(k)-9)
else:
    print(9*len(k)-9+k[0]-1)

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

N = int(input().split()[0])
n_list = [int(char) for char in str(N)]

if N % 10 == 0:
    ans = 9 * len(n_list[1:])
else:
    total_1 = sum(n_list)
    n_list_2 = n_list[:]
    target_idx = -1
    for i, n in enumerate(n_list):
        if n == 9:
            continue
        else:
            if n != 0:
                target_idx = i
            break
    if target_idx != -1:
        if target_idx == 0:
            n_list_2[0] = n_list[0] - 1
            if len(n_list) > 2:
                n_list_2[1:] = [9] * len(n_list_2[1:])
        else:
            n_list_2[target_idx - 1] = n_list[target_idx - 1] - 1
            n_list_2[target_idx:] = [9] * len(n_list_2[target_idx:])
    total_2 = sum(n_list_2)
    ans = max(total_2, total_1)

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n=str(input())

def solve(n):
  res=0
  if len(n)==1:
    return n
  v=n[0]
  for i in range(len(n)-1):
    v+='9'
  if v==n:
    return n
  for i in range(len(n)):
    if i==0:
      res+=int(n[0])-1
    else:
      res+=9
  return res
  
print(solve(n))

Submission status: Wrong Answer

Solution code:
n=str(input())

def solve(n):
  res=0
  if len(n)==1:
    return n
  for i in range(len(n)):
    if i==0:
      res+=int(n[0])-1
    else:
      res+=9
  return res
  
print(solve(n))

Submission status: Wrong Answer

Solution code:
n=str(input())

def solve(n):
  res=0
  for i in range(len(n)):
    if i==0:
      res+=int(n[0])-1
    else:
      res+=9
  return res
  
print(solve(n))

Submission status: Wrong Answer

Solution code:
n = input()

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

Submission status: Wrong Answer

Solution code:
n = input()

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if n[:-1] !="9"*(d-1):
    print(9*(d-1))
else:
    sum=0
    for i in range(d):
        sum =sum + int(n[i])
    print(sum)

Submission status: Wrong Answer

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

n = 0
k = l - 1
for i in range(l):
    if N[i] != '9':
        k = i
        break

if k == 0:
    if N[1:] == '9' * (l - 1):
        n = int(N[0]) + 9 * (l - 1)
    elif N[0] == '1':
        n = (l - 1) * 9
    else:
        n = (int(N[0]) - 1) + (l - 1) * 9
else:
    if N == '9' * l:
        print(9 * l)
    else:
        n = (k - 1) * 9 + 8 + 9 + (l - k - 1) * 9

print(n)

Submission status: Wrong Answer

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

n = 0
k = l - 1
for i in range(l):
    if N[i] != '9':
        k = i
        break

if k == 0:
    if N[1:] == '9' * (l - 1):
            n = int(N[0]) + 9 * (l - 1)
    elif N[0] == '1':
        n = (l - 1) * 9
    else:
        n = (int(N[0]) - 1) + (l - 1) * 9
else:
    n = (k - 1) * 9 + 8 + 9 + (l - k - 1) * 9

print(n)

Submission status: Wrong Answer

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

n = 0
k = l - 1
for i in range(l):
    if N[i] != '9':
        k = i
        break

if k == 0:
    if N[0] == '1':
        n = (l - 1) * 9
    else:
        n = (int(N[0]) - 1) + (l - 1) * 9
else:
    n = (k - 1) * 9 + 8 + 9 + (l - k - 1) * 9

print(n)

Submission status: Wrong Answer

Solution code:
n = input()

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

Submission status: Wrong Answer

Solution code:
n = input()

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

Submission status: Wrong Answer

Solution code:
n = input()

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

Submission status: Wrong Answer

Solution code:
N=input()

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

def calc(str):
    l=list(map(int,str))
    all=sum(l)
    
    return all

cand1=str(int(N)-int(N[-1])-1)
cand2=str(int(N[0])*10**(len(N)-1)-1)

print(cand1,cand2,N)
print(max(calc(cand1),calc(cand2),calc(N)))

Submission status: Wrong Answer

Solution code:
def isall9(n):
    while n>0:
        if n%10!=9:
            return False
        n//=10
    return True  

N=int(input())
f9=isall9(N)
d=1
while N>=10:
    N//=10
    d+=1
if d==1:
    ans=N
elif f9:
    ans=9*d
else:
    ans=(N-1)+9*(d-1)

print(ans)

Submission status: Wrong Answer

Solution code:
n = input()

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

Submission status: Wrong Answer

Solution code:
N = str(input())
count = 0
new_n = N

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

for i in range(len(N)):
    if N[i]!="9" and i==len(N)-1:
        new_n = str(int(N)-int(N[-1])-1)
        break
    elif N[i]!="9":
        new_n = str((int(N[i]) * 10**(len(N)-i-1)) -1)
        break

for j in new_n:
    count += int(j)

print(count)

Submission status: Wrong Answer

Solution code:
N=int(input())
d=1
while N>=10:
    N//=10
    d+=1
if d==1:
    ans=N
else:
    ans=(N-1)+9*(d-1)

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def resolve():
    n = input()
    ln = len(n)
    left = int(n[0])
    c1 = int(str(left) + "9" * (ln - 1))
    c2 = int(str(left - 1) + "9" * (ln - 1))
    if c1 < int(n):
        print(sum([int(ci) for ci in str(c1)]))
    else:
        print(sum([int(ci) for ci in str(c2)]))


if __name__ == "__main__":
    resolve()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = list(input())
num = 0

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

print(num)

Submission status: Wrong Answer

Solution code:
def solve(n):
    best = 0
    ans = 0
    for i in range(20):
        for d in range(9, -1, -1):
            v = best + d * (10**i)
            if v < n:
                best = v
                ans += d
                break
    return ans

assert solve(100) == 18
assert solve(9995) == 35
assert solve(3141592653589793) == 137

n = int(input())
print(solve(n))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if S[1:]=="9"*(M-1):
  print(N)
else:
  ans=sum(map(int,list(str(int(S[0])-1)+"9"*(M-1))))
  print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

ans1=9*(L-1)
ans2=sum(N)
for i in range(L-1):
    if N[i]>N[i+1]:
        N[i]=N[i]-1
        for j in range(i+1,L):
            N[j]=9
        break

ans3=sum(N)

print(max(ans1,ans2,ans3))

Submission status: Wrong Answer

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

ans1=9*(L-1)

for i in range(L-1):
    if N[i]>N[i+1]:
        N[i]=N[i]-1
        for j in range(i+1,L):
            N[j]=9
        break

ans2=sum(N)

print(max(ans1,ans2))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = (input())
l = len(N)
top =int( N[0])

print ((l - 1 ) * 9 + (top - 1))

Submission status: Wrong Answer

Solution code:
def main():
    n = input()
    if len(n) == 1:
        print(n)
        exit()
    else:
        ans = (len(n) - 1) * 9
        k = int(n)
        for i in range(len(n)-1):
            nn = str(k)
            m = int(nn[len(n) - i - 1:]) + 1
            k -= m
            t = 0
            for j in range(len(str(k))):
                t += int(str(k)[j])
            ans = max(ans, t)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
9999999999999999

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
number = input()
length = len(number)
t = True
ans = 0
for i in range(length):
    if i > 0 and number[i] != '9':
        t = False
    ans += int(number[i])

if t:
    print(ans)
else:
    print(ans-1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys, math, fractions, itertools, collections, copy, heapq


def solve(N: int):
    N = list(str(N))
    print(int(N[0]) - 1 + 9 * (len(N)-1))
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
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: Wrong Answer

Solution code:
def main():
    N = input().strip()
    count = 0
    if len(N) == 1:
        print(int(N))
    else:
        for i in range(len(N)):
            if N[i] != "9":
                break
            count += 1
        if count == len(N):
            print(9*count)
        elif count == len(N)-1:
            print(9*count+int(N[-2])-1)
        else:
            if N[count] == "0":
                print(9*(len(N)-1)+8)
            else:
                print(9*(len(N)-1)+int(N[count])-1)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    N = input().strip()
    count = 0
    if len(N) == 1:
        print(int(N))
    else:
        for i in range(len(N)):
            if N[i] != "9":
                break
            count += 1
        if count == len(N):
            print(9*count)
        elif count == len(N)-1:
            print(9*count+int(N[-2])-1)
        else:
            print(9*(len(N)-1)+int(N[count])-1)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

n = int(input())

d = 0
tmp = n
while tmp > 0:
    tmp //= 10
    d += 1

if d <= 1:
    print(n)
else:
    if str(n).count("9") == d:
        print(9*d)
    else:
        print(9*(d-1) + n//(10**(d-1)) -1)

Submission status: Wrong Answer

Solution code:
S = input()

ans = 0
for i in range(len(S)):
    if S[i] == "0":
        continue
    first_half = sum(map(int, list(S[0:i])))
    middle = (int(S[i]) - 1)
    last_half = 9 * (len(S) - i - 1)
    ans = max(ans, first_half + middle + last_half)
    
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
n = len(str(N))
nine = int("9" * n)

def f(n):
  return sum(map(int, str(n)))

if nine == N:
  print(f(N))
elif N < nine:
  l = str(N)
  print(f((int(l[0] + "0"*(len(l)-1)))-1))
elif N > nine:
  print(f(nine))

Submission status: Wrong Answer

Solution code:
n=[int(_) for _ in list(input())]
l=len(n)
if n[0]!=9:
    if len(set(n[1:]))==1 and n[1]==9:
        print(n[0]+9*(l-1))
    else:
        print(n[0]-1+9*(l-1))
else:
    if len(set(n))==1:
        print(9*l)
    else:
        print(9*(l-1)+8)

Submission status: Wrong Answer

Solution code:
a=input();print(int(a[0])+9*~-len(a)-(int(a)+1<10**len(a)>10))

Submission status: Wrong Answer

Solution code:
S = input()
l = len(S) - 1
if l == 0:
    print(S)

else:
    temp = 9
    if int(S[0] ) != 9:
        temp = int(S[0] )- 1
    ans = temp + l * 9
    print(ans)

Submission status: Wrong Answer

Solution code:
S = input()
l = len(S) - 1
if l == 0:
    print(S)

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# 9or8で埋めたやつが強い あるいはオリジナル
# 桁を一つ落として（1なら9999...）残りを9で埋める

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

Submission status: Wrong Answer

Solution code:
a = list(input())
if len(a) == 1:
    print(a[0])
else:
    c = 0
    for i in range(len(a)):
        b = ""
        d = 0
        for j in range(len(a)):
            if i == j:
                b += str(max(int(a[i]) - 1, 0))
                d += max(int(a[i]) - 1, 0)
            else:
                b += "9"
                d += 9
        if int(b) <= int("".join(a)):
            c = max(c, d)
    print(c)

Submission status: Wrong Answer

Solution code:
a = list(input())
if len(a) == 1:
    print(a)
else:
    c = 0
    for i in range(len(a)):
        b = ""
        d = 0
        for j in range(len(a)):
            if i == j:
                b += str(max(int(a[i]) - 1, 0))
                d += max(int(a[i]) - 1, 0)
            else:
                b += "9"
                d += 9
        if int(b) <= int("".join(a)):
            c = max(c, d)
    print(c)

Submission status: Wrong Answer

Solution code:
a = list(input())
c = 0
for i in range(len(a)):
    b = ""
    d = 0
    for j in range(len(a)):
        if i == j:
            b += str(max(int(a[i]) - 1, 0))
            d += max(int(a[i]) - 1, 0)
        else:
            b += "9"
            d += 9
    if int(b) <= int("".join(a)):
        c = max(c, d)
print(c)

Submission status: Wrong Answer

Solution code:
N = input()
lenN = len(N)
allnine = 1 #オロナインではない
sum = int(N[0])
for i in range(1, lenN):
    if N[i] != 9:
        allnine = 0
        break
    sum += int(N[i])
if allnine == 1: #Nは一桁 or Nは二桁以上で二桁目以降が全て9
    print(str(sum))
else:
    print(str(int(N[0]) + 9*(lenN-1) - 1))

Submission status: Wrong Answer

Solution code:
from sys import stdin
N = (stdin.readline().rstrip())
ans = 0
if len(N) == 1:
    print(N)

elif set(N) == {"9"}:
    ans = len(N) * 9
    print(ans)
    
elif set(N[1:]) == {"9"}:
    ans = (len(N)-1) * 9 + int(N[0])

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

Submission status: Wrong Answer

Solution code:
from sys import stdin
N = (stdin.readline().rstrip())
ans = 0
if set(N) == {"9"}:
    ans = len(N) * 9
    print(ans)
    
elif set(N[1:]) == {"9"}:
    ans = (len(N)-1) * 9 + int(N[0])

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

Submission status: Wrong Answer

Solution code:
# AGC021A - Digit Sum 2
def main():
    N = input().rstrip()
    f = int(N[0])  # first digit
    ans = f + 9 * (len(N) - 1)
    flg = set(N[1:]) == {9}
    print(ans if flg else ans - 1)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if S == 1:
  print(N)
elif N.count("9") == S:
  print(S * 9)
elif N[0] == "9":
  print(S * 9 - 1)
else:
  print(int(N[0]) - 10 + S * 9)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if N<=9:
    ans=N
elif N==int('9'*l):
    ans=9*l
else:
    ans= (N//10**(l-1) - 1) + 9*(l-1)

print(ans)

Submission status: Wrong Answer

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

if N==int('9'*l):
    ans=9*l
else:
    ans= (N//10**(l-1) - 1) + 9*(l-1)

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# 2019/07/16
# AtCoder Grand Contest 021 - A
# For the answer: 
# 1. 桁分割、桁数Count
# 2-1. 上位桁から9以外を探索
# 2-2. 9以外があれば、その1つ上桁を-1
# 2-3. ただし先頭の場合は自身を-1
# 3. (桁数-1)*9 + 上位桁-1

# Input
n = int(input())

# 1.
wn = n
ketalist = list()
while wn > 0:
    ketalist.append(wn % 10)
    wn = int(wn / 10)

# 2. 
ketalist.reverse()
allflg = False
for i in range(len(ketalist)):
    if ketalist[i] < 9:
        if i == 0:
            tgtnum = ketalist[i] - 1
        else:
            tgtnum = ketalist[i-1] - 1
        break
    # Case all 9:
    if i == len(ketalist) - 1:
        allflg = True

# 3.
if len(ketalist) > 1 and allflg == False:
    if ketalist[0] == 1:
        ans = (len(ketalist) - 1) * 9
    else:
        ans = (len(ketalist) - 1) * 9 + tgtnum
elif len(ketalist) > 1 and allflg == True:
    ans = len(ketalist) * 9
else:
    ans = n

# Output
print(ans)

Submission status: Wrong Answer

Solution code:
# 2019/07/16
# AtCoder Grand Contest 021 - A
# For the answer: 
# 1. 桁分割、桁数Count
# 2-1. 上位桁から9以外を探索
# 2-2. 9以外があれば、その1つ上桁を-1
# 2-3. ただし先頭の場合は自身を-1
# 3. (桁数-1)*9 + 上位桁-1

# Input
n = int(input())

# 1.
wn = n
ketalist = list()
while wn > 0:
    ketalist.append(wn % 10)
    wn = int(wn / 10)

# 2. 
ketalist.reverse()
allflg = False
for i in range(len(ketalist)):
    if ketalist[i] < 9:
        if i == 0:
            tgtnum = ketalist[i] - 1
        else:
            tgtnum = ketalist[i-1] - 1
        break
    # Case all 9:
    if i == len(ketalist) - 1:
        allflg = True

# 3.
if len(ketalist) > 1 and allflg == False:
    if ketalist[0] == 1:
        ans = (len(ketalist) - 1) * 9
    else:
        ans = (len(ketalist) - 1) * 9 + tgtnum
else:
    ans = n

# Output
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n=input()
l=len(n)
a,b=0,0
while True:
    b+=int(n[a])
    a+=1
    if a==l:
      break
print(b)