Cod sursa(job #3214448)

Utilizator EdyIordacheIordache Eduard EdyIordache Data 14 martie 2024 09:40:36
Problema Deque Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.53 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>

using namespace std;

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

int a[5000001];

int main() {
    int n, k;
    fin>>n>>k;

    for (int i = 1; i <= n; i++) fin>>a[i];

    deque<int> q;
    q.push_front(1);
    int s = 0;
    for (int i = k; i <= n; i++) {
        while (!q.empty() && a[q.front()] >= a[i]) q.pop_front();
        q.push_front(i);

        if (q.back() == i - k) q.pop_back();
        s += a[q.back()];
    }
    fout<<s;

    return 0;
}