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;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
#define fi first
#define se second
#define mp make_pair
#define fastIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
const int N = 52;
const int S = N * (N + 1);
const int M = S/2;
const int inf = (int)1e9;
int dpL[N][N][S]; // pick i items, indice sum j
int dpR[N][N][S];
int a[N];
void upd(int &a, int b){
a = min(a, b);
}
int main(){
fastIO;
int n, l, r, k;
cin >> n >> l >> r >> k;
for(int q = 0; q <= n + 1; q ++ ){
for(int d = 0; d <= n; d ++ ){
for(int s = 0; s < S; s ++ ){
dpL[q][d][s] = inf;
dpR[q][d][s] = inf;
}
}
}
dpL[0][0][M]=0;
int ans = 0;
for(int i = 1; i <= n; i ++ ){
cin >> a[i];
if(i >= l && i <= r) ans += a[i];
}
for(int i = 1; i <= r; i ++ ){
for(int j = 0 ; j <= n ; j ++ ){
for(int x = 0; x < S ; x ++ ){
if(i < l){
if(dpL[i-1][j][x] == inf) continue;
upd(dpL[i][j + 1][x - i], dpL[i-1][j][x] + a[i]);
upd(dpL[i][j][x], dpL[i-1][j][x]);
}
else{
if(dpL[i-1][j][x] == inf) continue;
if(j) upd(dpL[i][j - 1][x + i], dpL[i-1][j][x]);
upd(dpL[i][j][x], dpL[i-1][j][x] + a[i]);
}
}
}
}
for(int x = 0; x <= k ; x ++ ){
upd(ans, dpL[r][0][x + M]);
}
dpR[n + 1][0][M]=0;
for(int i = n; i >= l ; i -- ){
for(int j = 0 ; j <= n; j ++ ){
for(int x = 0; x < S; x ++ ){
if(dpR[i + 1][j][x] == inf) continue;
if(i > r){
upd(dpR[i][j + 1][x + i], dpR[i + 1][j][x] + a[i]);
upd(dpR[i][j][x], dpR[i + 1][j][x]);
}
else{
if(j) upd(dpR[i][j - 1][x - i], dpR[i + 1][j][x]);
upd(dpR[i][j][x], dpR[i + 1][j][x] + a[i]);
}
}
}
}
for(int x = 0; x <= k ; x ++ ){
upd(ans, dpR[l][0][x]);
}
for(int i = l ; i + 1 <= r; i ++ ){
for(int x = 0; x <= k; x ++ ){
if(dpL[i][0][x] == inf) continue;
for(int y = 0; x + y <= k; y ++ ){
if(dpR[i + 1][0][y] == inf) continue;
upd(ans, dpL[i][0][x] + dpR[i + 1][0][y]);
}
}
}
cout << ans << "\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... |