Cod sursa(job #1005763)

Utilizator manutrutaEmanuel Truta manutruta Data 5 octombrie 2013 19:03:25
Problema Energii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.19 kb
#include <iostream>
#include <fstream>
using namespace std;

#define baza 1
#define MAX 2000004
#define MOD 9973

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

int n, t, s, g, b[5002], d[1002], v[1002], sd[1002];
int a[5002];

int main()
{
    fin >> n;
    fin >> g;
    for (int i = 1; i <= n; i++) {
        fin >> d[i] >> v[i];
        sd[i] = sd[i - 1] + d[i];
    }
    for (int i = 1; i <= g; i++) {
        a[i] = -1;
    }

    for (int i = 1; i <= n; i++) {
        for (int j = 0; j <= g; j++) {
            if (j > sd[i]) {
                b[j] = -1;
            }
            else if (a[j]==-1) {
                if (d[i] < j) {
                    b[j] = v[i] + a[j - d[i]];
                }
                else {
                    b[j] = v[i];
                }
            }
            else {
                if (d[i] < j) {
                    b[j] = min(v[i] + a[j - d[i]], a[j]);
                }
                else {
                    b[j] = min(v[i], a[j]);
                }
            }

        }
        for(int j = 0; j <= g; j++) {
            a[j] = b[j];
        }
    }

    fout << a[g];


    return 0;
}