This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define pii pair<int,int>
#define ll long long
int a[311111];
priority_queue<ll,vector<ll>,greater<ll>> pq;
int main()
{
int n,k;
cin>>n>>k;
ll ans=0,cnt=0,cur=0;
for(int i=1; i<=n; i++){
cin>>a[i];
if(a[i]>=0&&a[i-1]>=0) {
cur+=a[i];
}
else if(a[i]<0&&a[i-1]<0)
{
cur-=a[i];
}
else if(a[i]>=0&&a[i-1]<0){
pq.push(cur);
cur=a[i];
}
else if(a[i]<0&&a[i-1]>=0)
{
pq.push(cur);
ans+=cur;
cnt++;
cur=-a[i];
}
//cout<<ans<<'\n';
}
if(a[n]>=0) pq.push(cur),cnt++,ans+=cur;
//cout<<'\n';
while(cnt>k)
{
//cout<<ans<<'\n';
int now=pq.top();
ans-=now;
cnt--;
pq.pop();
}
cout<<max(ans,(ll)0);
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |