Cod sursa(job #1154055)

Utilizator razboi4Manole Iulian razboi4 Data 25 martie 2014 22:22:06
Problema Energii Scor 5
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.83 kb
#include<stdio.h>
#include<algorithm>
using namespace std;
const int I_am_the_boss=21;//00000000;
int cost[1001],profit[1001],mat[2][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[l][j]=min(mat[1-l][j],cost[i]);
            else
                mat[l][j]=min(I_am_the_boss,mat[1-l][j-profit[i]]+cost[i]);
        }
    }

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