#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define a first
#define b second
#define pb push_back
typedef long long llo;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
llo ma=0;
llo n,k;
cin>>n>>k;
llo it[n];
for(llo i=0;i<n;i++){
cin>>it[i];
//cout<<i<<endl;
}
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[0]<0){
kk+=1;
}
if(aa[ll]<0){
ll-=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+=(llo)1;
}
if(i>kk and i<=ll){
le[i]=i-(llo)1;
}
else{
le[i]=(llo)-1;
}
}
for(llo i=0;i<aa.size();i++){
if(i>=kk and i<ll){
re[i]=i+(llo)1;
}
else{
re[i]=(llo)-1;
}
}
for(llo i=kk;i<=ll;i++){
if(aa[i]<0){
ac.insert({abs(aa[i]),i});
}
}
llo vis[aa.size()];
for(llo i=0;i<aa.size();i++){
vis[i]=(llo)0;
}
/* for(int i=0;i<aa.size();i++){
cout<<aa[i]<<" ";
}
cout<<endl;*/
while(tot>k){
auto xx=ac.begin();
llo ind=(*xx).b;
//cout<<ind<<endl;
ac.erase(xx);
if(le[ind]!=-1){
// ac.erase({abs(aa[le[ind]]),le[ind]});
aa[ind]+=aa[le[ind]];
aa[le[ind]]=0;
if(le[le[ind]]!=-1){
re[le[le[ind]]]=ind;
le[ind]=le[le[ind]];
}
else{
le[ind]=-1;
}
}
if(re[ind]!=-1){
// ac.erase({abs(aa[re[ind]]),re[ind]});
aa[ind]+=aa[re[ind]];
aa[re[ind]]=0;
if(re[re[ind]]!=-1){
le[re[re[ind]]]=ind;
re[ind]=re[re[ind]];
}
else{
re[ind]=-1;
}
}
tot-=1;
if(aa[ind]<0){
ac.insert({abs(aa[ind]),ind});
}
}
for(llo i=0;i<aa.size();i++){
if(aa[i]>0){
ma+=aa[i];
}
}
cout<<ma<<endl;
return 0;
}
Compilation message
feast.cpp: In function 'int main()':
feast.cpp:73:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(llo i=0;i<aa.size();i++){
~^~~~~~~~~~
feast.cpp:84:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(llo i=0;i<aa.size();i++){
~^~~~~~~~~~
feast.cpp:99:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(llo i=0;i<aa.size();i++){
~^~~~~~~~~~
feast.cpp:145:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(llo i=0;i<aa.size();i++){
~^~~~~~~~~~
feast.cpp:98:6: warning: variable 'vis' set but not used [-Wunused-but-set-variable]
llo vis[aa.size()];
^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
46 ms |
2552 KB |
Output is correct |
2 |
Correct |
44 ms |
2808 KB |
Output is correct |
3 |
Correct |
46 ms |
2680 KB |
Output is correct |
4 |
Correct |
45 ms |
2680 KB |
Output is correct |
5 |
Correct |
45 ms |
2680 KB |
Output is correct |
6 |
Correct |
44 ms |
2680 KB |
Output is correct |
7 |
Correct |
45 ms |
2680 KB |
Output is correct |
8 |
Correct |
44 ms |
2680 KB |
Output is correct |
9 |
Correct |
43 ms |
2680 KB |
Output is correct |
10 |
Correct |
45 ms |
2680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
2688 KB |
Output is correct |
2 |
Correct |
27 ms |
2680 KB |
Output is correct |
3 |
Correct |
29 ms |
2680 KB |
Output is correct |
4 |
Correct |
25 ms |
2688 KB |
Output is correct |
5 |
Correct |
48 ms |
2552 KB |
Output is correct |
6 |
Correct |
25 ms |
2552 KB |
Output is correct |
7 |
Correct |
27 ms |
2680 KB |
Output is correct |
8 |
Correct |
44 ms |
2680 KB |
Output is correct |
9 |
Correct |
46 ms |
2560 KB |
Output is correct |
10 |
Correct |
25 ms |
2688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
51 ms |
2680 KB |
Output is correct |
2 |
Correct |
49 ms |
2680 KB |
Output is correct |
3 |
Correct |
50 ms |
2680 KB |
Output is correct |
4 |
Correct |
47 ms |
2680 KB |
Output is correct |
5 |
Correct |
48 ms |
2680 KB |
Output is correct |
6 |
Correct |
48 ms |
2808 KB |
Output is correct |
7 |
Correct |
47 ms |
2680 KB |
Output is correct |
8 |
Correct |
50 ms |
2680 KB |
Output is correct |
9 |
Correct |
50 ms |
2660 KB |
Output is correct |
10 |
Correct |
49 ms |
2680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
46 ms |
2552 KB |
Output is correct |
2 |
Correct |
44 ms |
2808 KB |
Output is correct |
3 |
Correct |
46 ms |
2680 KB |
Output is correct |
4 |
Correct |
45 ms |
2680 KB |
Output is correct |
5 |
Correct |
45 ms |
2680 KB |
Output is correct |
6 |
Correct |
44 ms |
2680 KB |
Output is correct |
7 |
Correct |
45 ms |
2680 KB |
Output is correct |
8 |
Correct |
44 ms |
2680 KB |
Output is correct |
9 |
Correct |
43 ms |
2680 KB |
Output is correct |
10 |
Correct |
45 ms |
2680 KB |
Output is correct |
11 |
Correct |
25 ms |
2688 KB |
Output is correct |
12 |
Correct |
27 ms |
2680 KB |
Output is correct |
13 |
Correct |
29 ms |
2680 KB |
Output is correct |
14 |
Correct |
25 ms |
2688 KB |
Output is correct |
15 |
Correct |
48 ms |
2552 KB |
Output is correct |
16 |
Correct |
25 ms |
2552 KB |
Output is correct |
17 |
Correct |
27 ms |
2680 KB |
Output is correct |
18 |
Correct |
44 ms |
2680 KB |
Output is correct |
19 |
Correct |
46 ms |
2560 KB |
Output is correct |
20 |
Correct |
25 ms |
2688 KB |
Output is correct |
21 |
Correct |
51 ms |
2680 KB |
Output is correct |
22 |
Correct |
49 ms |
2680 KB |
Output is correct |
23 |
Correct |
50 ms |
2680 KB |
Output is correct |
24 |
Correct |
47 ms |
2680 KB |
Output is correct |
25 |
Correct |
48 ms |
2680 KB |
Output is correct |
26 |
Correct |
48 ms |
2808 KB |
Output is correct |
27 |
Correct |
47 ms |
2680 KB |
Output is correct |
28 |
Correct |
50 ms |
2680 KB |
Output is correct |
29 |
Correct |
50 ms |
2660 KB |
Output is correct |
30 |
Correct |
49 ms |
2680 KB |
Output is correct |
31 |
Correct |
5 ms |
384 KB |
Output is correct |
32 |
Correct |
5 ms |
384 KB |
Output is correct |
33 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
34 |
Halted |
0 ms |
0 KB |
- |