#include<cstdio> #include<functional> #include<algorithm> using namespace std; typedef long long LL; int n, m; LL a[30], c[200]; int test() { if (a[0] > c[0]) return 0; LL s = a[0]; int i=1, j=0; while (i<n) { s += a[i]; if (s > c[j]) { j++; if ((j==m) || (a[i] > c[j])) return 0; s = a[i]; } i++; } return j+1; } int main() { scanf("%d%d", &n, &m); for (int i=0; i<n; i++) scanf("%lld", a+i); for (int i=0; i<m; i++) scanf("%lld", c+i); sort(a, a+n, greater<LL>()); sort(c, c+m, greater<LL>()); int w = test(); if (w == 0) { printf("NIE\n"); return 0; } while (next_permutation(a, a+n, greater<LL>())) { int t = test(); if (t != 0) w = min(w, t); } printf("%d\n", w); return 0; }
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 | #include<cstdio> #include<functional> #include<algorithm> using namespace std; typedef long long LL; int n, m; LL a[30], c[200]; int test() { if (a[0] > c[0]) return 0; LL s = a[0]; int i=1, j=0; while (i<n) { s += a[i]; if (s > c[j]) { j++; if ((j==m) || (a[i] > c[j])) return 0; s = a[i]; } i++; } return j+1; } int main() { scanf("%d%d", &n, &m); for (int i=0; i<n; i++) scanf("%lld", a+i); for (int i=0; i<m; i++) scanf("%lld", c+i); sort(a, a+n, greater<LL>()); sort(c, c+m, greater<LL>()); int w = test(); if (w == 0) { printf("NIE\n"); return 0; } while (next_permutation(a, a+n, greater<LL>())) { int t = test(); if (t != 0) w = min(w, t); } printf("%d\n", w); return 0; } |