Pagini recente » Cod sursa (job #2137007) | Cod sursa (job #1169976) | Cod sursa (job #937484) | Cod sursa (job #362398) | Cod sursa (job #2264539)
#include <fstream>
#include <deque>
using namespace std;
ifstream fin("deque.in");
ofstream fout("deque.out");
struct fucker {
long long first;
int second;
fucker(long long v, int i) : first(v), second(i) {}
};
deque< fucker > DQ;
int main() {
int n, k;
fin >> n >> k;
long long x;
for(int i = 0; i < k - 1; i++)
{
fin >> x;
while (!DQ.empty() && DQ.back().first >= x)
DQ.pop_back();
DQ.emplace_back(x, i);
}
long long sum = 0;
for (int i = k - 1; i < n; i++) {
// while (i - DQ.front().second + 1 > k) DQ.pop_front();
while (DQ.front().second < i - k + 1) DQ.pop_front();
fin >> x;
while (!DQ.empty() && DQ.back().first >= x)
DQ.pop_back();
DQ.emplace_back( x,i );
sum += DQ.front().first;
}
fout << sum << '\n';
return 0;
}