#include <bits/stdc++.h>

using namespace std;

//#define int long long

int n, m;
int a[100010];
vector<int> g[100010];
char used_centroid[100010];
int sz[100010];
int cnt = 0;
long long an = 0;
int c1[100010];
int c[20][2];

void dfs(int v, int par, int sum, int centroid){
    an += sum;
    if(v != centroid){
        c1[v] = sum;
        int t = sum ^ a[centroid];
        for(int i = 0; i < 20; i++){
            if(t & (1 << i)) c[i][1]++;
            else c[i][0]++;
        }
    }
    for(auto to: g[v]){
        if(to != par && !used_centroid[to]){
            dfs(to, v, sum ^ a[to], centroid);
        }
    }
}

void upd(int v, int par, int val, int sum){
    for(int i = 0; i < 20; i++){
        if(sum & (1 << i)) c[i][1] += val;
        else c[i][0] += val;
    }
    for(auto to: g[v]){
        if(to != par && !used_centroid[to]){
            upd(to, v, val, sum ^ a[to]);
        }
    }
}

void add(int v, int par){
    for(int i = 0; i < 20; i++){
        if((1 << i) & c1[v]) an += 1ll * c[i][0] * (1 << i);
        else an += 1ll * c[i][1] * (1 << i);
    }
    for(auto to: g[v]){
        if(to != par && !used_centroid[to]){
            add(to, v);
        }
    }
}

void solve(int v){
    memset(c, 0, sizeof(c));
    dfs(v, -1, a[v], v);
    for(auto to: g[v]){
        if(!used_centroid[to]){
            upd(to, -1, -1, a[to]);
            add(to, -1);
        }
    }
}

void calc_sz(int v, int par){
    sz[v] = 1;
    cnt++;
    for(auto to: g[v]){
        if(to != par && !used_centroid[to]){
            calc_sz(to, v);
            sz[v] += sz[to];
        }
    }
}

int getCentroid(int v){
    cnt = 0;
    calc_sz(v, -1);
    int u = -1;
    bool srcCentroid = true;
    for(auto to: g[v]){
        if(!used_centroid[to]){
            if(sz[to] > cnt / 2){
                srcCentroid = false;
            }
            if(u == -1 || sz[to] > sz[u]){
                u = to;
            }
        }
    }
    if(srcCentroid) return v;
    return getCentroid(u);
}

void centroid_decomp(int v){
    int centroid = getCentroid(v);
    used_centroid[centroid] = true;
    solve(centroid);
    for(auto to: g[centroid]){
        if(!used_centroid[to]){
            centroid_decomp(to);
        }
    }
}

