#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
long long n, val[200020];
long long ans;
long long head[200020], pos;
struct edge {
  long long to, next;
} e[200020 << 1];
void add(long long a, long long b) {
  pos++;
  e[pos].to = b, e[pos].next = head[a], head[a] = pos;
}
long long f[200020][22][2];
long long g[200020][22][2];
void dfs(long long u, long long fa) {
  for (long long i = head[u]; i; i = e[i].next) {
    long long v = e[i].to;
    if (v == fa) continue;
    dfs(v, u);
    for (long long j = 0; j <= 20; j++) {
      bool c = (val[u] & (1 << j));
      f[u][j][0] += f[v][j][0 ^ c];
      f[u][j][1] += f[v][j][1 ^ c];
    }
  }
  for (long long j = 0; j <= 20; j++) {
    bool c = (val[u] & (1 << j));
    f[u][j][0] += (c == 0);
    f[u][j][1] += (c == 1);
  }
}
void dp(long long u, long long fa) {
  if (fa) {
    for (long long j = 0; j <= 20; j++) {
      bool c = (val[u] & (1 << j)), cf = (val[fa] & (1 << j));
      g[u][j][0] = g[fa][j][0 ^ c] + f[fa][j][0 ^ c] - f[u][j][0 ^ c ^ cf] +
                   (c == 0) - (cf == (0 ^ c));
      g[u][j][1] = g[fa][j][1 ^ c] + f[fa][j][1 ^ c] - f[u][j][1 ^ c ^ cf] +
                   (c == 1) - (cf == (1 ^ c));
    }
  } else {
    for (long long j = 0; j <= 20; j++) {
      bool c = (val[u] & (1 << j));
      g[u][j][0] += (c == 0);
      g[u][j][1] += (c == 1);
    }
  }
  for (long long i = head[u]; i; i = e[i].next) {
    long long v = e[i].to;
    if (v == fa) continue;
    dp(v, u);
  }
}
long long all;
int main() {
  n = read();
  for (long long i = 1; i <= n; i++) val[i] = read(), all += val[i];
  for (long long i = 1; i < n; i++) {
    long long x = read(), y = read();
    add(x, y);
    add(y, x);
  }
  dfs(1, 0);
  dp(1, 0);
  for (long long i = 1; i <= n; i++)
    for (long long j = 0; j <= 20; j++) {
      bool c = (val[i] & (1 << j));
      ans += (((f[i][j][1] + g[i][j][1]) - (c == 1)) << j);
    }
  ans = (ans - all) / 2 + all;
  printf("%lld\n", ans);
}
