#include <bits/stdc++.h>
using namespace std;
const int inf = 1.01e9;
const double eps = 1e-9;
const int N = 1e5 + 10;
vector<int> v[N];
int A[N];
int a[N];
int n;
long long RES;
pair<int, int> dfs(int x, int xx) {
  vector<pair<int, int> > t;
  for (int y : v[x])
    if (y != xx) {
      t.push_back(dfs(y, x));
    }
  int s0 = 0, s1 = 0;
  for (pair<int, int> z : t) {
    s0 += z.first;
    s1 += z.second;
  }
  if (a[x] == 0) {
    RES += s0 * (long long)s1 * 2;
    RES += s1 * 2;
    for (pair<int, int> z : t) {
      RES -= z.first * (long long)z.second * 2;
    }
    return {s0 + 1, s1};
  } else {
    RES += s0 * (long long)s0 + s1 * (long long)s1;
    RES += s0 * 2 + 2;
    for (pair<int, int> z : t) {
      RES -= z.first * (long long)z.first + z.second * (long long)z.second;
    }
    return {s1, s0 + 1};
  }
}
long long calc() {
  RES = 0;
  dfs(0, -1);
  return RES;
}
int main() {
  scanf("%d", &n);
  for (int i = 0; (i) < (n); ++i) scanf("%d", &A[i]);
  for (int i = 0; (i) < (n - 1); ++i) {
    int x, y;
    scanf("%d%d", &x, &y);
    --x, --y;
    v[x].push_back(y);
    v[y].push_back(x);
  }
  long long res = 0;
  for (int j = 0; (j) < (20); ++j) {
    for (int i = 0; (i) < (n); ++i) a[i] = (A[i] >> j) & 1;
    res += calc() * (1LL << (j)) / 2;
  }
  printf("%I64d\n", res);
  return 0;
}