main()
{
    ios_base::sync_with_stdio(0);
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    cin >> n;
    for(int i = 0; i < n; i++) cin >> a[i];
    for(int i = 0; i < n - 1; i++){
        int from, to;
        cin >> from >> to;
        g[from - 1].push_back(to - 1);
        g[to - 1].push_back(from - 1);
    }
    centroid_decomp(0);
    cout << an << endl;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair<int, int>
#define vi vector<int>
#define pb emplace_back
#define sz(x) (int)x.size()
#define all(v) v.begin(), v.end()
#define x first
#define y second
#define rep(i, j, k) for(i=j; i<k; i++)
#define sep(i, j, k) for(i=j; i>k; i--)
const int N = 2e5+5, inf = 1e9+7;
int n, k, i, j;
int A[N], num[N][20], c[N];
vi adj[N];

void dfs(int id, int p){
    c[id]=1;
    for(auto i:adj[id]) if(i!=p){
        dfs(i, id);
        rep(j, 0, 20)
            num[id][j] += num[i][j];
        c[id] += c[i];
    }
    rep(i, 0, 20)
        if(A[id]>>i&1)
            num[id][i] = c[id]-num[id][i];
}

void dfs2(int id, int p){
    int a, b, temp;
    if(id!=1)
    rep(i, 0, 20){
        a = (A[id]>>i&1);
        b = (A[p]>>i&1);
        if(a&b) num[id][i] = n-num[p][i];
        else if(a==0 && b==0) num[id][i] = num[p][i];
        else if(a==1){
            temp = num[p][i] - num[id][i];
            temp = (n-c[id]) - temp;
            num[id][i] += temp;
        }
        else{
            temp = num[p][i] - (c[id]-num[id][i]);
            num[id][i] += temp;
        }
    }
    for(auto i:adj[id]) if(i!=p)
        dfs2(i, id);
}

signed main()
{
    ios_base::sync_with_stdio(false); cin.tie(0);
    int i, j, a, b, ans=0;
    cin>>n;
    rep(i, 1, n+1) cin>>A[i], ans+=A[i];
    rep(i, 1, n){
        cin>>a>>b;
        adj[a].pb(b);
        adj[b].pb(a);
    }
    dfs(1, 1);
    dfs2(1, 1);
    rep(i, 1, n+1) rep(j, 0, 20) ans += num[i][j]*(1LL<<j);
    cout<<ans/2;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair<int, int>
#define vi vector<int>
#define pb emplace_back
#define sz(x) (int)x.size()
#define all(v) v.begin(), v.end()
#define x first
#define y second
#define rep(i, j, k) for(i=j; i<k; i++)
#define sep(i, j, k) for(i=j; i>k; i--)
const int N = 2e5+5, inf = 1e9+7, M=20;
int n, k, i, j;
int A[N], dp[N][M][2];
bitset<M>bit[N];
vi adj[N];

int dfs(int cur, int p){
    rep(i, 0, M)
        dp[cur][i][bit[cur][i]] = 1;
    int ans = A[cur], temp;
    for(auto i:adj[cur]) if(i!=p){
        ans += dfs(i, cur);
        rep(j, 0, M){
            temp = dp[cur][j][0]*dp[i][j][1] + dp[cur][j][1]*dp[i][j][0];
            ans += temp*(1<<j);
            dp[cur][j][0] += dp[i][j][bit[cur][j]];
            dp[cur][j][1] += dp[i][j][!bit[cur][j]];
        }
    }
    return ans;
}

signed main()
{
    ios_base::sync_with_stdio(false); cin.tie(0);
    int i, j, a, b, ans=0;
    cin>>n;
    rep(i, 1, n+1) {
        cin>>A[i]; a = A[i];
        rep(j, 0, M) bit[i][j] = a&1, a>>=1;
    }
    rep(i, 1, n){
        cin>>a>>b;
        adj[a].pb(b);
        adj[b].pb(a);
    }
    cout<<dfs(1, 1);
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair<int, int>
#define vi vector<int>
#define pb emplace_back
#define sz(x) (int)x.size()
#define all(v) v.begin(), v.end()
#define x first
#define y second
#define rep(i, j, k) for(i=j; i<k; i++)
#define sep(i, j, k) for(i=j; i>k; i--)
const int N = 2e5+5, inf = 1e9+7;
int n, k, i, j;
int A[N], num[N][20], c[N], f[N][20];
vi adj[N];

void dfs(int id, int p){
    c[id]=1;
    for(auto i:adj[id]) if(i!=p){
        dfs(i, id);
        rep(j, 0, 20)
            num[id][j] += num[i][j];
        c[id] += c[i];
    }
    for(auto i:adj[id]) if(i!=p){
        rep(j, 0, 4){
            if(A[id]>>j&1) f[id][j] += (num[id][j] - num[i][j])*num[i][j] + (c[i] - num[i][j])*( c[id]-1-c[i] - (num[id][j] - num[i][j])  );
            else f[id][j] += (num[id][j] - num[i][j])*(c[i]-num[i][j]) + num[i][j]*( c[id]-1-c[i] - (num[id][j] - num[i][j])  );
        }
    }
    rep(i, 0, 20)
        if(A[id]>>i&1)
            num[id][i] = c[id]-num[id][i];
}

signed main()
{
    ios_base::sync_with_stdio(false); cin.tie(0);
    int i, j, a, b, ans=0;
    cin>>n;
    rep(i, 1, n+1) cin>>A[i];
    rep(i, 1, n){
        cin>>a>>b;
        adj[a].pb(b);
        adj[b].pb(a);
    }
    dfs(1, 1);
    rep(i, 1, n+1) {
        c[i]=0;
        rep(j, 0, 20) c[i] += ( f[i][j]/2 + num[i][j])*(1LL<<j);
        ans+= c[i];
    }
    cout<<ans;
}


#include <bits/stdc++.h>
#define int long long
using namespace std;

const int maxn = 1e5 + 10, lg = 4;
int a[maxn], dp[2][lg][maxn], ans[lg];
vector<int> G[maxn];

void debug(int now) {
    cout << "now = " << now << endl;
    for (int i = 0; i < lg; ++i) cout << "bit = " << i << ": " << dp[1][i][now] << ' ' << dp[0][i][now] << endl;
}

void dfs(int now, int fa) {
    for (int u : G[now]) if (u != fa) {
        dfs(u, now);
        for (int i = 0; i < lg; ++i) {
            if (a[now] & (1 << i)) dp[1][i][now] += dp[0][i][u], dp[0][i][now] += dp[1][i][u];
            else dp[1][i][now] += dp[1][i][u], dp[0][i][now] += dp[0][i][u];
        }
    }
    vector<int> tmp(lg, 0);
    for (int u : G[now]) if (u != fa) {
        for (int i = 0; i < lg; ++i) {
            if (a[now] & (1 << i)) dp[1][i][now] -= dp[0][i][u], dp[0][i][now] -= dp[1][i][u];
            else dp[1][i][now] -= dp[1][i][u], dp[0][i][now] -= dp[0][i][u];
            // cout << "i = " << i << " dp[1] = " << dp[1][i][now] << " dp[0] = " << dp[0][i][now] << endl;
            tmp[i] += dp[1][i][now] * dp[0][i][u] + dp[1][i][u] * dp[0][i][now];
            if (a[now] & (1 << i)) dp[1][i][now] += dp[0][i][u], dp[0][i][now] += dp[1][i][u];
            else dp[1][i][now] += dp[1][i][u], dp[0][i][now] += dp[0][i][u];
        }
    }
    for (int i = 0; i < lg; ++i) {
        if (a[now] & (1 << i)) ++dp[1][i][now];
        else ++dp[0][i][now];
    }
    // debug(now);
    // cout << "tmp: "; for (int i = 0; i < lg; ++i) cout << tmp[i] << ' '; cout << endl;
    for (int i = 0; i < lg; ++i) ans[i] += tmp[i] / 2;
    for (int i = 0; i < lg; ++i) ans[i] += dp[1][i][now];
    // cout << "ans: "; for (int i = 0; i < lg; ++i) cout << ans[i] << ' '; cout << endl;
}

int32_t main() {
    ios_base::sync_with_stdio(false); cin.tie(0);
    int n; cin >> n;
    for (int i = 1; i <= n; ++i) cin >> a[i];
    for (int i = 0; i < n - 1; ++i) {
        int a, b; cin >> a >> b;
        G[a].emplace_back(b); G[b].emplace_back(a);
    }
    dfs(1, 0);
    long long sum = 0;
    for (int i = 0; i < lg; ++i) sum += (long long)(1 << i) * (long long)ans[i];
    cout << sum << endl;
    return 0;
}


#include <bits/stdc++.h>
#define int long long
using namespace std;

const int maxn = 1e5 + 10, lg = 20;
int a[maxn], dp[2][lg][maxn], ans[lg];
vector<int> G[maxn];

void debug(int now) {
    cout << "now = " << now << endl;
    for (int i = 0; i < lg; ++i) cout << "bit = " << i << ": " << dp[1][i][now] << ' ' << dp[0][i][now] << endl;
}

void dfs(int now, int fa) {
    for (int u : G[now]) if (u != fa) {
        dfs(u, now);
        for (int i = 0; i < lg; ++i) {
            if (a[now] & (1 << i)) dp[1][i][now] += dp[0][i][u], dp[0][i][now] += dp[1][i][u];
            else dp[1][i][now] += dp[1][i][u], dp[0][i][now] += dp[0][i][u];
        }
    }
    vector<int> tmp(lg, 0);
    for (int u : G[now]) if (u != fa) {
        for (int i = 0; i < lg; ++i) {
            if (a[now] & (1 << i)) dp[1][i][now] -= dp[0][i][u], dp[0][i][now] -= dp[1][i][u];
            else dp[1][i][now] -= dp[1][i][u], dp[0][i][now] -= dp[0][i][u];
            // cout << "i = " << i << " dp[1] = " << dp[1][i][now] << " dp[0] = " << dp[0][i][now] << endl;
            tmp[i] += dp[1][i][now] * dp[0][i][u] + dp[1][i][u] * dp[0][i][now];
            if (a[now] & (1 << i)) dp[1][i][now] += dp[0][i][u], dp[0][i][now] += dp[1][i][u];
            else dp[1][i][now] += dp[1][i][u], dp[0][i][now] += dp[0][i][u];
        }
    }
    for (int i = 0; i < lg; ++i) {
        if (a[now] & (1 << i)) ++dp[1][i][now];
        else ++dp[0][i][now];
    }
    // debug(now);
    // cout << "tmp: "; for (int i = 0; i < lg; ++i) cout << tmp[i] << ' '; cout << endl;
    for (int i = 0; i < lg; ++i) ans[i] += tmp[i] / 2;
    for (int i = 0; i < lg; ++i) ans[i] += dp[1][i][now];
    // cout << "ans: "; for (int i = 0; i < lg; ++i) cout << ans[i] << ' '; cout << endl;
}

int32_t main() {
    ios_base::sync_with_stdio(false); cin.tie(0);
    int n; cin >> n;
    for (int i = 1; i <= n; ++i) cin >> a[i];
    for (int i = 0; i < n - 1; ++i) {
        int a, b; cin >> a >> b;
        G[a].emplace_back(b); G[b].emplace_back(a);
    }
    dfs(1, 0);
    long long sum = 0;
    for (int i = 0; i < lg; ++i) sum += (long long)(1 << i) * (long long)ans[i];
    cout << sum << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int maxn = 1e5 + 10, lg = 4;
int a[maxn], dp[2][lg][maxn], ans[lg];
vector<int> G[maxn];

void debug(int now) {
    cout << "now = " << now << endl;
    for (int i = 0; i < lg; ++i) cout << "bit = " << i << ": " << dp[1][i][now] << ' ' << dp[0][i][now] << endl;
}

void dfs(int now, int fa) {
    for (int u : G[now]) if (u != fa) {
        dfs(u, now);
        for (int i = 0; i < lg; ++i) {
            if (a[now] & (1 << i)) dp[1][i][now] += dp[0][i][u], dp[0][i][now] += dp[1][i][u];
            else dp[1][i][now] += dp[1][i][u], dp[0][i][now] += dp[0][i][u];
        }
    }
    vector<int> tmp(lg, 0);
    for (int u : G[now]) if (u != fa) {
        for (int i = 0; i < lg; ++i) {
            if (a[now] & (1 << i)) dp[1][i][now] -= dp[0][i][u], dp[0][i][now] -= dp[1][i][u];
            else dp[1][i][now] -= dp[1][i][u], dp[0][i][now] -= dp[0][i][u];
            // cout << "i = " << i << " dp[1] = " << dp[1][i][now] << " dp[0] = " << dp[0][i][now] << endl;
            tmp[i] += dp[1][i][now] * dp[0][i][u] + dp[1][i][u] * dp[0][i][now];
            if (a[now] & (1 << i)) dp[1][i][now] += dp[0][i][u], dp[0][i][now] += dp[1][i][u];
            else dp[1][i][now] += dp[1][i][u], dp[0][i][now] += dp[0][i][u];
        }
    }
    for (int i = 0; i < lg; ++i) {
        if (a[now] & (1 << i)) ++dp[1][i][now];
        else ++dp[0][i][now];
    }
    // debug(now);
    // cout << "tmp: "; for (int i = 0; i < lg; ++i) cout << tmp[i] << ' '; cout << endl;
    for (int i = 0; i < lg; ++i) ans[i] += tmp[i] / 2;
    for (int i = 0; i < lg; ++i) ans[i] += dp[1][i][now];
    // cout << "ans: "; for (int i = 0; i < lg; ++i) cout << ans[i] << ' '; cout << endl;
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(0);
    int n; cin >> n;
    for (int i = 1; i <= n; ++i) cin >> a[i];
    for (int i = 0; i < n - 1; ++i) {
        int a, b; cin >> a >> b;
        G[a].emplace_back(b); G[b].emplace_back(a);
    }
    dfs(1, 0);
    long long sum = 0;
    for (int i = 0; i < lg; ++i) sum += (long long)(1 << i) * (long long)ans[i];
    cout << sum << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int maxn = 1e5 + 10, lg = 20;
int a[maxn];
long long dp[2][lg][maxn], ans[lg];
vector<int> G[maxn];

void dfs(int now, int fa) {
    for (int u : G[now]) if (u != fa) {
        dfs(u, now);
        for (int i = 0; i < lg; ++i) {
            if (a[now] & (1 << i)) dp[1][i][now] += dp[0][i][u], dp[0][i][now] += dp[1][i][u];
            else dp[1][i][now] += dp[1][i][u], dp[0][i][now] += dp[0][i][u];
        }
    }
    vector<long long> tmp(lg, 0);
    for (int u : G[now]) if (u != fa) {
        for (int i = 0; i < lg; ++i) {
            if (a[now] & (1 << i)) dp[1][i][now] -= dp[0][i][u], dp[0][i][now] -= dp[1][i][u];
            else dp[1][i][now] -= dp[1][i][u], dp[0][i][now] -= dp[0][i][u];
            tmp[i] += dp[1][i][now] * dp[0][i][u] + dp[1][i][u] * dp[0][i][now];
            if (a[now] & (1 << i)) dp[1][i][now] += dp[0][i][u], dp[0][i][now] += dp[1][i][u];
            else dp[1][i][now] += dp[1][i][u], dp[0][i][now] += dp[0][i][u];
        }
    }
    for (int i = 0; i < lg; ++i) {
        if (a[now] & (1 << i)) ++dp[1][i][now];
        else ++dp[0][i][now];
    }
    for (int i = 0; i < lg; ++i) ans[i] += tmp[i] / 2;
    for (int i = 0; i < lg; ++i) ans[i] += dp[1][i][now];
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(0);
    int n; cin >> n;
    for (int i = 1; i <= n; ++i) cin >> a[i];
    for (int i = 0; i < n - 1; ++i) {
        int a, b; cin >> a >> b;
        G[a].emplace_back(b); G[b].emplace_back(a);
    }
    dfs(1, 0);
    long long sum = 0;
    for (int i = 0; i < lg; ++i) sum += (long long)(1 << i) * ans[i];
    cout << sum << endl;
    return 0;
}


#include <iostream>
#include <vector>
#include <cstdio>
using namespace std;
typedef long long LL;
const int NICO = 100000 + 10;
int n, a[NICO];
vector<int> vec[NICO];
int dp[NICO][31][2];LL res;

void dfs(int x, int par)
{
    for(int i=0;i<30;i++)
    {
        if((1<<i)&a[x]) dp[x][i][1] = 1;
        else dp[x][i][0] = 1;
    }
    for(int i=0;i<vec[x].size();i++)
    {
        int cur = vec[x][i];
        if(cur == par) continue;
        dfs(cur, x);
        for(int j=0;j<30;j++)
        {
            res += ((LL)dp[cur][j][0]*dp[x][j][1] + (LL)dp[cur][j][1]*dp[x][j][0]) << j;
            if((1<<j)&a[x])
            {
                dp[x][j][0] += dp[cur][j][1];
                dp[x][j][1] += dp[cur][j][0];
            } else {
                dp[x][j][1] += dp[cur][j][1];
                dp[x][j][0] += dp[cur][j][0];
            }
        }
    }
}

int main()
{
    scanf("%d", &n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d", &a[i]);
        res += a[i];
    }
    for(int i=1;i<n;i++)
    {
        int u, v;
        scanf("%d %d", &u, &v);
        vec[u].push_back(v);
        vec[v].push_back(u);
    }
    dfs(1, 0);
    cout << res << endl;
}


#include <iostream>
#include <vector>
#include <cstdio>
using namespace std;
typedef long long LL;
const int NICO = 100000 + 10;
int n, a[NICO];
vector<int> vec[NICO];
int dp[NICO][31][2];LL res;
void dfs(int x, int par)
{
    for(int i=0;i<30;i++)
    {
        if((1<<i)&a[x]) dp[x][i][1] = 1;
        else dp[x][i][0] = 1;
    }
    for(int i=0;i<vec[x].size();i++)
    {
        int cur = vec[x][i];
        if(cur == par) continue;
        dfs(cur, x);
        for(int j=0;j<30;j++)
        {
            res += ((LL)dp[cur][j][0]*dp[x][j][1] + (LL)dp[cur][j][1]*dp[x][j][0]) << j;//请机智の读者自行思考该式。
            int t = (a[x]>>j)&1;
            dp[x][j][0^t] += dp[cur][j][0];
            dp[x][j][1^t] += dp[cur][j][1];
        }
    }
}
int main()
{
    scanf("%d", &n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d", &a[i]);
        res += a[i]; //长度为0的路径还没考虑！
    }
    for(int i=1;i<n;i++)
    {
        int u, v;
        scanf("%d %d", &u, &v);
        vec[u].push_back(v);
        vec[v].push_back(u);
    }
    dfs(1, 0);
    cout << res << endl;
}


#include <iostream>
#include <vector>
#include <cstdio>
using namespace std;
typedef long long LL;
const int NICO = 1000000 + 10;
int n, a[NICO];
vector<int> vec[NICO];
int dp[NICO][31][2];LL res;

void dfs(int x, int par)
{
    for(int i=0;i<30;i++)
    {
        if((1<<i)&a[x]) dp[x][i][1] = 1;
        else dp[x][i][0] = 1;
    }
    for(int i=0;i<vec[x].size();i++)
    {
        int cur = vec[x][i];
        if(cur == par) continue;
        dfs(cur, x);
        for(int j=0;j<30;j++)
        {
            res += (dp[cur][j][0]*dp[x][j][1] + dp[cur][j][1]*dp[x][j][0]) << j;
            if((1<<j)&a[x])
            {
                dp[x][j][0] += dp[cur][j][1];
                dp[x][j][1] += dp[cur][j][0];
            } else {
                dp[x][j][1] += dp[cur][j][1];
                dp[x][j][0] += dp[cur][j][0];
            }
        }
    }
}

int main()
{
    scanf("%d", &n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d", &a[i]);
        res += a[i];
    }
    for(int i=1;i<n;i++)
    {
        int u, v;
        scanf("%d %d", &u, &v);
        vec[u].push_back(v);
        vec[v].push_back(u);
    }
    dfs(1, 0);
    cout << res << endl;
}


#include <iostream>
#include <vector>
#include <cstdio>
using namespace std;
typedef long long LL;
const int NICO = 1000000 + 10;
int n, a[NICO];
vector<int> vec[NICO];
int dp[NICO][31][2];LL res;

void dfs(int x, int par)
{
    for(int i=0;i<30;i++)
    {
        if((1<<i)&a[x]) dp[x][i][1] = 1;
        else dp[x][i][0] = 1;
    }
    for(int i=0;i<vec[x].size();i++)
    {
        int cur = vec[x][i];
        if(cur == par) continue;
        dfs(cur, x);
        for(int j=0;j<30;j++)
        {
            res += ((LL)dp[cur][j][0]*dp[x][j][1] + (LL)dp[cur][j][1]*dp[x][j][0]) << j;
            if((1<<j)&a[x])
            {
                dp[x][j][0] += dp[cur][j][1];
                dp[x][j][1] += dp[cur][j][0];
            } else {
                dp[x][j][1] += dp[cur][j][1];
                dp[x][j][0] += dp[cur][j][0];
            }
        }
    }
}

int main()
{
    scanf("%d", &n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d", &a[i]);
        res += a[i];
    }
    for(int i=1;i<n;i++)
    {
        int u, v;
        scanf("%d %d", &u, &v);
        vec[u].push_back(v);
        vec[v].push_back(u);
    }
    dfs(1, 0);
    cout << res << endl;
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long LL;
const LL MOD = 1000000007;
int n, a[200002];
int vis1[20002], vis2[20002], vis[20002], s[20002];
int c[200];
vector<int> vec[20002], v[20002], v1[20002];
int tmp = 2*n;
void dfs1(int x)
{
    vis1[x] = 1;
    if(x==1)
    {
        v[1+n].push_back(1);
        v[1].push_back(1+n);
    }
    for(int i=0;i<vec[x].size();i++)
    {
        int cur = vec[x][i];
        if(vis1[cur]) continue;
        v[x].push_back(cur + n);
        v[cur].push_back(cur + n);
        v[cur + n].push_back(cur);
        v[cur + n].push_back(x);
        dfs1(cur);
    }
}
void dfs2(int x)
{
    vis2[x] = 1;
    for(int i=0;i<v[x].size();i++)
    {
        int cur = v[x][i];
        if(vis2[cur]) continue;
        if(i > 1)
        {
            v1[x+n].push_back(cur);
            v1[cur].push_back(x+n);
        } else {
            v1[x].push_back(cur);
            v1[cur].push_back(x);
        }
        dfs2(cur);
    }
}
void dfs(int x)
{
    vis[x] = 1;
    for(int i=0;i<v1[x].size();i++)
    {
        int cur = v1[x][i];
        if(vis[cur]) continue;
        s[cur] = a[cur] ^ s[x];
        if(cur <= n) cout << cur << " " << s[cur] << endl;
        dfs(cur);
    }
}
void solve()
{
    for(int i=1;i<=n;i++)
    {
        for(int j=0;j<30;j++)
        {
            if(s[i]&(1 << j))//记录每一位 1 的个数
                c[j]++;
        }
    }
    LL res = 0;
    for(int i=0;i<30;i++)
    {
        res += (LL)( n - c[i] ) * c[i] * ( 1 << i );//异或后在第i+1位有 (n-c[i])*c[i] 个 1
    }
    cout << 10 << endl;
}
int main()
{
    cin >> n;
    for(int i=1;i<=n;i++)
    {
        scanf("%d", &a[i]);
    }
    for(int i=1;i<=n-1;i++)
    {
        int u, v;
        scanf("%d %d", &u, &v);
        vec[u].push_back(v);
        vec[v].push_back(u);
    }
    dfs1(1); dfs2(1);
    dfs(1+n);
    solve();
}


#include <iostream>
#include <vector>
#include <cstdio>
using namespace std;
typedef long long LL;
const int NICO = 1000000 + 10;
int n, a[NICO];
vector<int> vec[NICO];
LL dp[NICO][31][2], res;

void dfs(int x, int par)
{
    for(int i=0;i<30;i++)
    {
        if((1<<i)&a[x]) dp[x][i][1] = 1;
        else dp[x][i][0] = 1;
    }
    for(int i=0;i<vec[x].size();i++)
    {
        int cur = vec[x][i];
        if(cur == par) continue;
        dfs(cur, x);
        for(int j=0;j<30;j++)
        {
            res += (dp[cur][j][0]*dp[x][j][1] + dp[cur][j][1]*dp[x][j][0]) << j;
            if((1<<j)&a[x])
            {
                dp[x][j][0] += dp[cur][j][1];
                dp[x][j][1] += dp[cur][j][0];
            } else {
                dp[x][j][1] += dp[cur][j][1];
                dp[x][j][0] += dp[cur][j][0];
            }
        }
    }
}

int main()
{
    scanf("%d", &n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d", &a[i]);
        res += a[i];
    }
    for(int i=1;i<n;i++)
    {
        int u, v;
        scanf("%d %d", &u, &v);
        vec[u].push_back(v);
        vec[v].push_back(u);
    }
    dfs(1, 0);
    cout << res << endl;
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long LL;
const LL MOD = 1000000007;
int n, a[200002];
int vis1[20002], vis2[20002], vis[20002], s[20002];
int c[200];
vector<int> vec[20002], v[20002], v1[20002];
int tmp = 2*n;
void dfs1(int x)
{
    vis1[x] = 1;
    if(x==1)
    {
        v[1+n].push_back(1);
        v[1].push_back(1+n);
    }
    for(int i=0;i<vec[x].size();i++)
    {
        int cur = vec[x][i];
        if(vis1[cur]) continue;
        v[x].push_back(cur + n);
        v[cur].push_back(cur + n);
        v[cur + n].push_back(cur);
        v[cur + n].push_back(x);
        dfs1(cur);
    }
}
void dfs2(int x)
{
    vis2[x] = 1;
    for(int i=0;i<v[x].size();i++)
    {
        int cur = v[x][i];
        if(vis2[cur]) continue;
        if(i > 1)
        {
            v1[x+n].push_back(cur);
            v1[cur].push_back(x+n);
        } else {
            v1[x].push_back(cur);
            v1[cur].push_back(x);
        }
        dfs2(cur);
    }
}
void dfs(int x)
{
    vis[x] = 1;
    for(int i=0;i<v1[x].size();i++)
    {
        int cur = v1[x][i];
        if(vis[cur]) continue;
        s[cur] = a[cur] ^ s[x];
        //if(cur <= n) cout << cur << " " << s[cur] << endl;
        dfs(cur);
    }
}
void solve()
{
    for(int i=1;i<=n;i++)
    {
        for(int j=0;j<30;j++)
        {
            if(s[i]&(1 << j))//记录每一位 1 的个数
                c[j]++;
        }
    }
    LL res = 0;
    for(int i=0;i<30;i++)
    {
        res += (LL)( n - c[i] ) * c[i] * ( 1 << i );//异或后在第i+1位有 (n-c[i])*c[i] 个 1
    }
    cout << 10 << endl;
}
int main()
{
    cin >> n;
    for(int i=1;i<=n;i++)
    {
        scanf("%d", &a[i]);
    }
    for(int i=1;i<=n-1;i++)
    {
        int u, v;
        scanf("%d %d", &u, &v);
        vec[u].push_back(v);
        vec[v].push_back(u);
    }
    dfs1(1); dfs2(1);
    dfs(1+n);
    solve();
}


#include <iostream>
#include <vector>
#include <cstdio>
using namespace std;
typedef long long LL;
const int NICO = 1000000 + 10;
int n, a[NICO];
vector<int> vec[NICO];
LL dp[NICO][66][2], res;

void dfs(int x, int par)
{
    for(int i=0;i<30;i++)
    {
        if((1<<i)&a[x]) dp[x][i][1] = 1;
        else dp[x][i][0] = 1;
    }
    for(int i=0;i<vec[x].size();i++)
    {
        int cur = vec[x][i];
        if(cur == par) continue;
        dfs(cur, x);
        for(int j=0;j<30;j++)
        {
            res += (dp[cur][j][0]*dp[x][j][1] + dp[cur][j][1]*dp[x][j][0]) << j;
            if((1<<j)&a[x])
            {
                dp[x][j][0] += dp[cur][j][1];
                dp[x][j][1] += dp[cur][j][0];
            } else {
                dp[x][j][1] += dp[cur][j][1];
                dp[x][j][0] += dp[cur][j][0];
            }
        }
    }
}

int main()
{
    scanf("%d", &n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d", &a[i]);
        res += a[i];
    }
    for(int i=1;i<n;i++)
    {
        int u, v;
        scanf("%d %d", &u, &v);
        vec[u].push_back(v);
        vec[v].push_back(u);
    }
    dfs(1, 0);
    cout << res << endl;
}


#include <bits/stdc++.h>

#define io ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define ll long long
#define ld long double
#define EPS (1e-9)
#define mod 1000000007
#define pii pair<ll, ll>
#define SIZE(x) (int)x.size()
#define to_string(x) static_cast< std::ostringstream & >( \
                        ( std::ostringstream() << std::dec << x ) ).str()
#define lp(i, a, n) for(int (i) = (a); (i) <= (int)(n); ++(i))
#define lpd(i, n, a) for(int (i) = (n); (i) >= (a); --(i))
#define bitcount(n) __builtin_popcountll(n)
using namespace std;
const int N = 100005;
ll n, arr[N], ans;
vector<int> adj[N];

pii dfs(int node, int p, int b) {
	pii ret;
	if (arr[node] & (1 << b))
		ret = {0, 1};
		else
		ret = {1, 0};
	for (auto cur : adj[node]) {
		if (cur != p) {
			pii curEvenOdd = dfs(cur, node, b);
			ans += curEvenOdd.first * ret.second * (1 << b)
					+ curEvenOdd.second * ret.first * (1 << b);
			if (arr[node] & (1 << b))
				ret.first += curEvenOdd.second, ret.second +=
						curEvenOdd.first;
			else
				ret.first += curEvenOdd.first, ret.second +=
						curEvenOdd.second;
		}
	}
	return ret;
}

int main() {
	scanf("%d", &n);
	lp (i, 0, n - 1)
		scanf("%d", &arr[i]), ans += arr[i];
	lp (i, 1, n - 1) {
		int x, y;
		scanf("%d %d", &x, &y);
		x--, y--;
		adj[x].push_back(y);
		adj[y].push_back(x);
	}
	lp (i, 0, 20)
		dfs(0, -1, i);
	cout << ans << endl;
	return 0;
}


#include <bits/stdc++.h>

#define io ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define ll long long
#define ld long double
#define EPS (1e-9)
#define mod 1000000007
#define pii pair<ll, ll>
#define SIZE(x) (int)x.size()
#define to_string(x) static_cast< std::ostringstream & >( \
                        ( std::ostringstream() << std::dec << x ) ).str()
#define lp(i, a, n) for(int (i) = (a); (i) <= (int)(n); ++(i))
#define lpd(i, n, a) for(int (i) = (n); (i) >= (a); --(i))
#define bitcount(n) __builtin_popcountll(n)
using namespace std;
const int N = 100005;
ll n, arr[N], ans;
ll subTreeSize[N], centroidParent[N], hs[2][20];
vector<int> adj[N], tree[N];

bool deleted[N];

void dfsCentroid(ll v, ll p) {
	subTreeSize[v] = 1;
	for(auto i : adj[v]) {
		if (i == p || deleted[i])
            continue;
		dfsCentroid(i, v);
		subTreeSize[v] += subTreeSize[i];
	}
}

ll findCentroid(ll v, ll p, ll cur) {
	while(true) {
		bool isCentroid = true;
		for(auto i : adj[v]) {
			if (i == p || deleted[i])
                continue;
			if (subTreeSize[i] * 2ll > cur) {
				p = v;
				v = i;
				isCentroid = false;
				break;
			}
		}
		if (isCentroid) {
			return v;
		}
	}
}

ll getCentroid(ll v, ll p) {
	dfsCentroid(v, p);
	return findCentroid(v, p, subTreeSize[v]);
}

ll decompose(ll v, ll p) {
	ll centroid = getCentroid(v, p);
	deleted[centroid] = true;
	for(auto node : adj[centroid]) {
		if (p == node || deleted[node])
            continue;
		ll other = decompose(node, centroid);
		centroidParent[other] = centroid;
	}
	centroidParent[centroid] = -1;
	return centroid;
}

pii dfs(int node, int p, int b) {
	pii ret;
	if (arr[node] & (1 << b))
		ret = {0, 1};
		else
		ret = {1, 0};
	for (auto cur : tree[node]) {
		if (cur != p) {
			pii curEvenOdd = dfs(cur, node, b);
			ans += curEvenOdd.first * ret.second * (1 << b)
					+ curEvenOdd.second * ret.first * (1 << b);
			if (arr[node] & (1 << b))
				ret.first += curEvenOdd.second, ret.second +=
						curEvenOdd.first;
			else
				ret.first += curEvenOdd.first, ret.second +=
						curEvenOdd.second;
		}
	}
	return ret;
}

int main() {
	scanf("%d", &n);
	lp (i, 0, n - 1)
		scanf("%d", &arr[i]), ans += arr[i];
	lp (i, 1, n - 1) {
		int x, y;
		scanf("%d %d", &x, &y);
		x--, y--;
		adj[x].push_back(y);
		adj[y].push_back(x);
	}
	ll rt = decompose(1, -1);
	lp (i, 0, n - 1) {
		if (i != rt) {
	    	tree[i].push_back(centroidParent[i]);
	        tree[centroidParent[i]].push_back(i);
	    }
	}
	lp (i, 0, 20)
		dfs(rt, -1, i);
	cout << ans << endl;
	return 0;
}


#include <bits/stdc++.h>

#define io ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define ll long long
#define ld long double
#define EPS (1e-9)
#define mod 1000000007
#define pii pair<ll, ll>
#define SIZE(x) (int)x.size()
#define to_string(x) static_cast< std::ostringstream & >( \
                        ( std::ostringstream() << std::dec << x ) ).str()
#define lp(i, a, n) for(int (i) = (a); (i) <= (int)(n); ++(i))
#define lpd(i, n, a) for(int (i) = (n); (i) >= (a); --(i))

using namespace std;
const ll OO = (1ll << 50);
const int N = 100005;

vector<ll> adj[N], sub[N];
ll subTreeSize[N], centroidParent[N];
ll n, m, a, b, curSize, rt, val[N], ans;
bool deleted[N];

void dfsCentroid(ll v, ll p) {
	subTreeSize[v] = 1;
	for(auto i : adj[v]) {
		if (i == p || deleted[i])
            continue;
		dfsCentroid(i, v);
		subTreeSize[v] += subTreeSize[i];
	}
}

ll findCentroid(ll v, ll p, ll cur) {
	while(true) {
		bool isCentroid = true;
		for(auto i : adj[v]) {
			if (i == p || deleted[i])
                continue;
			if (subTreeSize[i] * 2ll > cur) {
				p = v;
				v = i;
				isCentroid = false;
				break;
			}
		}
		if (isCentroid) {
			return v;
		}
	}
}

ll getCentroid(ll v, ll p) {
	dfsCentroid(v, p);
	return findCentroid(v, p, subTreeSize[v]);
}

ll decompose(ll v, ll p) {
	ll centroid = getCentroid(v, p);
	deleted[centroid] = true;
	for(auto node : adj[centroid]) {
		if (p == node || deleted[node])
            continue;
		ll other = decompose(node, centroid);
		centroidParent[other] = centroid;
	}
	centroidParent[centroid] = -1;
	return centroid;
}

void dfsCalc(int node, int p) {
    for (auto cur : adj[node]) {
        if(cur == p)
            continue;
        dfsCalc(cur, node);
        for (auto tmp : sub[cur])
            sub[node].push_back(tmp ^ val[node]);
    }
    lp (son1, 0, SIZE(adj[node]) - 1) {
        if (adj[node][son1] == p)
            continue;
        for (auto tmp1 : sub[adj[node][son1]]) {
            ans += tmp1 ^ val[node];
//            cout << (tmp1 ^ val[node]) << "    ::::" << endl;
            lp (son2, son1 + 1, SIZE(adj[node]) - 1) {
                if (adj[node][son2] == p)
                    continue;
                for (auto tmp2 : sub[adj[node][son2]]) {
                    ans += tmp1 ^ val[node] ^ tmp2;
                }
            }
        }
    }
    ans += val[node];
//    cout << node << " " << ans << endl;
    sub[node].push_back(val[node]);
}

int main() {
//  freopen("input.txt", "r", stdin);
//  freopen("output.txt", "w", stdout);
    scanf("%lld", &n);
    lp(i, 1, n)
        scanf("%lld", &val[i]);
    lp(i, 1, n - 1) {
        scanf("%lld %lld", &a, &b);
        adj[a].push_back(b);
        adj[b].push_back(a);
    }
    ll rt = decompose(1, -1);
    dfsCalc(rt, -1);
    /*lp (i, 1, n) {
        for (auto cur : sub[i]) {
            cout << cur << " ";
        }
        cout << endl;
    }*/
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair <int,int> PII;
typedef pair <long long,long long> PLL;

typedef unsigned long long int ULL;
typedef long long int LL;
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

#define FRE(i,a,b)      for(i = a; i <= b; i++)
#define FRL(i,a,b)      for(i = a; i < b; i++)

#define sf(n)           scanf("%d", &n)
#define sff(a,b)        scanf("%d %d", &a, &b)
#define sfff(a,b,c)     scanf("%d %d %d", &a, &b, &c)

#define si(a)           scanf("%d",&a)
#define sii(a,b)        scanf("%d %d",&a,&b)
#define siii(a,b,c)     scanf("%d %d %d",&a,&b,&c)

#define sl(a)           scanf("%lld",&a)
#define sll(a,b)        scanf("%lld %lld",&a,&b)
#define slll(a,b,c)     scanf("%lld %lld %lld",&a,&b,&c)

#define xx              first
#define yy              second
#define pb              push_back
#define mp              make_pair
#define all(v)          v.begin(),v.end()
#define un(x)           x.erase(unique(all(x)), x.end())
#define D(x)            cerr << #x " = " << x << '\n'
#define DBG             cerr << "Hi!" << '\n'

#define mem(t, v)       memset ((t) , v, sizeof(t))
#define CLR(a)          memset(a,0,sizeof(a))
#define SET(a)          memset(a,-1,sizeof(a))

#define eps             1e-9
#define PI              acos(-1.0)

int setBit(int n,int pos){ return n = n | (1 << pos); }
int resetBit(int n,int pos){ return n = n & ~(1 << pos); }
bool checkBit(int n,int pos){ return (bool)(n & (1 << pos)); }

//int fx[] = {+0, +0, +1, -1, -1, +1, -1, +1};
//int fy[] = {-1, +1, +0, +0, +1, +1, -1, -1}; //Four & Eight Direction

/******************************************************************************************/

const int MAX = 100010;

int ara[MAX];

int root = -1;

vector <int> ed[MAX] , G[MAX];

bool isCentroid[MAX];
int sub[MAX];
int cpar[MAX];
int clevel[MAX];
int dis[20][MAX]; // dis[i][j] = distance of node j from the root of the i'th level of decomposition

void calcSubTree(int s,int par) {
    int i,x;
    sub[s] = 1;
    for(i=0; i<ed[s].size(); i++) {
        x = ed[s][i];
        if(x!=par && !isCentroid[x]) {
            calcSubTree(x,s);
            sub[s] += sub[x];
        }
    }
}

int nn;// number of nodes in the part

int getCentroid(int s,int par) {
    int i,x;
    for(i=0; i<ed[s].size(); i++) {
        x = ed[s][i];
        if(!isCentroid[x] && x!=par && sub[x]>(nn/2)) return getCentroid(x,s);
    }
    return s;
}

void setDis(int s, int from, int par, int l) {
    dis[from][s] = l ^ ara[s];
    int i,x;
    for(i=0; i<ed[s].size(); i++) {
        x = ed[s][i];
        if(x!=par && !isCentroid[x]) {
            setDis(x, from, s, dis[from][s]);
        }
    }
}

void decompose(int s,int p,int l) {
    calcSubTree(s,p);
    nn = sub[s];
    int c = getCentroid(s,p);

    setDis(c,l,p,0);

    isCentroid[c] = true;

    if(root == -1) root = c;

    if(p!=-1) G[p].pb(c);

    cpar[c] = p;
    clevel[c] = l;

    int i,x;
    for(i=0; i<ed[c].size(); i++) {
        x = ed[c][i];
        if(!isCentroid[x]) decompose(x,c,l+1);
    }
}

const int D = 19;
long long ans = 0;

vector <int> nodes;

int cum[22][2];

inline void add(int s,int l){
    for(int j=0;j<D;j++){
        if(checkBit(dis[l][s],j)) cum[j][1]++;
        else cum[j][0]++;
    }
    for(int i=0;i<G[s].size();i++){
        int x = G[s][i];
        add(x,l);
    }
}

inline void rmv(int s,int l){

    nodes.pb(s);

    for(int j=0;j<D;j++){
        if(checkBit(dis[l][s],j)) cum[j][1]--;
        else cum[j][0]--;
    }
    for(int i=0;i<G[s].size();i++){
        int x = G[s][i];
        rmv(x,l);
    }
}

inline void calculate(int s)
{
    CLR(cum);
    add(s,clevel[s]);

    for(int i=0;i<G[s].size();i++){
        int x = G[s][i];
        nodes.clear();
        rmv(x,clevel[s]);

        for(int p=0;p<nodes.size();p++) {
            x = nodes[p];
            x = dis[clevel[s]][x] ^ ara[s];
            ll lala = ans;
            for(int j=0;j<D;j++){
                if(checkBit(x,j)) ans += (long long)cum[j][0] * (1LL<<j);
                else ans += (long long)cum[j][1] * (1LL<<j);
            }
        }

    }
}

int main()
{
//    freopen("in.txt","r",stdin);
//    freopen("out.txt","w",stdout);

//    ios_base::sync_with_stdio(false);
//    cin.tie(NULL);

    int n,i,u,v,j;
    si(n);

    for(i=1;i<=n;i++){
        si(ara[i]);
        ans += (long long)ara[i];
    }

    for(i=1;i<n;i++){
        sii(u,v);
        ed[u].pb(v);
        ed[v].pb(u);
    }

    decompose(1,-1,0);

    for(i=1;i<=n;i++){
        calculate(i);
    }

    printf("%lld\n",ans);

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair <int,int> PII;
typedef pair <long long,long long> PLL;

typedef unsigned long long int ULL;
typedef long long int LL;
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

#define FRE(i,a,b)      for(i = a; i <= b; i++)
#define FRL(i,a,b)      for(i = a; i < b; i++)

#define sf(n)           scanf("%d", &n)
#define sff(a,b)        scanf("%d %d", &a, &b)
#define sfff(a,b,c)     scanf("%d %d %d", &a, &b, &c)

#define si(a)           scanf("%d",&a)
#define sii(a,b)        scanf("%d %d",&a,&b)
#define siii(a,b,c)     scanf("%d %d %d",&a,&b,&c)

#define sl(a)           scanf("%lld",&a)
#define sll(a,b)        scanf("%lld %lld",&a,&b)
#define slll(a,b,c)     scanf("%lld %lld %lld",&a,&b,&c)

#define xx              first
#define yy              second
#define pb              push_back
#define mp              make_pair
#define all(v)          v.begin(),v.end()
#define un(x)           x.erase(unique(all(x)), x.end())
#define D(x)            cerr << #x " = " << x << '\n'
#define DBG             cerr << "Hi!" << '\n'

#define mem(t, v)       memset ((t) , v, sizeof(t))
#define CLR(a)          memset(a,0,sizeof(a))
#define SET(a)          memset(a,-1,sizeof(a))

#define eps             1e-9
#define PI              acos(-1.0)

int setBit(int n,int pos){ return n = n | (1 << pos); }
int resetBit(int n,int pos){ return n = n & ~(1 << pos); }
bool checkBit(int n,int pos){ return (bool)(n & (1 << pos)); }

//int fx[] = {+0, +0, +1, -1, -1, +1, -1, +1};
//int fy[] = {-1, +1, +0, +0, +1, +1, -1, -1}; //Four & Eight Direction

/******************************************************************************************/

const int MAX = 100010;

int ara[MAX];

int root = -1;

vector <int> ed[MAX] , G[MAX];

bool isCentroid[MAX];
int sub[MAX];
int cpar[MAX];
int clevel[MAX];
int dis[20][MAX]; // dis[i][j] = distance of node j from the root of the i'th level of decomposition

void calcSubTree(int s,int par) {
    int i,x;
    sub[s] = 1;
    for(i=0; i<ed[s].size(); i++) {
        x = ed[s][i];
        if(x!=par && !isCentroid[x]) {
            calcSubTree(x,s);
            sub[s] += sub[x];
        }
    }
}

int nn;// number of nodes in the part

int getCentroid(int s,int par) {
    int i,x;
    for(i=0; i<ed[s].size(); i++) {
        x = ed[s][i];
        if(!isCentroid[x] && x!=par && sub[x]>(nn/2)) return getCentroid(x,s);
    }
    return s;
}

void setDis(int s, int from, int par, int l) {
    dis[from][s] = l ^ ara[s];
    int i,x;
    for(i=0; i<ed[s].size(); i++) {
        x = ed[s][i];
        if(x!=par && !isCentroid[x]) {
            setDis(x, from, s, dis[from][s]);
        }
    }
}

void decompose(int s,int p,int l) {
    calcSubTree(s,p);
    nn = sub[s];
    int c = getCentroid(s,p);

    setDis(c,l,p,0);

    isCentroid[c] = true;

    if(root == -1) root = c;

    if(p!=-1) G[p].pb(c);

    cpar[c] = p;
    clevel[c] = l;

    int i,x;
    for(i=0; i<ed[c].size(); i++) {
        x = ed[c][i];
        if(!isCentroid[x]) decompose(x,c,l+1);
    }
}

const int D = 19;
long long ans = 0;

vector <int> nodes;

int cum[22][2];

void add(int s,int l){
    for(int j=0;j<D;j++){
        if(checkBit(dis[l][s],j)) cum[j][1]++;
        else cum[j][0]++;
    }
    for(int i=0;i<G[s].size();i++){
        int x = G[s][i];
        add(x,l);
    }
}

void rmv(int s,int l){

    nodes.pb(s);

    for(int j=0;j<D;j++){
        if(checkBit(dis[l][s],j)) cum[j][1]--;
        else cum[j][0]--;
    }
    for(int i=0;i<G[s].size();i++){
        int x = G[s][i];
        rmv(x,l);
    }
}

void calculate(int s)
{
    CLR(cum);
    add(s,clevel[s]);

    for(int i=0;i<G[s].size();i++){
        int x = G[s][i];
        nodes.clear();
        rmv(x,clevel[s]);

//        if(x==3)
//        for(int j=0;j<2;j++){
//            cout << cum[j][0] << " " << cum[j][1] << endl;
//        }

        for(int p=0;p<nodes.size();p++) {
            x = nodes[p];
            x = dis[clevel[s]][x] ^ ara[s];
            ll lala = ans;
            for(int j=0;j<D;j++){
                if(checkBit(x,j)) ans += cum[j][0] * (1LL<<j);
                else ans += cum[j][1] * (1LL<<j);
            }
        }

    }
}

int main()
{
//    freopen("in.txt","r",stdin);
//    freopen("out.txt","w",stdout);

//    ios_base::sync_with_stdio(false);
//    cin.tie(NULL);

    int n,i,u,v,j;
    si(n);

    for(i=1;i<=n;i++){
        si(ara[i]);
        ans += (long long)ara[i];
    }

    for(i=1;i<n;i++){
        sii(u,v);
        ed[u].pb(v);
        ed[v].pb(u);
    }

    decompose(1,-1,0);

    for(i=1;i<=n;i++){
        calculate(i);
    }

    printf("%lld\n",ans);

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair <int,int> PII;
typedef pair <long long,long long> PLL;

typedef unsigned long long int ULL;
typedef long long int LL;
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

#define FRE(i,a,b)      for(i = a; i <= b; i++)
#define FRL(i,a,b)      for(i = a; i < b; i++)

#define sf(n)           scanf("%d", &n)
#define sff(a,b)        scanf("%d %d", &a, &b)
#define sfff(a,b,c)     scanf("%d %d %d", &a, &b, &c)

#define si(a)           scanf("%d",&a)
#define sii(a,b)        scanf("%d %d",&a,&b)
#define siii(a,b,c)     scanf("%d %d %d",&a,&b,&c)

#define sl(a)           scanf("%lld",&a)
#define sll(a,b)        scanf("%lld %lld",&a,&b)
#define slll(a,b,c)     scanf("%lld %lld %lld",&a,&b,&c)

#define xx              first
#define yy              second
#define pb              push_back
#define mp              make_pair
#define all(v)          v.begin(),v.end()
#define un(x)           x.erase(unique(all(x)), x.end())
#define D(x)            cerr << #x " = " << x << '\n'
#define DBG             cerr << "Hi!" << '\n'

#define mem(t, v)       memset ((t) , v, sizeof(t))
#define CLR(a)          memset(a,0,sizeof(a))
#define SET(a)          memset(a,-1,sizeof(a))

#define eps             1e-9
#define PI              acos(-1.0)

ll setBit(ll n,ll pos){ return n = n | (1LL << pos); }
ll resetBit(ll n,ll pos){ return n = n & ~(1LL << pos); }
bool checkBit(ll n,ll pos){ return (bool)(n & (1LL << pos)); }

//int fx[] = {+0, +0, +1, -1, -1, +1, -1, +1};
//int fy[] = {-1, +1, +0, +0, +1, +1, -1, -1}; //Four & Eight Direction

/******************************************************************************************/

const int MAX = 100010;

int ara[MAX];

int root = -1;

vector <int> ed[MAX] , G[MAX];

bool isCentroid[MAX];
int sub[MAX];
int cpar[MAX];
int clevel[MAX];
int dis[20][MAX]; // dis[i][j] = distance of node j from the root of the i'th level of decomposition

void calcSubTree(int s,int par) {
    int i,x;
    sub[s] = 1;
    for(i=0; i<ed[s].size(); i++) {
        x = ed[s][i];
        if(x!=par && !isCentroid[x]) {
            calcSubTree(x,s);
            sub[s] += sub[x];
        }
    }
}

int nn;// number of nodes in the part

int getCentroid(int s,int par) {
    int i,x;
    for(i=0; i<ed[s].size(); i++) {
        x = ed[s][i];
        if(!isCentroid[x] && x!=par && sub[x]>(nn/2)) return getCentroid(x,s);
    }
    return s;
}

void setDis(int s, int from, int par, int l) {
    dis[from][s] = l ^ ara[s];
    int i,x;
    for(i=0; i<ed[s].size(); i++) {
        x = ed[s][i];
        if(x!=par && !isCentroid[x]) {
            setDis(x, from, s, dis[from][s]);
        }
    }
}

void decompose(int s,int p,int l) {
    calcSubTree(s,p);
    nn = sub[s];
    int c = getCentroid(s,p);

    setDis(c,l,p,0);

    isCentroid[c] = true;

    if(root == -1) root = c;

    if(p!=-1) G[p].pb(c);

    cpar[c] = p;
    clevel[c] = l;

    int i,x;
    for(i=0; i<ed[c].size(); i++) {
        x = ed[c][i];
        if(!isCentroid[x]) decompose(x,c,l+1);
    }
}

const int D = 20;
long long ans = 0;

vector <int> nodes;

int cum[25][2];

inline void add(int s,int l){
    for(int j=0;j<D;j++){
        if(checkBit(dis[l][s],j)) cum[j][1]++;
        else cum[j][0]++;
    }
    for(int i=0;i<G[s].size();i++){
        int x = G[s][i];
        add(x,l);
    }
}

inline void rmv(int s,int l){

    nodes.pb(s);

    for(int j=0;j<D;j++){
        if(checkBit(dis[l][s],j)) cum[j][1]--;
        else cum[j][0]--;
    }
    for(int i=0;i<G[s].size();i++){
        int x = G[s][i];
        rmv(x,l);
    }
}

inline void calculate(int s)
{
    CLR(cum);
    add(s,clevel[s]);

    for(int i=0;i<G[s].size();i++){
        int x = G[s][i];
        nodes.clear();
        rmv(x,clevel[s]);

        for(int p=0;p<nodes.size();p++) {
            x = nodes[p];
            x = dis[clevel[s]][x] ^ ara[s];
            ll lala = ans;
            for(int j=0;j<D;j++){
                if(checkBit(x,j)) ans += (long long)cum[j][0] * (1LL<<j);
                else ans += (long long)cum[j][1] * (1LL<<j);
            }
        }

    }
}

int main()
{
//    freopen("in.txt","r",stdin);
//    freopen("out.txt","w",stdout);

//    ios_base::sync_with_stdio(false);
//    cin.tie(NULL);

    int n,i,u,v,j;
    si(n);

    for(i=1;i<=n;i++){
        si(ara[i]);
        ans += (long long)ara[i];
    }

    for(i=1;i<n;i++){
        sii(u,v);
        ed[u].pb(v);
        ed[v].pb(u);
    }

    decompose(1,-1,0);

    for(i=1;i<=n;i++){
        calculate(i);
    }

    printf("%lld\n",ans);

    return 0;
}


#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
//using namespace __gnu_pbds;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair <int,int> PII;
typedef pair <long long,long long> PLL;

#define si(a)           scanf("%d",&a)
#define sii(a,b)        scanf("%d %d",&a,&b)
#define siii(a,b,c)     scanf("%d %d %d",&a,&b,&c)

#define sl(a)           scanf("%lld",&a)
#define sll(a,b)        scanf("%lld %lld",&a,&b)
#define slll(a,b,c)     scanf("%lld %lld %lld",&a,&b,&c)

#define un(x)           x.erase(unique(all(x)), x.end())
#define xx              first
#define yy              second
#define pb              push_back
#define mp              make_pair
#define all(v)          v.begin(),v.end()
#define D(x)            cerr << #x " = " << x << '\n'
#define DBG             cerr << "Hi!" << '\n'

#define CLR(a)          memset(a,0,sizeof(a))
#define SET(a)          memset(a,-1,sizeof(a))

#define PI              acos(-1.0)

int setBit(int n,int pos) { return n = n | (1 << pos); }
int resetBit(int n,int pos) { return n = n & ~(1 << pos); }
bool checkBit(ll n,ll pos) { return (bool)(n & (1LL << pos)); }

//int fx[] = {+0, +0, +1, -1, -1, +1, -1, +1};
//int fy[] = {-1, +1, +0, +0, +1, +1, -1, -1}; //Four & Eight Direction

/******************************************************************************************/

const int MAX = 100010;
const int INF = 2000000000;

int ara[MAX];
vector <int> ed[MAX];
bool isCentroid[MAX];
int sub[MAX], cpar[MAX], clevel[MAX], dis[20][MAX];
ll ans = 0;

void calcSubTree(int s,int p) {
    sub[s] = 1;
    for( auto x : ed[s] ) {
        if(x!=p && !isCentroid[x]) {
            calcSubTree(x,s);
            sub[s] += sub[x];
        }
    }
}

int nn;// number of nodes in the part

int getCentroid(int s,int p) {
    for( auto x : ed[s] ) {
        if(!isCentroid[x] && x!=p && sub[x]>(nn/2))
            return getCentroid(x,s);
    }
    return s;
}

int D = 20;

ll cum[22][2];
ll tmp[22][2];

void add(int x) {
    for(int i=0;i<D;i++) {
        cum[i][checkBit(x,i)]++;
    }
}

void rmv(int x) {
    for(int i=0;i<D;i++) {
        cum[i][checkBit(x,i)]--;
        tmp[i][checkBit(x,i)]++;
    }
}

void setDis(int s, int from, int p) {
    dis[from][s] = dis[from][p] ^ ara[s];
    add(dis[from][s]);
    for( auto x : ed[s] ) {
        if(x!=p && !isCentroid[x]) {
            setDis(x, from, s);
        }
    }
}

void Remove(int s, int from, int p) {
    rmv(dis[from][s]);
    for( auto x : ed[s] ) {
        if(x!=p && !isCentroid[x]) {
            Remove(x, from, s);
        }
    }
}

//complexity --> O(nlog(n))
void decompose(int s,int p,int l) {

    CLR(cum);

    calcSubTree(s,p);
    nn = sub[s];
    int c = getCentroid(s,p);
    setDis(c,l,0);

    isCentroid[c] = true;
    cpar[c] = p;
    clevel[c] = l;

    int cur = ara[c];
    for( auto x : ed[c] ) {
        if(!isCentroid[x]) {
            Remove(x,l,c);
            for(int i=0;i<D;i++) {
                if(checkBit(cur,i)) {
                    ans += tmp[i][1] * cum[i][1]*(1LL<<i);
                    ans += tmp[i][0] * cum[i][0]*(1LL<<i);
                }
                else {
                    ans += tmp[i][1] * cum[i][0]*(1LL<<i);
                    ans += tmp[i][0] * cum[i][1]*(1LL<<i);
                }
            }
            CLR(tmp);
        }
    }

    for( auto x : ed[c] ) {
        if(!isCentroid[x]) decompose(x,c,l+1);
    }
}

int main() {
//    freopen("in.txt","r",stdin);
//    freopen("out.txt","w",stdout);

    int n,u,v;
    si(n);
    for(int i=1;i<=n;i++) {
        si(ara[i]);
        ans += ara[i];
    }

    for(int i=1;i<n;i++) {
        sii(u,v);
        ed[u].pb(v);
        ed[v].pb(u);
    }

    decompose(1,0,0);

    printf("%lld\n",ans);

    return 0;
}


# include <bits/stdc++.h>
# define pb push_back
using namespace std;
typedef long long LL;
const int maxn = 1e5+3;
const LL mod = 1e9+7;
vector<int>v[maxn];
int a[maxn];
LL dp[maxn][35][2][2]={0}, ans = 0;
void dfs(int cur, int pre)
{

    for(auto to : v[cur])
    {
        if(to == pre) continue;
        for(int i=0,j=a[to];i<32;++i,j>>=1)
        {
            int b = j&1;
            dp[to][i][b^1][1] += dp[cur][i][1][0];
            dp[to][i][b^0][1] += dp[cur][i][0][0];
        }
        dfs(to, cur);
        for(int i=0,j=a[cur];i<32;++i,j>>=1)
        {
            int b = j&1;
            dp[cur][i][b^1][0] += dp[to][i][1][0];
            dp[cur][i][b^0][0] += dp[to][i][0][0];
        }
    }
    for(int i=0,j=a[cur]; i<32; ++i,j>>=1)
        ++dp[cur][i][j&1][0];
    for(int i=0; i<32; ++i)
    {
        ans = (ans+(1<<i)*dp[cur][i][1][0])%mod;
        ans = (ans+(1<<i)*dp[cur][i][1][1])%mod;
    }
}

int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1; i<=n; ++i) scanf("%d",&a[i]);
    for(int i=1; i<n; ++i)
    {
        int s, e;
        scanf("%d%d",&s,&e);
        v[s].pb(e), v[e].pb(s);
    }
    dfs(1, 0);
    printf("%lld\n",ans);
    return 0;
}


# include <bits/stdc++.h>
# define pb push_back
using namespace std;
typedef long long LL;
const int maxn = 1e5+3;
const LL mod = 1e9+7;
vector<int>v[maxn];
int a[maxn];
LL dp[maxn][35][2]={0}, ans = 0;
void dfs(int cur, int pre)
{
    for(int i=0; i<20; ++i)
        ++dp[cur][i][((1LL<<i)&a[cur])?1:0];
    for(auto to : v[cur])
    {
        if(to == pre) continue;
        dfs(to, cur);
        for(int i=0; i<20; ++i)
        {
            ans += (1LL<<i)*(dp[cur][i][0]*dp[to][i][1] + dp[cur][i][1]*dp[to][i][0]);
            int b = (1LL<<i&a[cur])?1:0;
            dp[cur][i][1^b] += dp[to][i][1];
            dp[cur][i][0^b] += dp[to][i][0];
        }
        //printf("%d %lld---\n",cur, ans);
    }
}
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1; i<=n; ++i) scanf("%d",&a[i]), ans += a[i];
    for(int i=1; i<n; ++i)
    {
        int s, e;
        scanf("%d%d",&s,&e);
        v[s].pb(e), v[e].pb(s);
    }
    dfs(1, 0);
    printf("%lld\n",ans);
    return 0;
}


#include <iostream>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <map>
#include <set>
#include <bitset>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <string>
#include <sstream>
#include <time.h>
#define x first
#define y second
#define pb push_back
#define mp make_pair
#define lson l,m,rt*2
#define rson m+1,r,rt*2+1
#define mt(A,B) memset(A,B,sizeof(A))
#define mod 1000000007
using namespace std;
typedef long long LL;
const double PI = acos(-1);
const int N=1e5+10;
const int inf = 0x3f3f3f3f;
const LL INF=0x3f3f3f3f3f3f3f3fLL;
LL dp[N][2],a[N];
LL ans=0;
vector<int> G[N];
LL dfs(int x,int fa,int bit)
{
    LL q=0;
    int b=(a[x]>>bit)&1;
    dp[x][b]=1;
    dp[x][b^1]=0;
    for(int i=0;i<G[x].size();i++)
    {
        int v=G[x][i];
        if(v==fa)continue;
        dfs(v,x,bit);
        q+=dp[x][0]*dp[v][1]+dp[x][1]*dp[v][0];
        dp[x][b^0]+=dp[v][0];
        dp[x][b^1]+=dp[v][1];
    }
    ans+=(q<<bit);
}
int main()
{
#ifdef Local
    freopen("data.txt","r",stdin);
#endif
    //ios::sync_with_stdio(false);
    //cin.tie(0);
    int n,u,v;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        ans+=a[i];
    }
    for(int i=0;i<n-1;i++)
    {
        cin>>u>>v;
        G[u].pb(v);
        G[v].pb(u);
    }
    for(int i=0;i<30;i++)
    {
        dfs(1,0,i);
    }
    cout<<ans<<endl;
#ifdef Local
    cerr << "time: " << (LL) clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
#endif
}


#include <iostream>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <map>
#include <set>
#include <bitset>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <string>
#include <sstream>
#include <time.h>
#define x first
#define y second
#define pb push_back
#define mp make_pair
#define lson l,m,rt*2
#define rson m+1,r,rt*2+1
#define mt(A,B) memset(A,B,sizeof(A))
#define mod 1000000007
using namespace std;
typedef long long LL;
const double PI = acos(-1);
const int N=1e5+10;
const int inf = 0x3f3f3f3f;
const LL INF=0x3f3f3f3f3f3f3f3fLL;
int dp[N][2],a[N];
LL ans=0;
vector<int> G[N];
LL dfs(int x,int fa,int bit)
{
    int q=0;
    int b=(a[x]>>bit)&1;
    dp[x][b]=1;
    dp[x][b^1]=0;
    for(int i=0;i<G[x].size();i++)
    {
        int v=G[x][i];
        if(v==fa)continue;
        dfs(v,x,bit);
        q+=dp[x][0]*dp[v][1]+dp[x][1]*dp[v][0];
        dp[x][b^0]+=dp[v][0];
        dp[x][b^1]+=dp[v][1];
    }
    ans+=(q<<bit);
}
int main()
{
#ifdef Local
    freopen("data.txt","r",stdin);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n,u,v;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        ans+=a[i];
    }
    for(int i=0;i<n-1;i++)
    {
        cin>>u>>v;
        G[u].pb(v);
        G[v].pb(u);
    }
    for(int i=0;i<30;i++)
    {
        dfs(1,0,i);
    }
    cout<<ans<<endl;
#ifdef Local
    cerr << "time: " << (LL) clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
#endif
}


#include <iostream>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <map>
#include <set>
#include <bitset>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <string>
#include <sstream>
#include <time.h>
#define x first
#define y second
#define pb push_back
#define mp make_pair
#define lson l,m,rt*2
#define rson m+1,r,rt*2+1
#define mt(A,B) memset(A,B,sizeof(A))
#define mod 1000000007
using namespace std;
typedef long long LL;
const double PI = acos(-1);
const int N=1e5+10;
const int inf = 0x3f3f3f3f;
const LL INF=0x3f3f3f3f3f3f3f3fLL;
LL dp[N][2],a[N];
LL ans=0;
vector<int> G[N];
LL dfs(int x,int fa,int bit)
{
    LL q=0;
    int b=(a[x]>>bit)&1;
    dp[x][b]=1;
    dp[x][b^1]=0;
    for(int i=0;i<G[x].size();i++)
    {
        int v=G[x][i];
        if(v==fa)continue;
        dfs(v,x,bit);
        q+=dp[x][0]*dp[v][1]+dp[x][1]*dp[v][0];
        dp[x][b^0]+=dp[v][0];
        dp[x][b^1]+=dp[v][1];
    }
    ans+=(q<<bit);
}
int main()
{
#ifdef Local
    freopen("data.txt","r",stdin);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n,u,v;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        ans+=a[i];
    }
    for(int i=0;i<n-1;i++)
    {
        cin>>u>>v;
        G[u].pb(v);
        G[v].pb(u);
    }
    for(int i=0;i<30;i++)
    {
        dfs(1,0,i);
    }
    cout<<ans<<endl;
#ifdef Local
    cerr << "time: " << (LL) clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
#endif
}


