# 编译优化已弄懂，详解

## 编译优化

7 3
1 2 3 4 5 6 7

7 4
1 2 3 4 5 6 7

15

Error!

N的大小对于不同数据有所不同：

1 40 11 2013
2 45 12 5000
3 50 13 10000
4 55 14 49999
5 200 15 111111
6 200 16 148888
7 1000 17 188888
8 2010 18 199999
9 2011 19 199999
10 2012 20 200000

remove!!!

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
#define INF 10000
#define N 200001
#define PER(i,a,b) for(int i=a;i<=b;i++)
int n,t[2*N],pos[N],l[N*2],r[N*2],val[N],fa,m,ans;
void up(int x)
{
while(val[t[x]]>val[t[x/2]]&&x>1) {swap(t[x],t[x/2]);swap(pos[t[x]],pos[t[x/2]]);x/=2;}
}
void down(int x)
{
int j;
while(x*2<=n)
{
if(x*2==n||val[t[x*2]]>=val[t[x*2+1]]) j=x*2;else j=x*2+1;
if(val[t[j]]>=val[t[x]])
{
swap(t[j],t[x]);swap(pos[t[j]],pos[t[x]]);
}
else break;
x=j;
}
}
int main()
{
scanf("%d%d",&n,&m);
if(n/2<m)
{
cout<<"Error!\n";
return 0;
}
PER(i,1,n)
{
scanf("%d",&val[i]);
pos[i]=i;
t[i]=i;
up(i);
l[i]=i-1;
r[i]=i+1;
}
l[1]=n;r[n]=1;
while(m--)
{
int x=t[1];
ans+=val[x];
val[x]=val[l[x]]+val[r[x]]-val[x];
val[l[x]]=-INF;
down(pos[l[x]]);val[r[x]]=-INF;down(pos[r[x]]);down(1);
l[x]=l[l[x]];r[x]=r[r[x]];
r[l[x]]=x;l[r[x]]=x;
}
cout<<ans<<"\n";
}
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556

03-14 5541

11-18 1534
05-14 935
11-03 1万+
08-12 2514
08-24 2110
07-30 7558
06-09 3812
12-20 2247
01-24 3257
01-19 2万+
11-26 1184
03-05 346
01-20 101
09-23 1062