Cod sursa(job #1154066)

Utilizator razboi4Manole Iulian razboi4 Data 25 martie 2014 22:28:30
Problema Energii Scor 5
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.82 kb
#include<stdio.h>
#include<algorithm>
using namespace std;
const int I_am_the_boss=2100000000;
int cost[1001],profit[1001],mat[1001][5002];
int main()
{
    int N,G;
    for(int i=0;i<=5000;i++)
        mat[0][i]=I_am_the_boss;
    freopen("energii.in","r",stdin);
    freopen("energii.out","w",stdout);
    scanf("%d%d",&N,&G);
    for(int i=1;i<=N;++i)
        scanf("%d%d",&profit[i],&cost[i]);
    int l=1;
    for(int i=1;i<=N;++i,l=1-l){
        for(int j=0;j<=G;j++){
            if(profit[i]>=j)
                mat[i][j]=min(mat[i][j],cost[i]);
            else
                mat[i][j]=min(mat[i-1][j],mat[i-1][j-profit[i]]+cost[i]);
        }
    }

    if(mat[N][G]!=I_am_the_boss)
        printf("%d",mat[N][G]);
    else
        printf("-1");
    fclose(stdin);fclose(stdout);
    return 0;
}