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 ll long long
#define pll pair<ll, ll>
#define pii pair<int, int>
#define fs first
#define sc second
#define pb push_back
void solve() {
int n,k;
cin>>n>>k;
ll a[n];
for(int i=0;i<n;i++)cin>>a[i];
vector<pair<pll,pll>> v;
ll sum=0,s=a[0]>=0,l=0,cnt=0,ans=0;
for(int i=0;i<n;i++){
if(s^(a[i]>=0)){
v.pb({{abs(sum),s},{l,i-1}});
if(s){
cnt++;
ans+=sum;
}
s=!s;
sum=0;
l=i;
}
sum+=a[i];
}
v.pb({{abs(sum),s},{l,n-1}});
if(s){
cnt++;
ans+=sum;
}
bool b[n];
for(int i=0;i<n;i++)b[i]=a[i]>=0;
sort(v.begin(),v.end());
int idx=0;
while(cnt>k){
if(idx==v.size())break;
auto[x,y]=v[idx].fs;
auto[l,r]=v[idx].sc;
idx++;
if(y){
if(l>0&&b[l-1]||r<n-1&&b[r+1])continue;
for(int i=l;i<=r;i++)b[i]=0;
ans-=x;
}else{
if(!l||!b[l-1]||r==n-1||!b[r+1])continue;
for(int i=l;i<=r;i++)b[i]=1;
ans-=x;
}
cnt--;
}
cout<<ans;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
solve();
}
Compilation message (stderr)
feast.cpp: In function 'void solve()':
feast.cpp:41:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<std::pair<long long int, long long int>, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
41 | if(idx==v.size())break;
| ~~~^~~~~~~~~~
feast.cpp:46:19: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
46 | if(l>0&&b[l-1]||r<n-1&&b[r+1])continue;
| ~~~^~~~~~~~
# | 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... |