#include <iostream>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <map>
#include <set>
#include <bitset>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <string>
#include <sstream>
#include <time.h>
#define x first
#define y second
#define pb push_back
#define mp make_pair
#define lson l,m,rt*2
#define rson m+1,r,rt*2+1
#define mt(A,B) memset(A,B,sizeof(A))
#define mod 1000000007
using namespace std;
typedef long long LL;
const double PI = acos(-1);
const int N=1e5+10;
const int inf = 0x3f3f3f3f;
const LL INF=0x3f3f3f3f3f3f3f3fLL;
LL dp[N][2],a[N];
LL ans=0;
vector<int> G[N];
LL dfs(int x,int fa,int bit)
{
    int q=0;
    int b=(a[x]>>bit)&1;
    dp[x][b]=1;
    dp[x][b^1]=0;
    for(int i=0;i<G[x].size();i++)
    {
        int v=G[x][i];
        if(v==fa)continue;
        dfs(v,x,bit);
        q+=dp[x][0]*dp[v][1]+dp[x][1]*dp[v][0];
        dp[x][b^0]+=dp[v][0];
        dp[x][b^1]+=dp[v][1];
    }
    ans+=(q<<bit);
}
int main()
{
#ifdef Local
    freopen("data.txt","r",stdin);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n,u,v;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        ans+=a[i];
    }
    for(int i=0;i<n-1;i++)
    {
        cin>>u>>v;
        G[u].pb(v);
        G[v].pb(u);
    }
    for(int i=0;i<30;i++)
    {
        dfs(1,0,i);
    }
    cout<<ans<<endl;
#ifdef Local
    cerr << "time: " << (LL) clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
#endif
}


#include <iostream>
#include <algorithm>
#include <math.h>
#include <vector>
#include <map>
#include <cstdio>
using namespace std;
void DFS (int , int );

typedef long long ll;
const int N = 100*1000 + 10;
vector <int> v[N];
int n, a[N][30];
ll DP[N][30][2], ans[30];

void DFS (int node, int pnode)
{
    ll s[25][2];

    memset (s, 0, sizeof(s));

    for (int i = 0; i < 25; i++)
        DP[node][i][a[node][i]] = 1;

    for (int i = 0; i < v[node].size(); i++)
    {
        if (v[node][i] != pnode)
        {
            DFS (v[node][i], node);

            for (int j = 0; j < 25; j++)
            {
                for (int x = 0; x < 2; x++)
                {
                    DP[node][j][x] += DP[v[node][i]][j][x^a[node][j]];
                    s[j][x] += DP[v[node][i]][j][x];
                }
            }
        }
    }

    for (int j = 0; j < 25; j++)
    {
        ll x0 = 0;
        ll x1 = 0;

        for (int i = 0; i < v[node].size(); i++)
        {
            if (v[node][i] != pnode)
            {
                x0 += (s[j][1] - DP[v[node][i]][j][1]) * DP[v[node][i]][j][1] + (s[j][0] - DP[v[node][i]][j][0]) * DP[v[node][i]][j][0];
                x1 += (s[j][1] - DP[v[node][i]][j][1]) * DP[v[node][i]][j][0] + (s[j][0] - DP[v[node][i]][j][0]) * DP[v[node][i]][j][1];
            }
        }

        if (a[node][j] == 1)
            ans[j] += x0/2;
        else
            ans[j] += x1/2;
    }

    for (int j = 0; j < 25; j++)
        ans[j] += DP[node][j][1];
}

int main ()
{
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        int k;
        cin >> k;

        for (int x = 0; x < 25; x++)
            a[i][x] = (bool)(k & (1 << x));
    }

    for (int i = 1; i < n; i++)
    {
        int k, m;
        cin >> k >> m;

        v[k].push_back(m);
        v[m].push_back(k);
    }

    DFS (1, 0);
    ll res = 0;

    for (int i = 0; i < 25; i++)
        res += (ans[i] * (1LL << i));

    cout << res << '\n';
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

/*
OK!!! CF Why are you doing this to me! The errors you find??
Terminal doesn't find those man. Chillll This problem took me so long to solve.
Happy Valentine's day BTW.
ALSO HAPPY BTS COMEBACK DAY.
*/

typedef long long ll;
const int N = 100*1000 + 10;
vector <int> v[N];
int n, a[N][30];
ll DP[N][30][2], ans[30];

void DFS (int node, int pnode)
{
    ll s[25][2];

    memset (s, 0, sizeof(s));

    for (int i = 0; i < 25; i++)
        DP[node][i][a[node][i]] = 1;

    for (int i = 0; i < v[node].size(); i++)
    {
        if (v[node][i] != pnode)
        {
            DFS (v[node][i], node);

            for (int j = 0; j < 25; j++)
            {
                for (int x = 0; x < 2; x++)
                {
                    DP[node][j][x] += DP[v[node][i]][j][x^a[node][j]];
                    s[j][x] += DP[v[node][i]][j][x];
                }
            }
        }
    }

    for (int j = 0; j < 25; j++)
    {
        ll x0 = 0;
        ll x1 = 0;

        for (int i = 0; i < v[node].size(); i++)
        {
            if (v[node][i] != pnode)
            {
                x0 += (s[j][1] - DP[v[node][i]][j][1]) * DP[v[node][i]][j][1] + (s[j][0] - DP[v[node][i]][j][0]) * DP[v[node][i]][j][0];
                x1 += (s[j][1] - DP[v[node][i]][j][1]) * DP[v[node][i]][j][0] + (s[j][0] - DP[v[node][i]][j][0]) * DP[v[node][i]][j][1];
            }
        }

        if (a[node][j] == 1)
            ans[j] += x0/2;
        else
            ans[j] += x1/2;
    }

    for (int j = 0; j < 25; j++)
        ans[j] += DP[node][j][1];
}

int main ()
{
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        int k;
        cin >> k;

        for (int x = 0; x < 25; x++)
            a[i][x] = (bool)(k & (1 << x));
    }

    for (int i = 1; i < n; i++)
    {
        int k, m;
        cin >> k >> m;

        v[k].push_back(m);
        v[m].push_back(k);
    }

    DFS (1, 0);
    ll res = 0;

    for (int i = 0; i < 25; i++)
        res += (ans[i] * (1LL << i));

    cout << res << '\n';
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define MP make_pair
#define PB push_back
typedef long long LL;
typedef pair<int,int> PII;
const double eps=1e-8;
const double pi=acos(-1.0);
const int K=1e5+7;
const int mod=1e9+7;

int n,v[K],dp[K][2];
LL ans;
vector<int>mp[K];

void dfs(int x,int f,int k)
{
    int t=v[x]>>k&1,sum=0;
    dp[x][t]=1,dp[x][t^1]=0;
    for(int i=0;i<mp[x].size();i++)
    if(mp[x][i]!=f)
    {
        int v=mp[x][i];
        dfs(v,x,k);
        sum+=dp[x][0]*dp[v][1]+dp[x][1]*dp[v][0];
        dp[x][t^0]+=dp[v][0];
        dp[x][t^1]+=dp[v][1];
    }
    ans+=(sum<<k);
}
int main(void)
{
    cin>>n;
    for(int i=1;i<=n;i++)
        scanf("%d",v+i),ans+=v[i];
    for(int i=1,u,v;i<n;i++)
        scanf("%d%d",&u,&v),mp[u].PB(v),mp[v].PB(u);
    for(int i=0;i<=20;i++)
        dfs(1,0,i);
    printf("%lld\n",ans);
    return 0;
}


#include <iostream>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <map>
#include <set>
#include <bitset>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <string>
#include <sstream>
#include <time.h>
#define x first
#define y second
#define pb push_back
#define mp make_pair
#define lson l,m,rt*2
#define rson m+1,r,rt*2+1
#define mt(A,B) memset(A,B,sizeof(A))
#define mod 1000000007
using namespace std;
typedef long long LL;
const double PI = acos(-1);
const int N=1e5+10;
const int inf = 0x3f3f3f3f;
const LL INF=0x3f3f3f3f3f3f3f3fLL;
LL dp[N][2],a[N];
LL ans=0;
vector<int> G[N];
LL dfs(int x,int fa,int bit)
{
    LL q=0;
    int b=(a[x]>>bit)&1;//取得当前的a[x]在第bit位是0还是1，
    dp[x][b]=1;//初始化
    dp[x][b^1]=0;//初始化
    for(int i=0;i<G[x].size();i++)
    {
        int v=G[x][i];
        if(v==fa)continue;
        dfs(v,x,bit);
        q+=dp[x][0]*dp[v][1]+dp[x][1]*dp[v][0];//统计子节点到x的路径上异或和，只需算1^0与0^1即可。
        dp[x][b^0]+=dp[v][0];//更新异或操作后的状态值。
        dp[x][b^1]+=dp[v][1];//更新异或操作后的状态值。
    }
    ans+=(q<<bit);//更新答案。
}
int main()
{
#ifdef Local
    freopen("data.txt","r",stdin);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n,u,v;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        ans+=a[i];
    }
    for(int i=0;i<n-1;i++)
    {
        cin>>u>>v;
        G[u].pb(v);
        G[v].pb(u);
    }
    for(int i=0;i<=20;i++)//由于权值最大为1e6，所以其实枚举到20位就足够了。
    {
        dfs(1,0,i);
    }
    cout<<ans<<endl;
#ifdef Local
    cerr << "time: " << (LL) clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
#endif
}


#include <bits/stdc++.h>

using namespace std;

#define MP make_pair
#define PB push_back
typedef long long LL;
typedef pair<int,int> PII;
const double eps=1e-8;
const double pi=acos(-1.0);
const int K=1e5+7;
const int mod=1e9+7;

int n,v[K],dp[K][2];
LL ans;
vector<int>mp[K];

void dfs(int x,int f,int k)
{
    int t=v[x]>>k&1;
    LL sum=0;
    dp[x][t]=1,dp[x][t^1]=0;
    for(int i=0;i<mp[x].size();i++)
    if(mp[x][i]!=f)
    {
        int v=mp[x][i];
        dfs(v,x,k);
        sum+=dp[x][0]*dp[v][1]+dp[x][1]*dp[v][0];
        dp[x][0]+=dp[v][0];
        dp[x][1]+=dp[v][1];
    }
    ans+=(sum<<k);
}
int main(void)
{
    cin>>n;
    for(int i=1;i<=n;i++)
        scanf("%d",v+i),ans+=v[i];
    for(int i=1,u,v;i<n;i++)
        scanf("%d%d",&u,&v),mp[u].PB(v),mp[v].PB(u);
    for(int i=0;i<=20;i++)
        dfs(1,0,i);
    printf("%lld\n",ans);
    return 0;
}


#include <iostream>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <map>
#include <set>
#include <bitset>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <string>
#include <sstream>
#include <time.h>
#define x first
#define y second
#define pb push_back
#define mp make_pair
#define lson l,m,rt*2
#define rson m+1,r,rt*2+1
#define mt(A,B) memset(A,B,sizeof(A))
#define mod 1000000007
using namespace std;
typedef long long LL;
const double PI = acos(-1);
const int N=1e5+10;
const int inf = 0x3f3f3f3f;
const LL INF=0x3f3f3f3f3f3f3f3fLL;
LL dp[N][2],a[N];
LL ans=0;
vector<int> G[N];
LL dfs(int x,int fa,int bit)
{
    LL q=0;
    int b=(a[x]>>bit)&1;//取得当前的a[x]在第bit位是0还是1，
    dp[x][b]=1;//初始化
    dp[x][b^1]=0;//初始化
    for(int i=0;i<G[x].size();i++)
    {
        int v=G[x][i];
        if(v==fa)continue;
        dfs(v,x,bit);
        q+=dp[x][0]*dp[v][1]+dp[x][1]*dp[v][0];//统计子节点到x的路径上异或和，只需算1^0与0^1即可。
        dp[x][b^0]+=dp[v][0];//更新异或操作后的状态值。
        dp[x][b^1]+=dp[v][1];//更新异或操作后的状态值。
    }
    ans+= q*(1<<bit);//更新答案。
}
int main()
{
#ifdef Local
    freopen("data.txt","r",stdin);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n,u,v;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        ans+=a[i];
    }
    for(int i=0;i<n-1;i++)
    {
        cin>>u>>v;
        G[u].pb(v);
        G[v].pb(u);
    }
    for(int i=0;i<=20;i++)//由于权值最大为1e6，所以其实枚举到20位就足够了。
    {
        dfs(1,0,i);
    }
    cout<<ans<<endl;
#ifdef Local
    cerr << "time: " << (LL) clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
#endif
}


#include <bits/stdc++.h>

using namespace std;

#define MP make_pair
#define PB push_back
typedef long long LL;
typedef pair<int,int> PII;
const double eps=1e-8;
const double pi=acos(-1.0);
const int K=1e5+7;
const int mod=1e9+7;

int n,v[K],dp[K][2];
LL ans;
vector<int>mp[K];

void dfs(int x,int f,int k)
{
    int t=v[x]>>k&1;
    LL sum=0;
    dp[x][t]=1,dp[x][t^1]=0;
    for(int i=0;i<mp[x].size();i++)
    if(mp[x][i]!=f)
    {
        int v=mp[x][i];
        dfs(v,x,k);
        sum+=dp[x][0]*dp[v][1]+dp[x][1]*dp[v][0];
        dp[x][t^0]+=dp[v][0];
        dp[x][t^1]+=dp[v][1];
    }
    ans+=(sum<<k);
}
int main(void)
{
    cin>>n;
    for(int i=1;i<=n;i++)
        scanf("%d",v+i),ans+=v[i];
    for(int i=1,u,v;i<n;i++)
        scanf("%d%d",&u,&v),mp[u].PB(v),mp[v].PB(u);
    for(int i=0;i<=20;i++)
        dfs(1,0,i);
    printf("%lld\n",ans);
    return 0;
}


//Catchharsh
//Legendary Sage Jiraiya Sensei
#include<bits/stdc++.h>
#pragma GCC optimize "03"
#define ll long long int
#define ld long double
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define FRE freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#define F first
#define S second
#define sz(x) (ll)x.size()
#define all(x) x.begin(),x.end()
#define pb push_back
#define endl '\n'
#define debug(x) cout<<#x<<" = "<<x<<endl
#define catchharsh cout<<"\n---------catchharsh---------\n"
const ll MOD = 1e9+7;
const ll INF = 1011111111;
const ll LLINF = 1000111000111000111LL;
const ld EPS = 1e-10;
const ld PI = 3.14159265358979323;
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// #define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
//inline ll add(ll a,ll b){a+=b;if(a>=MOD)a-=MOD;return a;}
//inline ll sub(ll a,ll b){a-=b;if(a<0)a+=MOD;return a;}
//inline ll mul(ll a,ll b){return (a*1ll*b)%MOD;}
ll power (ll base,ll exponent)
{
    ll ans=1;
    //base%=MOD;
    //exponent%=(MOD-1);
    while(exponent!=0)
    {
      if(exponent&1) ans=(1LL*ans*base);
      base=(1LL*base*base);
      exponent>>=1;
    }
    return ans;
}
const ll N=1e6+5;
const ll K=18;
ll val[N];
vector<ll> v[N];
// dp[x][0] , starting from x, number of paths having xor sum = 0 in its subtree
// dp[x][1] , starting from x, number of paths having xor sum = 1 in its subtree
ll dp[N][2];
ll ans=0;
void dfs(ll node,ll par,ll bit)
{
    for(auto child:v[node])
    {
        if(child==par) continue;
        dfs(child,node,bit);
        dp[node][0]+=dp[child][0];
        dp[node][1]+=dp[child][1];
    }
    ll cnt=0;
    // node is the middle point of path;
    // if node has set bit , then  all 0 xor values wuold become 1 and 1 xor values become 0( attn: we have not added its contribution in the subtree)
    if((1ll<<bit)&val[node]) swap(dp[node][0],dp[node][1]);
    for(auto child:v[node])
    {
        if(child==par) continue;
        if((1ll<<bit)&val[node])
        {
            // 1 is the node itself, 0 ^ 1 include or 1 ^ 0 include is sides, dhyaan se we have swapped the dp values;
            cnt+=(dp[child][1]*(dp[node][0]-dp[child][1]));
            cnt+=(dp[child][0]*(dp[node][1]-dp[child][0]));
        }
        else
        {
            // 0 is the node itself, 1^0 or 0^1 is sides;
            cnt+=(dp[child][1]*(dp[node][0]-dp[child][0]));
            cnt+=(dp[child][0]*(dp[node][1]-dp[child][1]));
        }
    }
    cnt/=2ll;
    if((1ll<<bit)&val[node]) dp[node][1]++;
    else dp[node][0]++;
    // node is the end point of path from subtree;
    // cnt+=dp[node][1];
    // cout<<node<<" "<<dp[node][1]<<" "<<cnt<<endl;
    ans+=(1ll<<bit)*(cnt+dp[node][1]);

}
int32_t main()
{
     IOS;
     ll n,m,i,j,k;
     cin>>n;
     for(i=1;i<=n;i++) cin>>val[i];
     for(i=0;i<n-1;i++)
     {
        ll x,y;
        cin>>x>>y;
        v[x].pb(y);
        v[y].pb(x);
     }
     for(ll bit=0;bit<=20;bit++)
     {
        for(i=0;i<=n;i++){
            dp[i][0]=dp[i][1]=0;
        }
        // cout<<bit<<endl;
        dfs(1,-1,bit);
        // catchharsh;
     }
     cout<<ans;

     cerr <<endl<< "Time elapsed : " << clock() * 1000.0 / CLOCKS_PER_SEC << " ms" << '\n';
}


//Catchharsh
//Legendary Sage Jiraiya Sensei
#include<bits/stdc++.h>
#pragma GCC optimize "03"
#define ll long long int
#define ld long double
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define FRE freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#define F first
#define S second
#define sz(x) (ll)x.size()
#define all(x) x.begin(),x.end()
#define pb push_back
#define endl '\n'
#define debug(x) cout<<#x<<" = "<<x<<endl
#define catchharsh cout<<"\n---------catchharsh---------\n"
const ll MOD = 1e9+7;
const ll INF = 1011111111;
const ll LLINF = 1000111000111000111LL;
const ld EPS = 1e-10;
const ld PI = 3.14159265358979323;
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// #define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
//inline ll add(ll a,ll b){a+=b;if(a>=MOD)a-=MOD;return a;}
//inline ll sub(ll a,ll b){a-=b;if(a<0)a+=MOD;return a;}
//inline ll mul(ll a,ll b){return (a*1ll*b)%MOD;}
ll power (ll base,ll exponent)
{
    ll ans=1;
    //base%=MOD;
    //exponent%=(MOD-1);
    while(exponent!=0)
    {
      if(exponent&1) ans=(1LL*ans*base);
      base=(1LL*base*base);
      exponent>>=1;
    }
    return ans;
}
const ll N=1e6+5;
const ll K=18;
ll val[N];
vector<ll> v[N];
// dp[x][0] , starting from x, number of paths having xor sum = 0 in its subtree
// dp[x][1] , starting from x, number of paths having xor sum = 1 in its subtree
ll dp[N][2];
ll ans=0;
void dfs(ll node,ll par,ll bit)
{
    for(auto child:v[node])
    {
        if(child==par) continue;
        dfs(child,node,bit);
        dp[node][0]+=dp[child][0];
        dp[node][1]+=dp[child][1];
    }
    ll cnt=0;
    for(auto child:v[node])
    {
      if(child==par) continue;
      dp[node][1]-=dp[child][1];
      dp[node][0]-=dp[child][0];
      if((1ll<<bit)&val[node]) swap(dp[node][1],dp[node][0]); // since including it would revrse parity s of xor paths
      cnt+=(dp[child][0]*dp[node][1])+(dp[child][1]*dp[node][0]);
      if((1ll<<bit)&val[node]) swap(dp[node][1],dp[node][0]); // revert the change, will do it later, when we will make transitions
    }
    if((1ll<<bit)&val[node]) dp[node][1]++;
    else dp[node][0]++;
    // transitions
    for(auto child:v[node])
    {
      if(child==par) continue;
      if((1ll<<bit)&val[node])
      {
        dp[node][1]+=dp[child][0];
        dp[node][0]+=dp[child][1];
      }
      else
      {
        dp[node][1]+=dp[child][1];
        dp[node][0]+=dp[child][0];
      }
    }
    cnt+=dp[node][1];
    ans+=(1ll<<bit)*cnt;

}
int32_t main()
{
     IOS;
     ll n,m,i,j,k;
     cin>>n;
     for(i=1;i<=n;i++) cin>>val[i];
     for(i=0;i<n-1;i++)
     {
        ll x,y;
        cin>>x>>y;
        v[x].pb(y);
        v[y].pb(x);
     }
     for(ll bit=0;bit<=20;bit++)
     {
        for(i=0;i<=n;i++){
            dp[i][0]=dp[i][1]=0;
        }
        // cout<<bit<<endl;
        dfs(1,-1,bit);
        // catchharsh;
     }
     cout<<ans;

     cerr <<endl<< "Time elapsed : " << clock() * 1000.0 / CLOCKS_PER_SEC << " ms" << '\n';
}


//Catchharsh
//Legendary Sage Jiraiya Sensei
#include<bits/stdc++.h>
#pragma GCC optimize "03"
#define ll long long int
#define ld long double
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define FRE freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#define F first
#define S second
#define sz(x) (ll)x.size()
#define all(x) x.begin(),x.end()
#define pb push_back
#define endl '\n'
#define debug(x) cout<<#x<<" = "<<x<<endl
#define catchharsh cout<<"\n---------catchharsh---------\n"
const ll MOD = 1e9+7;
const ll INF = 1011111111;
const ll LLINF = 1000111000111000111LL;
const ld EPS = 1e-10;
const ld PI = 3.14159265358979323;
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// #define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
//inline ll add(ll a,ll b){a+=b;if(a>=MOD)a-=MOD;return a;}
//inline ll sub(ll a,ll b){a-=b;if(a<0)a+=MOD;return a;}
//inline ll mul(ll a,ll b){return (a*1ll*b)%MOD;}
ll power (ll base,ll exponent)
{
    ll ans=1;
    //base%=MOD;
    //exponent%=(MOD-1);
    while(exponent!=0)
    {
      if(exponent&1) ans=(1LL*ans*base);
      base=(1LL*base*base);
      exponent>>=1;
    }
    return ans;
}
const ll N=1e6+5;
const ll K=18;
ll val[N];
vector<ll> v[N];
// dp[x][0] , starting from x, number of paths having xor sum = 0 in its subtree
// dp[x][1] , starting from x, number of paths having xor sum = 1 in its subtree
ll dp[N][2];
ll ans=0;
void dfs(ll node,ll par,ll bit)
{
    for(auto child:v[node])
    {
        if(child==par) continue;
        dfs(child,node,bit);
        dp[node][0]+=dp[child][0];
        dp[node][1]+=dp[child][1];
    }
    ll cnt=0;
    // node is the middle point of path;
    // if node has set bit , then  all 0 xor values wuold become 1 and 1 xor values become 0( attn: we have not added its contribution in the subtree)
    if((1ll<<bit)&val[node]) swap(dp[node][0],dp[node][1]);
    for(auto child:v[node])
    {
        if(child==par) continue;
        if((1ll<<bit)&val[node])
        {
            // 1 is the node itself, 0 ^ 1 include or 1 ^ 0 include is sides, dhyaan se we have swapped the dp values;
            cnt+=(dp[child][1]*(dp[node][0]-dp[child][1]));
            cnt+=(dp[child][0]*(dp[node][1]-dp[child][0]));
        }
        else
        {
            // 0 is the node itself, 1^0 or 0^1 is sides;
            cnt+=(dp[child][1]*(dp[node][0]-dp[child][0]));
            cnt+=(dp[child][0]*(dp[node][1]-dp[child][1]));
        }
    }
    cnt/=2ll;
    if((1ll<<bit)&val[node]) dp[node][1]++;
    else dp[node][0]++;
    // node is the end point of path from subtree;
    // cnt+=dp[node][1];
    // cout<<node<<" "<<dp[node][1]<<" "<<cnt<<endl;
    ans+=(1ll<<bit)*(cnt+dp[node][1]);

}
int32_t main()
{
     IOS;
     ll n,m,i,j,k;
     cin>>n;
     for(i=1;i<=n;i++) cin>>val[i];
     for(i=0;i<n-1;i++)
     {
        ll x,y;
        cin>>x>>y;
        v[x].pb(y);
        v[y].pb(x);
     }
     for(ll bit=0;bit<=18;bit++)
     {
        for(i=0;i<=n;i++){
            dp[i][0]=dp[i][1]=0;
        }
        // cout<<bit<<endl;
        dfs(1,-1,bit);
        // catchharsh;
     }
     cout<<ans;

     cerr <<endl<< "Time elapsed : " << clock() * 1000.0 / CLOCKS_PER_SEC << " ms" << '\n';
}


//Catchharsh
//Legendary Sage Jiraiya Sensei
#include<bits/stdc++.h>
#pragma GCC optimize "03"
#define ll long long int
#define ld long double
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define FRE freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#define F first
#define S second
#define sz(x) (ll)x.size()
#define all(x) x.begin(),x.end()
#define pb push_back
#define endl '\n'
#define debug(x) cout<<#x<<" = "<<x<<endl
#define catchharsh cout<<"\n---------catchharsh---------\n"
const ll MOD = 1e9+7;
const ll INF = 1011111111;
const ll LLINF = 1000111000111000111LL;
const ld EPS = 1e-10;
const ld PI = 3.14159265358979323;
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// #define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
//inline ll add(ll a,ll b){a+=b;if(a>=MOD)a-=MOD;return a;}
//inline ll sub(ll a,ll b){a-=b;if(a<0)a+=MOD;return a;}
//inline ll mul(ll a,ll b){return (a*1ll*b)%MOD;}
ll power (ll base,ll exponent)
{
    ll ans=1;
    //base%=MOD;
    //exponent%=(MOD-1);
    while(exponent!=0)
    {
      if(exponent&1) ans=(1LL*ans*base);
      base=(1LL*base*base);
      exponent>>=1;
    }
    return ans;
}
const ll N=1e6+5;
const ll K=18;
ll val[N];
vector<ll> v[N];
// dp[x][0] , starting from x, number of paths having xor sum = 0 in its subtree
// dp[x][1] , starting from x, number of paths having xor sum = 1 in its subtree
ll dp[N][2];
ll ans=0;
void dfs(ll node,ll par,ll bit)
{
    for(auto child:v[node])
    {
        if(child==par) continue;
        dfs(child,node,bit);
        dp[node][0]+=dp[child][0];
        dp[node][1]+=dp[child][1];
    }
    ll cnt=0;
    // node is the middle point of path;
    // if node has set bit , then  all 0 xor values wuold become 1 and 1 xor values become 0( attn: we have not added its contribution in the subtree)
    if((1ll<<bit)&val[node]) swap(dp[node][0],dp[node][1]);
    for(auto child:v[node])
    {
        if(child==par) continue;
        if((1ll<<bit)&val[node])
        {
            // 1 is the node itself, 0 ^ 0 or 1 ^ 1 is sides, dyaan se we have swapped node dp values;
            cnt+=(dp[child][1]*(dp[node][0]-dp[child][0]));
            cnt+=(dp[child][0]*(dp[node][1]-dp[child][1]));
        }
        else
        {
            // 0 is the node itself, 1^0 or 0^1 is sides;
            cnt+=(dp[child][1]*(dp[node][0]-dp[child][0]));
            cnt+=(dp[child][0]*(dp[node][1]-dp[child][1]));
        }
    }
    cnt/=2ll;
    if((1ll<<bit)&val[node]) dp[node][1]++;
    else dp[node][0]++;
    // node is the end point of path from subtree;
    // cnt+=dp[node][1];
    // cout<<node<<" "<<dp[node][1]<<" "<<cnt<<endl;
    ans+=(1ll<<bit)*(cnt+dp[node][1]);

}
int32_t main()
{
     IOS;
     ll n,m,i,j,k;
     cin>>n;
     for(i=1;i<=n;i++) cin>>val[i];
     for(i=0;i<n-1;i++)
     {
        ll x,y;
        cin>>x>>y;
        v[x].pb(y);
        v[y].pb(x);
     }
     for(ll bit=0;bit<=18;bit++)
     {
        for(i=0;i<=n;i++){
            dp[i][0]=dp[i][1]=0;
        }
        // cout<<bit<<endl;
        dfs(1,-1,bit);
        // catchharsh;
     }
     cout<<ans;

     cerr <<endl<< "Time elapsed : " << clock() * 1000.0 / CLOCKS_PER_SEC << " ms" << '\n';
}


#include <bits/stdc++.h>
using namespace std;

const int MAXN = 1e5;
vector<int> g[MAXN];
bool tk[MAXN];
int fat[MAXN], szt[MAXN], n, a[MAXN];
long long ans[20], acum[MAXN];

pair<long long, long long> dfs(int x, int par, int bit, int id){
  acum[x] = a[x];
  if(id != par){
    acum[x] ^= acum[par];
  }
  bool b = (acum[x]&(1<<bit));
  pair<long long, long long> ret = {b, !b};
  for(auto aux : g[x]){
    if(aux != par && !tk[aux]){
      auto rec = dfs(aux, x, bit, id);
      ret.first += rec.first;
      ret.second += rec.second;
    }
  }
  return ret;
}

void solve(int v, int bit){
  ans[bit] += (a[v]&(1<<bit)) > 0;
  long long am1 = 0, am0 = 0;
  for(auto x : g[v]){
    if(!tk[x]){
      auto aux = dfs(x, v, bit, v);
      if(a[v]&(1<<bit)){
        ans[bit] += aux.second;
        ans[bit] += aux.second*am0;
        ans[bit] += aux.first*am1;
      }
      else{
        ans[bit] += aux.first;
        ans[bit] += aux.first*am0;
        ans[bit] += aux.second*am1;
      }
      am1 += aux.first;
      am0 += aux.second;
    }
  }
}

int calcsz(int x, int f){
  szt[x]=1;
  for(auto y:g[x])if(y!=f&&!tk[y])szt[x]+=calcsz(y,x);
  return szt[x];
}

void cdfs(int bit, int x=0, int f=-1, int sz=-1){
  if(sz<0)sz=calcsz(x,-1);
  for(auto y:g[x])if(!tk[y]&&szt[y]*2>=sz){
  szt[x]=0;cdfs(bit,y,f,sz);return;
  }
  solve(x, bit);
  tk[x]=true;fat[x]=f;
  for(auto y:g[x])if(!tk[y])cdfs(bit,y,x);
}

void centroid(int bit){memset(tk,false,sizeof(tk));cdfs(bit);}

int main(){
  ios::sync_with_stdio(false);
  int i, x, y;
  cin >> n;
  long long res = 0;
  for(i = 0; i < n; i++){
    cin >> a[i];
  }
  for(i = 0; i < n-1; i++){
    cin >> x >> y;
    g[x-1].push_back(y-1);
    g[y-1].push_back(x-1);
  }
  for(i = 0; i < 20; i++){
    centroid(i);
  }
  for(i = 0; i < 20; i++){
    res += ans[i]*(1<<i);
  }
  cout << res << endl;
}


#include <bits/stdc++.h>
using namespace std;

const int MAXN = 1e5;
vector<int> g[MAXN];
bool tk[MAXN];
int fat[MAXN], szt[MAXN], n, a[MAXN];
long long ans[20], acum[MAXN], am1[20], am0[20];
pair<long long, long long> ret[MAXN][20];

void dfs(int x, int par, int id){
  acum[x] = a[x];
  if(id != par){
    acum[x] ^= acum[par];
  }
  for(int i = 0; i < 20; i++){
    bool b = (acum[x]&(1<<i));
    ret[x][i] = {b, !b};
  }
  for(auto aux : g[x]){
    if(aux != par && !tk[aux]){
      dfs(aux, x, id);
      for(int i = 0; i < 20; i++){
        ret[x][i].first += ret[aux][i].first;
        ret[x][i].second += ret[aux][i].second;
      }
    }
  }
}

void solve(int v){
  memset(am1, 0, sizeof(am1));
  memset(am0, 0, sizeof(am0));
  for(auto x : g[v]){
    if(!tk[x]){
      dfs(x, v, v);
      for(int i = 0; i < 20; i++){
        auto aux = ret[x][i];
        if(a[v]&(1<<i)){
          ans[i] += aux.second;
          ans[i] += aux.second*am0[i];
          ans[i] += aux.first*am1[i];
        }
        else{
          ans[i] += aux.first;
          ans[i] += aux.first*am0[i];
          ans[i] += aux.second*am1[i];
        }
        am1[i] += aux.first;
        am0[i] += aux.second;
      }
    }
  }
}

int calcsz(int x, int f){
  szt[x]=1;
  for(auto y:g[x])if(y!=f&&!tk[y])szt[x]+=calcsz(y,x);
  return szt[x];
}

void cdfs(int x=0, int f=-1, int sz=-1){
  if(sz<0)sz=calcsz(x,-1);
  for(auto y:g[x])if(!tk[y]&&szt[y]*2>=sz){
  szt[x]=0;cdfs(y,f,sz);return;
  }
  solve(x);
  tk[x]=true;fat[x]=f;
  for(auto y:g[x])if(!tk[y])cdfs(y,x);
}

void centroid(){memset(tk,false,sizeof(tk));cdfs();}

int main(){
  ios::sync_with_stdio(false);
  int i, x, y;
  cin >> n;
  long long res = 0;
  for(i = 0; i < n; i++){
    cin >> a[i];
    res += a[i];
  }
  for(i = 0; i < n-1; i++){
    cin >> x >> y;
    g[x-1].push_back(y-1);
    g[y-1].push_back(x-1);
  }
  centroid();
  for(i = 0; i < 20; i++){
    res += ans[i]*(1<<i);
  }
  cout << res << endl;
}


#include <bits/stdc++.h>
using namespace std;

const int MAXN = 1e5;
vector<int> g[MAXN];
bool tk[MAXN];
int fat[MAXN], szt[MAXN], n, a[MAXN];
long long ans[20], am1, am0, acum[MAXN];

pair<long long, long long> dfs(int x, int par, int bit, int id){
  acum[x] = a[x];
  if(id != par){
    acum[x] ^= acum[par];
  }
  am1 += (acum[x]&(1<<bit)) != 0;
  am0 += (acum[x]&(1<<bit)) == 0;
  pair<long long, long long> ret = {(acum[x]&(1<<bit)) != 0, (acum[x]&(1<<bit)) == 0};
  for(auto aux : g[x]){
    if(aux != par && !tk[aux]){
      auto rec = dfs(aux, x, bit, id);
      ret.first += rec.first;
      ret.second += rec.second;
    }
  }
  return ret;
}

void solve(int v, int bit){
  am1 = am0 = 0;
  vector<pair<long long, long long>> vals;
  for(auto x : g[v]){
    if(!tk[x]){
      vals.push_back(dfs(x, v, bit, v));
    }
  }
  long long sum = 0;
  if(a[v]&(1<<bit)){
    ans[bit]++;
    for(auto x : vals){
      ans[bit] += x.second;
      sum += x.second*(am0-x.second);
      sum += x.first*(am1-x.first);
    }
  }
  else{
    for(auto x : vals){
      ans[bit] += x.first;
      sum += x.first*(am0-x.second);
      sum += x.second*(am1-x.first);
    }
  }
  ans[bit] += sum/2;
}

int calcsz(int x, int f){
  szt[x]=1;
  for(auto y:g[x])if(y!=f&&!tk[y])szt[x]+=calcsz(y,x);
  return szt[x];
}

void cdfs(int bit, int x=0, int f=-1, int sz=-1){
  if(sz<0)sz=calcsz(x,-1);
  for(auto y:g[x])if(!tk[y]&&szt[y]*2>=sz){
  szt[x]=0;cdfs(bit,y,f,sz);return;
  }
  solve(x, bit);
  tk[x]=true;fat[x]=f;
  for(auto y:g[x])if(!tk[y])cdfs(bit,y,x);
}

void centroid(int bit){memset(tk,false,sizeof(tk));cdfs(bit);}

