Pagini recente » Cod sursa (job #1758801) | Cod sursa (job #709763) | Cod sursa (job #569312) | Cod sursa (job #1156311) | Cod sursa (job #1805052)
#include <fstream>
#include <deque>
using namespace std;
ifstream f("deque.in");
ofstream g("deque.out");
#define NMAX 5000010
deque<int>C;
int a[NMAX];
int n, k;
long long S;
int main()
{
int i;
f>>n>>k;
for(i = 1; i <= n; i++)
f>>a[i];
C.push_back(1);
for(i = 2; i <= k; i++)
{
while(a[i] <= a[C.back()] && !C.empty())
C.pop_back();
C.push_back(i);
}
S += a[C.front()];
for(i = k + 1; i <= n; i++) {
if(i - k >= C.front() && !C.empty())
C.pop_front();
while(a[i] <= a[C.back()] && !C.empty())
C.pop_back();
C.push_back(i);
S += a[C.front()];
}
g<<S<<'\n';
return 0;
}