Submission #797352

#TimeUsernameProblemLanguageResultExecution timeMemory
797352tch1cherinKitchen (BOI19_kitchen)C++17
100 / 100
42 ms1384 KiB
#include <bits/stdc++.h>
using namespace std;
 
const int MAX_S = 305 * 305;
 
int main() {
  cin.tie(nullptr)->sync_with_stdio(false);
  int N, M, K;
  cin >> N >> M >> K;
  vector<int> A(N), B(M);
  for (int &v : A) {
    cin >> v;
    if (v < K) {
      cout << "Impossible\n";
      exit(0);
    }
  }
  for (int &v : B) {
    cin >> v;
  }
  int SA = accumulate(A.begin(), A.end(), 0);
  bitset<MAX_S> bs;
  vector<int> dp(MAX_S, -1e9);
  dp[0] = 0;
  bs[0] = 1;
  for (int i = 0; i < M; i++) {
    if (B[i] < N) {
      bs |= bs << B[i];
    } else {
      vector<int> new_dp = dp;
      for (int j = 0; j + B[i] < MAX_S; j++) {
        new_dp[j + B[i]] = max(new_dp[j + B[i]], dp[j] + 1);  
      }
      dp = new_dp;
    }
  }
  vector<int> reachable;
  for (int i = 0; i < MAX_S; i++) {
    if (bs[i]) {
      reachable.push_back(i);
    }
  }
  int ans = INT_MAX;
  for (int j = 0; j < MAX_S; j++) {
    if (dp[j] >= 0) {
      auto it = lower_bound(reachable.begin(), reachable.end(), max(N * (K - dp[j]), SA - j));
      if (it != reachable.end()) {
        ans = min(ans, *it + j - SA);
      }
    }
  }
  if (ans == INT_MAX) {
    cout << "Impossible\n";
  } else {
    cout << ans << "\n";
  }
}
#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...