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

Solution in C (GCC 5.4.1) (judge verdict: Accepted)


#include <stdio.h>
#include <math.h>
int main()
{   long long n;
    scanf("%lld",&n);
    int dig=log10(n);
    int head=n/(pow(10,dig));
    if(n==9||n==99||n==999||n==9999||n==99999||
        n==999999||n==9999999||n==99999999||n==999999999||n==9999999999||
        n==99999999999||n==999999999999||n==9999999999999||n==99999999999999||
        n==999999999999999||
        n==8||
        n==89||n==899||n==8999||n==89999||
        n==899999||n==8999999||n==89999999||n==899999999||n==8999999999||
        n==89999999999||n==899999999999||n==8999999999999||n==89999999999999||
        n==899999999999999||
        n==7||
        n==79||n==799||n==7999||n==79999||
        n==799999||n==7999999||n==79999999||n==799999999||n==7999999999||
        n==79999999999||n==799999999999||n==7999999999999||n==79999999999999||
        n==799999999999999||
        n==6||n==69||n==699||n==6999||n==69999||
        n==699999||n==6999999||n==69999999||n==699999999||n==6999999999||
        n==69999999999||n==699999999999||n==6999999999999||n==69999999999999||
        n==699999999999999||
        n==5||
        n==59||n==599||n==5999||n==59999||
        n==599999||n==5999999||n==59999999||n==599999999||n==5999999999||
        n==59999999999||n==599999999999||n==5999999999999||n==59999999999999||
        n==599999999999999||
        n==4||
        n==49||n==499||n==4999||n==49999||
        n==499999||n==4999999||n==499999999||n==499999999||n==4999999999||
        n==49999999999||n==499999999999||n==4999999999999||n==49999999999999||
        n==499999999999999||
        n==3||
        n==39||n==399||n==3999||n==39999||
        n==399999||n==3999999||n==39999999||n==399999999||n==3999999999||
        n==39999999999||n==399999999999||n==3999999999999||n==39999999999999||
        n==399999999999999||
        n==2||
        n==29||n==299||n==2999||n==29999||
        n==299999||n==2999999||n==29999999||n==299999999||n==2999999999||
        n==29999999999||n==299999999999||n==2999999999999||n==29999999999999||
        n==299999999999999||
        n==1||
        n==19||n==199||n==1999||n==19999||
        n==199999||n==1999999||n==19999999||n==199999999||n==1999999999||
        n==19999999999||n==199999999999||n==1999999999999||n==19999999999999||
        n==199999999999999){
            dig*=9;
            dig+=head;
        }else{
    dig*=9;
    dig+=(head-1);
        }
        
        printf("%d",dig);
    

    return 0;
}