이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
const ll INF = numeric_limits<ll>::max();
const int inf = numeric_limits<int>::max();
const char nl = '\n', sp = ' ';
char f(int id, int mask, ll val, int N, vector<ll> &salary, vector<ll> &billz, vector<vector<char> > &dp){
if(dp[mask][val] != 'n'){
return dp[mask][val];
} else {
if(id == N){
return '1';
} else if(val == 0){
return f(id + 1,mask,salary[id + 1], N, salary, billz, dp);
} else {
int mx = upper_bound(billz.begin(),billz.end(), val) - billz.begin();
char flag = '0';
for(int i = 0; i < mx; i++){
if((mask & (1 << i)) != 0){
if(val - billz[i] >= 0){
char retv = f(id, mask ^ (1 << i),val - billz[i],N, salary,billz, dp);
if(retv == '1'){
flag = '1';
}
}
}
}
dp[mask][val] = flag;
return dp[mask][val];
}
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, M;
cin >> N >> M;
vector<vector<char> > dp((1 << M), vector<char>(1000,'n'));
vector<ll> salary;
vector<ll> billz;
ll num;
for(int i = 0; i < N; i++){
cin >> num;
salary.push_back(num);
}
for(int i = 0; i < M; i++){
cin >> num;
billz.push_back(num);
}
sort(billz.begin(),billz.end());
int mask = (1 << M) - 1;
char ans = f(0,mask,salary[0], N ,salary, billz, dp);
cout << ans << nl;
}
# | 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... |