Buggy python submission (verdict: Runtime Error):
# 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()
for i in range(len(ketalist)):
    if ketalist[i] < 9:
        if i == 0:
            tgtnum = ketalist[i] - 1
        else:
            tgtnum = ketalist[i-1] - 1
        break

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

# Output
print(ans)


Fixed submission:
# 2019/07/17
# 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()
defans = 0
while wn > 0:
    remn = wn % 10
    defans = defans + remn
    ketalist.append(remn)
    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(max(ans, defans))

Buggy python submission (verdict: Runtime Error):
# 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()
for i in range(len(ketalist)):
    if ketalist[i] < 9:
        if i == 0:
            tgtnum = ketalist[i] - 1
        else:
            tgtnum = ketalist[i-1] - 1
        break

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

# Output
print(ans)


Fixed submission:
# 2019/07/17
# 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()
defans = 0
while wn > 0:
    remn = wn % 10
    defans = defans + remn
    ketalist.append(remn)
    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(max(ans, defans))