-
Notifications
You must be signed in to change notification settings - Fork 4
/
energii.cpp
50 lines (41 loc) · 942 Bytes
/
energii.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
ifstream in("energii.in");
ofstream out("energii.out");
struct item {
int price,energy;
};
const int oo = 1000000;
int n,neededEnergy,maxEnergy;
vector<item> rucsac;
vector<int> dp;
int main() {
in >> n >> neededEnergy;
rucsac.resize(n);
for(int i = 0; i < n; ++i) {
in >> rucsac[i].energy >> rucsac[i].price;
maxEnergy=max(maxEnergy,rucsac[i].energy);
}
dp.resize(neededEnergy+maxEnergy+1);
for(auto &i:dp)
i=oo;
dp[0]=0;
int minCost = oo;
for(int i = 0; i < n; ++i) {
for(int j = neededEnergy; j >= 0; --j) {
if(dp[j]!=oo && dp[j+rucsac[i].energy]>dp[j]+rucsac[i].price)
dp[j+rucsac[i].energy]=dp[j]+rucsac[i].price;
}
}
for(int i = neededEnergy; i <= neededEnergy+maxEnergy; ++i) {
minCost = min(minCost,dp[i]);
}
if(minCost == oo)
out << -1 << '\n';
else
out << minCost << '\n';
return 0;
}