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>
#define int long long
#define M 302
using namespace std;
int n, m, k, A = 0, B = 0, res = -1;
bool flag = false;
vector<int> a, b;
vector<vector<bool>> dp;
signed main() {
cin >> n >> m >> k;
a = vector<int>(n), b = vector<int>(m);
for(int i = 0; i < n; i++) {
cin >> a[i];
A += a[i];
if(a[i] < k) flag = true;
}
for(int i = 0; i < m; i++) {
cin >> b[i];
B += b[i];
}
if(k > 1 || m <= 15) {
// try every subset
vector<int> diff;
bitset<M> bin;
int sus, sum;
bool check;
for(int i = 0; i < (1 << m); i++) {
sum = 0, sus = 0;
for(int j = 0; j < m; j++) {
if(i & (1 << j)) {
sum += b[j];
sus += min(n, b[j]);
}
}
if(sum >= A && sus >= n * k) {
if(res == -1) res = sum - A;
else res = min(res, sum - A);
}
}
if(flag || res == -1) cout << "Impossible\n";
else cout << res << "\n";
} else {
dp = vector<vector<bool>>(m + 1, vector<bool>(B + 1, false));
dp[0][0] = true;
for(int i = 1; i <= m; i++) {
for(int j = 0; j <= B; j++) {
if(j >= b[i - 1]) dp[i][j] = dp[i - 1][j] | dp[i - 1][j - b[i - 1]];
//cout << dp[i][j] << " ";
}
//cout << "\n";
}
//
int res = LLONG_MAX;
for(int i = 0; i <= m; i++) {
for(int j = A; j <= B; j++) {
if(dp[i][j]) res = min(res, j - A);
}
}
if(res == LLONG_MAX) cout << "Impossible\n";
else cout << res << "\n";}
}
Compilation message (stderr)
kitchen.cpp: In function 'int main()':
kitchen.cpp:29:10: warning: unused variable 'check' [-Wunused-variable]
29 | bool check;
| ^~~~~
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |