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>
using namespace std;

typedef long long ll;
const int n_num = 1e5+5;
const ll inf = 1e9+7;

typedef struct {
	int I;
	int from;
	int cost;
	vector<pair<int,int>> to;
} _node;

bool operator > (const _node& a,const _node& b) {
	return a.cost > b.cost;
}

_node node[n_num];
priority_queue<_node,vector<_node>,greater<_node>> pque;

string ans;

void ds_init() {
	for (int i = 0;i < n_num;i++) {
		node[i].I = i;
		node[i].from = i;
		node[i].cost = inf;
	}
}

void dijkstra() {
	while (!pque.empty()) {
		_node next = pque.top();
		pque.pop();
		if (next.cost > node[next.I].cost) continue;
		for (int i = 0;i < next.to.size();i++) {
			if (node[next.to[i].first].cost > next.cost + next.to[i].second) {
				node[next.to[i].first].cost = next.cost + next.to[i].second;
				node[next.to[i].first].from = next.I;
				pque.push(node[next.to[i].first]);
			}
		}
	}
}

void searchpath(int now) {
	if (node[now].from != now) {
		ans = to_string(node[now].from) + " " + ans;
		searchpath(node[now].from);
	} else {
		return;
	}
}

int main() {
	int k;
	cin >> k;
	for (int i = 0;i < k;i++) {
		node[i].to.push_back({(i+1)%k,1});
		node[i].to.push_back({i*10%k,0});
	}
	ds_init();
	node[1].cost = 1;
	for (int i = 0;i < node[1].to.size();i++) {
		node[node[1].to[i].first].cost = node[1].to[i].second;
		node[node[1].to[i].first].from = 0;
		pque.push(node[node[i].to[i].first]);
	}
	dijkstra();
	cout << node[0].cost << endl;
}