#include <bits/stdc++.h>
using namespace std;
const int LG = 20;
int N, A[100001], DP[100001][20][2];
vector<int> adj[100001];
long long res = 0;
void DFS(int u, int p) {
  for (int i = 0; i < LG; i++) DP[u][i][(A[u] >> i) & 1]++;
  for (auto &v : adj[u]) {
    if (v != p) {
      DFS(v, u);
      for (int i = 0; i < LG; i++) {
        DP[u][i][0] += DP[v][i][(A[u] >> i) & 1];
        DP[u][i][1] += DP[v][i][!((A[u] >> i) & 1)];
      }
    }
  }
  for (auto &v : adj[u]) {
    if (v != p) {
      for (int i = 0; i < LG; i++) {
        res += (1LL << i) * DP[v][i][0] *
               (DP[u][i][1] - DP[v][i][!((A[u] >> i) & 1)]);
        res += (1LL << i) * DP[v][i][1] *
               (DP[u][i][0] - DP[v][i][(A[u] >> i) & 1]);
      }
    }
  }
  for (int i = 0; i < LG; i++) {
    res += (1LL << i) * (DP[u][i][1] - ((A[u] >> i) & 1));
  }
}
int main() {
  ios ::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    res += 2 * A[i];
  }
  for (int i = 1; i < N; i++) {
    int U, V;
    cin >> U >> V;
    adj[U - 1].push_back(V - 1);
    adj[V - 1].push_back(U - 1);
  }
  DFS(0, -1);
  cout << (res >> 1) << "\n";
  return 0;
}
