Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int N = 1e3 + 10;
- int A[N], L[N], B[N], R[N];
- int ans = 0;
- int f(int u){
- int l = A[u] == 1 ? L[u]: f(L[u]);
- int r = B[u] == 1 ? R[u]: f(R[u]);
- ans += abs(l - r);
- return 2 * max(l, r);
- }
- int main(){
- int n;
- scanf("%d", &n);
- for(int i=1;i<=n;i++){
- scanf("%d %d %d %d", &A[i], &L[i], &B[i], &R[i]);
- }
- f(1);
- printf("%d", ans);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement