#include <algorithm>
#include <iostream>
#include <cstdio>
#include <utility>
#include <set>
#include <map>
#include <queue>
using namespace std;
typedef unsigned int LL;
#define FOR(x,b,e) for(int x=b; x<=(e); ++x)
#define REP(x,n) for(int x=0; x<(n); ++x)
#define FORD(x,b,e) for(int x=b; x>=(e); --x)
typedef pair<LL,LL> PII;
typedef pair<int,pair<int,int> > PIII;
#define MP make_pair
#define MAXN 25
#define MAXM 101
#define ST first
#define ND second
int n,m;
int N[MAXN];
int M[MAXM];
int vals[20000000];
priority_queue<PII> qq;
int inds[20000000];
inline int getInd(LL i){
return i /100000000;
}
inline int getVal(LL i){
return i %100000000;
}
inline LL toCos(int ind,int val){
//printf("tocos %lld %lld\n", ind,val);
return 100000000*(LL)ind+(LL)val;
}
void printBin(int i){
REP(x,n){
printf("%d",i%2);
i = i >> 1;
}
printf("\n");
}
int main() {
scanf("%d%d",&n,&m);
int a,b;
REP(x,n)scanf("%d",&N[x]);
REP(x,m)scanf("%d",&M[x]);
sort(N,N+n);
sort(M,M+m);
REP(x,n)inds[1<<x]=x;
/*
REP(x,n)printf("%d ",N[x]);
printf("\n");
REP(x,m)printf("%d ",M[x]);
printf("\n");
*/
for(int i = 0,j = m-1; i<j; j--,i++)
swap(M[i],M[j]);
m = min(n,m);
int full = (1<<n)-1;
qq.push(MP(0,full));
vals[full]=0;
int p,q,c;
LL cos;
PII current;
int it = 0,ind,val,r,z,xx;
while(!qq.empty()){
it++;
//if(it%10000==0)
//printf("%d\n",it);
current = qq.top();
qq.pop();
//printf("Current %d %d ",getInd(-current.ST), getVal(-current.ST));
//printBin(current.ND);
if(current.ND==0){
int res = 0;
res = (-current.ST)/100000000;
printf("%d\n",res+1);
return 0;
} else {
ind = (-current.ST)/100000000;
val = (-current.ST) %100000000;
p = current.ND;
//REP(x,n)if(p&(1<<x)){
r = p;
// printf("p %d\n",r);
while(r>0&&r^(r-1)>0){
z = r-(r&(r-1));
//printf("z %d\n",z);
r = r^z;
//printf("r %d\n",r);
xx = inds[z];
//printf("xx %d\n",xx);
//{
q = p^z;
// printf("q "); printBin(q);
if(val+N[xx]<=M[ind])cos = 100000000*(LL)ind+(LL)val+N[xx];
else if(ind<m-1 && N[xx]<=M[ind+1])cos = 100000000*(LL)(ind+1)+(LL)N[xx];
else continue;
//printf("COS %d %d\n",getInd(cos),getVal(cos));
if(vals[q]==0||vals[q]>cos){
vals[q] = cos;
qq.push(MP(-cos,q));
}
}
}
}
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 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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | #include <algorithm> #include <iostream> #include <cstdio> #include <utility> #include <set> #include <map> #include <queue> using namespace std; typedef unsigned int LL; #define FOR(x,b,e) for(int x=b; x<=(e); ++x) #define REP(x,n) for(int x=0; x<(n); ++x) #define FORD(x,b,e) for(int x=b; x>=(e); --x) typedef pair<LL,LL> PII; typedef pair<int,pair<int,int> > PIII; #define MP make_pair #define MAXN 25 #define MAXM 101 #define ST first #define ND second int n,m; int N[MAXN]; int M[MAXM]; int vals[20000000]; priority_queue<PII> qq; int inds[20000000]; inline int getInd(LL i){ return i /100000000; } inline int getVal(LL i){ return i %100000000; } inline LL toCos(int ind,int val){ //printf("tocos %lld %lld\n", ind,val); return 100000000*(LL)ind+(LL)val; } void printBin(int i){ REP(x,n){ printf("%d",i%2); i = i >> 1; } printf("\n"); } int main() { scanf("%d%d",&n,&m); int a,b; REP(x,n)scanf("%d",&N[x]); REP(x,m)scanf("%d",&M[x]); sort(N,N+n); sort(M,M+m); REP(x,n)inds[1<<x]=x; /* REP(x,n)printf("%d ",N[x]); printf("\n"); REP(x,m)printf("%d ",M[x]); printf("\n"); */ for(int i = 0,j = m-1; i<j; j--,i++) swap(M[i],M[j]); m = min(n,m); int full = (1<<n)-1; qq.push(MP(0,full)); vals[full]=0; int p,q,c; LL cos; PII current; int it = 0,ind,val,r,z,xx; while(!qq.empty()){ it++; //if(it%10000==0) //printf("%d\n",it); current = qq.top(); qq.pop(); //printf("Current %d %d ",getInd(-current.ST), getVal(-current.ST)); //printBin(current.ND); if(current.ND==0){ int res = 0; res = (-current.ST)/100000000; printf("%d\n",res+1); return 0; } else { ind = (-current.ST)/100000000; val = (-current.ST) %100000000; p = current.ND; //REP(x,n)if(p&(1<<x)){ r = p; // printf("p %d\n",r); while(r>0&&r^(r-1)>0){ z = r-(r&(r-1)); //printf("z %d\n",z); r = r^z; //printf("r %d\n",r); xx = inds[z]; //printf("xx %d\n",xx); //{ q = p^z; // printf("q "); printBin(q); if(val+N[xx]<=M[ind])cos = 100000000*(LL)ind+(LL)val+N[xx]; else if(ind<m-1 && N[xx]<=M[ind+1])cos = 100000000*(LL)(ind+1)+(LL)N[xx]; else continue; //printf("COS %d %d\n",getInd(cos),getVal(cos)); if(vals[q]==0||vals[q]>cos){ vals[q] = cos; qq.push(MP(-cos,q)); } } } } printf("NIE\n"); return 0; } |
English