Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. //#include <ext/pb_ds/detail/standard_policies.hpp>
  3. //#include <ext/pb_ds/assoc_container.hpp>
  4. //#include <ext/pb_ds/tree_policy.hpp>
  5.  
  6. #define pb push_back
  7. #define F first
  8. #define S second
  9. #define ll long long
  10. #define ld long double
  11. #define endl '\n'
  12. #define TIME 1.0*clock()/CLOCKS_PER_SEC
  13. #define null NULL
  14.  
  15. using namespace std;
  16.  
  17. //using namespace __gnu_pbds;
  18. //typedef tree <pii,null_type,less<pii>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
  19.  
  20. mt19937 gen(chrono::system_clock::now().time_since_epoch().count());
  21.  
  22. const int M = 1e9 + 7;
  23. const int FFTM = 998244353;
  24. const int N = 2e5 + 7;
  25.  
  26. struct segtree{
  27. int tn = 1;
  28. vector<int> t;
  29.  
  30. segtree(){}
  31.  
  32. segtree(int n){
  33. tn = 1;
  34. while (tn < n) tn <<= 1;
  35. t.resize(tn<<1|1, -1e9 - 7);
  36. }
  37.  
  38. resize(int n){
  39. tn = 1;
  40. while (tn < n) tn <<= 1;
  41. t.assign(tn<<1|1, -1e9 - 7);
  42. }
  43.  
  44. void clear(){
  45. t.assign(t.size(), -1e9 - 7);
  46. }
  47.  
  48. void upd(int v, int tl, int tr, int p, int x){
  49. if (tr - tl == 1){
  50. t[v] = max(t[v], x);
  51. return;
  52. }
  53. int m = (tl + tr)>>1;
  54. if (p < m) upd(v<<1, tl, m, p, x);
  55. else upd(v<<1|1, m, tr, p, x);
  56. t[v] = max(t[v<<1], t[v<<1|1]);
  57. }
  58.  
  59. void upd(int p, int x){
  60. upd(1, 0, tn, p, x);
  61. }
  62.  
  63. int q(int v, int tl, int tr, int l, int r){
  64. if (tr <= l || r <= tl) return -1e9 - 7;
  65. if (l <= tl && tr <= r) return t[v];
  66. int m = (tl + tr)>>1;
  67. return max(q(v<<1, tl, m, l, r), q(v<<1|1, m, tr, l, r));
  68. }
  69.  
  70. int q(int l, int r){
  71. return q(1, 0, tn, l, r);
  72. }
  73. };
  74.  
  75. int n, sz[N], U[N], x[N], p, a, b, VV;
  76. vector<int> now, g[N];
  77. segtree t;
  78. ll res = -1;
  79.  
  80. int Pos(int x){
  81. int p = upper_bound(now.begin(), now.end(), x) - now.begin();
  82. return p - 1;
  83. }
  84.  
  85. void calc(int v, int p){
  86. sz[v] = 1;
  87. for (auto to : g[v])
  88. if (to != p && !U[to]) calc(to, v), sz[v] += sz[to];
  89. }
  90.  
  91. int Find(int v, int p, int k){
  92. for (auto to : g[v])
  93. if (to != p && !U[to] && sz[to] >= k) return Find(to, v, k);
  94. return v;
  95. }
  96.  
  97. void upd(int v, int p, int d = 1){
  98. t.upd(Pos(x[v]), d);
  99. for (auto to : g[v])
  100. if (to != p && !U[to]) upd(to, v, d + 1);
  101. }
  102.  
  103. void solve(int v, int p, int d = 1){
  104. res = max(res, 1ll*(1ll*t.q(Pos(x[v]), now.size()) + d)*x[v]);
  105. for (auto to : g[v])
  106. if (to != p && !U[to]) solve(to, v, d + 1);
  107. }
  108.  
  109. void P(int v, int p, vector<int> &A){
  110. A.pb(x[v]);
  111. for (auto to : g[v])
  112. if (to != p && !U[to]) P(to, v, A);
  113. }
  114.  
  115. void go(int v){
  116. calc(v, v);
  117. int V = Find(v, v, sz[v]/2);VV = V;
  118. vector<int> all;
  119. P(V, V, all);
  120. sort(all.begin(), all.end());
  121. all.resize(distance(all.begin(), unique(all.begin(), all.end())));
  122. now = all;
  123. t.resize(all.size() + 1);
  124. for (int i = 0; i < g[V].size(); i++){
  125. int to = g[V][i];
  126. if (U[to]) continue;
  127. solve(to, V);
  128. upd(to, V);
  129. }
  130. t.clear();
  131. for (int i = g[V].size() - 1; i >= 0; i--){
  132. int to = g[V][i];
  133. if (U[to]) continue;
  134. solve(to, V);
  135. upd(to, V);
  136. }
  137. U[V] = 1;
  138. for (auto to : g[V])
  139. if (!U[to]) go(to);
  140. }
  141.  
  142. int main() {
  143. ios_base::sync_with_stdio(0);
  144. cin.tie(0);
  145. cout.tie(0);
  146. #ifdef LOCAL
  147. freopen("input.txt", "r", stdin);
  148. freopen("output.txt", "w", stdout);
  149. #else
  150. freopen("tree.in", "r", stdin);
  151. freopen("tree.out", "w", stdout);
  152. #endif // LOCAL
  153. cin >> n;
  154. for (int i = 0; i < n; i++) cin >> x[i];
  155. for (int i = 1; i < n; i++) cin >> a >> b, a--, b--, g[a].pb(b), g[b].pb(a);
  156. go(0);
  157. cout << res;
  158. return 0;
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement