Pagini recente » Cod sursa (job #503519) | Cod sursa (job #2913254) | Cod sursa (job #2133895) | Cod sursa (job #3282201) | Cod sursa (job #2726279)
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
ifstream fin("deque.in");
ofstream fout("deque.out");
int back = 0, front = 1;
int deque[5000010];
int v[5000010];
void push_back(int x)
{
++back;
deque[back] = x;
}
void pop_front()
{
++front;
}
void pop_back()
{
--back;
}
int get_front()
{
return deque[front];
}
int get_back()
{
return deque[back];
}
bool is_Empty()
{
return (front > back);
}
int main()
{
int N, K;
long long sum = 0;
fin >> N >> K;
for(int i=0; i<N; ++i)
fin >> v[i];
for(int i=0; i<N; ++i)
{
while(!is_Empty() && v[i] <= v[get_back()])
{
pop_back();
}
push_back(i);
if(i-K == get_front())
pop_front();
if(i>=K-1)
sum += v[get_front()];
}
fout << sum;
return 0;
}