Pagini recente » Cod sursa (job #2844117) | Cod sursa (job #2064668) | Cod sursa (job #2064655) | Cod sursa (job #2326189) | Cod sursa (job #2264524)
#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;
}