#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10, inf = 1e9 + 10, MOD = 1e9 + 7, Sq = 708;
const long long INF = 1e18 + 10;
vector<int> g[maxn];
int a[maxn], dp_down[maxn][25][2], dp_up[maxn][25][2], n;
long long ans = 0;
void dfs1(int u, int p, int i) {
  int x = (1 << i) & a[u];
  if (x) x = 1;
  dp_down[u][i][x]++;
  for (int v : g[u])
    if (v != p) {
      dfs1(v, u, i);
      dp_down[u][i][0] += dp_down[v][i][0 ^ x];
      dp_down[u][i][1] += dp_down[v][i][1 ^ x];
    }
}
void dfs2(int u, int p, int i) {
  int sum[2] = {0, 0}, x = (1 << i) & a[u];
  if (x) x = 1;
  for (int v : g[u])
    if (v != p) {
      sum[0] += dp_down[v][i][0];
      sum[1] += dp_down[v][i][1];
    }
  if (x) swap(sum[0], sum[1]);
  for (int v : g[u])
    if (v != p) {
      int t0 = sum[0] - dp_down[v][i][0 ^ x] + (x ^ 1) + dp_up[u][i][0];
      int t1 = sum[1] - dp_down[v][i][1 ^ x] + (x ^ 0) + dp_up[u][i][1];
      if ((1 << i) & a[v]) swap(t0, t1);
      dp_up[v][i][1] = t1;
      dp_up[v][i][0] = t0;
      dfs2(v, u, i);
    }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n;
  for (int i = 0; i < n; i++) cin >> a[i], ans += a[i];
  for (int i = 0, u, v; i < n - 1; i++) {
    cin >> u >> v;
    u--, v--;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  for (int i = 0; i <= 20; i++) dfs1(0, 0, i);
  for (int i = 0; i <= 20; i++) dfs2(0, 0, i);
  for (int i = 0; i < n; i++)
    for (int j = 0; j <= 20; j++)
      ans += 1ll * (dp_down[i][j][1] + dp_up[i][j][1]) * (1 << j);
  cout << ans / 2;
  return 0;
}
