题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2151
题解:此题=数据备份。喜闻乐见挂链表。
代码:
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<string>
#define inf 1000000000
#define maxn 200000+5
#define maxm 100000+5
#define eps 1e-10
#define ll long long
#define pa pair<int,int>
#define for0(i,n) for(int i=0;i<=(n);i++)
#define for1(i,n) for(int i=1;i<=(n);i++)
#define for2(i,x,y) for(int i=(x);i<=(y);i++)
#define for3(i,x,y) for(int i=(x);i>=(y);i--)
#define for4(i,x) for(int i=head[x],y=e[i].go;i;i=e[i].next,y=e[i].go)
#define mod 1000000007
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=*x+ch-'';ch=getchar();}
return x*f;
}
int n,m,a[maxn],l[maxn],r[maxn];
priority_queue<pa>q;
int main()
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
n=read();m=read();
if(m>n>>){printf("Error!\n");return ;}
for1(i,n)a[i]=read(),l[i]=i-,r[i]=i+,q.push(pa(a[i],i));
l[]=n;r[n]=;
int ans=;
for1(i,m)
{
while(!q.empty()&&q.top().first!=a[q.top().second])q.pop();
int x=q.top().second;q.pop();
ans+=a[x];
a[x]=a[l[x]]+a[r[x]]-a[x];q.push(pa(a[x],x));
a[l[x]]=a[r[x]]=inf;
r[l[x]=l[l[x]]]=x;l[r[x]=r[r[x]]]=x;
}
cout<<ans<<endl;
return ;
}