이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//we are all lost trying to be someone.
#include <bits/stdc++.h>
#define fastio ios_base::sync_with_stdio(0); cin.tie(0);
#define sz(x) ll(x.size())
#define reve(x) reverse(x.begin(),x.end())
#define ff first
#define ss second
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> ii;
const ll MAX=1e5+10;
const ll mod=1e9+7;
const ll inf=1e18+7;
vector<ll>s;
ll A[1010],B[1010];
map<ll,vector<ll>> masks;
map<ll,vector<ll>> c;
ll dp[21][(1<<20)+1000];
ll n, m;
void prin(ll x){
for(ll i=m-1; i>=0; i--){
cout<<((x>>i)&1);
}
cout<<endl;
return;
}
bool DP(ll i, ll mask){
if(mask==0 and i>0)return 0;
if(i<0 or mask==0)return 1;
if(dp[i][mask]!=-1)return dp[i][mask];
dp[i][mask]=0;
for(ll x: masks[s[i]]){
if((mask&x)==x){
if(i==0)dp[i][mask]=1;
dp[i][mask]=dp[i][mask] or DP(i-1,mask^x);
}
}
return dp[i][mask];
}
void solve(){
cin>>n>>m;
memset(dp, -1, sizeof dp);
for(ll i=0; i<n; i++){
ll a;
cin>>a;
s.push_back(a);
c[a].push_back(i);
}
for(ll i=0; i<m; i++){
cin>>B[i];
}
sort(s.begin(),s.end());
for(ll mask=0; mask<(1<<m); mask++){
ll sum=0;
for(ll j=0; j<m; j++){
if((mask>>j)&1){
sum+=B[j];
}
}
ll pos=lower_bound(s.begin(),s.end(),sum)-s.begin();
if(pos<n and s[pos]==sum){
masks[s[pos]].push_back(mask);
for(ll it : c[s[pos]]) dp[it][mask]=1;
}
}
if(DP(n-1,(1<<m)-1)){
cout<<"YES"<<endl;
}
else cout<<"NO"<<endl;
}
int main(){
fastio;
solve();
return 0;
}
# | 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... |