Pagini recente » Cod sursa (job #2373125) | Cod sursa (job #942290) | Cod sursa (job #2378033) | Cod sursa (job #2655700) | Cod sursa (job #2787138)
#include <fstream>
#include <deque>
using namespace std;
ifstream fin("deque.in");
ofstream fout("deque.out");
deque <int> dq;
int main()
{
int n,k,v[5000005];
long long s;
fin>>n>>k;
for(int i=1;i<=n;i++)
fin>>v[i];
for(int i=1;i<=k;i++){
while(!dq.empty() and v[dq.back()]>= v[i]){
dq.pop_back();
}
dq.push_back(i);
}
s+=dq.front();
for(int i=k+1;i<=n;i++)
{
while(!dq.empty() and v[dq.back()]>=v[i])
dq.pop_back();
dq.push_back(i);
while(!dq.empty() and dq.front()<i-k+1)
dq.pop_front();
s+=dq.front();
}
fout<<s;
}