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 <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int N, M, K, S, A[309], B[309];
bool dp[309][90009];
bool dp1[49][1609][1609];
int solve(vector<int>R) {
int s1 = 0, s2 = 0;
for (int i = 0; i < R.size(); i++) {
s1 += min(N, R[i]);
s2 += R[i];
}
if (s1 < N * K) return (1 << 30);
if (s2 < S) return (1 << 30);
return s2 - S;
}
int solve_subtask3() {
dp[0][0] = true;
for (int i = 0; i < M; i++) {
for (int j = 0; j <= 90000; j++) {
if (dp[i][j] == false) continue;
dp[i + 1][j] = true;
dp[i + 1][j + B[i]] = true;
}
}
for (int i = S; i <= 90000; i++) {
if (dp[M][i] == true) return i - S;
}
return (1 << 30);
}
int solve_subtask4() {
dp1[0][0][0] = true;
for (int i = 0; i < M; i++) {
for (int j = 0; j <= 1600; j++) {
for (int k = 0; k <= 1600; k++) {
if (dp1[i][j][k] == false) continue;
dp1[i + 1][j][k] = true;
dp1[i + 1][j + min(N, B[i])][k + B[i]] = true;
}
}
}
for (int i = S; i <= 1600; i++) {
for (int j = N * K; j <= 1600; j++) {
if (dp1[M][j][i] == true) return i - S;
}
}
return (1 << 30);
}
int main() {
cin >> N >> M >> K;
for (int i = 0; i < N; i++) {
cin >> A[i]; S += A[i];
if (A[i] < K) {
cout << "Impossible" << endl;
return 0;
}
}
for (int i = 0; i < M; i++) cin >> B[i];
if (K == 1) {
int ans = solve_subtask3();
if (ans == (1 << 30)) cout << "Impossible" << endl;
else cout << ans << endl;
}
else if (M <= 15) {
int ans = (1 << 30);
for (int i = 0; i < (1 << M); i++) {
vector<int> vec;
for (int j = 0; j < M; j++) {
if ((i / (1 << j)) % 2 == 1) vec.push_back(B[j]);
}
int ret = solve(vec);
ans = min(ans, ret);
}
if (ans == (1 << 30)) cout << "Impossible" << endl;
else cout << ans << endl;
}
else {
int ans = solve_subtask4();
if (ans == (1 << 30)) cout << "Impossible" << endl;
else cout << ans << endl;
}
return 0;
}
Compilation message (stderr)
kitchen.cpp: In function 'int solve(std::vector<int>)':
kitchen.cpp:12:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < R.size(); i++) {
~~^~~~~~~~~~
# | 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... |