Skip to content

Instantly share code, notes, and snippets.

@uenoku
Created January 13, 2017 11:23
Show Gist options
  • Save uenoku/101467bffd5766af86ff9440d8ee8dfb to your computer and use it in GitHub Desktop.
Save uenoku/101467bffd5766af86ff9440d8ee8dfb to your computer and use it in GitHub Desktop.
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define pb push_back
#define ALL(a) (a).begin(), (a).end()
#define mp make_pair
using namespace std;
typedef long long int lli;
lli MOD = 1000000007;
struct ob {
double w, v;
};
int main()
{
/*
* w > 0 v > 0 のものは v / w がでかい順にソート
* w < 0 v > 0 のもの必ず使う
* w > 0 v < 0 のもの使わない
*/
int n;
double ans = 0;
double w;
cin >> n >> w;
vector<ob> p;
vector<ob> d;
double a, b;
rep(i, n)
{
cin >> a >> b;
if (a >= 0 && b <= 0)
continue;
else if (a <= 0 && b >= 0)
d.push_back(ob{a, b});
else if (a < 0 && b < 0) {
w -= a;
ans += b;
p.push_back(ob{-a, -b});
} else
p.push_back(ob{a, b});
}
sort(p.begin(), p.end(), [](const ob l, const ob r) { return l.v / l.w > r.v / r.w; });
for (auto s : d) {
w -= s.w;
ans += s.v;
}
for (auto s : p) {
if (w - s.w > 0) {
w -= s.w;
ans += s.v;
} else {
ans += s.v * w / s.w;
w -= w;
}
}
printf("%.10f\n", ans);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment