Pagini recente » Cod sursa (job #1711925) | Cod sursa (job #3314648) | Cod sursa (job #1027391) | Cod sursa (job #523832) | Cod sursa (job #3356216)
#include <fstream>
using namespace std;
ifstream cin("energii.in");
ofstream cout("energii.out");
pair<int, int> v[1001]; /// energie, cost
pair<int, int> suma[15001]; /// se-poate-Sau-nu, cost
int main()
{
int g,w,pozmax;
cin>>g>>w;
for(int n2=1;n2<=g;n2++) {
cin>>v[n2].first>>v[n2].second;
}
w*=2;
if(v[1].first>w)
pozmax=w;
else
pozmax=v[1].first;
for(int n2=1;n2<=w;n2++) {
suma[n2].second=10000001;
}
suma[v[1].first].first=1;
suma[v[1].first].second=v[1].second;
for(int n2=2;n2<=g;n2++) {
suma[v[n2].first].first=1;
suma[v[n2].first].second=min(suma[v[n2].first].second, v[n2].second);
for(int n3=pozmax;n3>=0;n3--)
if(suma[n3].first==1 && n3+v[n2].first<=w) {
suma[n3+v[n2].first].first=1;
suma[n3+v[n2].first].second=suma[n3].second+v[n2].second;
}
if(pozmax+v[n2].first>w)
pozmax=w;
else
pozmax+=v[n2].first;
}
int s=suma[w].second;
for(int n2=w/2;n2<=w;n2++)
s=min(s,suma[n2].second);
if(s==10000001)
s=-1;
cout<<s;
return 0;
}