Pagini recente » Cod sursa (job #1305857) | Cod sursa (job #1693950) | Cod sursa (job #2376502) | Cod sursa (job #1817018) | Cod sursa (job #1518265)
#include <cstdio>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
#define rep(i, from, to) for (int i = from; i < (int)to; ++i)
#define trav(a, x) for (auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
const int N = 1111;
int g, w, ans;
struct gen {
int cantitate;
int cost;
} a[N];
bool compare1(gen a, gen b) {
return a.cantitate > b.cantitate;
}
bool compare2(gen a, gen b) {
return a.cost < b.cost;
}
int main() {
freopen("energii.in", "r", stdin);
freopen("energii.out", "w", stdout);
cin >> g >> w;
for (int i = 0; i < g; i++) {
cin >> a[i].cantitate >> a[i].cost;
}
int p = 0;
for (int i = 0; i < g; i++) {
p += a[i].cantitate;
}
if (p < w) {
cout << -1 << endl;;
return 0;
}
sort(a, a + g, compare1);
int k = 0, sum = 0;
int cost = 0;
while (k < g && sum < w) {
sum += a[k].cantitate;
cost += a[k].cost;
k++;
}
if (sum >= w) ans = max(ans, cost);
cost = 0, sum = 0, k = 0;
sort(a, a + g, compare2);
while (k < g && sum < w) {
sum += a[k].cantitate;
cost += a[k].cost;
k++;
}
if (sum >= w) ans = min(ans, cost);
cout << ans << endl;
}