Cod sursa(job #1951360)

Utilizator radu.leonardoThe Doctor radu.leonardo Data 3 aprilie 2017 16:17:26
Problema Deque Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.67 kb
#include <bits/stdc++.h>
using namespace std;
int n,k;
long long s=0;
vector <int> V;
deque <int> DQ;

ifstream f("deque.in");
ofstream g("deque.out");

int main()
{
    f>>n>>k;
    for(int i=0;i<k;i++)
    {
        int x;
        f>>x;
        V.push_back(x);
        while( !DQ.empty() && V[DQ.back()]>=x ) DQ.pop_back();
        DQ.push_back(i);
    }

    for(int i=k,st=0;i<n;i++,st++)
    {
        s+=V[DQ.front()];
        int x;
        f>>x;
        V.push_back(x);
        if(DQ.front()==st) DQ.pop_front();
        while( !DQ.empty() && V[DQ.back()]>=x ) DQ.pop_back();
        DQ.push_back(i);
    }
    s+=V[DQ.front()];

    g<<s;

}