Cod sursa(job #1329088)
Utilizator | Okros Alexandru okros_alexandru | Data | 29 ianuarie 2015 00:30:07 |
---|---|---|---|
Problema | Deque | Scor | 100 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 0.62 kb |
#include <fstream>
#define Nmax 5000100
using namespace std;
int N, K, A[Nmax], Deque[Nmax];
long long Answer;
int main() {
int i, Left, Right;
ifstream in("deque.in");
ofstream out("deque.out");
in >> N >> K;
Left = 1;
Right = 0;
for(i = 1; i <= N; i++) {
for(in >> A[i]; Left <= Right && A[Deque[Right]] > A[i]; Right--);
Deque[++Right] = i;
if(Deque[Left] <= i - K)
++Left;
if(i >= K)
Answer += A[Deque[Left]];
}
out << Answer << '\n';
in.close();
out.close();
return 0;
}