Pagini recente » Notiuni de baza | Cod sursa (job #888902) | Istoria paginii runda/sos_dp_cu_segtree_beats_persistent/clasament | Istoria paginii runda/moisil_round_1 | Cod sursa (job #2469056)
#include <fstream>
#include <deque>
using namespace std;
ifstream fin ("deque.in");
ofstream fout ("deque.out");
deque < int > a;
int main()
{
int n, k, i, x, s = 0;
int v[5000001];
fin >> n >> k;
for ( i = 1 ; i <= n ; i++ ) fin >> v[i];
for ( i = 1 ; i < k ; i++ )
{
x = v[i];
if ( a.empty() == 0 ) while ( x <= a.back() || a.empty() != 0 ) a.pop_back();
a.push_back ( x );
}
for ( i = k ; i <= n ; i++ )
{
x = v[i];
while ( x <= a.back() ) a.pop_back();
a.push_back ( x );
if ( a.front() == v[i-k] ) a.pop_front();
s += a.front();
}
fout << s;
return 0;
}