Cod sursa(job #2451481)
| Utilizator | Data | 26 august 2019 22:16:14 | |
|---|---|---|---|
| Problema | Deque | Scor | 10 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.48 kb |
#include <fstream>
#define nmax 5000000
using namespace std;
ifstream in("deque.in");
ofstream out("deque.out");
int main(){
int N, K, a[nmax], deque[nmax];
in >> N >> K;
for(int i = 1; i<=N; i++)
in >> a[i];
int Front = 1, Back = 0;
long long sum = 0;
for(int i = 1; i<=N; i++){
while(Front <= Back && a[i] < a[deque[Back]]) Back--;
deque[++Back] = i;
if(deque[Front] - i > 0) Front++;
if(i >= K) sum+= a[deque[Front]];
}
out << sum;
return 0;
}
