#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define INF 1000000000
int main(){
ll n, m, k, su = 0, bsu = 0;
cin>>n>>m>>k;
bool ca = 0;
if (m < k){
ca = 1;
}
vector<ll> a(n + 1), b(m + 1);
for (int i = 0; i< n; i++){
cin>>a[i + 1];
su += a[i + 1];
if (a[i + 1] < k){
ca = 1;
}
}
for (int i = 0; i < m; i++){
cin>>b[i + 1];
bsu += b[i + 1];
}
if (ca || bsu < su){
cout<<"Impossible";
return 0;
}
ll ans = INF;
if (m == 1){
ans = b[1] - su;
}
else if (m == 2){
if (k == 1 && max(b[1], b[2]) >= su){
if (b[1] >= su){
ans = min(ans, b[1] - su);
}
if (b[2] >= su){
ans = min(ans, b[2] - su);
}
}
else{
ans = bsu - su;
}
}
else{
vector<vector<ll>> dp(m + 1, vector<ll>(m + 1, 0));
for (int i = 1; i <= m; i++){
for (int j = 1; j <= i; j++){
ll va1 = dp[i - 1][j - 1] + b[j], va2 = dp[i - 1][j];
if (va1 > va2){
swap(va1, va2);
}
if (dp[i][j] < su){
dp[i][j] = max(dp[i][j], va2);
}
else if (dp[i][j] >= su && va1 >= su){
dp[i][j] = min(dp[i][j], va1);
}
else if (dp[i][j] >= su && va2 >= su){
dp[i][j] = min(dp[i][j], va2);
}
}
}
ll ans = INF;
for (int i = 1; i <= m; i++){
if (dp[m][i] >= su) ans = min(ans, dp[m][i] - su);
}
}
cout<<ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
860 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |