#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
typedef long long int ll;
typedef pair<ll,ll> pi;
typedef pair<ll, pair<ll, ll> > pii;
typedef vector <ll> vi;
#define forw(i,a,b) for (ll i=a;i<=(b);i++)
#define forb(i,a,b) for (ll i=a;i>=(b);i--)
#define fast {ios::sync_with_stdio(false); cin.tie(0); }
#define fi first
#define se second
#define pu push
#define puf push_front
#define pb push_back
#define pof pop_front
#define pob pop_back
#define fr front
#define all(a) a.begin(),a.end()
const ll oo=1e18;
const ll mod=(1<<31)-1;
const int base=31;
const int tx[5]={0,-1,0,1,0};
const int ty[5]={0,0,1,0,-1};
const ll maxN=5e5+5;
const ll maxM=10000/4+5;
const ll block=700;
int n,l,r,k,dp1[105][maxM][105],dp2[105][maxM][105],sum,a[105],ans,pre[105][maxM],suf[105][maxM];
void minimize(int &a,int b)
{
a=min(a,b);
}
void solve()
{
cin>>n>>l>>r>>k;
k=min(n*n/4,k);
forw(i,1,n)
{
cin>>a[i];
if (l<=i && i<=r)
sum+=a[i];
}
int num=r-l+1;
forw(i,0,n+1)
forw(cost,0,k)
forw(j,0,n)
dp1[i][cost][j]=dp2[i][cost][j]=1e9;
dp1[0][0][0]=dp2[n+1][0][0]=sum;
////////////////////////////////////// left to right
forw(i,0,l-2)
{
forw(cost,0,k)
{
int res=1e9;
forw(j,0,num)
{
res=min(res,dp1[i][cost][j]);
minimize(dp1[i+1][cost][j],res);
if (j<num && cost+ (l+j) - (i+1) <=k)
minimize(dp1[i+1][cost+ (l+j) - (i+1)][j+1],res-(a[l+j]-a[i+1]));
}
}
}
forw(j,0,num)
{
int res=1e9;
forw(cost,0,k)
{
minimize(res,dp1[l-1][cost][j]);
if (j>0) pre[j][cost]=min(res,pre[j-1][cost]);
else pre[j][cost]=res;
}
}
//////////////////////////////////////// right to left
forb(i,n+1,r+2)
{
forw(cost,0,k)
{
int res=1e9;
forw(j,0,num)
{
res=min(res,dp2[i][cost][j]);
minimize(dp2[i-1][cost][j],res);
if (j<num && cost + (i-1) - (r-j) <=k)
minimize(dp2[i-1][cost + (i-1) - (r-j)][j+1],res-(a[r-j]-a[i-1]));
}
}
}
forw(j,0,num)
{
int res=1e9;
forw(cost,0,k)
{
minimize(res,dp2[r+1][cost][j]);
if (j>0) suf[j][cost]=min(res,suf[j-1][cost]);
else suf[j][cost]=res;
}
}
ans=sum;
forw(j,0,num)
{
forw(cost,0,k)
{
//cout<<j<<" "<<cost<<" "<<pre[j][cost]<<" "<<suf[num-j][k-cost]<<endl;
minimize(ans,pre[j][cost]+suf[num-j][k-cost]-sum);
}
}
cout<<ans;
}
int main()
{
fast;
//freopen("test.inp","r",stdin);
//freopen("test.out","w",stdout);
solve();
return 0;
}
Compilation message
holding.cpp:21:21: warning: integer overflow in expression of type 'int' results in '2147483647' [-Woverflow]
21 | const ll mod=(1<<31)-1;
| ~~~~~~~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
588 KB |
Output is correct |
3 |
Correct |
0 ms |
588 KB |
Output is correct |
4 |
Correct |
0 ms |
844 KB |
Output is correct |
5 |
Correct |
0 ms |
588 KB |
Output is correct |
6 |
Correct |
1 ms |
1100 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
588 KB |
Output is correct |
3 |
Correct |
0 ms |
588 KB |
Output is correct |
4 |
Correct |
0 ms |
844 KB |
Output is correct |
5 |
Correct |
0 ms |
588 KB |
Output is correct |
6 |
Correct |
1 ms |
1100 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
8 |
Correct |
2 ms |
3148 KB |
Output is correct |
9 |
Correct |
2 ms |
4428 KB |
Output is correct |
10 |
Correct |
2 ms |
4812 KB |
Output is correct |
11 |
Correct |
4 ms |
9292 KB |
Output is correct |
12 |
Correct |
3 ms |
5284 KB |
Output is correct |
13 |
Correct |
14 ms |
27852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
588 KB |
Output is correct |
3 |
Correct |
0 ms |
588 KB |
Output is correct |
4 |
Correct |
0 ms |
844 KB |
Output is correct |
5 |
Correct |
0 ms |
588 KB |
Output is correct |
6 |
Correct |
1 ms |
1100 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
8 |
Correct |
2 ms |
3148 KB |
Output is correct |
9 |
Correct |
2 ms |
4428 KB |
Output is correct |
10 |
Correct |
2 ms |
4812 KB |
Output is correct |
11 |
Correct |
4 ms |
9292 KB |
Output is correct |
12 |
Correct |
3 ms |
5284 KB |
Output is correct |
13 |
Correct |
14 ms |
27852 KB |
Output is correct |
14 |
Correct |
1 ms |
972 KB |
Output is correct |
15 |
Correct |
2 ms |
4172 KB |
Output is correct |
16 |
Correct |
2 ms |
2892 KB |
Output is correct |
17 |
Correct |
4 ms |
7756 KB |
Output is correct |
18 |
Correct |
5 ms |
11340 KB |
Output is correct |
19 |
Correct |
13 ms |
27972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
588 KB |
Output is correct |
3 |
Correct |
0 ms |
588 KB |
Output is correct |
4 |
Correct |
0 ms |
844 KB |
Output is correct |
5 |
Correct |
0 ms |
588 KB |
Output is correct |
6 |
Correct |
1 ms |
1100 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
8 |
Correct |
2 ms |
3148 KB |
Output is correct |
9 |
Correct |
2 ms |
4428 KB |
Output is correct |
10 |
Correct |
2 ms |
4812 KB |
Output is correct |
11 |
Correct |
4 ms |
9292 KB |
Output is correct |
12 |
Correct |
3 ms |
5284 KB |
Output is correct |
13 |
Correct |
14 ms |
27852 KB |
Output is correct |
14 |
Correct |
1 ms |
972 KB |
Output is correct |
15 |
Correct |
2 ms |
4172 KB |
Output is correct |
16 |
Correct |
2 ms |
2892 KB |
Output is correct |
17 |
Correct |
4 ms |
7756 KB |
Output is correct |
18 |
Correct |
5 ms |
11340 KB |
Output is correct |
19 |
Correct |
13 ms |
27972 KB |
Output is correct |
20 |
Correct |
16 ms |
35148 KB |
Output is correct |
21 |
Correct |
24 ms |
55888 KB |
Output is correct |
22 |
Correct |
2 ms |
4560 KB |
Output is correct |
23 |
Correct |
89 ms |
210272 KB |
Output is correct |
24 |
Correct |
15 ms |
27844 KB |
Output is correct |
25 |
Correct |
102 ms |
211272 KB |
Output is correct |