#include <bits/stdc++.h>
#pragma GCC optimize("-O2")
using namespace std;
void err(istream_iterator<string> it) { cerr << endl; }
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << "\t";
  err(++it, args...);
}
template <typename T1, typename T2>
ostream& operator<<(ostream& c, pair<T1, T2>& v) {
  c << "(" << v.first << "," << v.second << ")";
}
template <template <class...> class TT, class... T>
ostream& operator<<(ostream& out, TT<T...>& c) {
  out << "{ ";
  for (auto& x : c) out << x << " ";
  out << "}";
}
const int LIM = 1e5 + 5, MOD = 1e9 + 7;
int t, n, m, k, x, y;
vector<vector<int> > v;
vector<int> a;
long long int ans = 0;
int lvl = 0;
pair<int, int> dfs(int i, int par) {
  pair<int, int> res = make_pair(0, 0);
  bool o = (a[i] & (1 << lvl));
  long long int cans = 0;
  for (auto& it : v[i]) {
    if (it != par) {
      pair<int, int> l = dfs(it, i);
      if (o)
        cans -= (l.first * (long long int)l.first +
                 l.second * (long long int)l.second);
      else
        cans -= (l.first * (long long int)l.second);
      res = {res.first + l.first, res.second + l.second};
    }
  }
  if (o)
    cans += res.first * (long long int)res.first +
            res.second * (long long int)res.second;
  else
    cans += res.first * (long long int)res.second;
  if (o) {
    cans /= 2;
  }
  ans += cans;
  if (o)
    ans += res.second + 1;
  else
    ans += res.first;
  res = {res.first, res.second + 1};
  if (o) res = make_pair(res.second, res.first);
  return res;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  v.assign(n, vector<int>(0));
  a.resize(n);
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < n - 1; i++) {
    cin >> x >> y;
    --x, --y;
    v[x].push_back(y), v[y].push_back(x);
  }
  long long int fans = 0;
  for (int i = 0; i < 20; i++) {
    ans = 0;
    dfs(0, -1);
    fans += ans * (1LL << lvl);
    lvl++;
  }
  cout << fans << "\n";
  return 0;
}
