Mai intai trebuie sa te autentifici.
Cod sursa(job #2308840)
Utilizator | Data | 27 decembrie 2018 20:35:24 | |
---|---|---|---|
Problema | Deque | Scor | 100 |
Compilator | cpp-64 | Status | done |
Runda | Arhiva educationala | Marime | 0.71 kb |
#include <iostream>
#include <fstream>
#include <queue>
using namespace std;
ifstream f("deque.in");
ofstream g("deque.out");
int n,k;
pair<int,int> q[5000001];
int first=0,last=-1;
int main()
{
f>>n>>k;
int a;
for(int i=1;i<k;i++)
{
f>>a;
while(last!=first-1 && q[last].first>a)
last--;
q[++last]=make_pair(a,i);
}
long long s=0;
for(int i=k;i<=n;i++)
{
if(q[first].second<=i-k)
first++;
f>>a;
while(last!=first-1 && q[last].first>a)
last--;
q[++last]=make_pair(a,i);
s+=q[first].first;
}
g<<s;
f.close();
g.close();
return 0;
}