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:
# -*- coding: utf-8 -*-


def count_digit(number):
    return len(number)


def main():
    n = list(str((input())))
    pos = 0

    for index, ni in enumerate(n):
        if ni != '9':
            pos = index
            break

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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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


def count_digit(number):
    return len(number)


def main():
    n = list(str((input())))
    pos = 0

    for index, ni in enumerate(n):
        if ni != '9':
            pos = index
            break

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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N = input()
N = list(map(int, N))
if len(N) == 1:
    print(N[0])
else:
    if not all(map(lambda n: n == 9, N)):
        for i in range(len(N)):
            if i == 0:
                N[i] -= 1
            else:
                N[i] = 9

    print(sum(N))

Submission status: Wrong Answer

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

if not all(map(lambda n: n == 9, N)):
    for i in range(len(N)):
        if i == 0:
            N[i] -= 1
        else:
            N[i] = 9

print(sum(N))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import math
N = int(input())
L = int(math.log10(N))
if N == 10 ** (L + 1) - 1:
    print(9 * (L + 1))
else:
    print(N // 10 ** L + 9 * L - 1)

Submission status: Wrong Answer

Solution code:
N = input()
a = str((int(N[0])-1))+"9"*(len(N)-1)
ans = 0
for ai in a:
    ans += int(ai)
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n=input()
if len(n)!=n.count("9"): 
    print(9*len(n)) 
elif n[0]=="1" and len(n)-1==n.count("9"): 
    print(1+9*(len(n)-1)) 
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)) if len(n)!=n.count("9") else 9*len(n))

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())
if sum(map(int, str(N+1))) == str(N+1)[0]:
    print(sum(map(int, str(N))))
else:
    print(int(str(N)[0])+9*len(str(N))-10)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

print("9"*(ln-1))
print(n[1:-1])

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
L = list(input())

if len(L) == 1 :
    ans = int(L[0])
else :
    if L.count("9") == len(L) :
        ans = 9*len(L)
    elif L[0] == "1" :
        ans = 9*(len(L)-1)
    else :
        ans = (int(L[0])-1) + 9*(len(L)-1)
print(ans)

Submission status: Wrong Answer

Solution code:
N = list(input())
n, i= len(N), 0
while N[i] == "9" and i < n-1:
    i += 1
if n == 1:
    print(int(N[0]))
elif i == n-1 and N[i] == "9":
    print(9 * n)
elif i == 0:
    print(int(N[0])-1 + 9 * (n-1))
else:
    print(9 * (n-1) + 8)

Submission status: Wrong Answer

Solution code:
N = list(input())
n, i= len(N), 0
while N[i] == "9" and i < n-1:
    i += 1
if i == n-1 and N[i] == "9":
    print(9 * n)
elif i == 0:
    print(int(N[0])-1 + 9 * (n-1))
else:
    print(9 * (n-1) + 8)

Submission status: Wrong Answer

Solution code:
N = input()
n = N

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

if N[1:].count('9') == len(N)-1:
    print(sum([int(i) for i in N]))
    exit()

if N[0]!='9' or any(map(lambda x:x!='9',N)):
    n = chr(ord(N[0])-1) + N[1:]
# elif any(map(lambda x:x!='9',N)):
#     n = chr(min([ord(x) for x in N])-1) + N[1:]

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

print(int(N),ans)
if int(N) >= ans:
    print(sum([int(i) for i in str(ans)]))
else:
    print(sum([int(i) for i in str(ans)]) -1)

Submission status: Wrong Answer

Solution code:
N = input()
n = N

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

if N[1:].count('9') == len(N)-1:
    print(N)
    exit()

if N[0]!='9' or any(map(lambda x:x!='9',N)):
    n = chr(ord(N[0])-1) + N[1:]
# elif any(map(lambda x:x!='9',N)):
#     n = chr(min([ord(x) for x in N])-1) + N[1:]

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

# print(int(N),ans)
if int(N) >= ans:
    print(sum([int(i) for i in str(ans)]))
else:
    print(sum([int(i) for i in str(ans)]) -1)

Submission status: Wrong Answer

