#include <cstdio> #include <algorithm> using namespace std; //#define LOG(args...) #define LOG(args...) fprintf(stderr, args) #define FOR(i, n) for(int i = 0, __n = (n); i<__n; i++) int mass[100]; int plecak[100]; int x[17000000]; int main() { int n, m; scanf("%d%d", &n, &m); FOR(i, n) scanf("%d", mass + i); FOR(i, m) scanf("%d", plecak + i); sort(plecak, plecak+m); reverse(plecak, plecak+m); int cmb = 1<<n; FOR(i, cmb) x[i] = -1; x[0] = 0; int res = -1; FOR(pl, min(n,m)) { //printf("PL: %d/%d\n", pl, cmb); int s = plecak[pl]; FOR(i, cmb) { if(x[i] >= 0) { x[i]=0; continue; } int best = s+1; FOR(item, n) if(i&(1<<item)) { int val = x[i^(1<<item)]; if(val == -1) break; val += mass[item]; best = min(best, val); } if(best <= s) x[i] = best; //printf("%x: %d\n", i, x[i]); } if(x[cmb-1]>= 0) { res = pl+1; break; } } if(res == -1) printf("NIE\n"); else printf("%d\n", res); }
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 | #include <cstdio> #include <algorithm> using namespace std; //#define LOG(args...) #define LOG(args...) fprintf(stderr, args) #define FOR(i, n) for(int i = 0, __n = (n); i<__n; i++) int mass[100]; int plecak[100]; int x[17000000]; int main() { int n, m; scanf("%d%d", &n, &m); FOR(i, n) scanf("%d", mass + i); FOR(i, m) scanf("%d", plecak + i); sort(plecak, plecak+m); reverse(plecak, plecak+m); int cmb = 1<<n; FOR(i, cmb) x[i] = -1; x[0] = 0; int res = -1; FOR(pl, min(n,m)) { //printf("PL: %d/%d\n", pl, cmb); int s = plecak[pl]; FOR(i, cmb) { if(x[i] >= 0) { x[i]=0; continue; } int best = s+1; FOR(item, n) if(i&(1<<item)) { int val = x[i^(1<<item)]; if(val == -1) break; val += mass[item]; best = min(best, val); } if(best <= s) x[i] = best; //printf("%x: %d\n", i, x[i]); } if(x[cmb-1]>= 0) { res = pl+1; break; } } if(res == -1) printf("NIE\n"); else printf("%d\n", res); } |