제출 #1327608

#제출 시각아이디문제언어결과실행 시간메모리
1327608candi_ositosFeast (NOI19_feast)C++20
63 / 100
203 ms16580 KiB
#include <bits/stdc++.h>
using namespace std;
void solve(){
    int n, k, tp=0;
    long long ts=0;
    cin>>n>>k;
    vector <pair <long long, pair <int, int> > > a(n), s;
    for(int i=0; i<n; ++i){
        cin>>a[i].first;
        a[i].second={i-1, i+1};
    }
    set <pair <long long, int> > nl, pl;
    s.push_back(a[0]);
    for(int i=1; i<n; ++i){
        if(a[i].first==0){
            continue;
        }
        if(a[i].first*a[i-1].first>0){
            s[s.size()-1].first+=a[i].first;
        }
        else{
            s.push_back(a[i]);
            s[s.size()-1].second={s.size()-2, s.size()};
        }
    }
    n=s.size();
    for(int i=0; i<n; ++i){
        if(s[i].first>0){
            pl.insert({s[i].first, i});
        }
        else{
            nl.insert({-s[i].first, i});
        }
    }
    while(k<pl.size()){
        pair <long long, int> d, e;
        for(auto i:pl){
            d=i;
            break;
        }
        for(auto i:nl){
            e=i;
            break;
        }
        int i;
        if(d.first<e.first){
            pl.erase(d);
            i=d.second;
        }
        else{
            nl.erase(e);
            i=e.second;
        }
        int l=s[i].second.first, r=s[i].second.second;
        if(l<0){
            s[r].second.first=l;
        }
        else if(r>=n){
            s[l].second.second=r;
        }
        else{
            s[i].first+=s[l].first+s[r].first;
            if(s[i].first>0){
                pl.erase({s[l].first, l});
                pl.erase({s[r].first, r});
                pl.insert({s[i].first, i});
            }
            else{
                nl.erase({-s[l].first, l});
                nl.erase({-s[r].first, r});
                nl.insert({-s[i].first, i});
            }
            s[i].second.second=s[r].second.second;
            s[i].second.first=s[l].second.first;
            l=s[i].second.first;
            r=s[i].second.second;
            if(l>=0){
                s[l].second.second=i;
            }
            if(r<n){
                s[r].second.first=i;
            }
        }
    }
    for(auto i:pl){
        ts+=i.first;
    }
    cout<<ts<<"\n";
    return;
}
int main(){
    int T=1;
    //cin>>T;
    while(T--){
        solve();
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...