Solution code:
N = input()
n = N

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

if N[1:].count('9') == len(N)-1:
    print(N)
    exit()

if N[0]!='9' or any(map(lambda x:x!='9',N)):
    n = chr(ord(N[0])-1) + N[1:]
# elif any(map(lambda x:x!='9',N)):
#     n = chr(min([ord(x) for x in N])-1) + N[1:]

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

print(int(N),ans)
if int(N) >= ans:
    print(sum([int(i) for i in str(ans)]))
else:
    print(sum([int(i) for i in str(ans)]) -1)

Submission status: Wrong Answer

Solution code:
N = input()
n = N

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

if N[1:].count('9') == len(N)-1:
    print(N)
    exit()

if N[0] != '9':
    n = chr(ord(N[0])-1) + N[1:]

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

# print(int(N),ans)
if int(N) >= ans:
    print(sum([int(i) for i in str(ans)]))
else:
    print(sum([int(i) for i in str(ans)]) -1)

Submission status: Wrong Answer

Solution code:
N = input()
n = N
if N[0] != '9':
    n = chr(ord(N[0])-1) + N[1:]

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

# print(int(N),ans)
if int(N) >= ans:
    print(sum([int(i) for i in str(ans)]))
else:
    print(sum([int(i) for i in str(ans)]) -1)

Submission status: Wrong Answer

Solution code:
N = input()

if len(N) == N.count("9"):
    print(9*len(N))
    exit()

if len(N) -1 == N.count("9"):
    sd = 0
    N = int(N)
    while N > 0:
        sd += N % 10
        N //= 10
    print(sd)
    exit()
if N[0] == 1:
    print(9 * (len(N) - 1) + 8)
else:
    print(int(N[0])-1 + 9 * (len(N) - 1))

Submission status: Wrong Answer

Solution code:
N = input()

if len(N) == N.count("9"):
    print(9*len(N))
    exit()

if len(N) -1 == N.count("9"):
    print(9*(len(N)-1) + 8)
    exit()

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

Submission status: Wrong Answer

Solution code:
s = input()
a = int(s[0])
k = len(s)
n = int(s)

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

if (n + 1) % 10**k == 0:
    print(a + 9*(k-1))
    exit()
else:
    print(a - 1 + 9*(k-1))

Submission status: Wrong Answer

Solution code:
s = input()
a = int(s[0])
k = len(s)
n = int(s)
if (n + 1) % 10**k == 0:
    print(a + 9*(k-1))
    exit()
else:
    print(a - 1 + 9*(k-1))

Submission status: Wrong Answer

Solution code:
N = raw_input()

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

flag=True
for i in range(1,len(N)):
    if int(N[i])!=9:
        flag=False

if flag:
    print a+9*(b-1)
else:
    print a+9*(b-1)+1

Submission status: Wrong Answer

Solution code:
S = input()
flag = 0
for dig in S[1:]:
    if dig != 9:
        flag = 1

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

if flag == 1:
    ans -= 1

print(ans)

Submission status: Wrong Answer

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

if listN[0] ==1:
    print(9*(len(listN)-1))
else:
    index=0
    while index < len(listN):
        if index ==0 and listN[index]!=9:
            print(listN[index]-1+(9*(len(listN)-1)))
            break
        else:
            if listN[index]!=9:
                listN[index-1]-=1
                index2 = index
                while index2 < len(listN):
                    listN[index2]=9
                    index2+=1
                index3=0
                count=0
                while index3 < len(listN):
                    count+=listN[index3]
                    index3+=1
                print(count)
                break
            else:
                index+=1
    if index == len(listN):
        print(9*len(listN))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = input()

work = 9
for i in range(len(N)):
  if N[i] != "9":
    work = int(N[i])
    break
    
print(work + 9 * (len(N)-1))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def a_digit_sum_2(N):
    n_lead = int(str(N)[0])  # Nの先頭の数字
    n_later = str(N)[1:]  # Nの先頭以外の数字列
    n_length = len(str(N))  # Nの桁数
    if set(n_later) == set('9'):  # 先頭以外が9のみから構成された数だった
        ans = n_lead + 9 * (n_length - 1)  # c9...9の各桁をそのまま足す
    else:
        ans = (n_lead - 1) + 9 * (n_length - 1)  # 先頭は1減らさなければならない
    return ans

