Submission #643713

#TimeUsernameProblemLanguageResultExecution timeMemory
643713ParsaSKitchen (BOI19_kitchen)C++14
100 / 100
19 ms992 KiB
// In the name of God
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
typedef long long ll;
const int N = 300 + 5, MOD = 1e9 + 7;
int n, A[N], B[N], k, m;
bitset<N * N> dp;
int dp2[N * N], suf[N * N + 10];

void solve() {
    cin >> n >> m >> k;
    for (int i = 0; i < n; i++)
        cin >> A[i];
    for (int i = 0; i < m; i++)
        cin >> B[i];
    if (*min_element(A, A + n) < k) {
        cout << "Impossible" << endl;
        return;
    }
    vector<int> b, vec;
    for (int i = 0; i < m; i++) {
        if (B[i] < n)
            b.pb(B[i]);
        else
            vec.pb(B[i]);
    }
    dp[0] = true;
    for (auto x : b) {
        dp |= dp << x;
    }
    fill(dp2, dp2 + N * N, -1e9);
    dp2[0] = 0;
    for (auto x : vec) {
        for (int s = N * N - 1; s >= x; s--) {
            dp2[s] = max(dp2[s - x] + 1, dp2[s]);
        }
    }
    suf[N * N] = -1;
    for (int i = N * N - 1; i >= 0; i--) {
        suf[i] = suf[i + 1];
        if (dp[i])
            suf[i] = i;
    }
    int sumA = accumulate(A, A + n, 0);
    int ans = 1e9 + 5;
    for (int s = 0; s < N * N; s++) {
        if (dp2[s] >= 0 && suf[max(0, max(sumA - s, n * (k - dp2[s])))] != -1) {
            ans = min(ans, s + suf[max(0, max(sumA - s, n * (k - dp2[s])))] - sumA);
        }
    }
    if (ans < 1e9) {
        cout << ans << endl;
    }
    else
        cout << "Impossible\n";
}

int32_t main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    solve();

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...