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 <iostream>
#include <bits/stdc++.h>
using namespace std;
#define a first
#define b second
#define pb push_back
typedef long long llo;
llo mod=1000000007;
/*llo dp[2001][2001];
llo cost[2001][2001];*/
llo ma=0;
/*llo dnc(llo ind,llo l,llo r,llo ll,llo rr){
llo mid=(l+r)/2;
pair<llo,llo> best={-1,-1};
for(llo i=ll;i<=rr;i++){
if(i>mid){
continue;
}
if(i==ll){
if(i==0){
best={-cost[0][mid],i};
}
else{
best={-dp[i-1][ind-1]-cost[i][mid],i};
}
}
else{
best=min(best,{-dp[i-1][ind-1]-cost[i][mid],i});
}
}
dp[mid][ind]=-best.a;
ma=max(ma,dp[mid][ind]);
if(mid>l){
dnc(ind,l,mid-1,ll,best.b);
}
if(mid<r){
dnc(ind,mid+1,r,best.b,rr);
}
}*/
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
llo n,k;
cin>>n>>k;
llo it[n];
for(llo i=0;i<n;i++){
cin>>it[i];
}
if(k==1){
llo su=0;
for(llo i=0;i<n;i++){
su+=it[i];
su=max(su,(llo)0);
ma=max(ma,su);
}
cout<<ma<<endl;
return 0;
}
llo ind=0;
vector<llo> aa;
while(ind<n){
if(it[ind]<=0){
llo su=0;
while(ind<n){
if(it[ind]>0){
break;
}
su+=it[ind];
ind+=1;
}
aa.pb(su);
}
if(it[ind]>0){
llo su=0;
while(ind<n){
if(it[ind]<0){
break;
}
su+=it[ind];
ind+=1;
}
aa.pb(su);
}
}
set<pair<llo,llo>> ac;
llo kk=0;
llo ll=aa.size()-1;
/* if(aa[aa.size()-1]<0){
ll-=1;
}
if(aa[0]<0){
kk+=1;
}*/
llo le[aa.size()];
llo re[aa.size()];
llo tot=0;
for(llo i=0;i<aa.size();i++){
if(aa[i]>0){
tot+=1;
}
if(i>kk and i<=ll){
le[i]=i-1;
}
else{
le[i]=-1;
}
}
for(llo i=0;i<aa.size();i++){
if(i>=kk and i<ll){
re[i]=i+1;
}
else{
re[i]=-1;
}
}
for(llo i=kk;i<=ll;i++){
ac.insert({abs(aa[i]),i});
}
while(tot>k ){
auto xx=ac.begin();
llo ind=(*xx).b;
ac.erase(xx);
int st=1;
int x=2;
if(le[ind]!=-1){
if(aa[ind]>0 and le[le[ind]]==-1){
// st=0;
x-=1;
}
else if(aa[ind]<0 and re[ind]==-1){
st=0;
}
else{
ac.erase({aa[le[ind]],le[ind]});
aa[ind]+=aa[le[ind]];
// le[ind]=-1;
if(le[le[ind]]!=-1){
le[ind]=le[le[ind]];
re[le[le[ind]]]=ind;
}
else{
le[ind]=-1;
}
}
}
if(re[ind]!=-1){
if(aa[ind]>0 and re[re[ind]]==-1){
// st=0;
x-=1;
}
else if(aa[ind]<0 and le[ind]==-1){
st=0;
}
else{
ac.erase({aa[re[ind]],re[ind]});
aa[ind]+=aa[re[ind]];
// re[ind]=-1;
if(re[re[ind]]!=-1){
re[ind]=re[re[ind]];
le[re[re[ind]]]=ind;
}
else{
re[ind]=-1;
}
}
}
if(st or x){
tot-=1;
ac.insert({abs(aa[ind]),ind});
}
}
for(llo i=kk;i<=ll;i++){
if(aa[i]>0){
ma+=aa[i];
}
}
cout<<ma<<endl;
/* llo j=0;
for(llo i=0;i<aa.size();i++){
if(aa[i]>0){
llo ma=0;
llo su=0;
llo ind=i;
llo ind2=j;
while(ind>=0){
su+=aa[ind];
ma=max(ma,su);
if(aa[ind]>0){
cost[ind2][j]=ma;
ind2-=1;
}
ind-=1;
}
j+=1;
}
}
for(llo i=0;i<j;i++){
dp[i][0]=cost[0][i];
ma=max(ma,cost[0][i]);
}
for(llo jj=1;jj<min(j,k);jj++){
dnc(jj,0,j-1,0,j-1);
}
cout<<ma<<endl;
*/
return 0;
}
Compilation message (stderr)
feast.cpp: In function 'int main()':
feast.cpp:100:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(llo i=0;i<aa.size();i++){
~^~~~~~~~~~
feast.cpp:111:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(llo i=0;i<aa.size();i++){
~^~~~~~~~~~
# | 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... |