N = int(input())
print(a_digit_sum_2(N))

Submission status: Wrong Answer

Solution code:
def a_digit_sum_2(N):
    n_lead = int(str(N)[0])  # Nの先頭の数字
    n_length = len(str(N))  # Nの桁数
    ans = (n_lead - 1) + 9 * (n_length - 1)
    return ans

N = int(input())
print(a_digit_sum_2(N))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = list(input())

a = [i for i in n if i == '9']
if len(a) == len(n):
    print(9*len(n))
else:
    k = [int(n[0])-1]
    for _ in range(len(n)-1):
        k.append(9)
    print(sum(k))

Submission status: Wrong Answer

Solution code:
def digit_sum(num:int, base:int) -> int:
    if num < base:
        return num
    else:
        return digit_sum(int(num/base), base) + (num % base)

def judge(n: int) -> int:
    if n < 10:
        return n
    
    else:
        # 先頭の位が1のとき、先頭の1を落として全部9にしたものが候補
        if str(n)[0:1] == '1':
            cand = n % 10**(len(str(n)) - 1)
            
            cand_str = "9"*len(str(cand))
            cand = int(cand_str)
            
            
            return max(digit_sum(n,10), digit_sum(cand,10))
        
        # そうでないとき、先頭の位を1小さくする
        else:
            top = n // 10**(len(str(n)) - 1)
            cand_str = str(top-1) + "9" * (len(str(n))-1)
            cand = int(cand_str)
            
            return max(digit_sum(n,10), digit_sum(cand,10))

n = int(input())

ans = judge(n)
print(ans)

Submission status: Wrong Answer

Solution code:
s = input()

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = input()
p = N[0]

ans = 9*(len(N)-1) + (int(p)-1)
if int(N)>ans:
    ans = int(N)
print(ans)

Submission status: Wrong Answer

Solution code:
N = input()
p = N[0]

ans = 9*(len(N)-1) + (int(p)-1)
print(ans)

Submission status: Wrong Answer

Solution code:
n = list(input())
if set(n) == {"9"}:
	print(9 * len(n))
elif set(n[1:]) == {"9"}:
	print(9 * (len(n) - 1) + int(n[0]))
elif 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 = list(input())
if set(n) == {"9"}:
	print(9 * len(n))
elif set(n[1:]) == {"0"}:
	print(9 * (len(n) - 1))
elif set(n[1:]) == {"9"}:
	print(9 * (len(n) - 1) + int(n[0]))
