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>
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
int n, m, k;
std::cin >> n >> m >> k;
std::vector<int> a(n), b(m);
for (int i = 0; i < n; i++) {
std::cin >> a[i];
}
for (int i = 0; i < m; i++) {
std::cin >> b[i];
}
int64_t suma = 0;
for (int i = 0; i < n; i++) {
suma += a[i];
}
int64_t sumb = 0;
for (int i = 0; i < m; i++) {
sumb += b[i];
}
if (k == 1) {
std::vector<std::vector<bool>> dp(m + 1, std::vector<bool>(sumb + 1, false));
dp[0][0] = 1;
for (int i = 1; i <= m; i++) {
for (int j = 0; j <= sumb; j++) {
dp[i][j] = max(dp[i][j], dp[i - 1][j]);
if (j >= b[i - 1]) {
dp[i][j] = max(dp[i][j], dp[i - 1][j - b[i - 1]]);
}
}
}
for (int j = suma; j <= sumb; j++) {
if (dp[m][j] == 1) {
std::cout << j - suma << "\n";
return 0;
}
}
std::cout << "Impossible\n";
return 0;
}
for (int i = 0; i < n; i++) {
if (a[i] < k) {
std::cout << "Impossible\n";
return 0;
}
}
int64_t ans = LLONG_MAX;
for (int mask = 0; mask < (1 << m); mask++) {
std::vector<int> chefs;
int64_t sum = 0;
for (int i = 0; i < m; ++i) {
if (mask & (1 << i)) {
chefs.push_back(b[i]);
sum += b[i];
}
}
if (sum < suma) {
continue;
}
std::sort(chefs.begin(), chefs.end());
/**
std::vector<int> towers(n), mars(n);
int ptr = 0;
int delta = 0;
for (auto x : chefs) {
mars[ptr]++;
if (ptr + x < n) {
mars[ptr + x]--;
}
while (ptr < n && delta + mars[ptr] == k) {
delta += mars[ptr++];
}
if (ptr == n) {
continue;
}
}
if (ptr == n) {
ans = std::min(ans, sum - suma);
}
**/
std::vector<int> towers(n);
for (auto x : chefs) {
int rem = x;
for (int i = 0; i < n; i++) {
if (towers[i] == k) {
continue;
}
towers[i]++;
rem--;
if (rem == 0) {
continue;
}
}
}
bool bad = 0;
for (int i = 0; i < n; i++) {
if (towers[i] != k) {
bad = 1;
}
}
if (bad == 0) {
ans = std::min(ans, sum - suma);
}
}
if (ans != LLONG_MAX) {
std::cout << ans << "\n";
return 0;
}
std::cout << "Impossible\n";
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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |