#include <bits/stdc++.h>
using namespace std;
const int maxn = 100 + 5;
const int maxk = 10000/4 + 5;
int N, K;
int L, R;
int a[maxn];
int suma;
int sol;
int dp[maxn][maxn][maxk];
int solL[maxn][maxk];
int main()
{
cin >> N >> L >> R >> K;
K = min(K, N * N / 4 + 2);
for (int i = 1; i <= N; i++)
cin >> a[i];
for (int i = L; i <= R; i++)
suma += a[i];
for (int i = L; i <= R; i++)
{
for (int j = L - 1; j > 0; j--)
{
for (int k = 0; k <= K; k++)
{
dp[i][j][k] = max(dp[i][j][k], dp[i - 1][j][k]);
dp[i][j][k] = max(dp[i][j][k], dp[i][j + 1][k]);
int cijena = i - j;
if (k >= cijena) dp[i][j][k] = max(dp[i][j][k], dp[i - 1][j + 1][k - cijena] + a[i] - a[j]);
}
}
}
for (int i = L - 1; i <= R; i++)
for (int j = 0; j <= K; j++)
solL[i][j] = dp[i][1][j];
memset(dp, 0, sizeof dp);
for (int i = R; i >= L; i--)
{
for (int j = R + 1; j <= N; j++)
{
for (int k = 0; k <= K; k++)
{
dp[i][j][k] = max(dp[i][j][k], dp[i + 1][j][k]);
dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k]);
int cijena = j - i;
if (k >= cijena) dp[i][j][k] = max(dp[i][j][k], dp[i + 1][j - 1][k - cijena] + a[i] - a[j]);
}
}
}
for (int i = L - 1; i <= R; i++)
for (int j = 0; j <= K; j++)
sol = max(sol, solL[i][j] + dp[i + 1][N][K - j]);
cout << suma - sol << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
92 ms |
108408 KB |
Output is correct |
2 |
Correct |
93 ms |
108368 KB |
Output is correct |
3 |
Correct |
92 ms |
108536 KB |
Output is correct |
4 |
Correct |
92 ms |
108440 KB |
Output is correct |
5 |
Correct |
92 ms |
108408 KB |
Output is correct |
6 |
Correct |
99 ms |
108492 KB |
Output is correct |
7 |
Correct |
93 ms |
108408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
92 ms |
108408 KB |
Output is correct |
2 |
Correct |
93 ms |
108368 KB |
Output is correct |
3 |
Correct |
92 ms |
108536 KB |
Output is correct |
4 |
Correct |
92 ms |
108440 KB |
Output is correct |
5 |
Correct |
92 ms |
108408 KB |
Output is correct |
6 |
Correct |
99 ms |
108492 KB |
Output is correct |
7 |
Correct |
93 ms |
108408 KB |
Output is correct |
8 |
Correct |
94 ms |
108540 KB |
Output is correct |
9 |
Correct |
95 ms |
108536 KB |
Output is correct |
10 |
Correct |
95 ms |
108536 KB |
Output is correct |
11 |
Correct |
94 ms |
108664 KB |
Output is correct |
12 |
Correct |
94 ms |
108640 KB |
Output is correct |
13 |
Correct |
96 ms |
108572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
92 ms |
108408 KB |
Output is correct |
2 |
Correct |
93 ms |
108368 KB |
Output is correct |
3 |
Correct |
92 ms |
108536 KB |
Output is correct |
4 |
Correct |
92 ms |
108440 KB |
Output is correct |
5 |
Correct |
92 ms |
108408 KB |
Output is correct |
6 |
Correct |
99 ms |
108492 KB |
Output is correct |
7 |
Correct |
93 ms |
108408 KB |
Output is correct |
8 |
Correct |
94 ms |
108540 KB |
Output is correct |
9 |
Correct |
95 ms |
108536 KB |
Output is correct |
10 |
Correct |
95 ms |
108536 KB |
Output is correct |
11 |
Correct |
94 ms |
108664 KB |
Output is correct |
12 |
Correct |
94 ms |
108640 KB |
Output is correct |
13 |
Correct |
96 ms |
108572 KB |
Output is correct |
14 |
Correct |
93 ms |
108400 KB |
Output is correct |
15 |
Correct |
95 ms |
108496 KB |
Output is correct |
16 |
Correct |
93 ms |
108408 KB |
Output is correct |
17 |
Correct |
93 ms |
108536 KB |
Output is correct |
18 |
Correct |
94 ms |
108468 KB |
Output is correct |
19 |
Correct |
96 ms |
108664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
92 ms |
108408 KB |
Output is correct |
2 |
Correct |
93 ms |
108368 KB |
Output is correct |
3 |
Correct |
92 ms |
108536 KB |
Output is correct |
4 |
Correct |
92 ms |
108440 KB |
Output is correct |
5 |
Correct |
92 ms |
108408 KB |
Output is correct |
6 |
Correct |
99 ms |
108492 KB |
Output is correct |
7 |
Correct |
93 ms |
108408 KB |
Output is correct |
8 |
Correct |
94 ms |
108540 KB |
Output is correct |
9 |
Correct |
95 ms |
108536 KB |
Output is correct |
10 |
Correct |
95 ms |
108536 KB |
Output is correct |
11 |
Correct |
94 ms |
108664 KB |
Output is correct |
12 |
Correct |
94 ms |
108640 KB |
Output is correct |
13 |
Correct |
96 ms |
108572 KB |
Output is correct |
14 |
Correct |
93 ms |
108400 KB |
Output is correct |
15 |
Correct |
95 ms |
108496 KB |
Output is correct |
16 |
Correct |
93 ms |
108408 KB |
Output is correct |
17 |
Correct |
93 ms |
108536 KB |
Output is correct |
18 |
Correct |
94 ms |
108468 KB |
Output is correct |
19 |
Correct |
96 ms |
108664 KB |
Output is correct |
20 |
Correct |
99 ms |
108508 KB |
Output is correct |
21 |
Correct |
97 ms |
108408 KB |
Output is correct |
22 |
Correct |
93 ms |
108460 KB |
Output is correct |
23 |
Correct |
96 ms |
108408 KB |
Output is correct |
24 |
Correct |
101 ms |
108712 KB |
Output is correct |
25 |
Correct |
126 ms |
109048 KB |
Output is correct |