Advertisement
YEZAELP

KU01: balance

Nov 26th, 2021
660
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.46 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. const int N = 1e3 + 10;
  5. int A[N], L[N], B[N], R[N];
  6. int ans = 0;
  7.  
  8. int f(int u){
  9.     int l = A[u] == 1 ? L[u]: f(L[u]);
  10.     int r = B[u] == 1 ? R[u]: f(R[u]);
  11.     ans += abs(l - r);
  12.     return 2 * max(l, r);
  13. }
  14.  
  15. int main(){
  16.  
  17.     int n;
  18.     scanf("%d", &n);
  19.  
  20.     for(int i=1;i<=n;i++){
  21.         scanf("%d %d %d %d", &A[i], &L[i], &B[i], &R[i]);
  22.     }
  23.  
  24.     f(1);
  25.  
  26.     printf("%d", ans);
  27.  
  28.     return 0;
  29. }
  30.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement