Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_DEPRECATE
- #include <iostream>
- #include <vector>
- #include <string>
- #include <map>
- #include <set>
- #include <algorithm>
- #define li long long
- using namespace std;
- li gcd(li a, li b) {
- return b ? gcd(b, a % b) : a;
- }
- bool cmp(pair<pair<li, li>, li> a, pair<pair<li, li>, li> b) {
- return a.first.first * b.first.second > a.first.second * b.first.first;
- }
- int main() {
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- int n, w;
- cin >> n >> w;
- vector<pair<pair<li, li>, li> > a(n);
- for (int i = 0; i < n; i++) {
- li q, w;
- cin >> q >> w;
- a[i] = {{ q, w }, w};
- li g = gcd(q, w);
- a[i].first.first = a[i].first.first / g;
- a[i].first.second = a[i].first.second / g;
- }
- sort(a.begin(), a.end(), cmp);
- pair<li, li> ans = { 0, 1 };
- for (int i = 0; i < a.size() && w; i++) {
- li k = a[i].second;
- if (w < a[i].second) {
- k = w;
- }
- w -= k;
- pair<li, li> r = { a[i].first.first * k, a[i].first.second };
- ans = { ans.first * r.second + ans.second * r.first, ans.second * r.second };
- li g = gcd(ans.first, ans.second);
- ans.first /= g;
- ans.second /= g;
- }
- if (ans.first / ans.second != 0) {
- cout << ans.first / ans.second << ' ';
- }
- if (ans.first % ans.second != 0) {
- cout << ans.first % ans.second << '/' << ans.second;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment