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 JavaScript (node.js v5.12) (judge verdict: Accepted)

process.stdin.setEncoding('utf8');

run(function * () {
	var num = yield;

	var first = parseInt(num[0]);
	var rests = num.length - 1;
	for (var i=1, n=num.length; i<n; ++i) {
		if ('9' !== num[i]) {
			--first;
			break;
		}
	}
	console.log(rests * 9 + first);
});

/**
 * yield で標準入力を利用する
 * @param {GeneratorFunction} main 
 */
function run (main) {
	var gen_main = main();
	var cin = null;
	var words = [null];

	process.stdin.pause();

	function get_chunk (chunk) {
		// 標準入力を止める
		process.stdin.pause();
		// 空白区切りで分割
		var nexts = chunk.split(/\s+/);
		for (var i=0, n=nexts.length; i<n; ++i) {
			if (nexts[i]) {
				words.push(nexts[i]);
			}
		}
		loop();
	}
	function loop () {
		while (0 < words.length) {
			var result = gen_main.next(words.shift());
			if (undefined !== result.value) {
				// yield に渡された値を標準出力
				process.stdout.cork();
				process.stdout.write(result.value)
				process.stdout.uncork();
			}
			// main が return しているなら終了
			if (result.done) return;
		}

		process.stdin.resume();
	};
	process.stdin.on('data', get_chunk);

	loop();
}