이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
void fast(){ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);}
int main(){
fast();
int n, m; cin>>n>>m;
int a[n], b[m];
for(int i=0; i<n; i++) cin>>a[i];
for(int i=0; i<m; i++) cin>>b[i];
int k=(1<<m);
vector<int> cov(k, -1), last(k, -1);
cov[0]=last[0]=0; //cover, last sum
for(int bit=0; bit<k; bit++){
for(int i=0; i<m; i++){
if(!(bit&(1<<i))) continue;
int prev=bit & ~(1<<i);
if(cov[prev]==-1) continue;
int new_sum=last[prev]+b[i], target=a[cov[prev]];
if(new_sum<target){
cov[bit]=cov[prev];
last[bit]=new_sum;
}
else if(new_sum==target){
cov[bit]=cov[prev]+1;
last[bit]=0;
}
}
if(cov[bit]==n){
cout<<"YES";
return 0;
}
}
cout<<"NO";
}
/*
const int N=2e4;
int main(){
fast();
int n, m; cin>>n>>m;
int a[n+1], b[m+1];
for(int i=1; i<=n; i++) cin>>a[i];
for(int i=1; i<=m; i++) cin>>b[i];
if(n==1){
vector<bool> dp(N+1);
dp[0]=true;
for(int i=1; i<=m; i++){
for(int j=a[1]; j>=1; j--){
if(j>=b[i] && dp[j-b[i]]) dp[j]=true;
}
}
if(dp[a[1]]) cout<<"YES";
else cout<<"NO";
}
}
*/
/*
#define int long long
const int INF=-1e18;
struct info{int x, y, z;};
bool cmp(info a, info b){
if(a.y!=b.y) return a.y>b.y;
return a.z<b.z;
}
signed main(){
fast();
vector<info> v;
int n, m=0; cin>>n;
for(int i=1; i<=n; i++){
int x, y, z; cin>>x>>y>>z;
v.push_back({x, y, -z});
m+=x;
}
cin>>n;
for(int i=1; i<=n; i++){
int x, y, z; cin>>x>>y>>z;
v.push_back({-x, y, z});
}
sort(v.begin(), v.end(), cmp);
vector<int> dp(m+1, INF);
dp[0]=0;
for(auto [x, y, z]:v){
vector<int> dp2=dp;
for(int i=0; i<=m; i++){
int next=i+x;
if(next>=0 && next<=m && dp[i]!=INF){
dp2[next]=max(dp2[next], dp[i]+z);
}
}
dp=dp2;
}
cout<<*max_element(dp.begin(), dp.end());
}
*/
# | 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... |