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 <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <string>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <stdio.h>
using namespace std;
#define int long long
int MOD = 1000000007;
signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	int N;
	cin >> N;
	int K = 0;
	int n = N;
	while (n > 0) {
		K += n % 10;
		n /= 10;
	}
	int a = 1;
	vector<int> dp(N, (int)1 << 60);
	vector<int> u;
	vector<int> u2;
	for (int i = 0; i < N; i++) {
		u.push_back(i);
	}
	for (int i = 0; i < 100; i++) {
		u2.clear();
		for (int k = 1; k < 10; k++) {
			dp[(k*a) % N] = min(dp[(k*a) % N], k);
		}
		for (auto j : u) {
			if (dp[j] < dp[0]) {
				u2.push_back(j);
				for (int k = 1; k < 10; k++) {
					dp[(j + k*a) % N] = min(dp[(j + k*a) % N], dp[j] + k);
				}
			}
		}
		a = (a * 10) % N;
		swap(u, u2);

	}
	cout << dp[0] << endl;
}