Cod sursa(job #3211209)

Utilizator TEODOR_782IOACHIMCIUC TEODOR TEODOR_782 Data 8 martie 2024 18:51:17
Problema Deque Scor 25
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.63 kb
#include <iostream>
#include <fstream>
#include <deque>

using namespace std;

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

int main()
{
    deque<int> a;
    int n, v[5000001], s = 0, k;
    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;
}