#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 1;
const int Log = 21;
int a[N], t[N], f[N];
vector<int> g[N];
long long ans = 0;
void dfs(int v, int par, int ind) {
  int F = 0, T = 0;
  for (auto x : g[v])
    if (x - par) {
      dfs(x, v, ind);
      F += f[x];
      T += t[x];
    }
  int CF = F, CT = T;
  for (auto x : g[v])
    if (x - par) {
      if ((a[v] >> ind) & 1) {
        ans += 1LL * (F - f[x]) * f[x] * (1LL << ind);
        ans += 1LL * (T - t[x]) * t[x] * (1LL << ind);
        ans += 1LL * f[x] * (1LL << ind);
      } else {
        ans += 1LL * (F - f[x]) * t[x] * (1LL << ind);
        ans += 1LL * (T - t[x]) * f[x] * (1LL << ind);
        ans += 1LL * t[x] * (1LL << ind);
      }
      F -= f[x];
      T -= t[x];
    }
  F = CF, T = CT;
  f[v] = F;
  t[v] = T;
  if ((a[v] >> ind) & 1) {
    swap(f[v], t[v]);
    t[v]++;
  } else
    f[v]++;
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) scanf("%d", a + i);
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    scanf("%d %d", &u, &v);
    --u, --v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  for (int i = 0; i < Log; i++) {
    memset(t, 0, sizeof t);
    memset(f, 0, sizeof f);
    dfs(0, -1, i);
  }
  for (int i = 0; i < n; i++) ans += a[i];
  printf("%I64d\n", ans);
}
