Samkit5025

Untitled

Jul 14th, 2022
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.29 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. struct cmp
  5. {
  6.     bool operator()(pair<int, int> &a, pair<int, int> &b)
  7.     {
  8.         return (a.second == b.second) ? (a.first < b.second) : (a.second < b.second);
  9.     }
  10. };
  11.  
  12. int maximumHealthiness(int N, int R, vector<int> &P, vector<int> &H)
  13. {
  14.  
  15.     vector<pair<int, int>> a(N);
  16.  
  17.     for (int i = 0; i < N; i++)
  18.     {
  19.         a[i].first = P[i];
  20.         a[i].second = H[i];
  21.     }
  22.  
  23.     sort(a.begin(), a.end());
  24.  
  25.     priority_queue<pair<int, int>, vector<pair<int, int>>, cmp> pq;
  26.     int ans = 0;
  27.     for (int i = 0; i < N; i++)
  28.     {
  29.  
  30.         while (!pq.empty() && (pq.top().first + a[i].first) > R)
  31.         {
  32.             pq.pop();
  33.         }
  34.         if (!pq.empty())
  35.         {
  36.             ans = max(ans, a[i].second + pq.top().second);
  37.         }
  38.         else
  39.         {
  40.             if (a[i].first <= R)
  41.             {
  42.                 ans = max(ans, a[i].second);
  43.             }
  44.         }
  45.         pq.push(a[i]);
  46.     }
  47.  
  48.     return ans;
  49. }
  50.  
  51. int main()
  52. {
  53.     int N, R;
  54.     cin >> N >> R;
  55.  
  56.     vector<int> P(N);
  57.     vector<int> H(N);
  58.  
  59.     for (int i = 0; i < N; i++)
  60.     {
  61.         cin >> P[i];
  62.     }
  63.     for (int i = 0; i < N; i++)
  64.     {
  65.         cin >> H[i];
  66.     }
  67.  
  68.     cout << maximumHealthiness(N, R, P, H) << endl;
  69. }
  70.  
Advertisement
Add Comment
Please, Sign In to add comment