Cod sursa(job #2621823)

Utilizator filipasvladVlad Filipas filipasvlad Data 30 mai 2020 20:33:04
Problema Deque Scor 30
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.8 kb
#include <iostream>
#include <fstream>

using namespace std;

ifstream fin("deque.in");
ofstream fout("deque.out");

int n, v[5000005], st = 1, dr, minim = 10999999, k;
long long sum;

int main()
{
    fin >> n >> k;
    for(int i = 1; i <= k; i++)
    {
        fin >> v[i];
        if(v[i] < minim)
            minim = v[i];
    }
    dr = k;
    sum += minim;
    for(int i = k + 1; i <= n; i++)
    {
        int x;
        fin >> x;
        v[++dr] = x;
        st++;
        if(v[dr] < minim)
            minim = v[dr];
        else if(v[st - 1] == minim)
        {
            minim = v[dr];
            for(int j = st; j < dr; j++)
                if(minim > v[j])
                    minim = v[j];
        }
        sum += minim;
    }
    fout << sum;
}