summaryrefslogtreecommitdiff
path: root/Year_2/Algorithms/resto.cc
blob: 87ab43d9d693708fed4fd614032c403e55acc9d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <vector>
#include <fstream>
#include <cstring>
#define INF 9999999

using namespace std;

int
change(vector<int>& coins, int sum)
{
	int n = coins.size();

	int* table = new int[sum+1];
	memset(table, 0, sum);

	table[0] = 0;
	for (int i = 1; i < sum+1; ++i) {
		table[i] = INF;
		for (int j = 0; j < n; ++j) {
			if (coins[j] <= i)
				table[i] = min(table[i], 1+table[i-coins[j]]);
		}
	}

	int result = table[sum];
	delete[] table;

	return result;
}

int
main(int argc, char** argv)
{
	int ts = (argc > 1) ? stoi(argv[1]) : 100;
	int s, n;
	ifstream fin("input.txt");
	ofstream fout("output.txt");

	for (int i = 0; i < ts; ++i) {
		fin >> s >> n;
		vector<int> v(n);
		for (int j = 0; j < n; ++j) {
			fin >> v[j];
		}

		fout << change(v, s) << endl;
	}


	fin.close();
	fout.close();
	return 0;
}