1 #include<cstdio> 2 #include<algorithm> 3 #include<queue> 4 #define LL long long 5 using namespace std; 6 const int maxn=1000010; 7 LL pre[maxn],nxt[maxn],a[maxn],n,m,x,ans=0; 8 bool mark[maxn]; 9 priority_queue<pair<LL,LL>,vector<pair<LL,LL> > >q; 10 inline int read(){ 11 int k=0,f=1; char c=getchar(); 12 while(c<‘0‘||c>‘9‘)c==‘-‘&&(f=-1),c=getchar(); 13 while(‘0‘<=c&&c<=‘9‘)k=k*10+c-‘0‘,c=getchar(); 14 return k*f; 15 } 16 void del(int x){ 17 mark[x]=1; 18 pre[nxt[x]]=pre[x]; nxt[pre[x]]=nxt[x]; 19 pre[x]=nxt[x]=0; 20 } 21 int main(){ 22 n=read(); m=read(); 23 for(int i=1;i<=n;i++) a[i]=read(),q.push(make_pair(a[i],i)); 24 if(m>n/2){puts("ERROR"); return 0;} 25 for(int i=1;i<=n;i++) pre[i]=i-1,nxt[i]=i+1; //pre[1]=n; nxt[n]=1; 26 for(int i=1;i<=m;i++){ 27 while(mark[q.top().second]) q.pop(); 28 int tmp=q.top().second; q.pop(); 29 if(a[tmp]<0) break; 30 ans+=a[tmp]; 31 a[tmp]=a[pre[tmp]]+a[nxt[tmp]]-a[tmp]; q.push(make_pair(a[tmp],tmp)); 32 del(pre[tmp]); del(nxt[tmp]); 33 } 34 return printf("%lld\n",ans),0; 35 }