/* Author : Mychecksdead */
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define MOD (1000000000+7)
#define MOD1 (998244353)
#define PI 3.1415926535
#define pb push_back
#define setp() cout << setprecision(15)
#define all(x) x.begin(), x.end()
const int N = 1e4+15, M = 110, F = 2147483646, K = 20;
int n, l, r, k, arr[M], dp[M][N], temp[M][N];
void solve(){
cin >> n >> l >> r >> k;
for(int i = 1; i <= n; ++i) cin >> arr[i];
int sum = 0;
for(int i = l; i <= r; ++i) sum += arr[i];
vector<pair<int, int>> a;
vector<pair<int, int>> b;
for(int i = 1; i <= n; ++i){
if(i < l || i > r) a.pb({arr[i], i});
else b.pb({arr[i], i});
}
int ans = sum;
for(int j = 0; j <= n; ++j){
for(int l = 0; l <= k; ++l) temp[j][l] = sum;
}
for(int i = 1; i <= a.size(); ++i){
for(int j = 0; j <= n; ++j){
for(int l = 0; l <= k; ++l){
dp[j][l] = temp[j][l];
}
}
for(int j = 1; j <= b.size(); ++j){
int dist = abs(a[i - 1].second - b[j - 1].second);
for(int l = 0; l <= k; ++l){
dp[j][l] = min({(i > 1 ? temp[j][l] : F), dp[j - 1][l], (l > 0 ? dp[j][l - 1] : F)});
if(l >= dist){
// cout << i << ' ' << j << ' ' << l << ' ' << dp[i][j][l] << ' ' << (a[i - 1].first - b[j - 1].first) << '\n';
dp[j][l] = min(dp[j][l], temp[j - 1][l - dist] + (a[i - 1].first - b[j - 1].first));
}
//
ans = min(ans, dp[j][l]);
}
}
for(int j = 0; j <= n; ++j){
for(int l = 0; l <= k; ++l){
// cout << i << ' ' << j << ' ' << l << ' ' << dp[j][l] << '\n';
temp[j][l] = dp[j][l];
}
}
}
cout << ans;
}
int main(){
cin.tie(0); ios::sync_with_stdio(0);
int T = 1, aa;
// cin >> T;aa=T;
while(T--){
// cout << "Case #" << aa-T << ": ";
solve();
}
return 0;
}
Compilation message
holding.cpp: In function 'void solve()':
holding.cpp:34:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
34 | for(int i = 1; i <= a.size(); ++i){
| ~~^~~~~~~~~~~
holding.cpp:40:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
40 | for(int j = 1; j <= b.size(); ++j){
| ~~^~~~~~~~~~~
holding.cpp: In function 'int main()':
holding.cpp:66:16: warning: unused variable 'aa' [-Wunused-variable]
66 | int T = 1, aa;
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
3 ms |
1228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
3 ms |
1228 KB |
Output is correct |
8 |
Correct |
1 ms |
716 KB |
Output is correct |
9 |
Correct |
1 ms |
716 KB |
Output is correct |
10 |
Correct |
1 ms |
716 KB |
Output is correct |
11 |
Correct |
1 ms |
716 KB |
Output is correct |
12 |
Correct |
1 ms |
716 KB |
Output is correct |
13 |
Correct |
37 ms |
4300 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
3 ms |
1228 KB |
Output is correct |
8 |
Correct |
1 ms |
716 KB |
Output is correct |
9 |
Correct |
1 ms |
716 KB |
Output is correct |
10 |
Correct |
1 ms |
716 KB |
Output is correct |
11 |
Correct |
1 ms |
716 KB |
Output is correct |
12 |
Correct |
1 ms |
716 KB |
Output is correct |
13 |
Correct |
37 ms |
4300 KB |
Output is correct |
14 |
Correct |
1 ms |
716 KB |
Output is correct |
15 |
Correct |
1 ms |
716 KB |
Output is correct |
16 |
Correct |
1 ms |
716 KB |
Output is correct |
17 |
Correct |
1 ms |
716 KB |
Output is correct |
18 |
Correct |
1 ms |
716 KB |
Output is correct |
19 |
Correct |
40 ms |
4300 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
3 ms |
1228 KB |
Output is correct |
8 |
Correct |
1 ms |
716 KB |
Output is correct |
9 |
Correct |
1 ms |
716 KB |
Output is correct |
10 |
Correct |
1 ms |
716 KB |
Output is correct |
11 |
Correct |
1 ms |
716 KB |
Output is correct |
12 |
Correct |
1 ms |
716 KB |
Output is correct |
13 |
Correct |
37 ms |
4300 KB |
Output is correct |
14 |
Correct |
1 ms |
716 KB |
Output is correct |
15 |
Correct |
1 ms |
716 KB |
Output is correct |
16 |
Correct |
1 ms |
716 KB |
Output is correct |
17 |
Correct |
1 ms |
716 KB |
Output is correct |
18 |
Correct |
1 ms |
716 KB |
Output is correct |
19 |
Correct |
40 ms |
4300 KB |
Output is correct |
20 |
Correct |
4 ms |
1356 KB |
Output is correct |
21 |
Correct |
5 ms |
1612 KB |
Output is correct |
22 |
Correct |
1 ms |
1100 KB |
Output is correct |
23 |
Correct |
18 ms |
4044 KB |
Output is correct |
24 |
Correct |
5 ms |
1356 KB |
Output is correct |
25 |
Correct |
164 ms |
8140 KB |
Output is correct |