Advertisement
Guest User

Untitled

a guest
Apr 28th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define f first
  3. #define s second
  4. #define mp make_pair
  5. #define pb push_back
  6. #define lp(i,a,n) for(int i=(a);i<=(int)(n);++i)
  7. #define lpd(i,a,n) for(int i=(a);i>=(int)(n);--i)
  8. #define mem(a,b) memset(a,b,sizeof a)
  9. #define all(v) v.begin(),v.end()
  10. #define println(a) cout <<(a) <<endl
  11. #define sz(x) ((int)(x).size())
  12. #define readi(x) scanf("%d",&x)
  13. #define read2i(x,y) scanf("%d%d",&x,&y)
  14. #define read3i(x,y,z) scanf("%d%d%d",&x,&y,&z)
  15. #define readll(x) scanf("%I64d",&x)
  16. #define mod 1000000007
  17. #define eps 1e-6
  18. #define infi 1000000000
  19. #define infll 1000000000000000000ll
  20. using namespace std;
  21. typedef long long ll;
  22. typedef pair<int, int> pii;
  23. typedef pair<ll, ll> pll;
  24. typedef vector<int> vi;
  25. typedef vector<vi> vvi;
  26. typedef vector<ll> vll;
  27. typedef set<int> si;
  28. typedef map<int,int> mii;
  29.  
  30. const int N = 300002;
  31. int n,w[N],parent[N];
  32. vvi g(N),adjL(N);
  33. ll ans = -infll;
  34. ll downP[N],downBestP[N],upP[N],upBestP[N];
  35. ll downN[N],downBestN[N],upN[N],upBestN[N];
  36.  
  37. void buildGraph(int i, int p){
  38. parent[i] = p;
  39. for(int j : adjL[i]) if(j != p){
  40. g[i].pb(j);
  41. buildGraph(j,i);
  42. }
  43. }
  44.  
  45. void dfs(int i, int p){
  46. downP[i] = downN[i] = downBestP[i] = downBestN[i] = w[i];
  47.  
  48. for(int j : g[i]) if(j != p){
  49. dfs(j,i);
  50. if(downP[j] > 0) downP[i] += downP[j];
  51. if(downN[j] < 0) downN[i] += downN[j];
  52. downBestP[i] = max(downBestP[i], downBestP[j]);
  53. downBestN[i] = min(downBestN[i], downBestN[j]);
  54. }
  55.  
  56. downBestP[i] = max(downBestP[i], downP[i]);
  57. downBestN[i] = min(downBestN[i], downN[i]);
  58. }
  59.  
  60. void getUp(int i, int p){
  61. upP[p] = upN[p] = upBestP[p] = upBestN[p] = w[p];
  62. for(int j : g[p]) if(j != i){
  63. if(downP[j] > 0) upP[p] += downP[j];
  64. if(downN[j] < 0) upN[p] += downN[j];
  65. upBestP[p] = max(upBestP[p], downBestP[j]);
  66. upBestN[p] = min(upBestN[p], downBestN[j]);
  67. }
  68. //parent of p
  69. if(upP[parent[p]] > 0) upP[p] += upP[parent[p]];
  70. if(upN[parent[p]] < 0) upN[p] += upN[parent[p]];
  71. upBestP[p] = max(upBestP[p], upBestP[parent[p]]);
  72. upBestN[p] = min(upBestN[p], upBestN[parent[p]]);
  73.  
  74. upBestP[p] = max(upBestP[p], upP[p]);
  75. upBestN[p] = min(upBestN[p], upN[p]);
  76. }
  77.  
  78. void go(int i, int p){
  79. getUp(i,p);
  80. ans = max(ans, max(downBestP[i] * upBestP[p], downBestN[i] * upBestN[p]));
  81. for(int j : g[i]) if(j != p) go(j, i);
  82. }
  83.  
  84. int main(){
  85. readi(n);
  86. lp(i,1,n) readi(w[i]);
  87. lp(i,1,n-1){
  88. int a,b;
  89. read2i(a,b);
  90. adjL[a].pb(b);
  91. adjL[b].pb(a);
  92. }
  93.  
  94. buildGraph(1,0);
  95. dfs(1,0);
  96. for(int i : g[1]) go(i,1);
  97. cout <<ans;
  98. }
  99.  
  100.  
  101. //freopen("input.txt","r",stdin);
  102. //freopen("output.txt","w",stdout);
  103. //ios::sync_with_stdio(0);cin.tie(0);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement