#include<bits/stdc++.h>
using i64 = long long;
using namespace std;
const int maxn = 100;
const int maxk = 10000;
int n,L,R,k;
i64 a[maxn+2] , f1[maxn+2][maxk+2] , dp1[maxn+2][maxn+2][maxk+2] , dp2[maxn+2][maxn+2][maxk+2] , f2[maxn+2][maxk+2];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
#define name "main"
// freopen(name".inp","r",stdin);
// freopen(name".out","w+",stdout);
cin >> n >> L >> R >> k;
for (int i = 1; i <= n; ++i) cin >> a[i];
i64 total = 0;
for (int i = L; i <= R; ++i) total += a[i];
//... BUILD FOR THE LEFT
i64 mx = 0;
for (int i = 1; i < L; ++i)
{
for (int j = L; j <= R; ++j)
{
for (int used = 0; used <= k; ++used)
{
dp1[i][j][used] = min({dp1[i - 1][j][used] , dp1[i][j - 1][used]});
if (used - abs(i - j) >= 0)
{
dp1[i][j][used] = min(dp1[i][j][used] , dp1[i - 1][j - 1][used - abs(i - j)] + a[i] - a[j]);
}
f1[j][used] = min({f1[j - 1][used] , dp1[i][j][used] , f1[j][used]});
if (used - 1 >= 0)
f1[j][used] = min(f1[j][used] , f1[j][used - 1]);
}
}
}
//... BUILD FOR THE RIGHT
for (int i = n; i > R; --i)
{
for (int j = R; j >= L; --j)
{
for (int used = 0; used <= k; ++used)
{
dp2[i][j][used] = min(dp2[i+1][j][used] , dp2[i][j+1][used]);
if (used - abs(i - j) >= 0)
{
dp2[i][j][used] = min(dp2[i][j][used] , dp2[i+1][j+1][used - abs(i - j)] + a[i] - a[j]);
}
f2[j][used] = min({f2[j + 1][used] , dp2[i][j][used] , f2[j][used]});
if (used - 1 >= 0)
f2[j][used] = min(f2[j][used - 1] , f2[j][used]);
}
}
}
i64 answer = total;
for (int i = L ; i <= R; ++i) for (int used = 0; used <= k; ++used)
{
if (i + 1 <= R)
answer = min(answer , total + f1[i][used] + f2[i + 1][k - used]);
answer = min(answer , total + f2[i][used]);
answer = min(answer , total + f1[i][used]);
}
cout << answer;
}
Compilation message
holding.cpp: In function 'int main()':
holding.cpp:23:7: warning: unused variable 'mx' [-Wunused-variable]
23 | i64 mx = 0;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
4608 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
3 ms |
7512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
4608 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
3 ms |
7512 KB |
Output is correct |
8 |
Correct |
4 ms |
43100 KB |
Output is correct |
9 |
Correct |
5 ms |
47300 KB |
Output is correct |
10 |
Correct |
5 ms |
51436 KB |
Output is correct |
11 |
Correct |
5 ms |
49244 KB |
Output is correct |
12 |
Correct |
3 ms |
31836 KB |
Output is correct |
13 |
Correct |
28 ms |
76980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
4608 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
3 ms |
7512 KB |
Output is correct |
8 |
Correct |
4 ms |
43100 KB |
Output is correct |
9 |
Correct |
5 ms |
47300 KB |
Output is correct |
10 |
Correct |
5 ms |
51436 KB |
Output is correct |
11 |
Correct |
5 ms |
49244 KB |
Output is correct |
12 |
Correct |
3 ms |
31836 KB |
Output is correct |
13 |
Correct |
28 ms |
76980 KB |
Output is correct |
14 |
Correct |
1 ms |
4956 KB |
Output is correct |
15 |
Correct |
2 ms |
17756 KB |
Output is correct |
16 |
Correct |
2 ms |
22364 KB |
Output is correct |
17 |
Correct |
3 ms |
22616 KB |
Output is correct |
18 |
Correct |
5 ms |
39768 KB |
Output is correct |
19 |
Correct |
29 ms |
76888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
4608 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
3 ms |
7512 KB |
Output is correct |
8 |
Correct |
4 ms |
43100 KB |
Output is correct |
9 |
Correct |
5 ms |
47300 KB |
Output is correct |
10 |
Correct |
5 ms |
51436 KB |
Output is correct |
11 |
Correct |
5 ms |
49244 KB |
Output is correct |
12 |
Correct |
3 ms |
31836 KB |
Output is correct |
13 |
Correct |
28 ms |
76980 KB |
Output is correct |
14 |
Correct |
1 ms |
4956 KB |
Output is correct |
15 |
Correct |
2 ms |
17756 KB |
Output is correct |
16 |
Correct |
2 ms |
22364 KB |
Output is correct |
17 |
Correct |
3 ms |
22616 KB |
Output is correct |
18 |
Correct |
5 ms |
39768 KB |
Output is correct |
19 |
Correct |
29 ms |
76888 KB |
Output is correct |
20 |
Correct |
10 ms |
88996 KB |
Output is correct |
21 |
Correct |
4 ms |
30812 KB |
Output is correct |
22 |
Correct |
8 ms |
82012 KB |
Output is correct |
23 |
Correct |
6 ms |
23388 KB |
Output is correct |
24 |
Correct |
16 ms |
180612 KB |
Output is correct |
25 |
Correct |
92 ms |
250192 KB |
Output is correct |