fork download
  1. /*
  2. *
  3. *************************************************************
  4. * AUTHOR : Ashish Gururani *
  5. * Language: C++14 *
  6. * Purpose: - *
  7. * IDE used: Visual Studio Code. *
  8. *************************************************************
  9. *
  10. Comments will be included in practice problems if it helps ^^
  11. */
  12. #include <bits/stdc++.h>
  13. typedef long long ll;
  14. typedef long double ld;
  15. #define rep(i,n,k) for(ll i=0;i<n;i+=k)
  16. #define rrep(i,n,k) for(ll i=n;i>=0;i-=k)
  17. #define rep1(i,n,k) for(ll i=1;i<n;i+=k)
  18. #define vi vector<int>
  19. #define vl vector<ll>
  20. #define vs vector<string>
  21. #define vvi vector<vi>
  22. #define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
  23. #define pb push_back
  24. #define mp make_pair
  25. #define fi first
  26. #define se second
  27. #define tc(t) long long t;cin>>t;while(t--)
  28. #define all(x) (x).begin(), (x).end()
  29. #define dbg(x) cout<<x<<" = "<<x<<endl
  30. #define inf 1e6+5
  31. #define mod ll(1e9+7)
  32. using namespace std;
  33.  
  34. template<typename T>
  35. void print_array(const T &arr, char c = ' ')
  36. {
  37. for (auto x : arr)
  38. {
  39. cout << x << c;
  40. }
  41. cout << endl;
  42. }
  43.  
  44. template<typename T>
  45. void input_array(vector< T> &arr)
  46. {
  47. for (ll i = 0; i < arr.size(); i++)
  48. {
  49. cin>>arr[i];
  50. }
  51. }
  52.  
  53.  
  54. int main()
  55. {
  56. #ifndef ONLINE_JUDGE
  57. freopen("input.txt", "r", stdin);
  58. freopen("error.txt", "w", stderr);
  59. freopen("output.txt", "w", stdout);
  60. #endif
  61. fast;
  62. ll a, b,c,d,e,f,m,n,p,q;
  63. string s,r;
  64.  
  65. tc(t)
  66. {
  67. cin>>n>>d;
  68. vector<pair<ll,ll>>v(n);
  69. rep(i,n,1)cin>>v[i].first;
  70. rep(i,n,1)cin>>v[i].second;
  71.  
  72. sort(all(v));
  73. vl check(n,0),swewtness(n,0);
  74.  
  75. ll mxsweet=0;
  76. for (ll i = 0; i < n; i++)
  77. {
  78. check[i]=v[i].first;
  79.  
  80. mxsweet=max(mxsweet, v[i].second);
  81.  
  82. swewtness[i]=mxsweet;
  83. }
  84. ll ans=0;
  85.  
  86. for (ll i = n-1; i>=0; i--)
  87. {
  88. if(v[i].first>d){
  89. continue;
  90. }
  91. ll sw=v[i].second;
  92. ll j=upper_bound(all(check),d-v[i].first)-check.begin();
  93.  
  94. j--;
  95. j=min(j,i-1);
  96. if(j>=0)
  97. sw+=swewtness[j];
  98. ans=max(ans, sw);
  99. }
  100. cout<<ans<<endl;
  101.  
  102. }
  103. return 0;
  104. }
Success #stdin #stdout 0s 5564KB
stdin
3
2 10
1 2 
2 1 
5 7
1 2 3 4 5
1 2 3 4 5
5 7
6 6 6 6 6
5 4 3 2 1
stdout
3
7
5