Mai intai trebuie sa te autentifici.
Cod sursa(job #3157060)
| Utilizator | Data | 14 octombrie 2023 11:00:59 | |
|---|---|---|---|
| Problema | Deque | Scor | 15 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.73 kb |
/*
ID: teparuf1
TASK: dualpal
LANG: C++
*/
#include <bits/stdc++.h>
#define MOD 1000000007
typedef unsigned long long int ll;
using namespace std;
ifstream fin("deque.in");
ofstream fout("deque.out");
ll a[5000001];
int main()
{
int n,k;
fin >> n >> k;
for(int i=1;i<=n;i++)fin >> a[i];
ll res=0;
deque<ll> Q;
for(int i=1;i<=k;i++)
{
while(!Q.empty() && a[Q.back()]>a[i])Q.pop_back();
Q.push_back(i);
}
res += a[Q.front()];
for(int i=k+1;i<=n;i++)
{
while(!Q.empty() && a[Q.back()]>a[i])Q.pop_back();
Q.push_back(i);
while(!Q.empty() && Q.front() < i-k+1)Q.pop_front();
res+=a[Q.front()];
}
fout << res;
}
