Pagini recente » Cod sursa (job #3303474) | Cod sursa (job #3332999) | Cod sursa (job #2533413) | Cod sursa (job #2738434) | Cod sursa (job #3326172)
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("deque.in");
ofstream fout ("deque.out");
long long n, k, sum, st, dr;
struct el{
int v, s, d;
};
el a[5000005];
stack <int> sky;
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
fin >> n >> k;
for(int i = 1; i <= n; i++){
fin >> a[i].v;
while(!sky.empty() && a[i].v <= a[sky.top()].v){
a[sky.top()].d = i;
sky.pop();
}
sky.push(i);
}
while(!sky.empty()){ sky.pop(); }
for(int i = n; i >= 1; i--){
if(a[i].d == 0) a[i].d = n + 1;
while(!sky.empty() && a[i].v <= a[sky.top()].v){
a[sky.top()].s = i;
sky.pop();
}
sky.push(i);
}
for(int i = 1; i <= n; i++){
//cout << a[i].s << " " << a[i].d << "\n";
st = max(1LL * (a[i].s + 1), 1LL * (i - k + 1));
dr = min(1LL * (a[i].d - k), 1LL * i);
if(st <= dr){
sum += a[i].v * (dr - st + 1);
}
}
fout << sum;
return 0;
}