Pagini recente » Cod sursa (job #2597082) | Cod sursa (job #1449515) | Cod sursa (job #1581311) | Cod sursa (job #3184809) | Cod sursa (job #1951360)
#include <bits/stdc++.h>
using namespace std;
int n,k;
long long s=0;
vector <int> V;
deque <int> DQ;
ifstream f("deque.in");
ofstream g("deque.out");
int main()
{
f>>n>>k;
for(int i=0;i<k;i++)
{
int x;
f>>x;
V.push_back(x);
while( !DQ.empty() && V[DQ.back()]>=x ) DQ.pop_back();
DQ.push_back(i);
}
for(int i=k,st=0;i<n;i++,st++)
{
s+=V[DQ.front()];
int x;
f>>x;
V.push_back(x);
if(DQ.front()==st) DQ.pop_front();
while( !DQ.empty() && V[DQ.back()]>=x ) DQ.pop_back();
DQ.push_back(i);
}
s+=V[DQ.front()];
g<<s;
}