#include <bits/stdc++.h>
#define printv(a, b) {\
for(auto pv : a) b << pv << " ";\
b << "\n";\
}
using namespace std;
typedef long long ll;
const ll MAX = INT_MAX;
void nosol(){
cout << "Impossible\n";
exit(0);
}
int n, m, k;
vector<int> a;
vector<int> b;
ll sa = 0;
const int SZ = 300;
ll calc(int h){
vector<vector<ll>> dp(m + 2, vector<ll>((m + 1) * SZ + 10, -MAX));
dp[0][0] = 0;
for(int i = 1; i <= m; i++){
for(int j = i; j >= 0; j--){
for(int s = j * SZ; s >= 0; s--){
if(dp[j][s] == -MAX) continue;
ll tmp = dp[j][s] + min(b[i], h + 1);
int nxt = j + (b[i] >= h);
dp[nxt][s + b[i]] = max(dp[nxt][s + b[i]], tmp);
}
}
}
ll ans = MAX;
for(int i = k; i <= m; i++){
for(int j = sa; j <= m * SZ; j++){
if(dp[i][j] >= n * k) ans = min(ans, j - sa);
}
}
return ans;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cerr.tie(0);
cin >> n >> m >> k;
a.resize(n + 1);
b.resize(m + 1);
ll sb = 0;
for(int i = 1; i <= n; i++) cin >> a[i], sa += a[i];
for(int i = 1; i <= m; i++) cin >> b[i], sb += b[i];
sort(a.begin() + 1, a.end(), greater<>());
sort(b.begin() + 1, b.end(), greater<>());
if(k > m || sa > sb){
nosol();
}
for(int i = 1; i <= n; i++) if(a[i] < k) nosol();
ll ans = MAX;
for(int i = 0; i <= SZ; i++){
ans = min(ans, calc(i));
}
if(ans == MAX) nosol();
cout << ans << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
121 ms |
980 KB |
Output is correct |
10 |
Correct |
106 ms |
992 KB |
Output is correct |
11 |
Correct |
94 ms |
1584 KB |
Output is correct |
12 |
Correct |
98 ms |
988 KB |
Output is correct |
13 |
Correct |
107 ms |
972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1088 ms |
163184 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1084 ms |
4644 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
121 ms |
980 KB |
Output is correct |
10 |
Correct |
106 ms |
992 KB |
Output is correct |
11 |
Correct |
94 ms |
1584 KB |
Output is correct |
12 |
Correct |
98 ms |
988 KB |
Output is correct |
13 |
Correct |
107 ms |
972 KB |
Output is correct |
14 |
Execution timed out |
1088 ms |
163184 KB |
Time limit exceeded |
15 |
Halted |
0 ms |
0 KB |
- |