Pagini recente » Cod sursa (job #1284353) | Istoria paginii runda/drastik_challange_3/clasament | Istoria paginii runda/wer/clasament | Monitorul de evaluare | Cod sursa (job #1713784)
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
ifstream in("energii.in");
ofstream out("energii.out");
vector<pair<int,int> > G;
int N,W;
long long int *WS;
long long int energy = 0;
long long int cost = 0;
bool compare(pair<int,int> T1,pair<int,int> T2)
{
if(T1.first >= T2.first)
return true;
return false;
}
int main()
{
in>>N>>W;
int en,co;
for(int i=1;i<=N;i++)
{
in>>en>>co;
G.push_back(make_pair(en,co));
energy+=en;
cost+=co;
}
in.close();
std::sort(G.begin(),G.end(),compare);
if(energy < W)
{
out<<-1<<"\n";
return 0;
}
if(energy == W)
{
out<<cost<<"\n";
return 0;
}
WS = new long long int[energy+1];
for(int i=0;i<=energy;i++)
WS[i] = 0;
long long int maxx = 0;
long long int best = (1<<30);
for(int i=0;i<=N;i++)
for(int j=maxx;j>=0;j--)
if((WS[j] || j==0) && (WS[j+G[i].first]==0 || WS[j+G[i].first] > WS[j] + G[i].second))
{
WS[j+G[i].first] = WS[j] + G[i].second;
if(j+G[i].first >= W && WS[j+G[i].first]<=best)
best = WS[j+G[i].first];
if((!(j+G[i].first >= W && best <= WS[j+G[i].first])) && maxx < j + G[i].first)
maxx = j + G[i].first;
}
out<<best<<"\n";
delete[] WS;
return 0;
}