#include <bits/stdc++.h>
using namespace std;
const int N = 102;
int n, L, R, k, s;
int a[102];
int dp[N][2505][N], Left[N][2505], Right[N][2505];
void MAX(int &A, int B)
{
if(A < B) A = B;
}
void sub2(int L)
{
int ans = 0;
for(int i = L; i <= R; i++)
for(int j = L - 1; j > 0; j--)
for(int c = 1; c <= k; c++)
{
MAX(dp[i][c][j], dp[i - 1][c][j]);
MAX(dp[i][c][j], dp[i][c][j + 1]);
if(c - i + j >= 0)
{
MAX(dp[i][c][j], dp[i - 1][c - i + j][j + 1] + a[i] - a[j]);
}
ans = max(ans, dp[i][c][j]);
}
for(int i = L; i <= R; i++) ans -= a[i];
cout << -ans;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n >> L >> R >> k;
for(int i = 1; i <= n; i++) cin >> a[i];
for(int i = L; i <= R; i++) s += a[i];
k = min(k, n * n / 4);
if(R == n)
{
sub2(L);
return 0;
}
int ans = 0;
for(int i = L; i <= R; i++)
for(int j = L - 1; j > 0; j--)
for(int c = 1; c <= k; c++)
{
MAX(dp[i][c][j], dp[i - 1][c][j]);
MAX(dp[i][c][j], dp[i][c][j + 1]);
if(c - i + j >= 0)
MAX(dp[i][c][j], dp[i - 1][c - i + j][j + 1] + a[i] - a[j]);
}
for(int i = L; i <= R; i++)
for(int j = 1; j <= k; j++)
{
Left[i][j] = dp[i][j][1];
MAX(Left[i][j], Left[i][j - 1]);
}
memset(dp, 0, sizeof dp);
for(int i = R; i >= L; i--)
for(int j = R + 1; j <= n; j++)
{
for(int c = 1; c <= k; c++)
{
MAX(dp[i][c][j], dp[i + 1][c][j]);
MAX(dp[i][c][j], dp[i][c][j - 1]);
if(c + i - j >= 0)
MAX(dp[i][c][j], dp[i + 1][c - i + j][j - 1] + a[i] - a[j]);
}
}
for(int i = R; i >= L; i--)
for(int j = 1; j <= k; j++)
{
Right[i][j] = dp[i][j][n];
MAX(Right[i][j], Right[i][j - 1]);
}
for(int i = L - 1; i <= R; i++)
for(int j = 0; j <= k; j++)
MAX(ans, Left[i][j] + Right[i + 1][k - j]);
cout << s - ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 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 |
604 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 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 |
604 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
856 KB |
Output is correct |
8 |
Correct |
1 ms |
600 KB |
Output is correct |
9 |
Correct |
1 ms |
860 KB |
Output is correct |
10 |
Correct |
1 ms |
1372 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
2140 KB |
Output is correct |
13 |
Correct |
3 ms |
7004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 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 |
604 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
856 KB |
Output is correct |
8 |
Correct |
1 ms |
600 KB |
Output is correct |
9 |
Correct |
1 ms |
860 KB |
Output is correct |
10 |
Correct |
1 ms |
1372 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
2140 KB |
Output is correct |
13 |
Correct |
3 ms |
7004 KB |
Output is correct |
14 |
Correct |
15 ms |
102232 KB |
Output is correct |
15 |
Correct |
15 ms |
102492 KB |
Output is correct |
16 |
Correct |
17 ms |
102492 KB |
Output is correct |
17 |
Correct |
17 ms |
102492 KB |
Output is correct |
18 |
Correct |
17 ms |
102492 KB |
Output is correct |
19 |
Correct |
4 ms |
7004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 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 |
604 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
856 KB |
Output is correct |
8 |
Correct |
1 ms |
600 KB |
Output is correct |
9 |
Correct |
1 ms |
860 KB |
Output is correct |
10 |
Correct |
1 ms |
1372 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
2140 KB |
Output is correct |
13 |
Correct |
3 ms |
7004 KB |
Output is correct |
14 |
Correct |
15 ms |
102232 KB |
Output is correct |
15 |
Correct |
15 ms |
102492 KB |
Output is correct |
16 |
Correct |
17 ms |
102492 KB |
Output is correct |
17 |
Correct |
17 ms |
102492 KB |
Output is correct |
18 |
Correct |
17 ms |
102492 KB |
Output is correct |
19 |
Correct |
4 ms |
7004 KB |
Output is correct |
20 |
Correct |
17 ms |
102488 KB |
Output is correct |
21 |
Correct |
16 ms |
102492 KB |
Output is correct |
22 |
Incorrect |
15 ms |
102492 KB |
Output isn't correct |
23 |
Halted |
0 ms |
0 KB |
- |