#include <bits/stdc++.h>
#define all(dataStructure) dataStructure.begin(),dataStructure.end()
#define ll long long
using namespace std;
namespace std {
template <typename T, int D>
struct _vector : public vector <_vector <T, D - 1>> {
static_assert(D >= 1, "Dimension must be positive!");
template <typename... Args>
_vector(int n = 0, Args... args) : vector <_vector <T, D - 1>> (n, _vector <T, D - 1> (args...)) {}
};
// _vector <int, 3> a(n, m, k);: int a[n][m][k].
// _vector <int, 3> a(n, m, k, x);: int a[n][m][k] initialized with x.
template <typename T>
struct _vector <T, 1> : public vector <T> {
_vector(int n = 0, const T& val = T()) : vector <T> (n, val) {}
};
}
const int MAX = 1e2 + 2;
const ll MOD[] = {1000000007, 998244353};
int dpL[102][102][2507];
int dpR[102][102][2507];
int n, K, a[102], l, r;
int sum = 0;
void Solve() {
cin >> n >> l >> r >> K;
K = min(K, n * n / 4);
for (int i = 1; i <= n; i++) cin >> a[i];
sum = accumulate(a + l, a + r + 1, 0);
for (int i = 1; i < l; i++) {
for (int j = l; j <= r; j++) {
for (int k = 0; k <= K; k++) {
dpL[i][j][k] = max(dpL[i - 1][j][k], dpL[i][j - 1][k]);
if (k >= j - i) dpL[i][j][k] = max(dpL[i][j][k], dpL[i - 1][j - 1][k - j + i] + a[j] - a[i]);
}
}
}
for (int i = n; i > r; i--) {
for (int j = r; j >= l; j--) {
for (int k = 0; k <= K; k++) {
dpR[j][i][k] = max(dpR[j + 1][i][k], dpR[j][i + 1][k]);
if (k >= i - j) dpR[j][i][k] = max(dpR[j][i][k], dpR[j + 1][i + 1][k - i + j] + a[j] - a[i]);
}
}
}
int res = max(dpL[l - 1][r][K], dpR[l][r + 1][K]);
for (int i = l; i <= r; i++) {
for (int k = 0; k <= K; k++) {
res = max(res, dpL[l - 1][i][k] + dpR[i + 1][r + 1][K - k]);
}
}
cout << sum - res;
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
#define TASK "HOLDING"
if (fopen(TASK".INP", "r")) {
freopen(TASK".INP", "r", stdin);
freopen(TASK".OUT", "w", stdout);
}
/* int TEST = 1; cin >> TEST; while (TEST--) */ Solve();
cerr << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << "s\n";
return 0;
}
Compilation message
holding.cpp: In function 'int32_t main()':
holding.cpp:65:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
65 | freopen(TASK".INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
holding.cpp:66:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
66 | freopen(TASK".OUT", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
604 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 |
604 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
604 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 |
604 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
2 ms |
2908 KB |
Output is correct |
10 |
Correct |
2 ms |
3160 KB |
Output is correct |
11 |
Correct |
3 ms |
3172 KB |
Output is correct |
12 |
Correct |
1 ms |
2136 KB |
Output is correct |
13 |
Correct |
4 ms |
4588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
604 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 |
604 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
2 ms |
2908 KB |
Output is correct |
10 |
Correct |
2 ms |
3160 KB |
Output is correct |
11 |
Correct |
3 ms |
3172 KB |
Output is correct |
12 |
Correct |
1 ms |
2136 KB |
Output is correct |
13 |
Correct |
4 ms |
4588 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
1116 KB |
Output is correct |
16 |
Correct |
1 ms |
1628 KB |
Output is correct |
17 |
Correct |
1 ms |
1884 KB |
Output is correct |
18 |
Correct |
1 ms |
2908 KB |
Output is correct |
19 |
Correct |
2 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
604 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 |
604 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
2 ms |
2908 KB |
Output is correct |
10 |
Correct |
2 ms |
3160 KB |
Output is correct |
11 |
Correct |
3 ms |
3172 KB |
Output is correct |
12 |
Correct |
1 ms |
2136 KB |
Output is correct |
13 |
Correct |
4 ms |
4588 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
1116 KB |
Output is correct |
16 |
Correct |
1 ms |
1628 KB |
Output is correct |
17 |
Correct |
1 ms |
1884 KB |
Output is correct |
18 |
Correct |
1 ms |
2908 KB |
Output is correct |
19 |
Correct |
2 ms |
4444 KB |
Output is correct |
20 |
Correct |
4 ms |
6492 KB |
Output is correct |
21 |
Correct |
2 ms |
2908 KB |
Output is correct |
22 |
Correct |
2 ms |
4444 KB |
Output is correct |
23 |
Correct |
2 ms |
2908 KB |
Output is correct |
24 |
Correct |
6 ms |
12248 KB |
Output is correct |
25 |
Correct |
18 ms |
25148 KB |
Output is correct |