Sreekar_0125

Untitled

Jul 30th, 2024
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.50 KB | None | 0 0
  1. // author : greedyman shashank
  2.  
  3. #include <bits/stdc++.h>
  4. using namespace std;
  5. void fast()
  6. {
  7.     ios_base::sync_with_stdio(false);
  8.     cin.tie(NULL);
  9.     cout.tie(NULL);
  10. }
  11. #define int long long int
  12. #define mod 1000000007
  13. #define pb push_back
  14. #define F first
  15. #define S second
  16. #define all(x) x.begin(), x.end()
  17. #define f(i, s, n) for (i = s; i < n; i++)
  18. #define yes cout << "yes" << endl;
  19. #define no cout << "no" << endl;
  20. #define vi vector<ll>
  21. #define vb vector<bool>
  22. #define pii pair<ll, ll>
  23. #define vpi vector<pii>
  24. #define umi unordered_map<ll, ll>
  25. #define mi map<ll, ll>
  26. #define si set<ll>
  27.  
  28. void init_code()
  29. {
  30. #ifndef ONLINE_JUDGE
  31.     freopen("input.txt", "r", stdin);
  32.     freopen("output.txt", "w", stdout);
  33.     freopen("error.txt", "w", stderr);
  34. #endif // ONLINE_JUDGE
  35. }
  36.  
  37. // fuck the implimentation im bad
  38. // good luck tho for next contst
  39.  
  40. bool func(int i, int par, int pref, vector<vector<int>> &v, vector<int> &a, int req)
  41. {
  42.  
  43.     int x = a[i] + pref;
  44.     if(x < -2e9 || pref < -2e9) {
  45.         return false;
  46.     }
  47.     int sum = 0;
  48.     //
  49.     for (auto child : v[i])
  50.     {
  51.         if (child != par)
  52.         {
  53.             sum++;
  54.         }
  55.     }
  56.     if (sum == 0)
  57.     {
  58.         if (x < 0)
  59.             return false;
  60.         else
  61.             return true;
  62.     }
  63.     //
  64.     if (i == 1)
  65.     {
  66.         if (x < req)
  67.         {
  68.             pref -= (req - x);
  69.         }
  70.     }
  71.     else
  72.     {
  73.         if (x < 0)
  74.         {
  75.             pref += x;
  76.         }
  77.     }
  78.     bool flag = true;
  79.     for (auto child : v[i])
  80.     {
  81.         if (child != par)
  82.         {
  83.             flag = (flag && func(child, i, pref, v, a, req));
  84.         }
  85.     }
  86.     return flag;
  87. }
  88.  
  89. void solve()
  90. {
  91.     int n;
  92.     cin >> n;
  93.     vector<int> a(n + 1);
  94.     int x, y;
  95.     int i, j, sum = 0, ans = 0;
  96.     f(i, 1, n + 1)
  97.     {
  98.         cin >> a[i];
  99.     }
  100.     vector<vector<int>> v(n + 1);
  101.     f(i, 0, n - 1)
  102.     {
  103.         cin >> x;
  104.         v[i + 2].pb(x);
  105.         v[x].pb(i + 2);
  106.     }
  107.     int low = a[1];
  108.     int high = 2e9;
  109.     int mid;
  110.     while (low <= high)
  111.     {
  112.         mid = (low + high) / 2;
  113.         int req = mid;
  114.         bool flag = func(1, -1, 0, v, a, req);
  115.         if (flag)
  116.         {
  117.             ans = req;
  118.             low = mid + 1;
  119.         }
  120.         else
  121.             high = mid - 1;
  122.     }
  123.     cout << ans << endl;
  124.     return;
  125. }
  126.  
  127. signed main()
  128. {
  129.     init_code();
  130.     fast();
  131.     int t;
  132.     cin >> t;
  133.     while (t--)
  134.     {
  135.         solve();
  136.     }
  137.     return 0;
  138. }
Add Comment
Please, Sign In to add comment