#include<cstdio> #include<algorithm> #include<vector> #include<set> #include<queue> #include<stack> #include<cmath> #include<map> using namespace std; typedef pair<int,int> PI; typedef long long LL; typedef double D; #define FI first #define SE second #define MP make_pair #define PB push_back #define R(I,N) for(int I=0;I<N;I++) #define F(I,A,B) for(int I=A;I<B;I++) #define FD(I,N) for(int I=N-1;I>=0;I--) #define make(A) scanf("%d",&A) #define MAX 24 int n,m,a[MAX],pl[MAX*5]; PI dp[1<<MAX]; main(){ make(n);make(m); R(i,n)make(a[i]); R(i,m)make(pl[i]); sort(pl,pl+m,greater<int>()); F(i,1,(1<<n)){ PI naj = MP(m+1,0); int pom = i; while(pom){ int ak = __builtin_ctz(pom); PI nowy = dp[i-(1<<ak)]; if(nowy.SE + a[ak] <= 0) nowy.SE += a[ak]; else{ nowy.SE = a[ak] - pl[nowy.FI]; if(nowy.SE > 0){ pom -= (1<<ak); continue; } nowy.FI++; } naj = min(naj,nowy); pom -= (1<<ak); } dp[i] = naj; } if(dp[(1<<n)-1].FI == m+1) puts("NIE"); else printf("%d\n",dp[(1<<n)-1].FI); }
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 | #include<cstdio> #include<algorithm> #include<vector> #include<set> #include<queue> #include<stack> #include<cmath> #include<map> using namespace std; typedef pair<int,int> PI; typedef long long LL; typedef double D; #define FI first #define SE second #define MP make_pair #define PB push_back #define R(I,N) for(int I=0;I<N;I++) #define F(I,A,B) for(int I=A;I<B;I++) #define FD(I,N) for(int I=N-1;I>=0;I--) #define make(A) scanf("%d",&A) #define MAX 24 int n,m,a[MAX],pl[MAX*5]; PI dp[1<<MAX]; main(){ make(n);make(m); R(i,n)make(a[i]); R(i,m)make(pl[i]); sort(pl,pl+m,greater<int>()); F(i,1,(1<<n)){ PI naj = MP(m+1,0); int pom = i; while(pom){ int ak = __builtin_ctz(pom); PI nowy = dp[i-(1<<ak)]; if(nowy.SE + a[ak] <= 0) nowy.SE += a[ak]; else{ nowy.SE = a[ak] - pl[nowy.FI]; if(nowy.SE > 0){ pom -= (1<<ak); continue; } nowy.FI++; } naj = min(naj,nowy); pom -= (1<<ak); } dp[i] = naj; } if(dp[(1<<n)-1].FI == m+1) puts("NIE"); else printf("%d\n",dp[(1<<n)-1].FI); } |