Cod sursa(job #529447)
| Utilizator | Data | 4 februarie 2011 23:49:36 | |
|---|---|---|---|
| Problema | Deque | Scor | 100 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.43 kb |
#include <fstream>
using namespace std;
ifstream fin("deque.in");
ofstream fout("deque.out");
const int maxn=5000009;
int i,N,K,A[maxn],D[maxn],Front,Back;
long long S;
int main()
{
fin >> N >> K;
for(i=1;i<=N;i++)
fin >> A[i];
Front=1; Back=0;
for(i=1;i<=N;i++)
{
while(Front <= Back && A[D[Back]] >= A[i])
Back--;
D[++Back]=i;
if(D[Front]==i-K) Front++;
if(i>=K) S+=A[D[Front]];
}
fout << S;
}