#include <cstdio> #include <algorithm> using namespace std; int n, m, tab[31], bat[107], dun; bool pck[31]; int main () { scanf ("%d%d", &n, &m); for (int i=0; i<n; ++i) { scanf ("%d", &tab[i]); } for (int i=0; i<m; ++i) { scanf ("%d", &bat[i]); } sort (tab, tab+n); sort (bat, bat+m); for (int i=m-1; i>=0; --i) { for (int j=n-1; j>=0; --j) { if (pck[j]==0 && tab[j]<=bat[i]) { pck[j]=1; bat[i]-=tab[j]; ++dun; } } if (dun==n) { printf ("%d\n", m-i); return 0; } } printf ("NIE\n"); 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 | #include <cstdio> #include <algorithm> using namespace std; int n, m, tab[31], bat[107], dun; bool pck[31]; int main () { scanf ("%d%d", &n, &m); for (int i=0; i<n; ++i) { scanf ("%d", &tab[i]); } for (int i=0; i<m; ++i) { scanf ("%d", &bat[i]); } sort (tab, tab+n); sort (bat, bat+m); for (int i=m-1; i>=0; --i) { for (int j=n-1; j>=0; --j) { if (pck[j]==0 && tab[j]<=bat[i]) { pck[j]=1; bat[i]-=tab[j]; ++dun; } } if (dun==n) { printf ("%d\n", m-i); return 0; } } printf ("NIE\n"); return 0; } |