Cod sursa(job #2787357)

Utilizator RaresLiscanLiscan Rares RaresLiscan Data 23 octombrie 2021 10:06:57
Problema Deque Scor 25
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.74 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin ("deque.in");
ofstream fout ("deque.out");
deque <pair <int, int> > q;

int main()
{
    int n, k;
    fin >> n >> k;
    int sum = 0;
    for (int i = 1; i <= n; i ++) {
        int a;
        fin >> a;
        if (!q.empty()) {
            while (!q.empty() && q.front().second <= i - k) {
                q.pop_front();
            }
            while (!q.empty() && q.back().first >= a) {
                q.pop_back();
            }
            q.push_back(make_pair(a, i));
        }
        else {
            q.push_back(make_pair(a, i));
        }
        if (i >= k) {
            sum += q.front().first;
        }
    }
    fout << sum;
    return 0;
}