#include<bits/stdc++.h> #define ALL(X) X.begin(),X.end() #define FOR(I,A,B) for(int (I) = (A); (I) <= (B); (I)++) #define FORW(I,A,B) for(int (I) = (A); (I) < (B); (I)++) #define FORD(I,A,B) for(int (I) = (A); (I) >= (B); (I)--) #define CLEAR(X) memset(X,0,sizeof(X)) #define PB push_back #define MP make_pair #define X first #define Y second using namespace std; typedef signed long long slong; typedef long double ldouble; const slong Infinity = 1000000100; const ldouble Epsilon = 1e-9; template<typename T, typename U> ostream& operator << (ostream& os, const pair<T,U>&p) {return os << "(" << p.X << "," << p.Y << ")"; } template<typename T> ostream& operator << (ostream &os, const vector<T>& V) { os << "["; FORW(i,0,size(V)) os << V[i] << ((i==size(V)-1) ? "" : ","); return os << "]"; } template<typename T> ostream& operator << (ostream &os, const set<T>& S) {os << "("; for(auto i: S) os << i << (i==*S.rbegin()?"":","); return os << ")"; } template<typename T, typename U> ostream& operator << (ostream &os, const map<T, U>& M){os << "{"; for(auto i: M) os << i << (i.X==M.rbegin()->X?"":","); return os << "}"; } template<typename T, typename F> T lbound(T p, T q, F f) { static_assert(is_integral<T>::value, "integral type required"); while(p < q) { T r = p+(q-p)/2; if(f(r)) q = r; else p = r+1; } return p; } template<typename T, typename F> T lboundl(T p, T q, F f) { static_assert(is_floating_point<T>::value, "floating point type required"); FOR(i,1,70) { T r = (p+q)/2; if(f(r)) q = r; else p = r; } return p; } template<typename T, typename U> bool contain(T t, U u) { return t.find(u) != t.end(); } template<typename T> int size(T t) { return t.size(); } const int MAXN = 111; int N, M; int A[MAXN]; int B[MAXN]; void read_data() { scanf("%d %d", &N, &M); FORW(i,0,N) scanf("%d", A+i); FORW(i,0,M) scanf("%d", B+i); } #define min_ele(s) (__builtin_ctzl(s)) int DP[2][1<<24]; int C[1<<24]; int T[24]; int x, y, w; int p, q; void rec(int k) { if(k == N) { if(C[p&(~q)] <= B[w] and DP[y][q]) { // cout << "W: " << w << " " << bitset<4>(p) << endl; DP[x][p] = true; } } else { p &= (~(1<<k)); q &= (~(1<<k)); rec(k+1); p |= (1<<k); rec(k+1); q |= (1<<k); rec(k+1); } } void solve() { sort(B,B+M); reverse(B,B+M); FORW(j,1,1<<N) C[j] = C[j^(1<<min_ele(j))] + A[min_ele(j)]; int result = -1; DP[0][0] = DP[1][0] = true; FORW(i,0,M) { w = i; x = i%2; y = (i+1)%2; rec(0); if(DP[x][(1<<N)-1]) { result = i+1; break; } } if(result == -1) printf("NIE\n"); else printf("%d\n", result); } int main() { read_data(); solve(); 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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | #include<bits/stdc++.h> #define ALL(X) X.begin(),X.end() #define FOR(I,A,B) for(int (I) = (A); (I) <= (B); (I)++) #define FORW(I,A,B) for(int (I) = (A); (I) < (B); (I)++) #define FORD(I,A,B) for(int (I) = (A); (I) >= (B); (I)--) #define CLEAR(X) memset(X,0,sizeof(X)) #define PB push_back #define MP make_pair #define X first #define Y second using namespace std; typedef signed long long slong; typedef long double ldouble; const slong Infinity = 1000000100; const ldouble Epsilon = 1e-9; template<typename T, typename U> ostream& operator << (ostream& os, const pair<T,U>&p) {return os << "(" << p.X << "," << p.Y << ")"; } template<typename T> ostream& operator << (ostream &os, const vector<T>& V) { os << "["; FORW(i,0,size(V)) os << V[i] << ((i==size(V)-1) ? "" : ","); return os << "]"; } template<typename T> ostream& operator << (ostream &os, const set<T>& S) {os << "("; for(auto i: S) os << i << (i==*S.rbegin()?"":","); return os << ")"; } template<typename T, typename U> ostream& operator << (ostream &os, const map<T, U>& M){os << "{"; for(auto i: M) os << i << (i.X==M.rbegin()->X?"":","); return os << "}"; } template<typename T, typename F> T lbound(T p, T q, F f) { static_assert(is_integral<T>::value, "integral type required"); while(p < q) { T r = p+(q-p)/2; if(f(r)) q = r; else p = r+1; } return p; } template<typename T, typename F> T lboundl(T p, T q, F f) { static_assert(is_floating_point<T>::value, "floating point type required"); FOR(i,1,70) { T r = (p+q)/2; if(f(r)) q = r; else p = r; } return p; } template<typename T, typename U> bool contain(T t, U u) { return t.find(u) != t.end(); } template<typename T> int size(T t) { return t.size(); } const int MAXN = 111; int N, M; int A[MAXN]; int B[MAXN]; void read_data() { scanf("%d %d", &N, &M); FORW(i,0,N) scanf("%d", A+i); FORW(i,0,M) scanf("%d", B+i); } #define min_ele(s) (__builtin_ctzl(s)) int DP[2][1<<24]; int C[1<<24]; int T[24]; int x, y, w; int p, q; void rec(int k) { if(k == N) { if(C[p&(~q)] <= B[w] and DP[y][q]) { // cout << "W: " << w << " " << bitset<4>(p) << endl; DP[x][p] = true; } } else { p &= (~(1<<k)); q &= (~(1<<k)); rec(k+1); p |= (1<<k); rec(k+1); q |= (1<<k); rec(k+1); } } void solve() { sort(B,B+M); reverse(B,B+M); FORW(j,1,1<<N) C[j] = C[j^(1<<min_ele(j))] + A[min_ele(j)]; int result = -1; DP[0][0] = DP[1][0] = true; FORW(i,0,M) { w = i; x = i%2; y = (i+1)%2; rec(0); if(DP[x][(1<<N)-1]) { result = i+1; break; } } if(result == -1) printf("NIE\n"); else printf("%d\n", result); } int main() { read_data(); solve(); return 0; } |