-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexactChange.cpp
61 lines (50 loc) · 1.13 KB
/
exactChange.cpp
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
55
56
57
58
59
60
61
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;
ifstream fin("exactChange.in");
ofstream fout("exactChange.out");
int main()
{
int testCase;
fin >> testCase;
for (int c = 1; c <= testCase; ++c)
{
int total;
fin >> total;
int n;
fin >> n;
if (total == 0 && n == 0)
{
fout << "0 0\n";
}
vector<int> a(n);
for (int i = 0; i <= n - 1; ++i)
{
fin >> a[i];
}
vector<int> dp(total + 10001, 10001);
dp[0] = 0;
for (int i = 0; i <= n - 1; ++i)
{
for (int j = total + 10000; j >= a[i]; --j)
{
dp[j] = min(dp[j], dp[j - a[i]] + 1);
}
}
int ans = total;
while (ans <= total + 10000 && dp[ans] == 10001)
{
++ans;
}
fout << ans << ' ' << dp[ans] << '\n';
}
return 0;
}