Cod sursa(job #1202892)

Utilizator AdrianaMAdriana Moisil AdrianaM Data 30 iunie 2014 00:41:29
Problema Deque Scor 25
Compilator cpp Status done
Runda Arhiva educationala Marime 0.58 kb
#include <fstream>
#include <deque>
using namespace std;

ifstream is("deque.in");
ofstream os("deque.out");

int n, m, answ;
int x[5000000];
deque<int> q;

int main()
{
    is >> n >> m;
    for ( int i = 1; i <= n; ++i )
    {
        is >> x[i];
        while ( !q.empty() && x[i] <= x[q.back()] )
            q.pop_back();
        q.push_back(i);
        if ( i < m )
            continue;
        if ( i - q.front() + 1 > m )
            q.pop_front();
        answ += x[q.front()];
    }
    os << answ;
    is.close();
    os.close();
    return 0;
}