Cod sursa(job #2978767)

Utilizator MAlex2019Melintioi George Alexandru MAlex2019 Data 14 februarie 2023 11:52:49
Problema Deque Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.95 kb
#include <iostream>
#include <fstream>
#include <deque>

using namespace std;

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

const int maxn = 5e6;
int v[maxn];

void printdeque(deque<int> a) {
    while (!a.empty()){
        cout << v[a.front()] << ' ';
        a.pop_front();}
    cout << '\n';
}

int main() {
    int n, k;
    fin >> n >> k;
    for (int i = 0; i < n; i++)
        fin >> v[i];

    long long sum = 0;
    deque<int> goolah;
    goolah.push_back(0);
    for (int i = 1; i < n; i++) {
        while (!goolah.empty() && v[i] <= v[goolah.back()])
            goolah.pop_back();
        goolah.push_back(i);
        while (goolah.back() - goolah.front() >= k)
            goolah.pop_front();
       // printdeque(goolah);
        if (i >= k - 1) {
            sum += 1LL*v[goolah.front()];  
            //cout << "adding " << v[goolah.front()] << '\n';
        }
    }
    fout << sum;

    return 0;
}