Cod sursa(job #1202894)

Utilizator AdrianaMAdriana Moisil AdrianaM Data 30 iunie 2014 00:49:03
Problema Deque Scor 100
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;
long long answ, x[5000001];
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;
}