#include<cstdio>
#include<algorithm>
#include<set>
using namespace std;
int A[102];
int C[102];
int main()
{
int n,m;
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,greater<int>());
sort(A,A+n,greater<int>());
int ans = 0;
for (int i = 1; i <= n; i++)
{
set<int> S (C,C+i);
bool ok = true;
set<int>::iterator it;
int x = 0;
for (int j = 0; j < n; j++)
{
it = lower_bound(S.begin(),S.end(),A[j]);
if (it == S.end()) {ok = false; break;}
else
{
x = *it;
S.erase(it);
x -= A[j];
if (x > 0) S.insert(x);
}
}
if (ok) {ans = i; break;}
}
if (ans == 0) puts("NIE");
else printf("%d\n",ans);
//system("pause");
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 | #include<cstdio> #include<algorithm> #include<set> using namespace std; int A[102]; int C[102]; int main() { int n,m; 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,greater<int>()); sort(A,A+n,greater<int>()); int ans = 0; for (int i = 1; i <= n; i++) { set<int> S (C,C+i); bool ok = true; set<int>::iterator it; int x = 0; for (int j = 0; j < n; j++) { it = lower_bound(S.begin(),S.end(),A[j]); if (it == S.end()) {ok = false; break;} else { x = *it; S.erase(it); x -= A[j]; if (x > 0) S.insert(x); } } if (ok) {ans = i; break;} } if (ans == 0) puts("NIE"); else printf("%d\n",ans); //system("pause"); return 0; } |
English