Advertisement
MaxObznyi

Untitled

Feb 8th, 2021
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #define pb push_back
  3. #define int long long
  4. #define all(x) (x).begin(), (x).end()
  5. using namespace std;
  6.  
  7. const int N = 5e5 + 5;
  8.  
  9. int a[N], n;
  10. mt19937 gen;
  11.  
  12. struct node {
  13. int key, val, mx, prior;
  14. node *l, *r;
  15. node(int k, int v) {
  16. key = k;
  17. mx = val = v;
  18. prior = gen();
  19. l = r = 0;
  20. }
  21. };
  22.  
  23. using tnode = node*;
  24.  
  25. int mx(tnode t) {
  26. return (t ? t -> mx : -1);
  27. }
  28.  
  29. void recalc(tnode &t) {
  30. if (t)
  31. t -> mx = max({t -> val, mx(t -> l), mx(t -> r)});
  32. }
  33.  
  34. void merge(tnode &t, tnode l, tnode r) {
  35. if (!l)
  36. return void(t = r);
  37. if (!r)
  38. return void(t = l);
  39. if (l -> prior > r -> prior)
  40. merge(l -> r, l -> r, r), t = l;
  41. else
  42. merge(r -> l, l, r -> l), t = r;
  43. recalc(t);
  44. }
  45.  
  46. void split(tnode &t, tnode l, tnode r, int key) { // <=
  47. if (!t)
  48. return void(l = r = 0);
  49. if (t -> key <= key)
  50. split(t -> r, t -> r, r, key), l = t;
  51. else
  52. split(t -> l, l, t -> l, key), r = t;
  53. recalc(t);
  54. }
  55.  
  56. void add(tnode &t, int s, int x) {
  57. tnode t1, t2, t3;
  58. split(t, t1, t3, s);
  59. t2 = new node(s, x);
  60. merge(t1, t1, t2);
  61. merge(t, t1, t3);
  62. }
  63.  
  64. void del(tnode &t, int key) { // del <= key
  65. cout << "IN" << endl;
  66. cout << key << ' ' << t << ' ' << mx(t) << ' ' << (t ? t -> key : -1) << endl;
  67. if (!t)
  68. return;
  69. if (t -> key > key)
  70. del(t -> l, key);
  71. else {
  72. cout << "M!" << endl;
  73. del(t -> r, key);
  74. cout << "MIDDLE" << endl;
  75. t = t -> r;
  76. }
  77. recalc(t);
  78. cout << key << ' ' << t << ' ' << mx(t) << endl;
  79. cout << "OUT" << endl;
  80. }
  81.  
  82. pair<int, int> get(tnode t) {
  83. if (!t)
  84. return {-1, -1};
  85. if (t -> val == t -> mx)
  86. return {t -> key, t -> val};
  87. if (t -> mx == mx(t -> l))
  88. return get(t -> l);
  89. return get(t -> r);
  90. }
  91.  
  92. signed main() {
  93. gen.seed(time(0));/*
  94. ios_base::sync_with_stdio(false);
  95. cin.tie(nullptr);*/
  96. cin >> n;
  97. for (int i = 1; i <= n; i++)
  98. cin >> a[i];
  99. sort(a + 1, a + n + 1);
  100. multiset<pair<int, int> > q;
  101. //q.insert({a[1] + a[2] + a[3] + a[4] - a[5], a[5]});
  102. tnode root = 0;
  103. add(root, a[1] + a[2] + a[3] + a[4] - a[5], a[5]);
  104. multiset<int> suff;
  105. for (int i = 7; i <= n; i++)
  106. suff.insert(a[i]);
  107.  
  108. int ans = -1;
  109. for (int i = 6; i < 7; i++) {
  110. cout << "FOR" << i << endl;
  111. del(root, a[i]);
  112. //cout << 1111 << endl;
  113. pair<int, int> res = get(root);
  114. int mx = res.second, sum = res.first + 2 * res.second;
  115.  
  116. if (mx != -1) {
  117. auto it = suff.lower_bound(mx + a[i]);
  118. if (it == suff.begin())
  119. continue;
  120. it--;
  121. ans = max(ans, sum + a[i] + *it);
  122. }
  123. add(root, a[i - 4] + a[i - 3] + a[i - 2] + a[i - 1] - a[i], a[i]);
  124. suff.erase(suff.find(a[i + 1]));
  125. }
  126. cout << ans;
  127. return 0;
  128. }
  129.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement