We need to find the maximum sum of digits among positive integers ≤ N. N up to 1e16. So we need to find the integer x in [1, N] that maximizes digit sum s(x). Classic problem: The maximum digit sum is often achieved by a number that is just below a power of 10, like 999...9, but we need to respect the upper bound N. So we need to consider numbers like: the number with all digits 9, but that might exceed N. So we need to find the maximum digit sum within the bound.
Standard approach: For each position, we try to decrease one digit by 1 and set all following digits to 9, ensuring the resulting number is ≤ N. Compute sum of digits. Also consider N itself. So we can loop over digits. Since N up to 1e16 (~16 digits), that's fine.
Let's formalize: Represent N as a string of digits d_k ... d_0 (most significant to least). For i from most significant to least, consider the number formed by taking the first i-1 digits as same as N, then at position i (0-index from most significant) reduce that digit by 1 (if after reduction it remains non-negative and the prefix is >0?), then set all following digits to 9. But careful: For the most significant digit, reducing it to 0? The number must be positive integer. So we cannot have leading zeros. If we reduce the most significant digit to 0, the number would have fewer digits. That's allowed as long as the number is positive; e.g., N=1000, we might consider 999 which is 3-digit number. So we need to handle that case: The candidate can have fewer digits than N. So we should also consider the number consisting entirely of 9's with one fewer digit than N (if N is not all 9's). But that's covered by considering the scenario where we decrease the most significant digit: if d[0] is >0, we can reduce it by 1; if after reduction it becomes 0, then we effectively have a number with length len-1. That's okay: we then have all following digits 9 (len-1 digits). That's exactly the all-9 number of length len-1. But be careful: If we reduce the most significant digit to 0, the number's leading digit becomes the next digit? Actually we cannot have a leading zero. So the number would be represented by the digits after the leading zero, but if the reduced digit becomes 0 and all following are 9, the number would have a leading zero and then 9's, which is effectively just the 9's. So we need to treat it as number composed of the remaining digits (all 9). For sum of digits, we should count only the non-leading zeros: i.e., if we set a digit to 0 at the front, it's not part of the number representation; the sum should only include digits after dropping leading zeros. So we can just compute the candidate number by constructing a string and then parse? But simpler: For each index, we produce a candidate number that is guaranteed ≤ N, compute its digit sum. We need to handle the leading zero case: When we decrease the first digit from, say, 1 to 0, the candidate becomes a number with fewer digits. The sum of digits is just 9 * (len-1). That's fine.
Also consider candidate = N itself.
Thus algorithm:
Convert N to string S, length L.
Initialize ans = sum of digits of N.
For each position i from 0 to L-1 (i indexing the digits from most significant 0):
If S[i] == '0': we cannot decrease this digit because would become negative. But we could still consider decreasing a more significant digit? Actually we are iterating over each position as candidate for reduction. For position i, we try to reduce digit at i by 1, but only if after reduction it's >=0. But if S[i]=='0', we skip because cannot reduce to -1. However, note that we could still reduce a more significant digit, that's covered when i is earlier. So we only consider i where digit >0. So for each i where int(S[i]) > 0:
Build candidate digits: copy digits from S up to i-1; at i, put (int(S[i]) - 1); then for positions > i, put 9.
If the candidate starts with a 0 (i.e., i==0 and the reduced digit becomes 0), then we effectively drop that leading zero: candidate number is the subsequent digits (all 9) of length L-1. So we compute sum as (for positions after i all 9) plus any previous digits? Actually if i==0 and we set digit to 0, the number would have no leading zero; the first non-zero digit would be at i+1. So sum = sum of digits from positions after i (all 9). That's 9*(L-1). But if i>0 and we reduce some digit to something maybe 0, leading digit remains non-zero, so we include that 0 in sum (0 doesn't affect sum). So we can compute sum directly: sum of digits of prefix before i (including them) but with digit at i reduced by 1, and then add 9*(L-i-1). That's the sum of digits of candidate, but careful: if i==0 and reduced digit becomes 0, the sum of digits for the number should not include that leading zero; but if we compute sum including that 0, it would be 0 + 9*(L-1), same as excluding it. So it's fine because 0 doesn't change the sum. So we can just compute:
pre_sum = sum_{j< i} digit_j + (digit_i - 1)
candidate_sum = pre_sum + 9*(L - i - 1)
However, we must ensure candidate number is ≤ N. The construction ensures candidate ≤ N because we decreased a digit and set rest to max. But what about the scenario where we reduce a digit but the prefix (up to i) becomes less than N's prefix? Actually we are decreasing digit at i by 1 and making the rest 9, so candidate is less than N because at position i it's smaller, regardless of later digits. So it's valid as long as we don't break the condition of positivity. Need to consider if the candidate could be 0? Example: N = 1. L=1. For i=0, digit=1 >0, reduce to 0, then rest none, candidate = 0. But we need positive integer. So we must exclude candidate = 0. So for i=0 and digit_i == 1, the candidate becomes 0. That's not allowed. So we need to skip if candidate becomes 0. More generally, if after reduction the entire number is 0, we skip. That's when L=1 and digit=1? Actually also if N=10? For i=0, digit=1, reduce to 0, then all following digits 9 (since L>1) => candidate = 09...9 which is number with L-1 digits all 9, which is positive. So that's fine. Only when L=1 and digit becomes 0, the candidate is 0. So we can just skip if candidate sum == 0? But candidate sum would be 0, which is lower anyway. But we should not consider 0 because it's not positive. So we can skip.
Edge Cases: N=10^k? Eg., N=1000. L=4. S="1000". For i=0: digit=1>0, pre_sum = (1-1)=0, candidate_sum=0+9*3=27 => candidate = 999 (digit sum 27). i=1: digit=0 skip. i=2: digit=0 skip. i=3: digit=0 skip. Also N sum = 1. So ans=27. correct.
N=9995: L=4. S="9995". For i=0: digit=9>0, pre_sum = (9-1)=8, candidate_sum=8+93=8+27=35 => candidate = 8999? Actually 8 then 9,9,9 => 8999 sum=8+9+9+9=35. i=1: digit=9>0, pre_sum = first digit 9 + (9-1)=9+8=17, candidate_sum = 17+92=17+18=35 => candidate = 9899? Actually digits: 9,8,9,9 => sum=9+8+9+9=35. i=2: digit=9>0, pre_sum = first two digits 9+9=18 + (9-1)=17? Wait careful: sum_{j<2}=digit0+digit1=9+9=18, plus (digit2-1)=8 => pre_sum=26, candidate_sum = 26+91=35 => candidate = 9989? Actually digits: 9,9,8,9 => sum=9+9+8+9=35. i=3: digit=5>0, pre_sum = sum first three=9+9+9=27 + (5-1)=4 => pre_sum=31, candidate_sum = 31+90=31 => candidate = 9994? Actually digits: 9,9,9,4 => sum=31. N sum = 9+9+9+5=32. So ans=35. correct.
Thus the algorithm is straightforward.
But careful: There might be a candidate with more digits? Not greater than N. The maximum sum of digits for numbers less than or equal to N is often achieved by a number of the form (some prefix decreased) then all 9s. This is standard and should work. But we need to confirm for all N. Let's reason: Suppose we have any number x ≤ N. Consider the most significant digit where x differs from N; at that position, x's digit is less than N's digit. Then we can increase the later digits to 9 to possibly increase digit sum without exceeding N? Actually if x already differs at that position with a smaller digit, then any digits after can be at most 9, so the maximum sum for numbers with that prefix is achieved by setting all following digits to 9. So any optimal number must have that property: after the first differing digit from N (from most significant), the digits are all 9. Additionally, the first differing digit must be less than N's digit; the prefix before that is same as N. So the optimal candidate is either N itself or one of these "prefix decreased + 9s". Also could be the case where we take a number with fewer digits (all 9's) which is covered by decreasing the most significant digit possibly to 0. So yes.
Therefore algorithm O(L) with L ≤ 17.
Implementation steps:
Read N as string.
Compute L = len(N).
Compute sumDigits(N) = sum(int(c) for c in N) as initial ans.
For i in range(L): # index from 0 to L-1
digit = int(N[i])
if digit == 0: continue
candidate_sum = sum(int(N[j]) for j in range(i)) + (digit - 1) + 9 * (L - i - 1)
ans = max(ans, candidate_sum)
Print ans.
But we need to be careful about leading zero case for i=0 where digit-1 = 0, candidate_sum = 0 + 9*(L-1). That's fine. But if L=1 and digit-1=0 and L-i-1 = 0, candidate_sum=0. That's not positive; but we can either skip if candidate_sum == 0? Actually ans will be at least digit sum of N which is >=1 for N>=1. So it's fine to include it; ans won't be set to 0. But it's okay to compute and compare; but if N=1, we compute candidate_sum = 0, ans initially = 1, so ans remains 1. That's correct because max digit sum for numbers ≤ 1 is 1 (1 itself). However, we should also consider number 0? But positive integers only, so 0 excluded. So ans=1 is correct. For N=0? but constraints N≥1, so ignore.
Edge: N=10^16 = 10000000000000000? Actually 10^16 is 1 followed by 16 zeros, length 17. N up to 10^16 inclusive, so max length 17. That's fine.
N=3141592653589793: we can compute quickly? Not manually.
Thus solution done.
But wait: there is also possibility of numbers with more digits than N? No.
Thus answer straightforward.
Double-check for N like 19? N=19. L=2. N sum=10. i=0: digit=1>0: candidate_sum = (1-1)+91=0+9=9. i=1: digit=9>0: candidate_sum = sum first digit 1 + (9-1)=1+8=9, plus 90=9. ans=10. So answer 10 (number 19). But is there a number with sum 10? 19 sum=10, 18 sum=9, 17 sum=8,... 10 sum=1,... 9 sum=9. So max=10. Good.
N=199: L=3. N sum=1+9+9=19. i=0: digit=1>0: candidate_sum =0+92=18. i=1: digit=9>0: candidate_sum = first digit1=1 + (9-1)=9? Actually 1 + 8 =9, plus 91=9 => total 18. i=2: digit=9>0: candidate_sum = sum first two:1+9=10 + (9-1)=8 => 18, plus 0=18. ans=19. So N itself is best.
N=9: L=1. N sum=9. i=0: digit=9>0: candidate_sum = (9-1)+0 =8, ans=9. So correct.
N=1: i=0: digit=1: candidate_sum=0. ans=1.
Now complexity fine.
Implementation in Python:
def solve():
import sys
N = sys.stdin.readline().strip()
L = len(N)
ans = sum(int(ch) for ch in N)
for i in range(L):
d = int(N[i])
if d == 0:
continue
# compute prefix sum up to i-1
prefix_sum = sum(int(N[j]) for j in range(i))
cand = prefix_sum + (d - 1) + 9 * (L - i - 1)
if cand > ans:
ans = cand
print(ans)
But careful: When i=0, prefix_sum sum over range(0) is 0. So okay.
But we can compute prefix sum incrementally to avoid O(L^2) though L is tiny; but fine.
Optimization: Precompute prefix sums array: pre[i] = sum of digits from 0 to i-1? Could do.
But given L<=17, O(L^2) is negligible.
Thus solution done.
But I want to double-check for N where some digits are zero: Eg., N=101. L=3. N sum=2. i=0: d=1>0: prefix sum 0 + (1-1)+92=0+0+18=18. i=1: d=0 skip. i=2: d=1>0: prefix sum = sum digits 0..1 = 1+0=1; cand=1+(1-1)+90=1+0=1. ans=18. Indeed candidate=099? Actually i=0 reduced to 0, then 9,9 => 99 sum=18. Good.
N=110: L=3. N sum=2. i=0: d=1>0: cand=0+0+18=18 -> 99. i=1: d=1>0: prefix sum = first digit 1; cand =1+(1-1)+9*1=1+0+9=10 -> candidate: first digit 1, second digit 0, then 9 => 109? Wait: digits: first same (1), second decreased to 0, then 9 => 109 sum=10. i=2: d=0 skip. ans=18. So correct.
Now ensure we are not missing the case where reducing a digit to less than 0? We skip if d==0.
Also note: If we reduce the digit and the prefix becomes all zeros? That only matters for positivity; e.g., N=100...0 with leading digit 1, i=0 reduces to 0, candidate number becomes 999...9 (len-1) which is positive. So fine.
Thus answer.
But wait, is there ever a case where the optimal number has a digit sum greater than any of these candidates? For example, consider N=1099. Let's compute. N sum=1+0+9+9=19. i=0: d=1>0: cand=0+0+93=27 -> 999? Actually candidate: 0999 -> 999 sum=27. i=1: d=0 skip. i=2: d=9>0: prefix sum = digits[0..1] =1+0=1; cand =1+(9-1)+91=1+8+9=18 -> candidate: 1,0,8,9? Actually first two digits same:1,0; third digit 8; last 9 => 1089 sum=1+0+8+9=18. i=3: d=9>0: prefix sum = first three digits =1+0+9=10; cand=10+(9-1)+0=18 -> candidate: 1,0,9,8 => 1098 sum=18. ans=27. Good.
Another thought: Could the optimal number be something like 999...8? But that would be captured by decreasing the last digit. The candidate with decreasing the last digit gives 1098? Actually for N=1099, decreasing last digit yields 1098, sum 18. So 999 is better.
Thus algorithm correct.
Now think about N=10^16 exactly. N = "10000000000000000" length 17. N sum = 1. Candidates: i=0: d=1>0: cand =0+0+9*16=144 => candidate = 999...9 (16 digits) sum=144. That is max. ans=144.
So output for N=10^16 would be 144. That's plausible.
Now check N= 9999999999999999 (16 9's). L=16. N sum=144. i=0: d=9>0: cand = 0+(9-1)+915 =8+135=143. i=1: cand = 9+(9-1)+914 =9+8+126=143. ... all later candidates have sum 143. So ans=144. So maximum is N itself.
Now check N= 10000000000000001? That's 1 followed by 14 zeros then 1? Actually length 17: "10000000000000001". N sum=2. i=0: cand = (1-1)+916=144. i=1..15 digits 0 skip. i=16: last digit=1>0: prefix sum = sum of first 16 digits = 1 + 015? Actually first 16 digits: 1 and then 15 zeros? Actually string: index0='1', indices1-15? Let's compute length: "10000000000000001" has 17 digits: positions 0 to 16. The zeros count: after first digit, 15 zeros? Actually 10^16 is 1 followed by 16 zeros => 17 digits. So 10000000000000001 is 1 then 15 zeros then 1? Because 10^16 + 1 = 10000000000000001 (since 10^16 is 1 followed by 16 zeros; adding 1 gives last digit 1, so there are 15 zeros in between? Let's count: 10^16 = 10000000000000000 (17 digits: 1 and 16 zeros). Then +1 => 10000000000000001: so digits: 1, then 15 zeros, then 1? Wait: 10^16 has 1 then 16 zeros: positions: 0:'1', 1-16:'0's. Adding 1 gives last digit becomes 1, carry? Actually 100...000 + 1 = 100...001, with zeros in between. So the number of zeros between the 1's is 15? Because total digits 17, index0=1, index16 (last) becomes 1, indices 1-15 are zeros. So there are 15 zeros. So sum prefix up to index15 (i=16? careful) We'll compute. But anyway candidate from i=0 gives 144. So max 144.
One can also compute using integer arithmetic but string approach is simpler.
But maybe we need to consider candidate numbers that are all 9's with the same number of digits? That is covered by decreasing some digit to 9? Actually not exactly: If N itself is something like 523, the all-9 number 999 is > N, so not allowed. But we consider decreasing the most significant digit from 5 to 4? That yields 499? Actually 5-1=4 then 9,9 => 499 sum=22, but what about 499 vs 489? Actually 499 sum=22, 489 sum=21, 599 not allowed. But maximum could be 499? But maybe 498? Not better. But also could be 399? That's lower. So the maximum sum from such class is when we reduce the digit at the first position where we can reduce without making the number negative, and set rest to 9. That's our candidate.
But is it always optimal to set all later digits to 9? Suppose we have a prefix same up to i-1, then at i we use a digit less than N[i] (by at least 1). Then to maximize digit sum, we want later digits as high as possible, i.e., 9. So yes.
But what about the case where we don't change any digit (N itself)? That's considered.
Thus algorithm yields max.
Potential pitfalls: The candidate constructed might be 0 if we reduce the only non-zero digit to 0 and there are no other digits (L=1). Skip or handle.
Another nuance: Positive integer not greater than N: does it include N itself? Yes. So we consider N.
Now we can code.
But wait: The problem statement says "positive integer", so N is positive. So okay.
Thus answer.
I'll produce final solution in Python.
But I'd like to confirm with the third sample: N=3141592653589793. Let's compute approximate sum: Let's compute quickly? Not needed. But we can trust.
Thus final answer.
But I should also consider if there is any scenario where the maximum digit sum is achieved by a number that is not of the form "prefix-1 then 9s" or N itself. For example, suppose N= 1299. N sum=1+2+9+9=21. i=0: d=1>0: cand = 0+0+93=27 (=> 999 sum=27). i=1: d=2>0: prefix sum = first digit 1; cand =1+(2-1)+92=1+1+18=20 => candidate = 1,1,9,9 => 1199 sum=20. i=2: d=9>0: prefix sum=1+2=3; cand=3+(9-1)+9*1=3+8+9=20 => candidate = 1,2,8,9 => 1289 sum=20. i=3: d=9>0: prefix sum=1+2+9=12; cand=12+(9-1)+0=20 => 1298 sum=20. ans=27. So 999 is best. So indeed that is covered.
Now test N= 1999: N sum=28. i=0: 1->0 => 999 sum=27. i=1: 9->8 => 1+8+9+9=27? Actually prefix sum=1, cand=1+8+9*2=1+8+18=27 => 1899? Actually 1,8,9,9 sum=27. i=2: d=9: prefix sum=1+9=10; cand=10+8+9=27 => 1989 sum=27. i=3: d=9: prefix sum=1+9+9=19; cand=19+8=27 => 1998 sum=27. So N itself is best with 28. So ans=28.
Now test N= 2000: N sum=2. i=0: d=2->1 => 1999 sum=28. ans=28.
Thus seems fine.
Now complexity is fine.
Thus I'll produce final solution.
But wait, we need to consider that the digit sum of 0 is 0, but not positive. So ignore.
Thus answer.
Implementation in Python:
import sys
def main():
N = sys.stdin.readline().strip()
L = len(N)
ans = sum(int(c) for c in N)
for i in range(L):
d = int(N[i])
if d == 0:
continue
# sum of digits before i
pre = sum(int(N[j]) for j in range(i))
cand = pre + (d - 1) + 9 * (L - i - 1)
if cand > ans:
ans = cand
print(ans)
if name == "main":
main()
We can micro-opt: compute prefix sums array:
pref = [0](L+1)
for i in range(L):
pref[i+1] = pref[i] + int(N[i])
Then for each i: pre_sum = pref[i]; cand = pre_sum + (int(N[i]) - 1) + 9(L-i-1)
That is simpler and O(L).
We'll use that.
Thus final code:
def solve():
N = input().strip()
L = len(N)
digits = [int(c) for c in N]
pref = [0] * (L+1)
for i in range(L):
pref[i+1] = pref[i] + digits[i]
ans = pref[L] # sum of all digits
for i in range(L):
d = digits[i]
if d == 0:
continue
cand = pref[i] + (d - 1) + 9 * (L - i - 1)
if cand > ans:
ans = cand
print(ans)
Test with examples.
Now test with edge: N=1: digits=[1], L=1. pref=[0,1]. ans=1. i=0: d=1>0, cand = pref[0]+(1-1)+9*0 =0+0+0=0; ans remains 1. Output 1.
N=9: ans=9; cand from i=0: pref[0]+8+0=8; ans 9.
N=10: digits=[1,0], L=2. pref=[0,1,1]. ans=1. i=0: d=1>0: cand =0+(1-1)+9*1=0+0+9=9; ans=9. i=1: d=0 skip. Output 9. That corresponds to number 9, which is ≤10 and sum=9. Indeed 9 is best; 10 sum=1, 8 sum=8, etc.
Now consider N=10000000000000001 (10^16+1). digits: 1, zeros(15), 1? Actually length 17: digits[0]=1, digits[1..15]? Actually need to count precisely: Let's just trust: i=0: cand=144; others: i=1..15 zeros skip; i=16 d=1>0: cand = pref[16] + (1-1)+0. pref[16] is sum of first 16 digits: 1 + 0*15? Actually first 16 digits are digits[0..15], which includes the first 1 and 15 zeros? Wait indices: digits[0] = 1; indices 1-15 are zeros (that's 15 zeros); index16 is the last digit 1. So pref[16] = 1 (since sum of digits[0..15] = 1 + zeros). So cand = 1 + 0 =1. So ans=144. Good.
Thus correct.
Now about reading input: may have newline; we strip.
Now ensure to output as integer.
Thus final answer.
But before finalizing, I want to consider if there is any scenario where the candidate number formed by decreasing a digit and setting all following to 9 could be greater than N? The process ensures it's less because at the first differing position (i), the candidate digit is one less than N's digit. So it's definitely less. However, there is a subtlety: What if N has a digit that we decrease but the digit is 0? We skip. But what if we consider decreasing a digit that is not the first differing? Actually we are iterating all positions. For each i, we consider a number that matches N up to i-1, then at i it is d-1, then 9s. That number is definitely ≤ N because at position i it is less. So it's valid. Even if earlier digits are the same, it's fine.
Thus algorithm is correct.
Now we can produce final solution.
But wait, what about N= 1000? i=0 gives 999. That is valid. Good.
Now, is there a case where decreasing a digit leads to a leading zero and then the number is effectively a number with fewer digits but the number might be 0? That's only for N of length 1. So skip.
Thus done.
Thus answer.
Now I can write explanation as well.
But the instruction: "Find the maximum possible sum of the digits (in base 10) of a positive integer not greater than N." So output the sum, not the integer.
Now final code.
Let's write final solution in Python.
I'll include input reading with sys.stdin.
Now produce final answer. ```python
import sys
def solve() -> None:
N = sys.stdin.readline().strip()
L = len(N)
digits = [int(c) for c in N]
code
# prefix sums: pref[i] = sum of digits[0..i-1]
pref = [0] * (L + 1)
for i in range(L):
pref[i + 1] = pref[i] + digits[i]
ans = pref[L] # sum of digits of N itself
for i in range(L):
d = digits[i]
if d == 0:
continue
# candidate: same prefix up to i-1, digit i decreased by 1, then all 9's
cand = pref[i] + (d - 1) + 9 * (L - i - 1)
if cand > ans:
ans = cand
print(ans)
Also consider the possibility of candidate being exactly N? That we already have.
Also, consider the all-9 number with L-1 digits if L>1. That's covered by i=0 and S[0]='1'? Actually if S[0]='1' and we reduce to 0, candidate becomes all 9's of length L-1. But if S[0] > '1', reducing it to non-zero yields a number with same digit length, but that's also considered. So our loop covers all such.