Cod sursa(job #2185274)

Utilizator TonuMihaelaTonu Mihaela TonuMihaela Data 24 martie 2018 14:19:40
Problema Energii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.47 kb
#include <fstream>
using namespace std;
ifstream fin("energii.in");
ofstream fout("energii.out");
int n,m,x,y,i,j,a[7000];
int main(){
    fin>>n>>m;
    for(i=1;i<=m;i++) a[i]=999999;
    a[0]=0;
    for(i=1;i<=n;i++){
        fin>>x>>y;
        for(j=m;j>=0;j--)
        if(j+x<m){
           	 if(a[j+x]>a[j]+y) a[j+x]=a[j]+y;
        }
        else if(a[m]>a[j]+y) a[m]=a[j]+y;
    }
    if(a[m]!=999999) fout<<a[m];
    else fout<<-1;
    return 0;
}