#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll inf = 1e18;
int main()
{
int n, L, R, k;
cin >> n >> L >> R >> k;
L--, R--;
ll sm = 0;
vector<int> v(n);
for(int i = 0; i < n; i++)
cin >> v[i];
vector<int> f, s, t;
int sf = L, ss = R - L + 1, st = n - R - 1;
for(int i = 0; i < sf; i++)
f.push_back(v[i]);
for(int j = 0; j < ss; j++)
sm += v[sf + j], s.push_back(v[sf + j]);
for(int i = 0; i < st; i++)
t.push_back(v[ss + sf + i]);
ll dp1[2][ss + 1][1 + k];
for(int i = 0; i <= ss; i++)
for(int j = 0; j <= k; j++)
dp1[1][i][j] = dp1[0][i][j] = 0;
for(int i = 1; i <= sf; i++)
for(int j = 1; j <= ss; j++)
for(int c = 0; c <= k; c++)
{
bool b = i & 1, nb = (i - 1) & 1;
dp1[b][j][c] = max(dp1[nb][j][c], dp1[b][j - 1][c]);
int cost = sf + j - i;
if(cost <= c)
dp1[b][j][c] = max(dp1[b][j][c], dp1[nb][j - 1][c - cost] + (s[j - 1] - f[i - 1]));
}
ll dp[2][ss + 1][1 + k];
for(int i = 0; i <= ss; i++)
for(int j = 0; j <= k; j++)
dp[1][i][j] = dp[0][i][j] = 0;
for(int i = 1; i <= st; i++)
for(int j = 1; j <= ss; j++)
for(int c = 0; c <= k; c++)
{
bool b = i & 1, nb = (i - 1) & 1;
dp[b][j][c] = max(dp[nb][j][c], dp[b][j - 1][c]);
int cost = j + i - 1;
if(cost <= c)
dp[b][j][c] = max(dp[b][j][c], dp[nb][j - 1][c - cost] + (s[ss - j] - t[i - 1]));
}
ll ans = 0;
for(int i = 0; i <= ss; i ++)
for(int c = 0; c <= k; c++)
ans = max(ans, dp1[sf & 1][i][c] + dp[st & 1][ss - i][k - c]);
// cerr << ans << endl;
cout << sm - ans << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 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 |
2 ms |
2904 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 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 |
2 ms |
2904 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
436 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
14 ms |
8796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 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 |
2 ms |
2904 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
436 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
14 ms |
8796 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
14 ms |
8796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 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 |
2 ms |
2904 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
436 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
14 ms |
8796 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
14 ms |
8796 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
2 ms |
604 KB |
Output is correct |
24 |
Correct |
2 ms |
1004 KB |
Output is correct |
25 |
Correct |
58 ms |
16472 KB |
Output is correct |