Cod sursa(job #3211231)

Utilizator TEODOR_782IOACHIMCIUC TEODOR TEODOR_782 Data 8 martie 2024 19:05:10
Problema Deque Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.65 kb
#include <iostream>
#include <fstream>
#include <deque>

using namespace std;

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

int main()
{
    deque<int> a;
    int v[5000001], k;
    long long s = 0, n;
    in >> n >> k;
    for(int i = 0; i < n; i++)
        in >> v[i];

    for(int i = 0; i < n; i++)
    {
        while(!a.empty() && (v[i] < v[a.back()]))
        {
            a.pop_back();
        }
        a.push_back(i);
        if(i == a.front() + k)
        {
            a.pop_front();
        }
        if(i >= k - 1)
        {
            s = s + v[a.front()];
        }
    }
    out << s;
    return 0;
}