else:
	print(9 * (len(n) - 1) + int(n[0]) - 1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
#digit sum
import sys
import numpy as np

N = int(input())

if N == 0:
    print(0)
else:
    a = int(np.log10(N))
    b = int(str(N)[0])
    string = str(N)
    poss = 0
    count = 0
    max_count = len(string) - 1
    for s in string[1:]:
        if s is '9': 
            count += 1
    if count is max_count:
        print(9 * a + b)
    else:
        print(9 * a + b - 1)

Submission status: Wrong Answer

Solution code:
#digit sum
import sys
import numpy as np

N = int(input())
a = int(np.log10(N))
b = int(str(N)[0])
string = str(N)
poss = 0
count = 0
max_count = len(string) - 1
for s in string[1:]:
    if s is '9': 
        count += 1
if count is max_count:
    print(9 * a + b)
else:
    print(9 * a + b - 1)

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np

N = int(input())
a = int(np.log10(N))
b = int(str(N)[0])
string = str(N)
poss = 0
for s in string[1:]:
    if s is 9: 
        poss = 1
if poss is 0:
    print(9 * a + b - 1)
else:
    print(9 * a + b)

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np

N = float(input())

a = int(np.log10(N))
b = int(str(N)[0]) - 1
print(9 * a + b)

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np

N = int(input())

a = int(np.log10(N))
b = int(str(N)[0]) - 1
print(9 * a + b)

Submission status: Wrong Answer

Solution code:
N = input()
a = [int(c) for c in N]
naga = len(a)
for i in range(naga):
  if a[i] < 9:
    a[i] -= 1
    for j in range(i+1,naga):
      a[j] = 9
    break
if a[naga-1] != 9:
  a[naga-2] -= 1
  a[naga-1] = 9
print(sum(a))

Submission status: Wrong Answer

Solution code:
N = input()
a = [int(c) for c in N]
naga = len(a)
for i in range(naga):
  if a[i] < 9:
    a[i] -= 1
    for j in range(i+1,naga):
      a[j] = 9
    break
print(sum(a))

Submission status: Wrong Answer

Solution code:
N=list(input())
mark=0
for i in range (len(N)):
    if N[i]!='9':
        mark=i
        break
N[mark]=str(int(N[mark])-1)
for j in range (mark+1,len(N)):
    N[j]='9'
count=0
for k in range (len(N)):
    count+=int(N[k])
print(count)

Submission status: Wrong Answer

Solution code:
from math import log10

N = int(input())
d = int(log10(N))+1
ans = (d-1)*9 + max(0, N//10**(d-1)-1)
print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
def inpl(): return map(int, input().split())
N = input()
ans = 0

while N:
    if N[0] == "9":
        ans += 9
        N = N[1:]
    else:
        break

if all([n=="9" for n in list(N[1:])]) and len(N) > 2:
    print(ans + int(N[0]) + len(N[1:])*9)
elif ans == 0:
    if len(N) == 1:
        ans = int(N)
    else:
        ans += int(N[0]) - 1 + len(N[1:])*9
    print(ans)
elif len(N)==0:
    print(ans)
else:
    print(ans - 1 + len(N)*9)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
def inpl(): return map(int, input().split())
N = input()
ans = 0

while N:
    if N[0] == "9":
        ans += 9
        N = N[1:]
    else:
        break

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

Submission status: Wrong Answer

Solution code:
N = input()

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

Submission status: Wrong Answer

Solution code:
N=list(input())
mark=0
for i in range (len(N)):
    if N[i]!='9':
        mark=i
        print (mark)
        break
if mark==0:
    answer=list((''.join(['9']*(len(N)-1))))
else:
    answer=list((''.join(N[:mark-1]+[str(int(N[mark-1])-1)]+['9']*len(N[mark:]))))
    
answer_sum=0
for j in range (len(answer)):
    answer_sum+=int(answer[j])
print(answer_sum)

Submission status: Wrong Answer

Solution code:
import sys

n = list(input())
order10 = len(n)

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

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

Submission status: Wrong Answer

Solution code:
a =input()
b = [x for x in list(a)]
if len(a) == 1:
    print(int(a))
elif len(a) == a.count('9'):
    print(len(a)*9)
elif len(a) == a.count('9') + 1 and a.count('8') == 1:
    print((len(a)-1)*9+8)
else:
    print(int(b[0])-1+(len(a)-1)*9)

Submission status: Wrong Answer

Solution code:
a =input()
b = [x for x in list(a)]
if len(a) == a.count('9'):
    print(len(a)*9)
elif len(a) == a.count('9') + 1 and a.count('8') == 1:
    print((len(a)-1)*9+8)
else:
    print(int(b[0])-1+(len(a)-1)*9)

Submission status: Wrong Answer

Solution code:
a =input()
b = [x for x in list(a)]
if len(a) == a.count('9'):
    print(len(a)*9)
elif len(a) == a.count('9') + a.count('8'):
    print((len(a)-1)*9+8)
else:
    print(int(b[0])-1+(len(a)-1)*9)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N=input()
num=0
for i in range(len(N)-2):
    num+=9

x=max(int(N[0])+int(N[len(N)-1]),(int(N[0])-1)+9)
print(num+x)

Submission status: Wrong Answer

Solution code:
s = input()

res = 0

i = True
for c in s:
    if c != '9':
        i = False
        break

if len(s) == 1:
    res = int(s)
elif i:
    res += int(s[0])
else:
    res += int(s[0]) - 1

res += (len(s) - 1) * 9
print(res)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
n=input()
if len(n)==1:
    print(n)
    sys.exit()
flag=True
for i in range(1,len(n)):
    if n[i]!=9:
        flag=False
tot=0
for i in range(len(n)):
    tot+=int(n[i])
if flag:
    print(tot)
else:
    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)-9+int(N[0])-1)

