/*
khoi orz, go check out his algo
-normie-
Tested with library-checker.
*/
#include <bits/stdc++.h>
using namespace std;
#define FILE_IN "birds.inp"
#define FILE_OUT "birds.out"
#define prev prv
#define ofile freopen(FILE_IN,"r",stdin);freopen(FILE_OUT,"w",stdout)
#define fio ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define nfio cin.tie(0);cout.tie(0)
#define max(x,y) (((x)>(y))?(x):(y))
#define min(x,y) (((x)<(y))?(x):(y))
#define ord(a,b,c) ((a>=b)and(b>=c))
#define MOD (ll(1000000007))
#define MAX 300001
#define mag 320
#define p1 first
#define p2 second.first
#define p3 second.second
#define fi first
#define se second
#define pow2(x) (ll(1)<<x)
#define pll pair<ll,ll>
#define plll pair<ll,pll>
#define For(i,__,___) for(int i=__;i<=___;i++)
#define Rep(i,__,___) for(int i=__;i>=___;i--)
#define ordered_set tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>
#define bi BigInt
typedef long long ll;
#define pi 3.1415926535897
//------START-----------//
struct slope_func_left
{
public:
ll a,b,shft;
priority_queue<ll> bp;
slope_func_left(ll aa=0, ll bb=0, vector<ll> v={})
{
a=aa;
b=bb;
shft=0;
for (ll g : v) bp.push(g);
}
void reset (ll aa, ll bb, vector<ll> v)
{
a=aa;
b=bb;
shft=0;
for (ll g : v) bp.push(g);
}
void slope_add (ll aa, ll bb, vector<ll> v)
{
a+=aa;
b+=bb+aa*shft;
for (ll g : v) bp.push(g-shft);
}
void add_shift(ll a)
{
shft+=a;
}
void slope_min()
{
while(a+bp.size()>0) bp.pop();
}
};
struct slope_func_right
{
public:
ll a,b,shft;
priority_queue<ll, vector<ll>,greater<ll>> bp;
slope_func_right(ll aa=0, ll bb=0, vector<ll> v={})
{
a=aa;
b=bb;
shft=0;
for (ll g : v) bp.push(g);
}
void reset (ll aa, ll bb, vector<ll> v)
{
a=aa;
b=bb;
shft=0;
for (ll g : v) bp.push(g);
}
void slope_add (ll aa, ll bb, vector<ll> v)
{
a+=aa;
b+=bb-aa*shft;
for (ll g : v) bp.push(g-shft);
}
void add_shift(ll aa)
{
shft+=aa;
}
void slope_min()
{
while(a+bp.size()>0) bp.pop();
}
};
ll n,m,i,j,k,u,v,t,t1,a,b,l,r,ress=0;
ll al[200001],ar[200001];
slope_func_left le; slope_func_right ri;
//------END-----------//
int main()
{
// ofile;
fio;
cin>>n>>m;
for (i=1;i<=n;i++) cin>>al[i],ar[i]=al[i]+m;
le.reset(0,0,{});
ri.reset(0,0,{});
for (i=1;i<=n;i++)
{
if (i>1)
{
le.add_shift(-(ar[i]-al[i]));
ri.add_shift((ar[i-1]-al[i-1]));
}
// if (i>1) cout<<le.bp.top()<<endl;
if ((le.bp.size()==0)or(al[i]<le.bp.top()+le.shft))
{
le.slope_add(-1,al[i],{al[i],al[i]});
ll bruh=le.bp.top()+le.shft;
// cout<<"left to right "<<bruh<<endl;
le.bp.pop();
ri.slope_add(0,0,{bruh});
}
else
{
ri.slope_add(0,0,{al[i],al[i]});
ll bruh=ri.bp.top()+ri.shft;
// cout<<"right to left "<<bruh<<endl;
ri.bp.pop();
le.slope_add(-1,al[i],{bruh});
}
}
// cout<<le.a<<' '<<le.b<<endl;
ll res=le.b;
for (i=0;i<abs(le.a);i++)
{
ll x=le.bp.top()+le.shft;
le.bp.pop();
ll y;
if (i==abs(le.a)-1) y=le.shft; else y=le.bp.top()+le.shft;
// cout<<i<<' '<<x<<' '<<y<<endl;
res-=(i+1)*(x-y);
}
cout<<res;
}
// 2 5
// -2 2
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
61 ms |
6900 KB |
Output is correct |
2 |
Correct |
88 ms |
8380 KB |
Output is correct |
3 |
Correct |
91 ms |
8508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
492 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
492 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
492 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
492 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
3 ms |
748 KB |
Output is correct |
29 |
Correct |
3 ms |
620 KB |
Output is correct |
30 |
Correct |
2 ms |
620 KB |
Output is correct |
31 |
Correct |
3 ms |
492 KB |
Output is correct |
32 |
Correct |
2 ms |
748 KB |
Output is correct |
33 |
Correct |
4 ms |
620 KB |
Output is correct |
34 |
Correct |
2 ms |
620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
492 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
1 ms |
492 KB |
Output is correct |
31 |
Correct |
1 ms |
364 KB |
Output is correct |
32 |
Correct |
1 ms |
364 KB |
Output is correct |
33 |
Correct |
1 ms |
364 KB |
Output is correct |
34 |
Correct |
3 ms |
748 KB |
Output is correct |
35 |
Correct |
3 ms |
620 KB |
Output is correct |
36 |
Correct |
2 ms |
620 KB |
Output is correct |
37 |
Correct |
3 ms |
492 KB |
Output is correct |
38 |
Correct |
2 ms |
748 KB |
Output is correct |
39 |
Correct |
4 ms |
620 KB |
Output is correct |
40 |
Correct |
2 ms |
620 KB |
Output is correct |
41 |
Correct |
3 ms |
620 KB |
Output is correct |
42 |
Correct |
2 ms |
492 KB |
Output is correct |
43 |
Correct |
2 ms |
620 KB |
Output is correct |
44 |
Correct |
3 ms |
620 KB |
Output is correct |
45 |
Correct |
2 ms |
620 KB |
Output is correct |
46 |
Correct |
3 ms |
620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
61 ms |
6900 KB |
Output is correct |
20 |
Correct |
88 ms |
8380 KB |
Output is correct |
21 |
Correct |
91 ms |
8508 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
364 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
1 ms |
364 KB |
Output is correct |
31 |
Correct |
1 ms |
492 KB |
Output is correct |
32 |
Correct |
1 ms |
364 KB |
Output is correct |
33 |
Correct |
1 ms |
492 KB |
Output is correct |
34 |
Correct |
1 ms |
364 KB |
Output is correct |
35 |
Correct |
1 ms |
364 KB |
Output is correct |
36 |
Correct |
1 ms |
364 KB |
Output is correct |
37 |
Correct |
3 ms |
748 KB |
Output is correct |
38 |
Correct |
3 ms |
620 KB |
Output is correct |
39 |
Correct |
2 ms |
620 KB |
Output is correct |
40 |
Correct |
3 ms |
492 KB |
Output is correct |
41 |
Correct |
2 ms |
748 KB |
Output is correct |
42 |
Correct |
4 ms |
620 KB |
Output is correct |
43 |
Correct |
2 ms |
620 KB |
Output is correct |
44 |
Correct |
3 ms |
620 KB |
Output is correct |
45 |
Correct |
2 ms |
492 KB |
Output is correct |
46 |
Correct |
2 ms |
620 KB |
Output is correct |
47 |
Correct |
3 ms |
620 KB |
Output is correct |
48 |
Correct |
2 ms |
620 KB |
Output is correct |
49 |
Correct |
3 ms |
620 KB |
Output is correct |
50 |
Correct |
95 ms |
8708 KB |
Output is correct |
51 |
Correct |
81 ms |
7884 KB |
Output is correct |
52 |
Correct |
64 ms |
7756 KB |
Output is correct |
53 |
Correct |
75 ms |
8400 KB |
Output is correct |
54 |
Correct |
80 ms |
8332 KB |
Output is correct |
55 |
Correct |
64 ms |
7940 KB |
Output is correct |
56 |
Correct |
70 ms |
8264 KB |
Output is correct |
57 |
Correct |
67 ms |
7760 KB |
Output is correct |
58 |
Correct |
56 ms |
6860 KB |
Output is correct |
59 |
Correct |
78 ms |
8128 KB |
Output is correct |