Cod sursa(job #736554)

Utilizator alex_unixPetenchea Alexandru alex_unix Data 18 aprilie 2012 22:25:40
Problema Energii Scor 45
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.95 kb

#include <fstream>
#include <cmath>

const unsigned int SIZE(100000);
unsigned int v [SIZE];

int main (void)
{
    unsigned int n,g;
    std::ifstream input("energii.in");
    input >> n >> g;
    unsigned int p,c;
    input >> p >> c;
    unsigned int *it,*limit(v + p);
    v[p] = c;
    --n;
    do
    {
        input >> p >> c;
        it = limit;
        do
        {
            if (*it && (it[p] > *it + c || !it[p]))
                it[p] = *it + c;
            --it;
        }
        while (it >= v);
        if (!v[p] || v[p] > c)
            v[p] = c;
        limit += p;
        --n;
    }
    while (n);
    input.close();
    unsigned int result(-1);
    it = v + g - 1;
    do
    {
        if (*it && result > *it)
            result = *it;
        ++it;
    }
    while (it < limit);
    std::ofstream output("energii.out");
    if (result & (1 << 31))
        output << "-1";
    else
        output << result;
    output.put('\n');
    return 0;
}