This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int N = 20;
int dp[1<<N], rest[1<<N];
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
int a[n];
for(int i=0; i< n; i++)cin >> a[i];
int b[m];
for(int i=0; i < m; i++)cin >> b[i];
sort(a, a+n);
sort(b, b+m);
int mask=1<<m;
rest[0]=a[0];
for(int i=1; i<mask; i++){
int bestdp=0;
int bestrest=10000;
for(int j=0; j< m; j++){
int bit=1<<j;
if(i&bit){
if(rest[i^bit]==b[j]){
if(bestdp<=dp[i^bit]){
bestdp=dp[i^bit]+1;
if(bestdp==n){
cout << "YES\n";
return 0;
}
else{
bestrest=a[bestdp];
}
}
}
else if(rest[i^bit]>b[j]){
if(bestdp==dp[i^bit]){
bestrest=min(bestrest, rest[i^bit]-b[j]);
}
else if(bestdp<dp[i^bit]){
bestdp=dp[i^bit];
bestrest=rest[i^bit]-b[j];
}
}
}
}
dp[i]=bestdp;
rest[i]=min(bestrest, a[dp[i]]);
}
cout << "NO\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |