Cod sursa(job #3214095)

Utilizator EdyIordacheIordache Eduard EdyIordache Data 13 martie 2024 19:48:18
Problema Deque Scor 25
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.92 kb
#include <iostream>
#include <fstream>
#include <deque>

using namespace std;

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

int find_min(deque<int> a) {
    int mn = 10000001;
    while (!a.empty()) {
        if (a.front() < mn) mn = a.front();
        a.pop_front();
    }

    return mn;
}

int main() {
    int n, k, x;
    deque<int> q;

    fin>>n>>k;

    int mn = 10000001;
    for (int i = 1; i <= k; i++) {
        fin>>x;
        q.push_front(x);
        if (x < mn) mn = x;
    }

    int s = 0;
    for (int i = k; i <= n; i++) {
        fin>>x;

        s += mn;
        if (x < mn) {
            mn = x;

            q.pop_back();
            q.push_front(x);
        }
        else if (q.back() == mn) {
            q.pop_back();
            q.push_front(x);

            mn = find_min(q);
        }
        else {
            q.pop_back();
            q.push_front(x);
        }
    }

    fout<<s;

    return 0;
}