Cod sursa(job #2224406)
| Utilizator | Data | 23 iulie 2018 21:47:58 | |
|---|---|---|---|
| Problema | Deque | Scor | 100 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.5 kb |
#include <bits/stdc++.h>
using namespace std;
ifstream fin("deque.in");
ofstream fout("deque.out");
const int NMAX=5000005;
int N,K;
long long a[NMAX],S;
deque <int> d;
int main()
{
fin >> N >> K;
for(int i=1;i<=N;i++)
fin >> a[i];
for(int i=1;i<=N;i++)
{
while(!d.empty() and a[i]<=a[d.back()])
d.pop_back();
d.push_back(i);
if(d.front()==i-K)
d.pop_front();
if(i>=K)
S+=a[d.front()];
}
fout << S;
return 0;
}
