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;
typedef long long ll;
ll const INF = 1e8;
struct state{
int id = INF, left = INF, cnt = INF;
bool operator < (const state &other){
if(id == other.id){
if(cnt == other.cnt) {
return left < other.left;
}
return cnt < other.cnt;
}
return id < other.id;
}
};
int main(){
int n, m; cin >> n >> m;
vector<int> peo(n), cash(m);
for(auto &it : peo) cin >> it;
for(auto &it : cash) cin >> it;
sort(peo.rbegin(), peo.rend());
sort(cash.rbegin(), cash.rend());
vector<state> take(1 << m);
take[0].id = 0;
take[0].left = peo[0];
take[0].cnt = 0;
for(int mask = 0; mask < (1 << m); mask++){
for(int j = 0; j < m; j++){
if(mask & (1 << j)) continue;
state z = take[mask];
if(cash[j] <= z.left){
z.left -= cash[j];
z.cnt ++;
}
if(z.left == 0){
z.id ++;
z.left = (z.id < n ? peo[z.id] : 0);
}
if(z < take[mask | (1 << j)]){
take[mask | (1 << j)] = z;
}
}
}
bool f = 0;
for(int mask = 0; mask < (1 << m); mask ++){
if(take[mask].id == n && take[mask].left == 0){
f = 1;
}
}
if(f) cout << "YES";
else cout << "NO";
}
# | 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... |