-
Notifications
You must be signed in to change notification settings - Fork 0
/
aba12c.cpp
56 lines (55 loc) · 1.6 KB
/
aba12c.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
#include<cstdio>
#include<map>
#include<list>
using namespace std;
int compute(int n, int k, list<pair<int, int> >&weightPriceList, map<pair<int, int>, int>& mp);
int main() {
int test;
scanf("%d", &test);
while(test--) {
int n, k;
scanf("%d %d", &n, &k);
list<pair<int, int> > weightPriceList;
for(int i=0;i<k;++i) {
int price;
scanf("%d", &price);
if(price != -1) {
pair<int, int> pr;
pr.first = i + 1;
pr.second = price;
weightPriceList.push_back(pr);
}
}
map<pair<int, int>, int>mp;
int minPrice = compute(n, k, weightPriceList, mp);
printf("%d\n", minPrice);
}
}
int compute(int n, int k, list<pair<int, int> >&weightPriceList, map<pair<int, int>, int>& mp) {
if(k == 0) {
return 0;
}
if(k < 0) {
return -1;
}
pair<int, int> key;
key.first = n;
key.second = k;
if(mp.count(key)) {
return mp[key];
} else {
// It needs further computation
int minPrice = -1;
for(list<pair<int, int> >::iterator itr=weightPriceList.begin();itr != weightPriceList.end();++itr) {
int candidatePrice = compute(n-1, k - (*itr).first, weightPriceList, mp);
if(candidatePrice != -1) {
candidatePrice += (*itr).second;
if(minPrice == -1 || minPrice > candidatePrice) {
minPrice = candidatePrice;
}
}
}
mp[key] = minPrice;
return minPrice;
}
}