Cod sursa(job #2929303)
| Utilizator | Data | 25 octombrie 2022 15:27:34 | |
|---|---|---|---|
| Problema | Deque | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.58 kb |
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("deque.in");
ofstream fout ("deque.out");
const int NM = 5e6 + 5;
int n, k, a[NM];
deque<int>dq;
long long ans = 0;
int main(){
ios_base::sync_with_stdio(false);
fin.tie(0);
fout.tie(0);
fin >> n >> k;
for (int i = 1; i <= n; i++){
fin >> a[i];
while (!dq.empty() && a[i] <= a[dq.back()]){
dq.pop_back();
}
dq.push_back(i);
if (i >= k){
ans += a[dq.front()];
if (i - dq.front() + 1 >= k){
dq.pop_front();
}
}
}
fout << ans;
}
