#include <stdio.h> #include <algorithm> #include <vector> using namespace std; int main() { int C, n, m; int x, s, q; int cost[512], profit[512]; int cases = 0; while (scanf("%d", &C) == 1) { scanf("%d %d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d %d", &cost[i], &profit[i]), profit[i] -= cost[i]; vector< pair<int, int> > items; for (int i = 0; i < m; i++) { scanf("%d", &x); int c = 0, p = 0; for (int j = 0; j < x; j++) { scanf("%d %d", &s, &q); c += cost[s] * q; p += profit[s] * q; } if (c <= C && p > 0) items.push_back(make_pair(c, p)); } if (cases++) puts(""); if (items.size() == 0) { puts("0"); continue; } if (items.size() == 1) { printf("%d\n", items[0].second); continue; } int g = C; for (int i = 0; i < items.size(); i++) g = __gcd(g, items[i].first); C /= g; for (int i = 0; i < items.size(); i++) items[i].first /= g; vector<int> dp(C + 1, 0); for (int i = 0; i < items.size(); i++) { for (int j = C; j >= items[i].first; j--) dp[j] = max(dp[j], dp[j - items[i].first] + items[i].second); } printf("%d\n", dp[C]); } return 0; }
|