#include <algorithm> #include <cctype> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <sstream> #include <string> #include <vector> #define ALL(v) v.begin(), v.end() #define CLR(array) memset(array, 0, sizeof(array)) #define DEBUG cout << "bug " #define Fr first #define FOR(i, n) for(int i = 0; i < n; i++) #define MEM(array) memset(array, 0x3f, sizeof(array)) #define OFF(array) memset(array, -1, sizeof(array)) #define PB push_back #define PR(x) cout << #x << " = " << x << " " #define PRLN(x) cout << #x << " = " << x << endl #define rep(i, a, b) for(int i = a; i <= b; i++) #define Rep(i, a, b) for(int i = a; i >= b; i--) #define repk(i, a, b) for(i = a; i <= b; i++) #define Repk(i, a, b) for(i = a; i >= b; i--) #define Sc second #define SZ size() using namespace std; typedef long long ll; typedef pair<int, int> P; const int inf = 0x3f3f3f3f, maxn = 1e5 + 5; int s, m, n, goal; int salary[125], course[125]; int dp[125][maxn]; int encode(int* a) { int res = 0; rep(i, 1, s) res = res * 3 + a[i]; return res; } void decode(int x, int* a) { Rep(i, s, 1) { a[i] = x % 3; x /= 3; } } int dfs(int i, int st) { if(i == m + n) return st == goal ? 0 : inf; int& res = dp[i][st]; if(res >= 0) return res; res = inf; if(i >= m) res = dfs(i + 1, st); int p[10], a[10]; decode(course[i], p); decode(st, a); rep(j, 1, s) { a[j] += p[j]; if(a[j] > 2) a[j] = 2; } res = min(res, salary[i] + dfs(i + 1, encode(a))); return res; } int solve(void) { CLR(course); OFF(dp); string in; getline(cin, in); int a[10]; fill(a + 1, a + s + 1, 2); goal = encode(a); FOR(i, m + n) { getline(cin, in); stringstream ss(in); ss >> salary[i]; int u; CLR(a); while(ss >> u) a[u] = 1; course[i] = encode(a); } return dfs(0, 0); } int main(void) { while(cin >> s >> m >> n && s + m + n) cout << solve() << '\n'; return 0; }
|