이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define a first
#define b second
#define pb push_back
typedef long long int llo;
llo mod=1000000007;
llo ma=0;
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[0]<0){
kk+=1;
}
if(aa[ll]<0){
ll-=1;
}
llo le[aa.size()];
llo re[aa.size()];
llo tot=0;
for(llo i=kk;i<=ll;i++){
if(aa[i]>0){
tot+=1;
}
if(i>kk and i<=ll){
le[i]=i-1;
}
else{
le[i]=-1;
}
}
for(llo i=kk;i<=ll;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});
}
int vis[aa.size()];
for(int i=0;i<aa.size();i++){
vis[i]=0;
}
while(tot>k){
auto xx=ac.begin();
llo ind=(*xx).b;
ac.erase(xx);
if(le[ind]!=-1){
vis[le[ind]]=1;
ac.erase({aa[le[ind]],le[ind]});
aa[ind]+=aa[le[ind]];
if(le[le[ind]]!=-1){
re[le[le[ind]]]=ind;
le[ind]=le[le[ind]];
}
else{
le[ind]=-1;
}
}
if(re[ind]!=-1){
vis[re[ind]]=1;
ac.erase({aa[re[ind]],re[ind]});
aa[ind]+=aa[re[ind]];
if(re[re[ind]]!=-1){
le[re[re[ind]]]=ind;
re[ind]=re[re[ind]];
}
else{
re[ind]=-1;
}
}
tot-=1;
ac.insert({abs(aa[ind]),ind});
}
for(llo i=kk;i<=ll;i++){
if(aa[i]>0 and vis[i]==0){
ma+=aa[i];
}
}
cout<<ma<<endl;
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
feast.cpp: In function 'int main()':
feast.cpp:94:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int 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... |