Cod sursa(job #3274504)

Utilizator SimifilLavrente Simion Simifil Data 6 februarie 2025 22:26:47
Problema Deque Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.86 kb
#include <bits/stdc++.h>
#include <deque>
using namespace std;
ifstream f ("deque.in");
ofstream g ("deque.out");

int main() {
	int n, k;
    long long sum = 0;
    f >> n >> k;
    deque<pair<int, int>> v;
    for( int i = 1; i < k; ++i )
    {
        pair<int, int> c;
        f >> c.first;
        c.second = i;

        while( v.empty() == false && v.front().first > c.first )
            v.pop_front();
        v.push_front( c );

    }
    for( int i = k; i <= n; ++i )
    {
        pair<int, int> c;
        cin >> c.first;
        c.second = i;

        while( v.empty() == false && v.back().second < i - k + 1 )
            v.pop_back();
        while( v.empty() == false && v.front().first > c.first )
            v.pop_front();
        v.push_front( c );
        //cout << v.back().first << " ";
        sum += v.back().first;
    }
    g << sum;
    return 0;
}