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;
int n, m;
int getSum(int mask, int banknotes[]){
int ans = 0;
for(int i = 0; i < m; ++i){
if(mask & (1 << i) ) ans+= banknotes[i];
}
return ans;
}
int CanWePay(int person, int mask, vector<vector<int>>& DP, unordered_map<int, vector<int>>& WaysToPay, int salaries[]){
if(person == n) return true;
if(mask == ((1<<n)-1)) return false;
if(DP[mask][person] != -1) return DP[mask][person];
DP[mask][person] = 0;
for(int payMask: WaysToPay[salaries[person]]){
if(!(mask & payMask)){
if(CanWePay(person+1, mask|payMask, DP, WaysToPay, salaries)){
DP[mask][person] = 1;
break;
}
}
}
return DP[mask][person];
}
int main(){
cin.tie(NULL);
ios::sync_with_stdio(false);
int MaxSal;
cin >> n >> m;
int salaries[n];
int banknotes[m];
cin >> salaries[0];
MaxSal = salaries[0];
for(int i = 1; i < n; ++i) {
cin >> salaries[i];
MaxSal = max(MaxSal, salaries[i]);
}
for(int i = 0; i < m; ++i) cin >> banknotes[i];
unordered_map <int, vector<int>> WaysToArrive;
WaysToArrive.reserve(MaxSal);
for(int mask = 1; mask < (1<<m); ++mask){
int SumOfNotes = getSum(mask,banknotes);
if(SumOfNotes > MaxSal) continue;
if(WaysToArrive.find(SumOfNotes)== WaysToArrive.end()) WaysToArrive[SumOfNotes] = {mask};
else WaysToArrive[SumOfNotes].push_back(mask);
}
vector<vector<int>> DP(1<<m, vector<int>(n, -1));
if(CanWePay(0, 0, DP, WaysToArrive, salaries)) cout << "YES" << endl;
else cout << "NO" << endl;
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... |