Cod sursa(job #3224620)
Utilizator | Iancu Cezar-Stefan Ics.maker09 | Data | 15 aprilie 2024 18:45:48 |
---|---|---|---|
Problema | Deque | Scor | 25 |
Compilator | cpp-64 | Status | done |
Runda | Arhiva educationala | Marime | 0.59 kb |
#include <bits/stdc++.h>
using namespace std;
ifstream f("deque.in");
ofstream g("deque.out");
deque<int>q;
int v[5000005],S;
int main()
{
int n,k;
f>>n >>k;
for(int i=1;i<=n;i++)f>>v[i];
for(int i=1;i<=k;i++)
{
while(!q.empty() && v[q.back()]>=v[i])q.pop_back();
q.push_back(i);
}
int nr=v[q.front()];
S+=v[q.front()];
for(int i=k+1;i<=n;i++)
{
while(!q.empty() && v[q.back()]>=v[i])q.pop_back();
q.push_back(i);
if(i-k==q.front())q.pop_front();
S+=v[q.front()];
}
g<<S;
}