fork(2) download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. vector<long long> diffcount, wealth;
  5.  
  6. struct emp{
  7. long long wealth;
  8. long long pos;
  9. long long boss;
  10. };
  11.  
  12. void wealthdfs(emp point, vector<emp> a[]){
  13. long long counter = 0;
  14. wealth.push_back(point.wealth);
  15. for(long long i=0; i<a[point.pos].size(); i++){
  16.  
  17. for(int j=0; j<wealth.size(); j++){
  18. counter = wealth[j]-a[point.pos][i].wealth;
  19. diffcount.push_back(counter);
  20. }
  21. wealthdfs(a[point.pos][i], a);
  22. wealth.pop_back();
  23. }
  24. }
  25.  
  26. int main(){
  27. long long n;
  28. cin >> n;
  29. vector<emp> a(n);
  30. for(long long i=0; i<n; i++){
  31. cin >> a[i].wealth;
  32. a[i].pos = i+1;
  33. }
  34.  
  35. for(long long i=0; i<n; i++){
  36. cin >> a[i].boss;
  37. if(a[i].boss==-1){
  38. a[i].boss = 0;
  39. }
  40. }
  41.  
  42. vector<emp> al[n+2];
  43. for(long long i=0; i<n; i++){
  44. al[a[i].boss].push_back(a[a[i].pos-1]);
  45. }
  46. wealthdfs(al[0][0], al);
  47.  
  48. cout << *max_element(diffcount.begin(), diffcount.end());
  49. }
  50.  
Runtime error #stdin #stdout #stderr 0s 2876KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc