Cod sursa(job #1752746)

Utilizator ducu34Albastroiu Radu Gabriel ducu34 Data 5 septembrie 2016 00:34:09
Problema Energii Scor 95
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.35 kb
//
//  main.cpp
//  Energii
//
//  Created by Albastroiu Radu on 9/4/16.
//  Copyright © 2016 Albastroiu Radu. All rights reserved.
//

#include <iostream>
#include <fstream>
#include <algorithm>
#include <unordered_map>
#include <vector>
#define MULTFOARTEMULT (1<<30)-1
using namespace std;

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

int i,j,nr_of_gen, min_power, power, cost, cost_total[10001], Min_cost;
bool vizitari[10001];

int main()
{
    
    fin>>nr_of_gen>>min_power;
    
    for(i=1;i<=nr_of_gen;i++)
    {
        fin>>power>>cost;
        
        for(j=min_power;j>0;j--)
        {
            if(vizitari[j])
            {
                if(cost_total[j + power]==0 || cost_total[j + power] > cost_total[j] + cost)
                {
                    cost_total[j + power] = cost_total[j] + cost;
                    vizitari[j+power] = true;
                }
            }
        }
        
        if(cost_total[power]==0 || cost_total[power] > cost)
        {
            cost_total[power]=cost;
            vizitari[power] = i;
        }
    }
    
    Min_cost = MULTFOARTEMULT;
    
    for(i=min_power;i<=10000;i++)
    {
        if(cost_total[i])
        {
            Min_cost = min(Min_cost, cost_total[i]);
        }
    }
    
    if(Min_cost!=MULTFOARTEMULT)
        fout<<Min_cost;
    else
        fout<<-1;
    
    return 0;
}