#include <cstdio>
#include <algorithm>
#include <utility>
#include <set>
using namespace std;
const pair<int, int> infty = make_pair(1e9, 1e9);
int n, m;
int a[105], c[105];
pair<int, int> odl[1 << 24];
inline bool cmp(const pair<int, int> &a, const pair<int, int> &b)
{
if(a.first == b.first)
return a.second > b.second;
return a.first < b.first;
}
pair<int, int> dod(const pair<int, int> &a, int b)
{
if(a.second >= b)
return make_pair(a.first, a.second - b);
if(a.first + 1 == m || c[a.first + 1] < b)
return infty;
return make_pair(a.first + 1, c[a.first + 1] - b);
}
int main()
{
scanf("%d%d", &n, &m);
for(int i = 0; i < n; i++)
scanf("%d", a + i);
for(int i = 0; i < m; i++)
scanf("%d", c + i);
sort(c, c + m);
reverse(c, c + m);
m = min(m, n);
int odw = (1 << n) - 1;
for(int i = 0; i <= odw; i++)
odl[i] = infty;
odl[0] = make_pair(0, c[0]);
for(int w = 0; w <= odw; w++)
{
int s = w ^ odw;
while(s)
{
const int t = __builtin_ctz(s);
const int g = (1 << t);
s ^= g;
const pair<int, int> dodanie = dod(odl[w], a[t]);
if(cmp(dodanie, odl[w ^ g]))
odl[w ^ g] = dodanie;
}
}
if(odl[odw] == infty)
printf("NIE\n");
else
printf("%d\n", odl[odw].first + 1);
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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | #include <cstdio> #include <algorithm> #include <utility> #include <set> using namespace std; const pair<int, int> infty = make_pair(1e9, 1e9); int n, m; int a[105], c[105]; pair<int, int> odl[1 << 24]; inline bool cmp(const pair<int, int> &a, const pair<int, int> &b) { if(a.first == b.first) return a.second > b.second; return a.first < b.first; } pair<int, int> dod(const pair<int, int> &a, int b) { if(a.second >= b) return make_pair(a.first, a.second - b); if(a.first + 1 == m || c[a.first + 1] < b) return infty; return make_pair(a.first + 1, c[a.first + 1] - b); } int main() { scanf("%d%d", &n, &m); for(int i = 0; i < n; i++) scanf("%d", a + i); for(int i = 0; i < m; i++) scanf("%d", c + i); sort(c, c + m); reverse(c, c + m); m = min(m, n); int odw = (1 << n) - 1; for(int i = 0; i <= odw; i++) odl[i] = infty; odl[0] = make_pair(0, c[0]); for(int w = 0; w <= odw; w++) { int s = w ^ odw; while(s) { const int t = __builtin_ctz(s); const int g = (1 << t); s ^= g; const pair<int, int> dodanie = dod(odl[w], a[t]); if(cmp(dodanie, odl[w ^ g])) odl[w ^ g] = dodanie; } } if(odl[odw] == infty) printf("NIE\n"); else printf("%d\n", odl[odw].first + 1); return 0; } |
English