Cod sursa(job #831496)

Utilizator 2dorTudor Ciurca 2dor Data 8 decembrie 2012 18:14:58
Problema Energii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.03 kb
#include <fstream>
using namespace std;

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

int G, E, i, j, cost, nrg;
long long s[5001];
int main() {
    fin >> G >> E;
    for (i = 0; i <= E; ++i)
        s[i] = -1;
    for (i = 1; i <= G; i++) {
    	fin >> nrg >> cost;
    	for (j = E; j >= 0; --j) {
            if (s[j] != -1) {
                if (j + nrg >= E) {
                    if ((s[E] > s[j] + cost) || s[E] == -1) {
                        s[E] = s[j] + cost;
                    }
                }else {
                    if ((s[j + nrg] > s[j] + cost) || s[j + nrg] == -1)
                        s[j + nrg] = s[j] + cost;
                }
            }
            if (j == nrg) {
                if (s[j] == -1) {
                    s[j] = cost;
                }else {
                    if (s[j] > cost) {
                        s[j] = cost;
                    }
                }
            }
    	}
    }
    fin.close();
    fout << s[E];
    fout.close();
    return 0;
}