#include <cstdio> #include <algorithm> using namespace std; const int SIZE = 1 << 24, INF = 1000000000; int t[SIZE]; int a[24]; int c[100]; int main() { int n, m, k; scanf("%d %d", &n, &m); k = 1 << n; for(int i = 0; i < n; i++) scanf("%d", a + i); for(int i = 0; i < m; i++) scanf("%d", c + i); for(int i = 1; i < k; i++) t[i] = INF; sort(c, c + m); for(int i = m - 1; i >= m - n; i--) { for(int j = 0; j < k; j++) if(t[j] != -1 && t[j] <= c[i]) { for(int b = 1, l = 0; l < n; b <<= 1, l++) t[j | b] = min(t[j | b], t[j] + a[l]); t[j] = t[j] == 0 ? -1 : 0; } if(t[k-1] == 0) { printf("%d\n", m - i); return 0; } } puts("NIE"); }
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 | #include <cstdio> #include <algorithm> using namespace std; const int SIZE = 1 << 24, INF = 1000000000; int t[SIZE]; int a[24]; int c[100]; int main() { int n, m, k; scanf("%d %d", &n, &m); k = 1 << n; for(int i = 0; i < n; i++) scanf("%d", a + i); for(int i = 0; i < m; i++) scanf("%d", c + i); for(int i = 1; i < k; i++) t[i] = INF; sort(c, c + m); for(int i = m - 1; i >= m - n; i--) { for(int j = 0; j < k; j++) if(t[j] != -1 && t[j] <= c[i]) { for(int b = 1, l = 0; l < n; b <<= 1, l++) t[j | b] = min(t[j | b], t[j] + a[l]); t[j] = t[j] == 0 ? -1 : 0; } if(t[k-1] == 0) { printf("%d\n", m - i); return 0; } } puts("NIE"); } |