#include <bits/stdc++.h>
using namespace std;
#define int long long
#define forinc(a,b,c) for(int a=b, __c=c; a<=__c; ++a)
#define fordec(a,b,c) for(int a=b, __c=c; a>=__c; --a)
#define forv(a,b) for(auto &a:b)
#define forb(a,b) for(int a=b._Find_first(); a<b.size(); a=b._Find_next(a))
#define ii pair<int,int>
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define all(a) begin(a),end(a)
#define rall(a) rbegin(a),rend(a)
#define reset(f,x) memset(f,x,sizeof(f))
#define fasty ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define gg exit(0);
#define bit(x,i) (x>>(i)&1ll)
#define on(x,i) (x|(1ll<<(i-1)))
#define off(x,i) (x&~(1ll<<(i-1)))
typedef int64_t ll;
typedef unsigned long long ull;
const int N=1e5+5;
int n,a[N];
vector<int> ad[N];
int f[N][30][2];
int ans=0;
void dfs(int u,int par){
forinc(i,0,25) f[u][i][bit(a[u],i)]++;
forv(v,ad[u]) if(v!=par){
dfs(v,u);
forinc(i,0,25){
ans+=(1ll<<i)*f[u][i][1]*f[v][i][0];
ans+=(1ll<<i)*f[u][i][0]*f[v][i][1];
if(bit(a[u],i)){
f[u][i][0]+=f[v][i][1];
f[u][i][1]+=f[v][i][0];
}else{
f[u][i][1]+=f[v][i][1];
f[u][i][0]+=f[v][i][0];
}
}
}
ans+=a[u];
}
int32_t main(){
fasty;
#define task "TASK"
if(fopen(task".inp","r")) freopen(task".inp","r",stdin);
cin>>n;
forinc(i,1,n) cin>>a[i];
forinc(i,1,n-1){
int u,v;
cin>>u>>v;
ad[u].pb(v); ad[v].pb(u);
}
dfs(1,0);
cout<<ans<<'\n';
}
Compilation message
deblo.cpp: In function 'int32_t main()':
deblo.cpp:58:37: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
58 | if(fopen(task".inp","r")) freopen(task".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2688 KB |
Output is correct |
2 |
Correct |
2 ms |
2688 KB |
Output is correct |
3 |
Correct |
2 ms |
2816 KB |
Output is correct |
4 |
Correct |
3 ms |
3200 KB |
Output is correct |
5 |
Correct |
3 ms |
3200 KB |
Output is correct |
6 |
Correct |
103 ms |
60152 KB |
Output is correct |
7 |
Correct |
99 ms |
60024 KB |
Output is correct |
8 |
Correct |
95 ms |
56824 KB |
Output is correct |
9 |
Correct |
98 ms |
56312 KB |
Output is correct |
10 |
Correct |
139 ms |
55928 KB |
Output is correct |