#include <bits/stdc++.h>
using namespace std;
int main() {
int n, l, r, K;
cin >> n >> l >> r >> K;
vector<int> v(n+1);
for(int i=1; i<=n; i++) cin >> v[i];
int sum = 0;
for(int i=l; i<=r; i++) sum += v[i];
int dpL[l][2][K+1], dpR[2][n+2][K+1], mnL[n+1][K+1], mnR[n+1][K+1];
memset(dpR, 0, sizeof(dpR));
memset(dpL, 0, sizeof(dpL));
memset(mnL, 0, sizeof(mnL));
memset(mnR, 0, sizeof(mnR));
for(int j=l; j<=r; j++) {
for(int i=1; i<l; i++) {
for(int k=0; k<=K; k++) {
dpL[i][1][k] = min(dpL[i][1][k], dpL[i-1][1][k]);
dpL[i][1][k] = min(dpL[i][1][k], dpL[i][0][k]);
if(j - i <= k) dpL[i][1][k] = min(dpL[i][1][k], dpL[i-1][0][k-j+i] + v[i] - v[j]);
if(i == l-1) mnL[j][k] = dpL[i][1][k];
}
}
for(int i=1; i<l; i++) {
for(int k=0; k<=K; k++) {
dpL[i][0][k] = dpL[i][1][k];
dpL[i][1][k] = 0;
}
}
}
for(int i=r; i>=l; i--) {
for(int j=n; j>=r+1; j--) {
for(int k=0; k<=K; k++) {
dpR[1][j][k] = min(dpR[1][j][k], dpR[0][j][k]);
dpR[1][j][k] = min(dpR[1][j][k], dpR[1][j+1][k]);
if(j - i <= k) dpR[1][j][k] = min(dpR[1][j][k], dpR[0][j+1][k-j+i] + v[j] - v[i]);
if(j == r + 1) mnR[i][k] = dpR[1][j][k];
}
}
for(int j=n; j>=r+1; j--) {
for(int k=0; k<=K; k++) {
dpR[0][j][k] = dpR[1][j][k];
dpR[1][j][k] = 0;
}
}
}
int mn = min(mnL[r][K], mnR[l][K]);
for(int i=l; i<r; i++)
for(int j=0; j<=K; j++)
mn = min(mn, mnL[i][j] + mnR[i+1][K-j]);
cout << sum + mn << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
436 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
440 KB |
Output is correct |
7 |
Correct |
3 ms |
2904 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 |
436 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
440 KB |
Output is correct |
7 |
Correct |
3 ms |
2904 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
0 ms |
440 KB |
Output is correct |
13 |
Correct |
21 ms |
10448 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 |
436 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
440 KB |
Output is correct |
7 |
Correct |
3 ms |
2904 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
0 ms |
440 KB |
Output is correct |
13 |
Correct |
21 ms |
10448 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
352 KB |
Output is correct |
16 |
Correct |
1 ms |
352 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
20 ms |
10452 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 |
436 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
440 KB |
Output is correct |
7 |
Correct |
3 ms |
2904 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
0 ms |
440 KB |
Output is correct |
13 |
Correct |
21 ms |
10448 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
352 KB |
Output is correct |
16 |
Correct |
1 ms |
352 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
20 ms |
10452 KB |
Output is correct |
20 |
Correct |
2 ms |
1112 KB |
Output is correct |
21 |
Correct |
2 ms |
1628 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
7 ms |
9052 KB |
Output is correct |
24 |
Correct |
2 ms |
864 KB |
Output is correct |
25 |
Correct |
70 ms |
20172 KB |
Output is correct |