#include<bits/stdc++.h>
using namespace std;
int n;
int lim;
int a[200005];
namespace sub12567
{
long long dp[5005][5005];
void solve()
{
for(int i=1;i<=n;i++)
for(int j=1;j<=5000;j++)
dp[i][j]=1e18;
for(int i=0;i<=5000;i++)dp[1][i]=abs(a[1]-i);
for(int i=2;i<=n;i++)
{
deque<int>dq;
int dd=0;
for(int j=0;j<=5000;j++)
{
int l=max(0,j-lim),r=min(5000,j+lim);
while(dd<=r)
{
while(dq.size()&&dp[i-1][dq.back()]>=dp[i-1][dd])dq.pop_back();
dq.push_back(dd);
dd++;
}
while(dq.size()&&dq.front()<l)dq.pop_front();
if(dq.size())dp[i][j]=dp[i-1][dq.front()]+abs(a[i]-j);
}
}
long long ans=1e18;
for(int i=0;i<=5000;i++)ans=min(ans,dp[n][i]);
cout<<ans;
}
}
namespace sub4
{
pair<long long,int>tree[200005];
void upd(int id,long long val,int cnt)
{
while(id<=n){tree[id].first+=val;tree[id].second+=cnt;id+=(id&(-id));}
}
pair<long long,int>get(int id)
{
pair<long long,int>ans={0,0};
while(id>0){ans.first+=tree[id].first;ans.second+=tree[id].second;id-=(id&(-id));}
return ans;
}
void solve()
{
vector<int>vec;
for(int i=1;i<=n;i++)vec.push_back(a[i]);
sort(vec.begin(),vec.end());
vec.erase(unique(vec.begin(),vec.end()),vec.end());
for(int i=1;i<=n;i++)
{
int k=lower_bound(vec.begin(),vec.end(),a[i])-vec.begin()+1;
upd(k,a[i],1);
}
long long ans=1e18;
for(int i=1;i<=n;i++)
{
long long sum=0;
int k=lower_bound(vec.begin(),vec.end(),a[i])-vec.begin()+1;
pair<long long,int>vv=get(k);
sum+=1LL*vv.second*a[i]-vv.first;
vv={get(n).first-vv.first,get(n).second-vv.second};
sum-=1LL*vv.second*a[i]-vv.first;
ans=min(ans,sum);
}
cout<<ans;
}
}
int maxx=0;
int main()
{
//freopen("safety.inp","r",stdin);
//freopen("safety.out","w",stdout);
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n>>lim;
for(int i=1;i<=n;i++){cin>>a[i];maxx=max(maxx,a[i]);}
if(n<=5000&&maxx<=5000){sub12567::solve();return 0;}
if(lim==0){sub4::solve();return 0;}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4440 KB |
Output is correct |
4 |
Correct |
2 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
2 ms |
4440 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
2 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4568 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
2396 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
60 ms |
5848 KB |
Output is correct |
2 |
Correct |
83 ms |
10012 KB |
Output is correct |
3 |
Correct |
87 ms |
10192 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4440 KB |
Output is correct |
4 |
Correct |
2 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
2 ms |
4440 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
2 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4568 KB |
Output is correct |
13 |
Correct |
24 ms |
24920 KB |
Output is correct |
14 |
Correct |
29 ms |
23128 KB |
Output is correct |
15 |
Correct |
21 ms |
22872 KB |
Output is correct |
16 |
Correct |
24 ms |
24920 KB |
Output is correct |
17 |
Correct |
21 ms |
20824 KB |
Output is correct |
18 |
Correct |
18 ms |
18776 KB |
Output is correct |
19 |
Correct |
23 ms |
22876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4440 KB |
Output is correct |
4 |
Correct |
2 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
2 ms |
4440 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
2 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4568 KB |
Output is correct |
13 |
Correct |
24 ms |
24920 KB |
Output is correct |
14 |
Correct |
29 ms |
23128 KB |
Output is correct |
15 |
Correct |
21 ms |
22872 KB |
Output is correct |
16 |
Correct |
24 ms |
24920 KB |
Output is correct |
17 |
Correct |
21 ms |
20824 KB |
Output is correct |
18 |
Correct |
18 ms |
18776 KB |
Output is correct |
19 |
Correct |
23 ms |
22876 KB |
Output is correct |
20 |
Correct |
21 ms |
22876 KB |
Output is correct |
21 |
Correct |
24 ms |
24924 KB |
Output is correct |
22 |
Correct |
20 ms |
20824 KB |
Output is correct |
23 |
Correct |
17 ms |
18776 KB |
Output is correct |
24 |
Correct |
22 ms |
22876 KB |
Output is correct |
25 |
Correct |
18 ms |
19032 KB |
Output is correct |
26 |
Correct |
23 ms |
24924 KB |
Output is correct |
27 |
Correct |
23 ms |
23040 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4440 KB |
Output is correct |
4 |
Correct |
2 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
2 ms |
4440 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
2 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4568 KB |
Output is correct |
13 |
Correct |
24 ms |
24920 KB |
Output is correct |
14 |
Correct |
29 ms |
23128 KB |
Output is correct |
15 |
Correct |
21 ms |
22872 KB |
Output is correct |
16 |
Correct |
24 ms |
24920 KB |
Output is correct |
17 |
Correct |
21 ms |
20824 KB |
Output is correct |
18 |
Correct |
18 ms |
18776 KB |
Output is correct |
19 |
Correct |
23 ms |
22876 KB |
Output is correct |
20 |
Correct |
21 ms |
22876 KB |
Output is correct |
21 |
Correct |
24 ms |
24924 KB |
Output is correct |
22 |
Correct |
20 ms |
20824 KB |
Output is correct |
23 |
Correct |
17 ms |
18776 KB |
Output is correct |
24 |
Correct |
22 ms |
22876 KB |
Output is correct |
25 |
Correct |
18 ms |
19032 KB |
Output is correct |
26 |
Correct |
23 ms |
24924 KB |
Output is correct |
27 |
Correct |
23 ms |
23040 KB |
Output is correct |
28 |
Correct |
301 ms |
197660 KB |
Output is correct |
29 |
Correct |
193 ms |
168532 KB |
Output is correct |
30 |
Correct |
220 ms |
197712 KB |
Output is correct |
31 |
Correct |
208 ms |
164648 KB |
Output is correct |
32 |
Correct |
187 ms |
164640 KB |
Output is correct |
33 |
Correct |
251 ms |
197480 KB |
Output is correct |
34 |
Correct |
221 ms |
195448 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4440 KB |
Output is correct |
4 |
Correct |
2 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
2 ms |
4440 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
2 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4568 KB |
Output is correct |
13 |
Incorrect |
0 ms |
2396 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4440 KB |
Output is correct |
4 |
Correct |
2 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
2 ms |
4440 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
2 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4568 KB |
Output is correct |
13 |
Incorrect |
0 ms |
2396 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |