Cod sursa(job #2215147)
| Utilizator | Data | 21 iunie 2018 10:33:45 | |
|---|---|---|---|
| Problema | Deque | Scor | 100 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.65 kb |
#include <fstream>
#include <deque>
using namespace std;
ifstream cin("deque.in");
ofstream cout("deque.out");
deque<pair<int, int> > q;
int main()
{
long long ans = 0;
int n, k;
cin >> n >> k;
for(int i = 1; i <= n; ++i)
{
int x;
cin >> x;
while(!q.empty() && q.back().first >= x)
{
q.pop_back();
}
q.push_back({x, i});
if(q.front().second == i - k)
{
q.pop_front();
}
if(i >= k)
{
ans = ans + q.front().first;
}
}
cout << ans;
return 0;
}
