Problem:
Score : 700 points

Problem Statement

Find the smallest possible sum of the digits in the decimal notation of a positive multiple of K.

Constraints

2 \leq K \leq 10^5

K is an integer.

Input

Input is given from Standard Input in the following format:

K

Output

Print the smallest possible sum of the digits in the decimal notation of a positive multiple of K.

Sample Input 1

6

Sample Output 1

3

12=6×2 yields the smallest sum.

Sample Input 2

41

Sample Output 2

5

11111=41×271 yields the smallest sum.

Sample Input 3

79992

Sample Output 3

36

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include<bits/stdc++.h>
#define int long long

using namespace std;

const int maxN = 1e5, r = 10, s = 45;

int readInt ()
{
    bool minus = false;
    int result = 0;
    char ch;
    ch = getchar();
    while (true)
    {
        if (ch == '-') break;
        if (ch >= '0' && ch <= '9') break;
        ch = getchar();
    }
    if (ch == '-') minus = true;
    else result = ch-'0';
    while (true)
    {
        ch = getchar();
        if (ch < '0' || ch > '9') break;
        result = result*10 + (ch - '0');
    }
    if (minus)
        return -result;
    else
        return result;
}
int p[s + 1][maxN + 1], k, res = 45;
bool flag;
void go(int depth, int cur, int mod, int sum, int ans) {
    if(flag)
        return;
    if(mod == 0) {
        if(sum)
            res = min(res, sum);
        flag = true;
        return;
//        cout << sum;
//        exit(0);
    }
    if(cur > depth)
        return;
    for(int i = (cur == 1) ? 1 : 0; i <= 9; i++) {
        go(depth, cur + 1, (ans * 10 + i) % k, sum + i, (ans * 10 + i) % k);
        go(depth, cur + 1, ans % k, sum, ans % k);
    }
}
main() {
    k = readInt();
//    cout << -1;
    for(int d = 20; d >= 1; d--) {
        flag = false;
        go(d, 1, -1, 0, 0);
    }
    cout << res;
}