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++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef pair<P,P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define fi first
#define sc second
#define rep(i,x) for(int i=0;i<x;i++)
#define repn(i,x) for(int i=1;i<=x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())

int n;
bool h[2005];
vector<int>edge[2005];
int dist[2005][2005];
int sum[2005][2005];
int cnt[2005][2005];
P dfs(int v,int u,int g,int d){
	dist[g][v] = d;
	if(h[v]){
		sum[g][v] = d;
		cnt[g][v] = 1;
	}
	rep(i,edge[v].size()){
		if(edge[v][i] == u) continue;
		P p = dfs(edge[v][i],v,g,d+1);;
		sum[g][v] += p.fi;
		cnt[g][v] += p.sc;
	}
	return P(sum[g][v],cnt[g][v]);
}
int gen(int v,int u,int g){
	int S = 0, mx = 0;
	rep(i,edge[v].size()){
		if(edge[v][i] == u) continue;
		S += sum[g][edge[v][i]];
		mx = max(mx,sum[g][edge[v][i]]);
	}
	if(mx <= S/2) return cnt[g][v]+S%2;
	else{
		int id;
		rep(i,edge[v].size()){
			if(edge[v][i] == u) continue;
			if(mx == sum[g][edge[v][i]]) id = edge[v][i];
		}
		int reduction = gen(id,v,g);
		if(reduction <= S-mx) return cnt[g][v]+S%2;
		else return (reduction-S+mx)+cnt[g][v];
	}
}
int check(int v,int u,int g){
	int S = 0, mx = 0;
	rep(i,edge[v].size()){
		if(edge[v][i] == u) continue;
		S += sum[g][edge[v][i]];
		mx = max(mx,sum[g][edge[v][i]]);
	}
	if(u == -1){
		if(mx <= S/2) return S;
		int id = -1;
		rep(i,edge[v].size()){
			if(edge[v][i] == u) continue;
			if(sum[g][edge[v][i]] == mx) id = edge[v][i];
		}
		int reduction = gen(id,v,g);
		//cout << id << " " << v << " " << g << " " << reduction << endl;
		if(reduction > S-mx) return INF;
		else return S;
	}
}
int main(){
	cin >> n;
	string s; cin >> s;
	rep(i,n) if(s[i] == '1') h[i+1] = 1;
	rep(i,n-1){
		int a,b; cin >> a >> b;
		edge[a].pb(b);edge[b].pb(a);
	}
	repn(i,n) dfs(i,-1,i,0);
	int ans = INF;
	repn(i,n){
		if(sum[i][i]%2 == 1) continue;
		ans = min(ans,check(i,-1,i));
		assert(ans%2 == 0);
		//cout << i << " " << check(i,-1,i) << endl;
	}
	cout << (ans>INF/2?-1:ans/2) << endl;
}