Mai intai trebuie sa te autentifici.
Cod sursa(job #3164741)
| Utilizator | Data | 4 noiembrie 2023 10:56:25 | |
|---|---|---|---|
| Problema | Deque | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.72 kb |
#include <bits/stdc++.h>
using namespace std;
string file = "deque";
ifstream fin(file + ".in");
ofstream fout(file + ".out");
int n, k;
deque <pair<int, int>> D;
int main() {
fin >> n >> k;
long long S = 0;
for (int i = 1; i < k; i++) {
int x;
fin >> x;
while (!D.empty() && D.back().second >= x)
D.pop_back();
D.push_back({i, x});
}
for (int i = k; i <= n; i++) {
int x;
fin >> x;
while (!D.empty() && D.front().first <= i - k)
D.pop_front();
while (!D.empty() && D.back().second >= x)
D.pop_back();
D.push_back({i, x});
S += D.front().second;
}
fout << S;
return 0;
}