Submission status: Wrong Answer

Solution code:
s = input()

res = 0

i = True
for c in s:
    if c != '9':
        i = False
        break

if i:
    res += int(s[0])
else:
    res += int(s[0]) - 1

res += (len(s) - 1) * 9
print(res)

Submission status: Wrong Answer

Solution code:
a = [1,2,3]
a = set(a)

Submission status: Wrong Answer

Solution code:
n_str = str(input())
n = int(n_str)

digits = len(n_str)
if len(n_str) > 1:
    start = pow(10, digits)
    max_number = start - 1
else:
    start = 1
    max_number = 0

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

print(max_number)

Submission status: Wrong Answer

Solution code:
s = input()

sum = 0
for i in range( len(s) -1 ):
  sum += int(s[i])

print(sum)

Submission status: Wrong Answer

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

ans = 0
num = 0
for i in range(L):
    d = N[i]
    if d == '0':
        continue
    num += int(d)
    ans = max(num - 1 + 9*(L-i-1), ans)
print(ans)

Submission status: Wrong Answer

Solution code:
import math

N = int(input())
digit_num = math.ceil(math.log10(N))
msd =int(N/(10**(digit_num-1)))

if (N+1)%10 == 0:
    print(msd + 9*(digit_num-1))
else:
    print(msd + 9*(digit_num-1) - 1)

Submission status: Wrong Answer

Solution code:
import math

N = int(input())
digit_num = math.ceil(math.log10(N))
msd =int(N/(10**(digit_num-1)))
if N%10 == 0:
    print(msd + 9*(digit_num-1))
else:
    print(msd + 9*(digit_num-1) - 1)

Submission status: Wrong Answer

Solution code:
import math

N = int(input())
digit_num = math.ceil(math.log10(N))
msd =int(N/(10**(digit_num-1)))
print(msd)
if N%10 == 0:
    print(msd + 9*(digit_num-1))
else:
    print(msd + 9*(digit_num-1) - 1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = 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 = int(input())
M=0
temp=0
l = [int(x) for x in list(str(N))]

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

Submission status: Wrong Answer

Solution code:
N = int(input())
M=0
temp=0
l = [int(x) for x in list(str(N))]

#print(l)

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
a = input()
b = []
l = len(a) 
for i in range(1, l):
    b.append(a[i])
p = str(9) * (l-1)
if b == p:
    ans = 9 * (l-1) + int(a[0])
else:
    ans = 9 * (l-1) + int(a[0]) - 1
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import math

def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline

def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [N] = [long(input_raw[0])]

    s = 0
##    if N > (10L**9L):
##        digit =long(math.log(N/(10**9), 10)+1)
##        digit += 9
##    else:
##        digit =long(math.log(N, 10)+1)

    tmp = N
    digit = 0
    while 1:
        tmp /= 10
        digit += 1
        if tmp == 0:
            break;

    if long (10L ** (digit) - 1L)   == N:
        print digit * 9
        exit()

    for i in range(digit , 0, -1):
        h = N / (10 ** (i - 1))
        s += 9 if i != digit else h - 1
        N -= h * (10 ** (i - 1))

    print s

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
import math

def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline

def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [N] = [long(input_raw[0])]

    s = 0
    if N > (10L**9L):
        digit =long(math.log(N/(10**9), 10)+1)
        digit += 9
    else:
        digit =long(math.log(N, 10)+1)


    if long (10L ** (digit) - 1L)   == N:
        print digit * 9
        exit()

    for i in range(digit , 0, -1):
        h = N / (10 ** (i - 1))
        s += 9 if i != digit else h - 1
        N -= h * (10 ** (i - 1))

    print s

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:

import sys
import math

def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline

def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [N] = [long(input_raw[0])]

    s = 0
    digit =long(math.log(N, 10)+1)

    if long (10L ** (digit) - 1L)   == N:
        print digit * 9
        raise RuntimeError
        exit()

    for i in range(digit , 0, -1):
        h = N / (10 ** (i - 1))
        s += 9 if i != digit else h - 1
        N -= h * (10 ** (i - 1))

    print s

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
import math

def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline

def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [N] = [long(input_raw[0])]

    s = 0
    digit =long(math.log(N, 10)+1)

    if long (10L ** (digit) - 1L)   == N:
        print digit * 9
        exit()

    for i in range(digit , 0, -1):
        h = N / (10 ** (i - 1))
        s += 9 if i != digit else h - 1
        N -= h * (10 ** (i - 1))

    print s

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:

import sys
import math

def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline

def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [N] = [long(input_raw[0])]

    s = 0
    digit =int(math.log10(N)+1)

    if 10L ** (digit) - 1L   == N:
        print digit * 9
        exit()

    for i in range(digit , 0, -1):
        h = N / (10 ** (i - 1))
        s += 9 if i != digit else h - 1
        N -= h * (10 ** (i - 1))

    print s

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())

