-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathP11450.cpp
44 lines (40 loc) · 1.1 KB
/
P11450.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
#include <iostream>
#include <stdio.h>
int main() {
int cases, M, C, K, prices[20];
bool obtainablePrices[201];
std::cin >> cases;
for(int cas = 0; cas < cases; ++cas) {
std::cin >> M >> C;
for(int i = 1; i <= M; ++i)
obtainablePrices[i] = false;
obtainablePrices[0] = true; // 0 product types OK.
for(int i = 0; i < C; ++i) {
std::cin >> K;
for(int j = 0; j < K; ++j)
std::cin >> prices[j];
for(int price = M; price >= 0; --price) {
if(!obtainablePrices[price])
continue; // already set to false and can't propagate.
for(int j = 0; j < K; ++j) {
if(prices[j] + price <= M) {
obtainablePrices[prices[j]+price] = true;
//std::cerr << "Price " << j << "=" << prices[j] << " => " << prices[j]+price << " obtainable." << std::endl;
}
}
obtainablePrices[price] = false;
} // for price
} // for i
bool written = false;
for(int i = M; i >= 0; --i) {
if(obtainablePrices[i]) {
written = true;
std::cout << i << std::endl;
break;
}
}
if(!written)
std::cout << "no solution" << std::endl;
}
return 0;
}