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>
#define all(s) s.begin(),s.end()
using namespace std;
typedef int ll;
ll n,l,r,k,a[109];
ll dp1[109][109][10009];
ll dp2[109][109][10009];
ll best2(ll i,ll j,ll op)
{
if(op < 0)return 1e9 ;
if(j==n+2)
return 1e9;
if(i==r+1)
return 0;
ll &ret=dp2[i-l+1][j-r+1][op];
//cout<<2<<" "<<i<<" "<<j<<" "<<op<<endl;
if(ret!=-1)
return ret;
ret=best2(i,j+1,op);
ret=min(ret,best2(i+1,j,op)+a[i]);
ret=min(ret,best2(i+1,j+1,op-j+i)+a[j]);
return ret;
}
ll best1(ll i,ll j,ll op)
{
//cout<<1<<" "<<i<<" "<<j<<" "<<op<<endl;
if(op < 0)return 1e9 ;
if(j==r+2)
return 1e9;
if(i==l)
return best2(j,r+1,op);
ll &ret=dp1[i][j-l+1][op];
// cout << ret << endl ;
if(ret!=-1)
return ret;
ret=best1(i+1,j,op);
ret=min(ret,best1(i,j+1,op)+a[j]);
ret=min(ret,best1(i+1,j+1,op-j+i)+a[i]);
return ret;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>l>>r>>k;
for(ll i=1; i<=n; i++)
cin>>a[i];
for(ll i=0; i<=l; i++)
for(ll j=0; j<=r-l+2; j++)
for(ll op=0; op<=k; op++)
dp1[i][j][op]=-1;
for(ll i=0; i<=r-l+2; i++)
for(ll j=0; j<=n-r+2; j++)
for(ll op=0; op<=k; op++)
dp2[i][j][op]=-1;
cout<<best1(1,l,k);
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... |