Advertisement
erek1e

IOI '14 P5 - Friend (Standard I/O)

Mar 5th, 2023
484
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.87 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. int main() {
  8.     int n; cin >> n;
  9.     vector<int> p(n), q(n), h(n), type(n);
  10.     for (int &x : p) cin >> x; // p is benefit from choosing, q is benefit from not choosing
  11.     for (int i = 1; i < n; ++i) cin >> h[i] >> type[i];
  12.     for (int i = n-1; i > 0; --i) {
  13.         if (type[i] == 0) { // IAmYourFriend
  14.             p[h[i]] += q[i];
  15.             q[h[i]] += max(p[i], q[i]);
  16.         } else if (type[i] == 1) { // MyFriendsAreYourFriends
  17.             p[h[i]] += max(p[i], q[i]);
  18.             q[h[i]] += q[i];
  19.         } else { // WeAreYourFriends
  20.             p[h[i]] = max(p[h[i]] + q[i], p[i] + q[h[i]]);
  21.             q[h[i]] += q[i];
  22.         }
  23.         p[h[i]] = max(p[h[i]], q[h[i]]); // not choosing never makes things worse
  24.     }
  25.     cout << max(p[0], q[0]) << endl;
  26.     return 0;
  27. }
  28.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement