Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- struct cmp
- {
- bool operator()(pair<int, int> &a, pair<int, int> &b)
- {
- return (a.second == b.second) ? (a.first < b.second) : (a.second < b.second);
- }
- };
- int maximumHealthiness(int N, int R, vector<int> &P, vector<int> &H)
- {
- vector<pair<int, int>> a(N);
- for (int i = 0; i < N; i++)
- {
- a[i].first = P[i];
- a[i].second = H[i];
- }
- sort(a.begin(), a.end());
- priority_queue<pair<int, int>, vector<pair<int, int>>, cmp> pq;
- int ans = 0;
- for (int i = 0; i < N; i++)
- {
- while (!pq.empty() && (pq.top().first + a[i].first) > R)
- {
- pq.pop();
- }
- if (!pq.empty())
- {
- ans = max(ans, a[i].second + pq.top().second);
- }
- else
- {
- if (a[i].first <= R)
- {
- ans = max(ans, a[i].second);
- }
- }
- pq.push(a[i]);
- }
- return ans;
- }
- int main()
- {
- int N, R;
- cin >> N >> R;
- vector<int> P(N);
- vector<int> H(N);
- for (int i = 0; i < N; i++)
- {
- cin >> P[i];
- }
- for (int i = 0; i < N; i++)
- {
- cin >> H[i];
- }
- cout << maximumHealthiness(N, R, P, H) << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment