Language: C++

#include <bits/stdc++.h>

using namespace std;
int n;
string s;
const int N = 3000;
vector<int> ed[N];
int sz[N];
int hg[N];
int dp[N];
int sumh[N];
void precalc(int v, int p, int h) {
    sz[v] = s[v - 1] - '0';
    hg[v] = h;
    sumh[v] = sz[v] * hg[v];
    for (auto to: ed[v]) {
        if (to == p) {
            continue;
        }
        precalc(to, v, h + 1);
        sz[v] += sz[to];
        sumh[v] += sumh[to];
    }
}


void dfs(int v, int p) {
    priority_queue<pair<int, int> > hp;
    for (auto to: ed[v]) {
        if (to == p) {
            continue;
        }
        dfs(to, v);
        hp.push(make_pair(sumh[to] - hg[v] * sz[to], to));
    }
    while (hp.size() > 2) {
        auto a = hp.top();
        hp.pop();
        auto b = hp.top();
        hp.pop();
        if (a.first == 0) {
            break;
        } else if (b.first == 0) {
            hp.push(a);
        } else {
            a.first--;
            b.first--;
            hp.push(a);
            hp.push(b);
        }
    }s
    if (hp.size() == 2) {
        auto a = hp.top();
        hp.pop();
        auto b = hp.top();
        hp.pop();
        int u = min(a.first, b.first);
        a.first -= u;
        b.first -= u;
        if (a.first != 0) {
            hp.push(a);
        }
        if (b.first != 0) {
            hp.push(b);
        }
    }
    if (hp.empty()) {
        dp[v] = sz[v] * hg[v];
    } else {
        auto a = hp.top();
        int delta = sumh[a.second] - dp[a.second];
        dp[v] = max(sz[v] * hg[v], a.first + sz[v] * hg[v] - delta);
    }
}


int main() {
    cin >> n;
    cin >> s;
    for (int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;
        ed[u].push_back(v);
        ed[v].push_back(u);
    }
    int ans = n * n + 20, vv = 1;
    for (int root = 1; root <= n; root++) {
        precalc(root, 0, 0);
        int sum = 0;
        for (int i = 1; i <= n; i++) {
            if (s[i - 1] == '1') {
                sum += hg[i];
            }
        }
        if (sum % 2 == 1) {
            continue;
        }
        if (sum / 2 < ans) {
            dfs(root, 0);
            if (dp[root] == 0) {
                ans = sum / 2;
            }
        }
    }
    if (ans == n * n + 20) {
        cout << "-1\n";
    } else {
        cout << ans << "\n";
    }
    return 0;
}
/*
 #include <bits/stdc++.h>

using namespace std;
int n;
string s;
const int N = 3000;
vector<int> ed[N];
int sz[N];
int hg[N];
int dp[N];
int sumh[N];
void precalc(int v, int p, int h) {
    sz[v] = s[v - 1] - '0';
    hg[v] = h;
    sumh[v] = sz[v] * hg[v];
    for (auto to: ed[v]) {
        if (to == p) {
            continue;
        }
        precalc(to, v, h + 1);
        sz[v] += sz[to];
        sumh[v] += sumh[to];
    }
}

void dfs(int v, int p) {
    priority_queue<pair<int, int> > hp;
    for (auto to: ed[v]) {
        if (to == p) {
            continue;
        }
        dfs(to, v);
        hp.push(make_pair(dp[to], to));
    }
    while (hp.size() > 1) {
        auto a = hp.top();
        hp.pop();
        auto b = hp.top();
        hp.pop();
        if (a.first == hg[v] * sz[a.second] || b.first == hg[v] * sz[b.second]) {
            hp.push(a);
            hp.push(b);
            break;
        } else {
            a.first--;
            b.first--;
            hp.push(a);
            hp.push(b);
        }
    }
    dp[v] = 0;
    while (!hp.empty()) {
        dp[v] += hp.top().first;
        hp.pop();
    }
    if (s[v - 1] == '1') {
        dp[v] += hg[v];
    }
}

int main() {
    cin >> n;
    cin >> s;
    for (int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;
        ed[u].push_back(v);
        ed[v].push_back(u);
    }
    int ans = n * n + 20;
    for (int root = 1; root <= n; root++) {
        precalc(root, 0, 0);
        int sum = 0;
        for (int i = 1; i <= n; i++) {
            if (s[i - 1] == '1') {
                sum += hg[i];
            }
        }
        if (sum % 2 == 1) {
            continue;
        }
        dfs(root, 0);
        priority_queue<pair<int, int> > hp;
        for (auto to: ed[root]) {
            hp.push(make_pair(sumh[to], to));
        }
        while (hp.size() > 1) {
            auto a = hp.top();
            hp.pop();
            auto b = hp.top();
            hp.pop();
            if (a.first == 0) {
                continue;
            } else if (b.first == 0) {
                hp.push(a);
            } else {
                a.first--;
                b.first--;
                hp.push(a);
                hp.push(b);
            }
        }
        if (hp.empty()) {
            ans = min(ans, sum / 2);
        } else {
            auto a = hp.top();
            int delta = sumh[a.second] - dp[a.second];
            if (delta >= a.first) {
                ans = min(ans, sum / 2);
            }
        }
    }
    if (ans == n * n + 20) {
        cout << "-1\n";
    } else {
        cout << ans << "\n";
    }
    return 0;
}

 */

Compiler output:
a.cc: In function 'void dfs(int, int)':
a.cc:51:7: error: expected ';' before 'if'
   51 |     }s
      |       ^
      |       ;
   52 |     if (hp.size() == 2) {
      |     ~~