// C++ program to implement above approach
#include <bits/stdc++.h>
using namespace std;
// Function on the basis of which we will
// perform the sorting
bool comp(pair<int, int> a, pair<int, int> b) {
return (double)a.second / a.first > (double)b.second / b.first;
}
// Function to sort the array on its value/weight
// and perform-sum on both weight and value
void preProcess(pair<int, int> arr[], int n) {
sort(arr, arr + n, comp);
for (int i = 1; i < n; i++) {
arr[i].first += arr[i - 1].first;
arr[i].second += arr[i - 1].second;
}
}
// Function to answer queries
double maxValue(int w, pair<int, int> arr[], int n) {
// If w is large enough
// to cover all weights
if (arr[n - 1].first <= w) return arr[n - 1].second;
// Value to search on arr
pair<int, int> search_bound = {w, INT_MAX};
// Index of the item which we will put
// partially in our knap-sack
int x = upper_bound(arr, arr + n, search_bound) - arr;
// Required value
if (x == 0)
return (double)w * arr[0].second / arr[0].first;
else
return arr[x - 1].second + (double)(w - arr[x - 1].first) *
(arr[x].second - arr[x - 1].second) /
(arr[x].first - arr[x - 1].first);
}
void PrintQueries(int query[], pair<int, int> arr[], int n, int m) {
for (int i = 0; i < m; i += 1) {
cout << maxValue(query[i], arr, n) << endl;
}
}
// Driver code
int main() {
// Input array representing the data of w[] and v[]
pair<int, int> arr[] = {{1, 2}, {1, 3}, {3, 7}};
int query[5] = {1, 2, 3, 4, 5};
// Size of the array
int n = sizeof(arr) / sizeof(pair<int, int>);
int m = sizeof(query) / sizeof(query[0]);
// Pre-processing
preProcess(arr, n);
PrintQueries(query, arr, n, m);
return 0;
}