#include<algorithm> #include<iostream> #include<queue> usingnamespace std; constint MAX_N = 1e5 + 10; structsweet { int ness;//sweetness int price; } sweets[MAX_N]; boolcmp(sweet x, sweet y){ return x.ness < y.ness; } int v, n, m; int sum1[MAX_N], sum2[MAX_N]; intmain(){ int ans = 0; cin >> v >> n >> m; for (int i = 1; i <= n; i++) cin >> sweets[i].ness >> sweets[i].price; sort(sweets + 1, sweets + 1 + n, cmp); priority_queue<int> q1, q2; // compute the sum of prices from the left end of the array for (int i = 1; i <= n; i++) { q1.push(sweets[i].price); sum1[i] = sum1[i - 1] + sweets[i].price; if (q1.size() > m / 2 - 1 + m % 2) sum1[i] -= q1.top(), q1.pop(); } // compute the sum of prices from the right end of the array for (int i = n; i > 0; i--) { for (int i = n; i > 0; i--) { q2.push(sweets[i].price); sum2[i] = sum2[i + 1] + sweets[i].price; if (q2.size() > m / 2) sum2[i] -= q2.top(), q2.pop(); } if (m % 2) { // search for the sweet with the maximum sweetness that satisfies the budget constraint for (int i = n - m / 2; i >= m / 2 + 1; i--) if (sum1[i - 1] + sum2[i + 1] + sweets[i].price <= v) { cout << sweets[i].ness; break; } } else { for (int i = m / 2; i <= n - m / 2; i++) { int l = i + 1, r = n - m / 2 + 1; int tmp = 0; while (l <= r) { int mid = l + (r - l) / 2; if (sum1[i - 1] + sum2[mid] + sweets[i].price <= v) l = mid + 1, tmp = mid; else r = mid - 1; } if (tmp > i) ans = max(ans, sweets[i].ness + sweets[tmp].ness); } cout << ans / 2 << "\n"; } return0; }