fork download
  1. #include <bits/stdc++.h>
  2. #define ll long long
  3.  
  4. using namespace std;
  5.  
  6. const int MOD = 1e9 + 7;
  7.  
  8. void solve(){
  9. ll n, k;
  10. cin >> n >> k;
  11.  
  12. vector<pair<int, int>> a(n);
  13.  
  14. for(int i = 0; i < n; i++){
  15. cin >> a[i].first;
  16. }
  17.  
  18. for(int i = 0; i < n; i++){
  19. cin >> a[i].second;
  20. }
  21.  
  22. sort(a.begin(), a.end());
  23. ll ans = 0;
  24. int idx = -1;
  25. for(int i = n - 1; i >= 0; i--){
  26. if(a[i].second == 1){
  27. if(i > (n - 1) / 2 - (n % 2)){
  28. ans = max(ans, a[i].first + a[(n - 1) / 2 - (n % 2)].first + k);
  29. }else{
  30.  
  31. ans = max(ans, a[i].first + k + a[(n - 1) / 2 + !(n % 2)].first);
  32. }
  33. }else{
  34. idx = max(idx, i);
  35. }
  36.  
  37. }
  38. if(idx == -1){
  39. cout << ans << "\n";
  40. return;
  41.  
  42. }
  43.  
  44.  
  45.  
  46. ll s = 0, e = 1e10;
  47.  
  48. while(s <= e){
  49. ll rem = k;
  50. ll mid = s + (e - s) / 2;
  51. vector<pair<int, int>> b;
  52.  
  53. for(int i = 0; i< n; i++){
  54. if(i == idx)continue;
  55. b.push_back(a[i]);
  56. }
  57.  
  58. for(int i = n - 2; i >= 0; i--){
  59. if(b[i].second == 1 && b[i].first < mid){
  60. ll p = min(rem, mid - b[i].first);
  61. b[i].first += p;
  62. rem -= p;
  63. }
  64. }
  65. sort(b.begin(), b.end());
  66. if(b[(n - 2) / 2].first >= mid){
  67. ans = max(ans, a[idx].first + mid);
  68. s = mid + 1;
  69. }else{
  70. e = mid - 1;
  71. }
  72. }
  73. cout << ans << "\n";
  74. }
  75.  
  76. int main(){
  77. ios_base::sync_with_stdio(false);
  78. cin.tie(nullptr);
  79.  
  80. int t = 1;
  81. cin >> t;
  82.  
  83. for(int i = 1; i <= t; i++){
  84. solve();
  85. }
  86. return 0;
  87. }
Success #stdin #stdout 0.01s 5284KB
stdin
8
2 10
3 3
1 1
3 10
3 3 3
0 0 0
4 4
2 1 5 1
0 1 0 1
5 4
7 5 2 5 4
0 0 1 0 1
5 1
5 15 15 2 11
1 0 0 1 1
5 2
10 11 4 10 15
1 1 0 1 0
4 4
1 1 2 5
1 1 0 0
2 1000000000
1000000000 1000000000
1 1
stdout
16
6
8
13
21
26
8
3000000000