infO(1)Cup 2025 Day 1 - Mirror | Nines

This was the problem page during the contest. Access the current page here.
Time limit: 0.3s Memory limit: 512MB Input: Output:

You are given an integer N>0N \gt 0 and a number KK. We first define, for any natural number xx, the number

PK(x)=max((the number of appearances of the digit 9 in x)K,0)P_K(x) = \max((\text{the number of appearances of the digit } 9 \text{ in } x) - K, 0)

We then define the value of an integer xx, denoted by valueK(x)\text{value}_K(x), by:

valueK(x)=x+10PK(x)\text{value}_K(x) = x + 10^{P_K(x)}

Task

Given NN and KK, what is the maximum value of any integer less than NN? In other words, what is

answer(N,K)=max0x<NvalueK(x)\text{answer}(N, K) = \max_{0\leq x\lt N}\text{value}_K(x)

Input data

The first line in the input data contains the number of test cases TT. The 2T2T lines that follow contain the test cases we are interested in. The first line of a test case contains DD, the number of digits of NN, and KK. The second line of a test case contains the integer NN.

Output data

Output TT lines, the answers for the TT values of NN and KK you are given.

Constraints and clarifications

  • Let D\sum D be the sum of DD for all test cases in a file.
  • 1T100 0001 \leq T \leq 100 \ 000;
  • 1D1 000 0001 \leq \sum D \leq 1 \ 000 \ 000;
  • 1KD-1 \leq K \leq D.
# Points Restrictions
1 7 T1 000T \leq 1 \ 000, D4D \leq 4
2 3 K=1K = -1, D9D \leq 9
3 6 K=0K = 0, D9D \leq 9
4 6 D9D \leq 9
5 4 K=DK = D
6 26 T200T \leq 200, D200D \leq 200
7 48 No further restrictions.

Example

stdin

2
3 0
100
18 0
998877665544332211

stdout

199
1097999999999999999

Explanation

For the first test case, the optimum is given by value0(99)=199\text{value}_0(99) = 199.
For the second test case, the optimum is given by value0(997 999 999 999 999 999)=1 097 999 999 999 999 999\text{value}_0(997 \ 999 \ 999 \ 999 \ 999 \ 999) = 1 \ 097 \ 999 \ 999 \ 999 \ 999 \ 999.

Log in or sign up to be able to send submissions!