#include<bits/stdc++.h>
using namespace std;
int const N = 1e5 + 5;
typedef long long LL;
int a[N];
vector<int> graph[N];
int val[N];
void dfs(int x, int d = 0, int fa = -1){
d = d ^ a[x];
val[x] = d;
for(auto i : graph[x]){
if(i == fa)
continue;
dfs(i, d, x);
}
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
int u, v;
LL sum = 0;
for(int i = 1; i <= n; i++){
cin >> a[i];
sum += a[i];
}
for(int i = 0; i < n - 1; i++){
cin >> u >> v;
graph[u].push_back(v);
graph[v].push_back(u);
}
if(n <= 1000){
LL ans = 0;
for(int i = 1; i <= n; i++){
dfs(i);
for(int j = 1; j <= n; j++){
if(i != j)
ans += val[j];
}
}
cout << ans / 2 + sum << '\n';
}else{
LL ans = 0;
for(int i = 0; i <= 22; i++){
LL zro = 0, on = 0;
for(int j = 1; j <= n; j++){
if(a[j] & (1 << i)){
swap(zro, on);
on++;
}else{
zro++;
}
ans += on * (1 << i);
}
}
cout << ans << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
14 ms |
2732 KB |
Output is correct |
5 |
Correct |
15 ms |
2728 KB |
Output is correct |
6 |
Correct |
43 ms |
6360 KB |
Output is correct |
7 |
Correct |
41 ms |
6396 KB |
Output is correct |
8 |
Incorrect |
44 ms |
6472 KB |
Output isn't correct |
9 |
Incorrect |
42 ms |
6344 KB |
Output isn't correct |
10 |
Incorrect |
46 ms |
6544 KB |
Output isn't correct |