Pagini recente » Cod sursa (job #254276) | Cod sursa (job #2663012) | Cod sursa (job #1421847) | Cod sursa (job #3151904) | Cod sursa (job #1492405)
#include <iostream>
#include <fstream>
#include <deque>
using namespace std;
ifstream f("deque.in");
ofstream g("deque.out");
const int nmax = 5005000;
int n, v[nmax], k;
deque < pair <int, int> > D;
long long Suma;
void rezolvare(){
deque < pair <int, int> > :: iterator it;
for(int i = 1; i <= k; ++i){
while( !D.empty() && v[i] <= D[ D.size() - 1].first )
D.pop_back();
D.push_back( make_pair(v[i], i) );
}
Suma += D[0].first;
if(D[0].second == 1)
D.pop_front();
for(int i = k + 1; i <= n; ++i){
it = D.end();
while( !D.empty() && v[i] <= D[ D.size() - 1].first )
D.pop_back();
D.push_back( make_pair(v[i], i) );
Suma += D[0].first;
if( D[0].second == i - k + 1)
D.pop_front();
}
}
int main(){
f>>n>>k;
for(int i = 1; i <= n; ++i)
f>>v[i];
rezolvare();
g<<Suma<<'\n';
return 0;
}