#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
const int LOG = 22;
int n, u, v;
vector<int> g[N];
int cur[N][LOG][2], arr[N];
long long ans = 0;
void DFS(int node, int prnt) {
  ans += arr[node];
  for (int i = 0; i < LOG; i++) {
    cur[node][i][((arr[node] >> i) & 1)] = 1;
  }
  for (int i = 0; i < (int)g[node].size(); i++) {
    if (g[node][i] == prnt) continue;
    DFS(g[node][i], node);
    for (int j = 0; j < LOG; j++) {
      for (int k = 0; k < 2; k++) {
        for (int l = 0; l < 2; l++) {
          ans += (long long)cur[node][j][k] * (long long)cur[g[node][i]][j][l] *
                 (k ^ l) * (long long)(1 << j);
        }
      }
      if (((arr[node] >> j) & 1) == 1)
        cur[node][j][0] += cur[g[node][i]][j][1],
            cur[node][j][1] += cur[g[node][i]][j][0];
      else
        cur[node][j][1] += cur[g[node][i]][j][1],
            cur[node][j][0] += cur[g[node][i]][j][0];
    }
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &arr[i]);
  for (int i = 0; i < n - 1; i++) {
    scanf("%d%d", &u, &v);
    g[u].push_back(v);
    g[v].push_back(u);
  }
  DFS(1, -1);
  cout << ans << endl;
  return 0;
}
