-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path163.cpp
executable file
·68 lines (56 loc) · 1.3 KB
/
163.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
typedef pair<int,int> PII;
const int N = 100010;
int n,m,l[N],a[N],r[N];
bool st[N];
priority_queue<PII,vector<PII>,greater<PII> > heap;
void remove(int x)
{
l[r[x]]=l[x];
r[l[x]]=r[x];
st[x]=1;
}
int main()
{
cin>>n>>m;
int k=0;
for(int i=1;i<=n;i++)
{
int xi;
scanf("%d",&xi);
if(!xi)continue;
if(!k ||(long long) a[k] * xi < 0 )a[++k]+=xi;
else a[k]+=xi;
}
// for(int i=1;i<=k;i++)printf("%d ",a[i]);
// puts("");
int res=0,cnt=0;
for(int i=1;i<=k;i++)
{
l[i]=i-1;
r[i]=i+1;
heap.push( {abs(a[i]), i });
if(a[i]>0)res+=a[i],cnt++;
}
while( cnt>m )
{
PII u=heap.top() ;
heap.pop() ;
if(st[u.second])continue;
if(a[u.second]>0 || (l[u.second]!=0 && r[u.second]!=k+1) )
{
// printf("%d\n",u.first);
res-=u.first;
a[u.second]+=a[ l[u.second] ] + a[ r[u.second] ];
heap.push( { abs(a[u.second]) , u.second } );
remove(r[u.second]);
remove(l[u.second]);
cnt--;
}
}
cout<<res;
}