Cod sursa(job #2732227)
| Utilizator | Data | 28 martie 2021 20:20:24 | |
|---|---|---|---|
| Problema | Deque | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.55 kb |
#include <fstream>
using namespace std;
ifstream fin("deque.in");
ofstream fout("deque.out");
int N, K, A[5000001], deq[5000001], Front, Back;
long long Suma;
int main(){
Suma=0;
fin>>N>>K;
for(int i=1; i<=N; i++){
fin>>A[i];
}
Front=1; Back=0;
for(int i=1; i<=N; i++){
while(Front<=Back && A[i]<=A[deq[Back]]){
Back--;
}
Back++;
deq[Back]=i;
if(deq[Front]==i-K) Front++;
if(i>=K) Suma=Suma+A[deq[Front]];
}
fout<<Suma;
return 0;
}