def digit_sum_simple(n):
    
    max_sum = -1

    for i in range(10**(len(str(n))-1)-1, n+1):
        
        i_parts = []
        for i_part in str(i):
            i_parts.append(int(i_part))

        this_sum = sum(i_parts)
        
        if max_sum < this_sum:
            max_sum = this_sum
            max_sum_i = i

    return max_sum

def digit_sum_temp(n):
    
    max_sum, one_num_max_sum = divmod(n+1, 10)
    if 0 <= n < 9:
        return one_num_max_sum-1
    else:
        return max_sum+8

print(digit_sum_temp(n))

Submission status: Wrong Answer

Solution code:

patterns = ["temp"]

n = int(input())

if "simple" in patterns:
    
    max_sum = -1

    for i in range(10**(len(str(n))-1)-1, n+1):
        
        i_parts = []
        for i_part in str(i):
            i_parts.append(int(i_part))

        this_sum = sum(i_parts)
        
        if max_sum < this_sum:
            max_sum = this_sum
            max_sum_i = i

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

if "temp" in patterns:
    max_sum_i = int(str(n)[0]) * 10**(len(str(n))-1) 
    if not 0 <= n <= 9:
        max_sum_i -= 1
    max_sum = sum([int(i) for i in str(max_sum_i)])

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

Submission status: Wrong Answer

Solution code:

patterns = ["temp"]

n = int(input())

if "simple" in patterns:
    
    max_sum = -1

    for i in range(10**(len(str(n))-1)-1, n+1):
        
        i_parts = []
        for i_part in str(i):
            i_parts.append(int(i_part))

        this_sum = sum(i_parts)
        
        if max_sum < this_sum:
            max_sum = this_sum
            max_sum_i = i

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

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

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

Submission status: Wrong Answer

Solution code:

pattern = "simple"

n = int(input())

if pattern == "simple":
    
    max_sum = -1

    for i in range(10**(len(str(n))-1)-1, n+1):
        
        i_parts = []
        for i_part in str(i):
            i_parts.append(int(i_part))

        this_sum = sum(i_parts)
        
        if max_sum < this_sum:
            max_sum = this_sum
            max_sum_i = i

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

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

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

Submission status: Wrong Answer

Solution code:
n = int(input())
max_sum_i = int(str(n)[0]) * 10**(len(str(n))-1) -1
max_sum = sum([int(i) for i in str(max_sum_i)])

print(max_sum)

Submission status: Wrong Answer

Solution code:
n = int(input())

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

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

Submission status: Wrong Answer

Solution code:
n = int(input())
keta = len(str(n))
res = [int(x) for x in list(str(n))]
print(res[0]-1+(keta-1)*9)

Submission status: Wrong Answer

Solution code:
N = [int(x) for x in input()]
print(max(sum(N), N[0]-1+9*len(N)))

Submission status: Wrong Answer

Solution code:
n = int(input())
 
for i in range(10):
	if n >= int(str(i) + '9'*(len(str(n))-1)):
		print(i + 9*(len(str(n))-1))
		break