#include "bits/extc++.h"
using namespace std;
template <typename T>
void dbgh(const T& t) {
cerr << t << endl;
}
template <typename T, typename... U>
void dbgh(const T& t, const U&... u) {
cerr << t << " | ";
dbgh(u...);
}
#ifdef DEBUG
#define dbg(...) \
cerr << "L" << __LINE__ << " [" << #__VA_ARGS__ << "]" \
<< ": "; \
dbgh(__VA_ARGS__)
#else
#define cerr \
if (false) \
cerr
#define dbg(...)
#endif
#define endl "\n"
#define long int64_t
#define sz(x) int((x).size())
const int maxn = 305, maxm = 2 * maxn * maxn, ninf = -1e9;
struct DS {
vector<pair<int, int>> vals;
void insert(int i, int x) {
vals.emplace_back(i, x);
}
int query(int q) const {
int ans = 1e9;
for (auto& [i, x] : vals) {
if (x >= q) {
ans = min(ans, i);
}
}
return ans;
}
};
void solve() {
int n, m, k;
cin >> n >> m >> k;
int arr[n], brr[m];
for (auto& a : arr) {
cin >> a;
}
for (auto& a : brr) {
cin >> a;
}
for (auto& a : arr) {
if (a < k) {
cout << "Impossible" << endl;
return;
}
}
int c[maxm];
fill(begin(c), end(c), ninf);
c[0] = 0;
for (auto& a : brr) {
if (a < n) {
continue;
}
for (int i = maxm - 1; i >= a; i--) {
c[i] = max(c[i], 1 + c[i - a]);
}
}
bitset<maxm> d;
d.set(0);
for (auto& a : brr) {
if (a >= n) {
continue;
}
d |= d << a;
}
int ans = 1e9, asum = accumulate(arr, arr + n, 0);
DS ds;
auto ins = [&](int j) -> void {
if (0 <= j && j < maxm && c[j] >= 0) {
dbg(j, c[j]);
ds.insert(j, n * c[j]);
}
};
for (int i = asum + 1; i < maxm; i++) {
ins(i);
}
for (int i = 0; i < maxm; i++) {
ins(asum - i);
if (d[i]) {
int j = ds.query(k * n - i);
if (j < maxm) {
ans = min(ans, j + i - asum);
}
}
}
if (ans < int(1e9)) {
cout << ans << endl;
} else {
cout << "Impossible" << endl;
}
}
int main() {
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
cin.exceptions(ios::failbit);
solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1108 KB |
Output is correct |
2 |
Correct |
2 ms |
1108 KB |
Output is correct |
3 |
Correct |
1 ms |
1108 KB |
Output is correct |
4 |
Correct |
2 ms |
1112 KB |
Output is correct |
5 |
Correct |
1 ms |
1108 KB |
Output is correct |
6 |
Correct |
1 ms |
1108 KB |
Output is correct |
7 |
Correct |
1 ms |
1108 KB |
Output is correct |
8 |
Correct |
1 ms |
1108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1108 KB |
Output is correct |
2 |
Correct |
2 ms |
1108 KB |
Output is correct |
3 |
Correct |
1 ms |
1108 KB |
Output is correct |
4 |
Correct |
2 ms |
1112 KB |
Output is correct |
5 |
Correct |
1 ms |
1108 KB |
Output is correct |
6 |
Correct |
1 ms |
1108 KB |
Output is correct |
7 |
Correct |
1 ms |
1108 KB |
Output is correct |
8 |
Correct |
1 ms |
1108 KB |
Output is correct |
9 |
Correct |
4 ms |
1108 KB |
Output is correct |
10 |
Correct |
4 ms |
1108 KB |
Output is correct |
11 |
Correct |
3 ms |
1108 KB |
Output is correct |
12 |
Correct |
2 ms |
1112 KB |
Output is correct |
13 |
Correct |
2 ms |
1116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1108 KB |
Output is correct |
2 |
Correct |
3 ms |
1108 KB |
Output is correct |
3 |
Correct |
4 ms |
1108 KB |
Output is correct |
4 |
Correct |
27 ms |
1396 KB |
Output is correct |
5 |
Correct |
8 ms |
1108 KB |
Output is correct |
6 |
Correct |
3 ms |
1108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
1108 KB |
Output is correct |
2 |
Correct |
4 ms |
1108 KB |
Output is correct |
3 |
Correct |
3 ms |
992 KB |
Output is correct |
4 |
Correct |
2 ms |
1108 KB |
Output is correct |
5 |
Correct |
1 ms |
1108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1108 KB |
Output is correct |
2 |
Correct |
2 ms |
1108 KB |
Output is correct |
3 |
Correct |
1 ms |
1108 KB |
Output is correct |
4 |
Correct |
2 ms |
1112 KB |
Output is correct |
5 |
Correct |
1 ms |
1108 KB |
Output is correct |
6 |
Correct |
1 ms |
1108 KB |
Output is correct |
7 |
Correct |
1 ms |
1108 KB |
Output is correct |
8 |
Correct |
1 ms |
1108 KB |
Output is correct |
9 |
Correct |
4 ms |
1108 KB |
Output is correct |
10 |
Correct |
4 ms |
1108 KB |
Output is correct |
11 |
Correct |
3 ms |
1108 KB |
Output is correct |
12 |
Correct |
2 ms |
1112 KB |
Output is correct |
13 |
Correct |
2 ms |
1116 KB |
Output is correct |
14 |
Correct |
3 ms |
1108 KB |
Output is correct |
15 |
Correct |
3 ms |
1108 KB |
Output is correct |
16 |
Correct |
4 ms |
1108 KB |
Output is correct |
17 |
Correct |
27 ms |
1396 KB |
Output is correct |
18 |
Correct |
8 ms |
1108 KB |
Output is correct |
19 |
Correct |
3 ms |
1108 KB |
Output is correct |
20 |
Correct |
7 ms |
1108 KB |
Output is correct |
21 |
Correct |
4 ms |
1108 KB |
Output is correct |
22 |
Correct |
3 ms |
992 KB |
Output is correct |
23 |
Correct |
2 ms |
1108 KB |
Output is correct |
24 |
Correct |
1 ms |
1108 KB |
Output is correct |
25 |
Correct |
31 ms |
1108 KB |
Output is correct |
26 |
Correct |
37 ms |
1108 KB |
Output is correct |
27 |
Correct |
17 ms |
1492 KB |
Output is correct |
28 |
Correct |
26 ms |
1488 KB |
Output is correct |
29 |
Correct |
42 ms |
2248 KB |
Output is correct |
30 |
Correct |
28 ms |
1764 KB |
Output is correct |