#include<bits/stdc++.h>
using namespace std;
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, l, r, k;
cin >> n >> l >> r >> k;
vector <int> a(n);
for(int i = 0; i < n; i++)
{
cin >> a[i];
}
l--;
r--;
int sum = 0, ans = 0;
vector <pair <int, int> > mass1, mass2, mass3;
for(int i = 0; i < n; i++)
{
if(i < l)
{
mass1.push_back({a[i], i});
}
else if(i <= r){
sum += a[i];
ans += a[i];
mass2.push_back({a[i], i});
}
else
{
mass3.push_back({a[i], i});
}
}
vector <vector <int> > dp1(n + 1, vector <int> (k + 1, 0));
vector <vector <int> > dp2(n + 1, vector <int> (k + 1, 0));
vector <int> dp0(k + 1, 0);
vector <vector <int> > dpleft(mass2.size(), vector <int> (k + 1, 0));
vector <vector <int> > dpright(mass2.size(), vector <int> (k + 1, 0));
for(int i = 0; i < mass2.size(); i++)
{
for(int t = 0; t <= k; t++)
{
dp0[t] = 0;
}
for(int j = 0; j < mass1.size(); j++)
{
for(int t = 0; t <= k; t++)
{
dp0[t] = min(dp0[t], dp1[j][t]);
}
for(int t = 0; t + abs(mass2[i].second - mass1[j].second) <= k; t++)
{
dp2[j + 1][t + abs(mass2[i].second - mass1[j].second)] =
min(dp2[j + 1][t + abs(mass2[i].second - mass1[j].second)], dp0[t] + mass1[j].first - mass2[i].first);
}
}
for(int j = 0; j <= mass1.size(); j++)
{
for(int t = 0; t <= k; t++)
{
dp1[j][t] = dp2[j][t];
dpleft[i][t] = min(dpleft[i][t], dp1[j][t]);
}
}
}
for(int j = 0; j <= mass3.size(); j++)
{
for(int t= 0; t <= k; t++)
{
dp1[j][t] = 0;
dp2[j][t] = 0;
}
}
for(int i = mass2.size() - 1; i >= 0; i--)
{
for(int t = 0; t <= k; t++)
{
dp0[t] = 0;
}
for(int j = mass3.size(); j > 0; j--)
{
for(int t = 0; t <= k; t++)
{
dp0[t] = min(dp0[t], dp1[j][t]);
}
for(int t = 0; t + abs(mass2[i].second - mass3[j - 1].second) <= k; t++)
{
dp2[j - 1][t + abs(mass2[i].second - mass3[j - 1].second)] =
min(dp2[j - 1][t + abs(mass2[i].second - mass3[j - 1].second)], dp0[t] + mass3[j - 1].first - mass2[i].first);
}
}
for(int j = 0; j <= mass3.size(); j++)
{
for(int t = 0; t <= k; t++)
{
dp1[j][t] = dp2[j][t];
dpright[i][t] = min(dpright[i][t], dp1[j][t]);
}
}
}
for(int j = 0; j <= k; j++)
{
ans = min(ans, sum + dpleft[mass2.size() - 1][j]);
ans = min(ans, sum + dpright[0][j]);
}
vector <int> dppred(k + 1, 0);
for(int i = 0; i < mass2.size() - 1; i++)
{
for(int j = 0; j <= k; j++)
{
dppred[j] = dpright[i + 1][j];
}
for(int j = 1; j <= k; j++)
{
dppred[j] = min(dppred[j], dppred[j - 1]);
}
for(int j = 0; j <= k; j++)
{
ans = min(ans, sum + dpleft[i][j] + dppred[k - j]);
}
}
cout << ans;
return 0;
}
Compilation message
holding.cpp: In function 'int main()':
holding.cpp:40:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < mass2.size(); i++)
~~^~~~~~~~~~~~~~
holding.cpp:46:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j = 0; j < mass1.size(); j++)
~~^~~~~~~~~~~~~~
holding.cpp:58:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j = 0; j <= mass1.size(); j++)
~~^~~~~~~~~~~~~~~
holding.cpp:67:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j = 0; j <= mass3.size(); j++)
~~^~~~~~~~~~~~~~~
holding.cpp:93:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j = 0; j <= mass3.size(); j++)
~~^~~~~~~~~~~~~~~
holding.cpp:108:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < mass2.size() - 1; i++)
~~^~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
1920 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
1920 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
512 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
57 ms |
6400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
1920 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
512 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
57 ms |
6400 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
2 ms |
512 KB |
Output is correct |
19 |
Correct |
52 ms |
6400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
1920 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
512 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
57 ms |
6400 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
2 ms |
512 KB |
Output is correct |
19 |
Correct |
52 ms |
6400 KB |
Output is correct |
20 |
Correct |
4 ms |
640 KB |
Output is correct |
21 |
Correct |
3 ms |
896 KB |
Output is correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
23 |
Correct |
8 ms |
3328 KB |
Output is correct |
24 |
Correct |
6 ms |
768 KB |
Output is correct |
25 |
Correct |
226 ms |
12288 KB |
Output is correct |