Pagini recente » Cod sursa (job #2981532) | Cod sursa (job #3188562) | Cod sursa (job #2639007) | Cod sursa (job #1251213) | Cod sursa (job #369973)
Cod sursa(job #369973)
#include <iostream>
#include <fstream>
using namespace std;
int main() {
fstream f1, f2;
int g, w, e[1002], c[1002], s[10001], i, j, p, q, k, sumatr, obiect, total=0, max, min=999999;
char uz[1001][1001];
f1.open("energii.in", ios::in);
f1>>g>>w;
for(i=1; i<=g; i++) {
f1>>e[i]>>c[i];
total+=e[i];
}
f1.close();
for(i=0; i<=total; i++) {
s[i]=100000001;
for(j=1; j<=g; j++) {
uz[i][j]=0;
}
}
s[0]=0;
for(i=1; i<=total; i++) {
max=100000000;
for(j=1; j<=g; j++) {
if((i-e[j]>=0) && (s[i-e[j]] + c[j] < max) && (uz[i-e[j]][j]==0)) {
sumatr=i-e[j];
obiect=j;
max=s[i-e[j]] + c[j];
}
}
if(max<100000000) {
s[i]=max;
for(j=1; j<=g; j++) {
uz[i][j]=uz[sumatr][j];
}
uz[i][obiect]=1;
}
}
for(i=w; i<=total; i++) {
if(s[i]<min) { min=s[i]; }
}
f2.open("energii.out", ios::out);
if(min<999999) { f2<<min<<endl; }
else { f2<<-1<<endl; }
f2.close();
return 0;
}