Pagini recente » Cod sursa (job #449999) | Cod sursa (job #749965) | Cod sursa (job #710335) | Cod sursa (job #2111478) | Cod sursa (job #2546463)
#include <bits/stdc++.h>
#define pb push_back
using namespace std;
class Parser {
private:
static const int SIZE = 1e5;
char str[SIZE];
int ptr;
FILE *fin;
char getChar() {
if (ptr == SIZE) {
fread(str, sizeof(char), SIZE, fin);
ptr = 0;
}
return str[ptr++];
}
int getInt() {
char chr = getChar();
while (!isdigit(chr) && chr != '-')
chr = getChar();
int sgn = +1;
if (chr == '-') {
sgn = -1;
chr = getChar();
}
int nr = 0;
while (isdigit(chr)) {
nr = nr * 10 + chr - '0';
chr = getChar();
}
return nr * sgn;
}
public:
Parser(const char* str) :
ptr(SIZE), fin(fopen(str, "r")) { }
~Parser() {
fclose(fin);
}
friend Parser& operator>>(Parser& in, int& nr) {
nr = in.getInt();
return in;
}
};
Parser fin("deque.in");
ofstream fout("deque.out");
void debug_out() { cerr << '\n'; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << H; debug_out(T...);}
#define dbg(...) cerr << #__VA_ARGS__ << " ->", debug_out(__VA_ARGS__)
#define dbg_v(x, n) do{cerr<<#x"[]: ";for(int _=0;_<n;++_)cerr<<x[_]<<" ";cerr<<'\n';}while(0)
#define dbg_ok cerr<<"OK!\n"
typedef pair<int,int> pii;
typedef long long int ll;
typedef long double ld;
const int DMAX = 1e5+10;
deque <pii> deq;
int n,k;
ll ans;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int i,x;
fin>>n>>k;
for(i=1;i<=k;i++){
fin>>x;
while(!deq.empty() && deq.back().first >= x)
deq.pop_back();
deq.push_back({x,i});
}
ans+=deq.front().first;
for(i=k+1;i<=n;i++){
fin>>x;
if(deq.front().second <= i-k)
deq.pop_front();
while(!deq.empty() && deq.back().first >= x)
deq.pop_back();
deq.push_back({x,i});
ans+=deq.front().first;
}
fout<<ans<<'\n';
return 0;
}