Cod sursa(job #1049597)

Utilizator gigel123Ionut. gigel123 Data 7 decembrie 2013 15:47:25
Problema Deque Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.51 kb
#include<fstream>
#include<deque>

using namespace std;

    ifstream f("deque.in");
    ofstream g("deque.out");
    deque <int> deq;
    int i,n,k,v[5000001];
    long long s=0;

int main()
{
    f>>n>>k;
    for(i=1;i<=n;i++){
        f>>v[i];
        while(deq.empty()==0 && v[i]<v[deq.back()])
            deq.pop_back();
        deq.push_back(i);
        if(deq.front()==i-k)
            deq.pop_front();
        if(i>=k)
            s+=v[deq.front()];


    }
    g<<s;
    return 0;
}