int main(){
  int i, x, y;
  cin >> n;
  long long res = 0;
  for(i = 0; i < n; i++){
    cin >> a[i];
  }
  for(i = 0; i < n-1; i++){
    cin >> x >> y;
    g[x-1].push_back(y-1);
    g[y-1].push_back(x-1);
  }
  for(i = 0; i < 20; i++){
    centroid(i);
  }
  for(i = 0; i < 20; i++){
    res += ans[i]*(1<<i);
  }
  cout << res << endl;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define vec vector
#define pb push_back
#define s(x) scanf("%d",&x)
#define all(x)	x.begin(),x.end()
#define pii pair<int,int>
#define fi first
#define se second
#define SZ 100005
#define boost ios::sync_with_stdio(0);

ll dp[100005][25][2];
bool arr[100005][25];
vec<int> adj[100005];

ll ans[25];

void dfs(int u,int p)
{
	ll tmp[25][2];
	memset(tmp,0,sizeof (tmp));
	for(int i=0; i<25; i++)
	{
		dp[u][i][arr[u][i]]=1;
		dp[u][i][0]=(arr[u][i]==0)?1:0;
		//ans[i]+=dp[u][i][1];
	}

	for(int v:adj[u])
	{
		if(v-p)
		{
			dfs(v,u);
			for(int j=0; j<25; j++)
			{
				if(arr[u][j])
				 dp[u][j][1]+=dp[v][j][0]
				,dp[u][j][0]+=dp[v][j][1];
				else
				 dp[u][j][1]+=dp[v][j][1]
				,dp[u][j][0]+=dp[v][j][0];

				tmp[j][0]+=dp[v][j][0];
				tmp[j][1]+=dp[v][j][1];

			}
		}
	}
	for(int j=0; j<25; j++)
	{
		ll p=0;
		for (int v:adj[u] )
		{
			if(arr[u][j])
			p+=1LL*(tmp[j][0]-dp[v][j][0])*dp[v][j][0]+1LL*(tmp[j][1]-dp[v][j][1])*dp[v][j][1];
			else
			p+=1LL*(tmp[j][1]-dp[v][j][1])*dp[v][j][0]+1LL*(tmp[j][0]-dp[v][j][0])*dp[v][j][1];
		}
		//if(j<4) cout<<"p="<<p<<endl;
		ans[j]+=p/2;
	}
	for(int i=0; i<25; i++)
	{
		ans[i]+=dp[u][i][1];
	}

}
int main()
{
	//freopen("input.txt","r",stdin);
	ll n,a,u,v;
	cin>>n;
	for(int i=1; i<=n; i++)
	{
		cin>>a;
		for(int j=0;j<25;j++)
		arr[i][j]=((1LL<<j)&a)?1:0;
	}
	for(int i=0; i<n-1; i++)
	{
		cin>>u>>v;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	dfs(1,0);
	ll Ans=0;
	for(ll i=0; i<25; i++)
	{
		Ans+=(1LL<<i)*ans[i];
	}
	cout<<Ans;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define vec vector
#define pb push_back
#define s(x) scanf("%d",&x)
#define all(x)	x.begin(),x.end()
#define pii pair<int,int>
#define fi first
#define se second
#define SZ 100005
#define boost ios::sync_with_stdio(0);

ll dp[100005][25][2];
bool arr[100005][25];
vec<int> adj[100005];

ll ans[25];

void dfs(int u,int p)
{
	ll tmp[25][2];
	memset(tmp,0,sizeof (tmp));
	for(int i=0; i<25; i++)
	{
		dp[u][i][arr[u][i]]=1;
		//ans[i]+=dp[u][i][1];
	}

	for(int v:adj[u])
	{
		if(v-p)
		{
			dfs(v,u);
			for(int j=0; j<25; j++)
			{
				if(arr[u][j])
				 dp[u][j][1]+=dp[v][j][0]
				,dp[u][j][0]+=dp[v][j][1];
				else
				 dp[u][j][1]+=dp[v][j][1]
				,dp[u][j][0]+=dp[v][j][0];

				tmp[j][0]+=dp[v][j][0];
				tmp[j][1]+=dp[v][j][1];

			}
		}
	}
	for(int j=0; j<25; j++)
	{
		ll thru=0;
		for (int v:adj[u] )
		{
			if(v-p){
			if(arr[u][j])
			thru+=1LL*(tmp[j][0]-dp[v][j][0])*dp[v][j][0]+1LL*(tmp[j][1]-dp[v][j][1])*dp[v][j][1];
			else
			thru+=1LL*(tmp[j][1]-dp[v][j][1])*dp[v][j][0]+1LL*(tmp[j][0]-dp[v][j][0])*dp[v][j][1];

			}
		}
		ans[j]+=thru/2;
		//if(j<4) //cout<<"thru="<<thru//<<endl;

	}
	for(int i=0; i<25; i++)
	{
		ans[i]+=dp[u][i][1];
	}

}
int main()
{
	//freopen("input.txt","r",stdin);
	ll n,a,u,v;
	cin>>n;
	for(int i=1; i<=n; i++)
	{
		cin>>a;
		for(int j=0;j<25;j++)
		arr[i][j]=((1LL<<j)&a)?1:0;
	}
	for(int i=0; i<n-1; i++)
	{
		cin>>u>>v;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	dfs(1,0);
	ll Ans=0;
	for(ll i=0; i<25; i++)
	{
		Ans+=(1LL<<i)*ans[i];
	}
	cout<<Ans;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define vec vector
#define pb push_back
#define s(x) scanf("%d",&x)
#define all(x)	x.begin(),x.end()
#define pii pair<int,int>
#define fi first
#define se second
#define SZ 100005
#define boost ios::sync_with_stdio(0);

ll dp[100005][25][2];
bool arr[100005][25];
vec<int> adj[100005];
ll tmp[25][2];
ll ans[25];

void dfs(int u,int p)
{
	memset(tmp,0,sizeof (tmp));
	for(int i=0; i<25; i++)
	{
		dp[u][i][arr[u][i]]=1;
		dp[u][i][0]=(arr[u][i]==0)?1:0;
		//ans[i]+=dp[u][i][1];
	}

	for(int v:adj[u])
	{
		if(v-p)
		{
			dfs(v,u);
			for(int j=0; j<25; j++)
			{
				if(arr[u][j])
				 dp[u][j][1]+=dp[v][j][0]
				,dp[u][j][0]+=dp[v][j][1];
				else
				 dp[u][j][1]+=dp[v][j][1]
				,dp[u][j][0]+=dp[v][j][0];

				tmp[j][0]+=dp[v][j][0];
				tmp[j][1]+=dp[v][j][1];

			}
		}
	}
	for(int j=0; j<25; j++)
	{
		ll p=0;
		for (int v:adj[u] )
		{
			if(arr[u][j])
			p+=1LL*(tmp[j][0]-dp[v][j][0])*dp[v][j][0]+1LL*(tmp[j][1]-dp[v][j][1])*dp[v][j][1];
			else
			p+=1LL*(tmp[j][1]-dp[v][j][1])*dp[v][j][0]+1LL*(tmp[j][0]-dp[v][j][0])*dp[v][j][1];
		}
		//if(j<4) cout<<"p="<<p<<endl;
		ans[j]+=p/2;
	}
	for(int i=0; i<25; i++)
	{
		ans[i]+=dp[u][i][1];
	}

}
int main()
{
	//freopen("input.txt","r",stdin);
	ll n,a,u,v;
	cin>>n;
	for(int i=1; i<=n; i++)
	{
		cin>>a;
		for(int j=0;j<25;j++)
		arr[i][j]=((1LL<<j)&a)?1:0;
	}
	for(int i=0; i<n-1; i++)
	{
		cin>>u>>v;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	dfs(1,0);
	ll Ans=0;
	for(ll i=0; i<25; i++)
	{
		Ans+=(1LL<<i)*ans[i];
	}
	cout<<Ans;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define vec vector
#define pb push_back
#define s(x) scanf("%d",&x)
#define all(x)	x.begin(),x.end()
#define pii pair<int,int>
#define fi first
#define se second
#define SZ 100005
#define boost ios::sync_with_stdio(0);

ll dp[100005][25][2];
bool arr[100005][25];
vec<int> adj[100005];
ll tmp[25][2];
ll ans[25];

void dfs(int u,int p)
{
	memset(tmp,0,sizeof (tmp));
	for(int i=0; i<25; i++)
	{
		dp[u][i][arr[u][i]]=1;
		dp[u][i][0]=(arr[u][i]==0)?1:0;
		//ans[i]+=dp[u][i][1];
	}

	for(int v:adj[u])
	{
		if(v-p)
		{
			dfs(v,u);
			for(int j=0; j<25; j++)
			{
				if(arr[u][j])
				 dp[u][j][1]+=dp[v][j][0]
				,dp[u][j][0]+=dp[v][j][1];
				else
				 dp[u][j][1]+=dp[v][j][1]
				,dp[u][j][0]+=dp[v][j][0];

				tmp[j][0]=dp[v][j][0];
				tmp[j][1]=dp[v][j][1];

			}
		}
	}
	for(int j=0; j<25; j++)
	{
		ll p=0;
		for (int v:adj[u] )
		{
			if(arr[u][j])
			p+=1LL*(tmp[j][0]-dp[v][j][0])*dp[v][j][0]+1LL*(tmp[j][1]-dp[v][j][1])*dp[v][j][1];
			else
			p+=1LL*(tmp[j][1]-dp[v][j][1])*dp[v][j][0]+1LL*(tmp[j][0]-dp[v][j][0])*dp[v][j][1];
		}
		//if(j<4) cout<<"p="<<p<<endl;
		ans[j]+=p/2;
	}
	for(int i=0; i<25; i++)
	{
		ans[i]+=dp[u][i][1];
	}

}
int main()
{
	//freopen("input.txt","r",stdin);
	ll n,a,u,v;
	cin>>n;
	for(int i=1; i<=n; i++)
	{
		cin>>a;
		for(int j=0;j<25;j++)
		arr[i][j]=((1LL<<j)&a)?1:0;
	}
	for(int i=0; i<n-1; i++)
	{
		cin>>u>>v;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	dfs(1,0);
	ll Ans=0;
	for(ll i=0; i<25; i++)
	{
		Ans+=(1LL<<i)*ans[i];
	}
	cout<<Ans;
}


#include <bits/stdc++.h>

#define int long long
//#pragma GCC optimize("Ofast")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4")

#define file(s) freopen(s".in","r",stdin); freopen(s".out","w",stdout);
#define forev(i, b, a) for(int i = (b); i >= (a); --i)
#define forn(i, a, b) for(int i = (a); i <= (b); ++i)
#define sz(s) (int)s.size()
#define all(x) x.bg, x.en
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define rbg rbegin()
#define bg begin()
#define en end()
#define s second
#define f first

using namespace std;

typedef pair < long long, long long > pll;
typedef pair < int, int > pii;
typedef unsigned long long ull;
typedef vector < pii > vpii;
typedef vector < int > vi;
typedef long double ldb;
typedef long long ll;
typedef double db;

const int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1}, block = 265;
const pii base = mp(1171, 3307), Mod = mp(1e9 + 7, 1e9 + 9);
const int inf = 1e9, N = 1e5 + 11, mod = 1e9 + 7;
const db eps = 1e-12, pi = 3.14159265359;
const ll INF = 1e18;

int n, v, u, a[N], Sz[N], curSz, ans, dp[2][20], av;
bool was[N];
vi g[N];

void dfs (int v, int p = 0) {
	Sz[v] = 1;
	for (auto to : g[v]) {
		if (was[to] || to == p)
			continue;
		dfs(to, v);
		Sz[v] += Sz[to];
	}
}

void add (int d) {
	forn (i, 0, 19)
		++dp[(d >> i) & 1][i];
}

void dfs1 (int v, int p = 0, int d = 0) {
	d ^= a[v];
	ans += d ^ av;
	forn (i, 0, 19)
		ans += (dp[((d >> i) & 1) ^ 1][i] << i);
	for (auto to : g[v]) {
		if (was[to] || to == p)
			continue;
		dfs1(to, v, d);
	}
}

void dfs2 (int v, int p = 0, int d = av) {
	d ^= a[v];
	add(d);
	for (auto to : g[v]) {
		if (was[to] || to == p)
			continue;
		dfs2(to, v, d);
	}
}

int Find (int v) {
	for (auto to : g[v]) {
		if (was[to] || Sz[to] > Sz[v])
			continue;
		if (Sz[to] * 2 > curSz)
			return Find(to);
	}
	return v;
}

void calc (int v) {
	dfs(v);
	curSz = Sz[v];
	memset(dp, 0, sizeof(dp));
	v = Find(v);
	was[v] = 1;
	ans += a[v];
	av = a[v];
	for (auto to : g[v]) {
		if (was[to])
			continue;
		dfs1(to);
		dfs2(to);
	}
	for (auto to : g[v])
		if (!was[to])
			calc(to);
}

main () {
//	file("");
	scanf("%lld", &n);
	forn (i, 1, n)
		scanf("%lld", &a[i]);

	forn (i, 2, n) {
		scanf("%lld%lld", &v, &u);
		g[v].pb(u);
		g[u].pb(v);
	}
	calc(1);
	printf("%lld\n", ans);
	return 0;
}


#include <bits/stdc++.h>

#define int long long
//#pragma GCC optimize("Ofast")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4")

#define file(s) freopen(s".in","r",stdin); freopen(s".out","w",stdout);
#define forev(i, b, a) for(int i = (b); i >= (a); --i)
#define forn(i, a, b) for(int i = (a); i <= (b); ++i)
#define sz(s) (int)s.size()
#define all(x) x.bg, x.en
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define rbg rbegin()
#define bg begin()
#define en end()
#define s second
#define f first

using namespace std;

typedef pair < long long, long long > pll;
typedef pair < int, int > pii;
typedef unsigned long long ull;
typedef vector < pii > vpii;
typedef vector < int > vi;
typedef long double ldb;
typedef long long ll;
typedef double db;

const int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1}, block = 265;
const pii base = mp(1171, 3307), Mod = mp(1e9 + 7, 1e9 + 9);
const int inf = 1e9, N = 1e5 + 11, mod = 1e9 + 7;
const db eps = 1e-12, pi = 3.14159265359;
const ll INF = 1e18;

int n, v, u, a[N], Sz[N], curSz, ans, dp[2][20], av;
bool was[N];
vi g[N];

void dfs (int v, int p = 0) {
	Sz[v] = 1;
	for (auto to : g[v]) {
		if (was[to] || to == p)
			continue;
		dfs(to, v);
		Sz[v] += Sz[to];
	}
}

void add (int d) {
	forn (i, 0, 19)
		++dp[(d >> i) & 1][i];
}

void dfs1 (int v, int p = 0, int d = 0) {
	d ^= a[v];
	ans += d ^ av;
	forn (i, 0, 19)
		ans += (dp[((d >> i) & 1) ^ 1][i] << i);
	for (auto to : g[v]) {
		if (was[to] || to == p)
			continue;
		dfs1(to, v, d);
	}
}

void dfs2 (int v, int p = 0, int d = av) {
	d ^= a[v];
	add(d);
	for (auto to : g[v]) {
		if (was[to] || to == p)
			continue;
		dfs2(to, v, d);
	}
}

int Find (int v) {
	for (auto to : g[v]) {
		if (was[to] || Sz[to] > Sz[v])
			continue;
		if (Sz[to] * 2 > curSz)
			return Find(to);
	}
	return v;
}

void calc (int v) {
	dfs(v);
	curSz = Sz[v];
	memset(dp, 0, sizeof(dp));
	v = Find(v);
	was[v] = 1;
	ans += a[v];
	av = a[v];
	for (auto to : g[v]) {
		if (was[to])
			continue;
		dfs1(to);
		dfs2(to);
	}
	for (auto to : g[v])
		if (!was[to])
			calc(to);
}

main () {
//	file("");
	scanf("%lld", &n);
	forn (i, 1, n)
		scanf("%lld", &a[i]);

	forn (i, 2, n) {
		scanf("%lld", &v, &u);
		g[v].pb(u);
		g[u].pb(v);
	}
	calc(1);
	printf("%lld\n", ans);
	return 0;
}