-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathuva-10130.cpp
71 lines (48 loc) · 1.13 KB
/
uva-10130.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
62
63
64
65
66
67
68
69
70
71
//uva 10130
//SuperSale
#include <iostream>
#include <algorithm>
#include <vector>
#include <iomanip>
using namespace std;
int max(int a, int b);
int main(void)
{
int T = 0;
cin >> T;
while (T--) {
int n = 0;
cin >> n;
vector < pair <int, int> > objects;
for (int i = 0; i < n; ++i){
int p, w;
cin >> p >> w;
objects.push_back(make_pair(p, w));
}
int people;
cin >> people;
vector <int> weights(people);
for (int i = 0; i < people; ++i)
cin >> weights[i];
int maxWeight = *max_element(weights.begin(), weights.end());
vector < vector <int> > arr(objects.size() + 1, vector <int> (maxWeight + 1));
for (int j = 1; j < maxWeight + 1; ++j)
for (int i = 1; i < objects.size() + 1; ++i)
if (j - objects[i - 1].second >= 0){
int price = objects[i - 1].first;
int wei = objects[i - 1].second;
arr[i][j] = max(arr[i - 1][j], arr[i - 1][j - wei] + price);
}
else
arr[i][j] = arr[i - 1][j];
int totalValue = 0;
for (auto a : weights)
totalValue += arr[objects.size()][a];
cout << totalValue << endl;
}
return 0;
}
int max(int a, int b)
{
return a > b ? a : b;
}