Problem:
Score : 1500 points

Problem Statement

You are given a tree with N vertices numbered 1, 2, ..., N. The i-th edge connects Vertex a_i and Vertex b_i.
You are also given a string S of length N consisting of 0 and 1. The i-th character of S represents the number of pieces placed on Vertex i.

Snuke will perform the following operation some number of times:

Choose two pieces the distance between which is at least 2, and bring these pieces closer to each other by 1. More formally, choose two vertices u and v, each with one or more pieces, and consider the shortest path between them. Here the path must contain at least two edges. Then, move one piece from u to its adjacent vertex on the path, and move one piece from v to its adjacent vertex on the path.

By repeating this operation, Snuke wants to have all the pieces on the same vertex. Is this possible?
If the answer is yes, also find the minimum number of operations required to achieve it.

Constraints

2 \leq N \leq 2000

|S| = N

S consists of 0 and 1, and contains at least one 1.

1 \leq a_i, b_i \leq N(a_i \neq b_i)

The edges (a_1, b_1), (a_2, b_2), ..., (a_{N - 1}, b_{N - 1}) forms a tree.

Input

Input is given from Standard Input in the following format:

N
S
a_1 b_1
a_2 b_2
:
a_{N - 1} b_{N - 1}

Output

If it is impossible to have all the pieces on the same vertex, print -1. If it is possible, print the minimum number of operations required.

Sample Input 1

7
0010101
1 2
2 3
1 4
4 5
1 6
6 7

Sample Output 1

3

We can gather all the pieces in three operations as follows:

Choose the pieces on Vertex 3 and 5.

Choose the pieces on Vertex 2 and 7.

Choose the pieces on Vertex 4 and 6.

Sample Input 2

7
0010110
1 2
2 3
1 4
4 5
1 6
6 7

Sample Output 2

-1

Sample Input 3

2
01
1 2

Sample Output 3

0

Solution in C++ (GCC 9.2.1) (judge verdict: Wrong Answer)

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <queue>
#include <stack>
#include <cstdlib>
#include <map>
#include <iomanip>
#include <set>
#include <stdio.h>
#include <ctype.h>
#include <random>
#include <string.h>
#include <cmath>
#include <unordered_map>
#include <cstdio>
using namespace std;
#define all(vec) vec.begin(),vec.end()
typedef long long ll;

ll gcd(ll x, ll y) {
	if (y == 0)return x;
	return gcd(y, x%y);
}
ll lcm(ll x, ll y) {
	return x / gcd(x, y)*y;
}
ll kai(ll x, ll y, ll m) {
	ll res = 1;
	for (ll i = x - y + 1; i <= x; i++) {
		res *= i; res %= m;
	}
	return res;
}
ll mod_pow(ll x, ll y, ll m) {
	ll res = 1;
	while (y > 0) {
		if (y & 1) {
			res = res * x % m;
		}
		x = x * x % m;
		y >>= 1;
	}
	return res;
}

ll comb(ll x, ll y, ll m) {
	if (y > x)return 0;
	return kai(x, y, m) * mod_pow(kai(y, y, m), m - 2, m) % m;
}

const int INF = 1000000000;

int n;
string s;
vector<int> vec[2010];
int ans = INF;
int cnt[2010], d[2010], cnt2[2010], d2[2010], pp[2010];
vector<int> ve;

void dfs(int v, int p) {
	pp[v] = p;
	for (int i = 0; i < (int)vec[v].size(); i++) {
		if (vec[v][i] == p)continue;
		dfs(vec[v][i], v);
		d[v] += d[vec[v][i]] + cnt[vec[v][i]];
		cnt[v] += cnt[vec[v][i]];
	}
	if (s[v - 1] == '1')cnt[v]++;
}

void dfs2(int v, int p) {
	if (s[v - 1] == '1')cnt2[v]++;
	for (int i = 0; i < (int)vec[v].size(); i++) {
		if (vec[v][i] == p)continue;
		cnt2[vec[v][i]] = cnt2[v] + cnt[v] - cnt[vec[v][i]];
		d2[vec[v][i]] = d2[v] + cnt2[v] + d[v] - d[vec[v][i]];
		dfs2(vec[v][i], v);
	}
}

signed main() {
	cin >> n >> s;
	int k = 0;
	for (int i = 0; i < n; i++)if (s[i] == '1')k++;
	if (k == 1) {
		cout << 0 << endl;
		return 0;
	}
	for (int i = 1; i < n; i++) {
		int a, b; cin >> a >> b;
		vec[a].push_back(b);
		vec[b].push_back(a);
	}
	dfs(1, 0);
	dfs2(1, 0);
	for (int i = 1; i <= n; i++) {
		int k = 0, ma = 0;
		for (int j = 0; j < (int)vec[i].size(); j++) {
			if (vec[i][j] == pp[i])k += d2[i], ma = max(ma, d2[i]);
			else k += d[vec[i][j]] + cnt[vec[i][j]], ma = max(ma, d[vec[i][j]] + cnt[vec[i][j]]);
		}
		if (k % 2 == 0 && ma <= k / 2)ans = min(ans, k / 2);
	}
	if (ans == INF)cout << -1 << endl;
	else cout << ans << endl;
}