Cod sursa(job #2728322)

Utilizator EmiHHodoroaba Emanuel EmiH Data 23 martie 2021 00:47:22
Problema Deque Scor 60
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.58 kb
	
#include <fstream>
#include <deque>
 
using namespace std;

int main()
{
    ifstream in("deque.in");
    ofstream out("deque.out");
    int n,k;
	long long rez=0;
    deque<int> d;
    int v[500001];
    in >> n;
    in >> k;
    for(int i=1;i<=n;i++){
        in >> v[i];
    }
     for(int i=1;i<=n;i++){
            while(d.size()>0  && v[i]<=v[d.back()])
                d.pop_back();
                d.push_back(i);
            if(d.front()==i-k)d.pop_front();
            if(i>=k)rez+=v[d.front()];
         
    }
 
    out << rez;
    return 0; ////    1 3
}