#include<bits/stdc++.h>
using namespace std;
#define all(a) a.begin(),a.end()
#define F first
#define S second
#define pb push_back
#define ll long long
#define vi vector<int>
#define pi pair<int,int>
#define mp make_pair
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
const int mod=1e9+7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int mul(int a,int b)
{
return ((a)*1ll*(b))%mod;
}
void add(int &a,int b)
{
a+=b;
if(a>=mod)a-=mod;
}
int sub(int a,int b){
a-=b;
if(a<0){
a+=mod;
}
return a;
}
int powz(int a,int b)
{
int res=1;
while(b)
{
if(b&1){
res=mul(res,a);
}
b/=2;
a=mul(a,a);
}
return res;
}
template <typename A, typename B>
istream& operator>>(istream& input,pair<A,B>& x) {
input>>x.F>>x.S;
return input;
}
template <typename A>
istream& operator>>(istream& input,vector<A>& x) {
for(auto& i:x)
input>>i;
return input;
}
template<typename A>
ostream& operator<<(ostream& output,vector<A>& x) {
for(auto& i:x)
output<<i<<' ';
return output;
}
const int N=1000002;
int dp[105][10005],dp2[105][10005],new_dp[105][10005];
void solve(){
int n,l,r,k;
cin>>n>>l>>r>>k;
vi a(n);
cin>>a;
int tot=r-l+1;
l--;r--;
for(int i=0;i<l;i++){
for(int j=0;j<=tot-1;j++){
for(int put=j+1;put<=tot;put++){
int tott=abs(put+l-1-i),val=a[l+put-1]-a[i];
if(val<=0){
continue;
}
for(int cost=0;cost<=k-tott;cost++){
new_dp[put][cost+tott]=max(new_dp[put][cost+tott],dp[j][cost]+val);
}
}
}
for(int j=0;j<=tot;j++){
for(int cost=0;cost<=k;cost++){
dp[j][cost]=max(dp[j][cost],new_dp[j][cost]);
new_dp[j][cost]=0;
}
}
}
for(int i=n-1;i>r;i--){
for(int j=0;j<=tot-1;j++){
for(int put=j+1;put<=tot;put++){
int tott=abs(r-put+1-i),val=a[r-put+1]-a[i];
if(val<=0){
continue;
}
for(int cost=0;cost<=k-tott;cost++){
new_dp[put][cost+tott]=max(new_dp[put][cost+tott],dp2[j][cost]+val);
}
}
}
for(int j=0;j<=tot;j++){
for(int cost=0;cost<=k;cost++){
dp2[j][cost]=max(dp2[j][cost],new_dp[j][cost]);
new_dp[j][cost]=0;
}
}
}
vector<vector<int>>calc(n+1,vector<int>(10005));
for(int j=0;j<=tot;j++){
for(int cost=0;cost<=k;cost++){
if(cost){
calc[j][cost]=max({calc[j][cost],calc[j][cost-1],dp[j][cost]});
}
else{
calc[j][cost]=dp[j][cost];
}
}
}
int ans=0;
for(int i=0;i<=tot;i++){
for(int j=0;j<=tot-i;j++){
for(int cost=0;cost<=k;cost++){
int cost2=k-cost;
ans=max(ans,calc[i][cost2]+dp2[j][cost]);
}
}
}
int sum=ans;
sum*=-1;
for(int i=l;i<=r;i++){
sum+=a[i];
}
cout<<sum;
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int tc=1;
//~cin>>tc;
for(int _=0;_<tc;_++){
//~ cout<<"Case #"<<_+1<<": ";
solve();
if(_!=tc-1)
cout<<"\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
632 KB |
Output is correct |
2 |
Correct |
3 ms |
1016 KB |
Output is correct |
3 |
Correct |
3 ms |
888 KB |
Output is correct |
4 |
Correct |
3 ms |
1016 KB |
Output is correct |
5 |
Correct |
3 ms |
1016 KB |
Output is correct |
6 |
Correct |
3 ms |
1016 KB |
Output is correct |
7 |
Correct |
6 ms |
1528 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
632 KB |
Output is correct |
2 |
Correct |
3 ms |
1016 KB |
Output is correct |
3 |
Correct |
3 ms |
888 KB |
Output is correct |
4 |
Correct |
3 ms |
1016 KB |
Output is correct |
5 |
Correct |
3 ms |
1016 KB |
Output is correct |
6 |
Correct |
3 ms |
1016 KB |
Output is correct |
7 |
Correct |
6 ms |
1528 KB |
Output is correct |
8 |
Correct |
3 ms |
2552 KB |
Output is correct |
9 |
Correct |
4 ms |
2580 KB |
Output is correct |
10 |
Correct |
5 ms |
2552 KB |
Output is correct |
11 |
Correct |
6 ms |
2680 KB |
Output is correct |
12 |
Correct |
5 ms |
2680 KB |
Output is correct |
13 |
Correct |
105 ms |
4440 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
632 KB |
Output is correct |
2 |
Correct |
3 ms |
1016 KB |
Output is correct |
3 |
Correct |
3 ms |
888 KB |
Output is correct |
4 |
Correct |
3 ms |
1016 KB |
Output is correct |
5 |
Correct |
3 ms |
1016 KB |
Output is correct |
6 |
Correct |
3 ms |
1016 KB |
Output is correct |
7 |
Correct |
6 ms |
1528 KB |
Output is correct |
8 |
Correct |
3 ms |
2552 KB |
Output is correct |
9 |
Correct |
4 ms |
2580 KB |
Output is correct |
10 |
Correct |
5 ms |
2552 KB |
Output is correct |
11 |
Correct |
6 ms |
2680 KB |
Output is correct |
12 |
Correct |
5 ms |
2680 KB |
Output is correct |
13 |
Correct |
105 ms |
4440 KB |
Output is correct |
14 |
Correct |
4 ms |
2424 KB |
Output is correct |
15 |
Correct |
5 ms |
2424 KB |
Output is correct |
16 |
Correct |
5 ms |
2424 KB |
Output is correct |
17 |
Correct |
4 ms |
2424 KB |
Output is correct |
18 |
Correct |
5 ms |
2680 KB |
Output is correct |
19 |
Correct |
101 ms |
4568 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
632 KB |
Output is correct |
2 |
Correct |
3 ms |
1016 KB |
Output is correct |
3 |
Correct |
3 ms |
888 KB |
Output is correct |
4 |
Correct |
3 ms |
1016 KB |
Output is correct |
5 |
Correct |
3 ms |
1016 KB |
Output is correct |
6 |
Correct |
3 ms |
1016 KB |
Output is correct |
7 |
Correct |
6 ms |
1528 KB |
Output is correct |
8 |
Correct |
3 ms |
2552 KB |
Output is correct |
9 |
Correct |
4 ms |
2580 KB |
Output is correct |
10 |
Correct |
5 ms |
2552 KB |
Output is correct |
11 |
Correct |
6 ms |
2680 KB |
Output is correct |
12 |
Correct |
5 ms |
2680 KB |
Output is correct |
13 |
Correct |
105 ms |
4440 KB |
Output is correct |
14 |
Correct |
4 ms |
2424 KB |
Output is correct |
15 |
Correct |
5 ms |
2424 KB |
Output is correct |
16 |
Correct |
5 ms |
2424 KB |
Output is correct |
17 |
Correct |
4 ms |
2424 KB |
Output is correct |
18 |
Correct |
5 ms |
2680 KB |
Output is correct |
19 |
Correct |
101 ms |
4568 KB |
Output is correct |
20 |
Correct |
8 ms |
4600 KB |
Output is correct |
21 |
Correct |
6 ms |
4472 KB |
Output is correct |
22 |
Correct |
6 ms |
4520 KB |
Output is correct |
23 |
Correct |
8 ms |
4444 KB |
Output is correct |
24 |
Correct |
19 ms |
5324 KB |
Output is correct |
25 |
Correct |
686 ms |
8444 KB |
Output is correct |