Cod sursa(job #2887169)

Utilizator DragosG12Ghinea Dragos DragosG12 Data 8 aprilie 2022 22:22:07
Problema Deque Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.78 kb
#include<fstream>
using namespace std;

int v[5000005];
int deque[5000005];
int front=0, back=0;

int main() {
    ifstream fin("deque.in");
    ofstream fout("deque.out");

    int n,k;
    fin>>n>>k;

    long long solutie=0;

    for(int i=0;i<k-1;i++){
        fin>>v[i];
        while(back>=front && v[i]<=v[deque[back]]){
            back--;
        }

        back++;
        deque[back]=i;
    }

    for(int i=k-1;i<n;i++){
        fin>>v[i];
        while(back>=front && v[i]<=v[deque[back]]){
            back--;
        }

        back++;
        deque[back]=i;

        solutie+=v[deque[front]];
        if(deque[front]==i-k+1){
            front++;
        }
    }

    fout<<solutie;

    fout.close();
    fin.close();

    return 0;
}