This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, l, r, K;
cin >> n >> l >> r >> K;
vector<int> v(n+1);
for(int i=1; i<=n; i++) cin >> v[i];
int sum = 0;
for(int i=l; i<=r; i++) sum += v[i];
int dpL[l][2][K+1], dpR[2][n+2][K+1], mnL[n+1][K+1], mnR[n+1][K+1];
memset(dpR, 0, sizeof(dpR));
memset(dpL, 0, sizeof(dpL));
memset(mnL, 0, sizeof(mnL));
memset(mnR, 0, sizeof(mnR));
for(int j=l; j<=r; j++) {
for(int i=1; i<l; i++) {
for(int k=0; k<=K; k++) {
dpL[i][1][k] = min(dpL[i][1][k], dpL[i-1][1][k]);
dpL[i][1][k] = min(dpL[i][1][k], dpL[i][0][k]);
if(j - i <= k) dpL[i][1][k] = min(dpL[i][1][k], dpL[i-1][0][k-j+i] + v[i] - v[j]);
if(i == l-1) mnL[j][k] = dpL[i][1][k];
}
}
for(int i=1; i<l; i++) {
for(int k=0; k<=K; k++) {
dpL[i][0][k] = dpL[i][1][k];
dpL[i][1][k] = 0;
}
}
}
for(int i=r; i>=l; i--) {
for(int j=n; j>=r+1; j--) {
for(int k=0; k<=K; k++) {
dpR[1][j][k] = min(dpR[1][j][k], dpR[0][j][k]);
dpR[1][j][k] = min(dpR[1][j][k], dpR[1][j+1][k]);
if(j - i <= k) dpR[1][j][k] = min(dpR[1][j][k], dpR[0][j+1][k-j+i] + v[j] - v[i]);
if(j == r + 1) mnR[i][k] = dpR[1][j][k];
}
}
for(int j=n; j>=r+1; j--) {
for(int k=0; k<=K; k++) {
dpR[0][j][k] = dpR[1][j][k];
dpR[1][j][k] = 0;
}
}
}
int mn = min(mnL[r][K], mnR[l][K]);
for(int i=l; i<r; i++)
for(int j=0; j<=K; j++)
mn = min(mn, mnL[i][j] + mnR[i+1][K-j]);
cout << sum + mn << '\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |