Submission #1328059

#TimeUsernameProblemLanguageResultExecution timeMemory
1328059GabrielFeast (NOI19_feast)C++20
71 / 100
1095 ms34728 KiB
#include "bits/stdc++.h"
using namespace std;
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n, k;
    cin>>n>>k;
    deque<long long> a;
    for(int i = 0; i < n; i++){
        long long v;
        cin>>v;
        if(v == 0) continue;
        if(a.empty()) a.push_back(v);
        else if((a.back() / abs(a.back())) == (v / abs(v))) a.back() += v;
        else a.push_back(v);
    }
    while(!a.empty() and a[0] < 0) a.pop_front();
    while(!a.empty() and a.back() < 0) a.pop_back();
    n = a.size();
    if(n == 0){
        cout<<0;
        return 0;
    }
    vector< vector< vector<long long> > > PD(2, vector< vector<long long> >(k + 1, vector<long long>(2, 0)));
    long long Respuesta = 0;
    for(int i = n - 1; i > -1; i--){
        for(int j = 1; j <= k; j++){
            PD[0][j][0] = max(PD[1][j][0], PD[1][j][1]);
            PD[0][j][1] = max(PD[1][j][1], PD[1][j - 1][0]) + a[i];
            Respuesta = max(Respuesta, max(PD[0][j][0], PD[0][j][1]));
        }
        PD[0][0][0] = max(PD[1][0][0], PD[1][0][1]);
        PD[0][0][1] = -2222222222222222;
        swap(PD[0], PD[1]);
    }
    cout<<Respuesta;
    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...