Cod sursa(job #1310164)

Utilizator Vele_GeorgeVele George Vele_George Data 6 ianuarie 2015 15:31:41
Problema Energii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.79 kb
#include <iostream>
#include <fstream>
#define inf (1<<30)
#define nmax 5005
using namespace std;

ifstream f("energii.in");
ofstream g("energii.out");

int min(int x, int y)
{
    if (x<y) return x;
             return y;
}

int main()
{
    int n,w,c,e,a[nmax],b[nmax];
    int * first=a, *second=b, *aux;

    f>>n>>w;
    for(int i=1; i<=w; i++)
    {
        *(first+i)=inf;
    }

    for(int i=1; i<=n; i++)
    {
        f>>e>>c;
        for(int j=1; j<=w; j++)
        {
            if (j-e>=1) *(second+j) = min(*(first+j-e)+c, *(first+j));
            else *(second+j) = min(c, *(first+j));
        }
        aux=first;
        first=second;
        second=aux;
    }

    if (*(first+w)<inf) g<<*(first+w);
        else g<<"-1";




    return 0;
}