Cod sursa(job #2353570)
Utilizator | Data | 24 februarie 2019 13:06:07 | |
---|---|---|---|
Problema | Deque | Scor | 60 |
Compilator | cpp-64 | Status | done |
Runda | Arhiva educationala | Marime | 0.67 kb |
#include <fstream>
using namespace std;
ifstream fin("deque.in");
ofstream fout("deque.out");
long long n, k;
struct nr
{
long long value;
int poz;
};
nr a[5000001];
int main()
{
int i, pr, ul;
long long s,x;
fin >> n >> k;
fin >> x;
a[1].value = x;
a[1].poz = 1;
pr = ul = 1;
s = 0;
for(i = 2; i <= n; i++)
{
fin >> x;
if(i - a[pr].poz + 1 > k)
pr++;
while(x < a[ul].value && ul >= pr)
ul--;
a[++ul].value = x;
a[ul].poz = i;
if(i > k - 1)
s = s + a[pr].value;
}
fout << s;
return 0;
}