#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("-O3")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define m_p make_pair
#define f first
#define s second
#define vec vector
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define pw(x) (1LL<<(x))
#define sz(x) (int)x.size()
#define fast_ioi ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef long double ld;
typedef pair<long long,long long> pll;
template <class T> bool umin(T &a,const T &b){return (a>b?a=b,1:0);}
template <class T> bool umax(T &a,const T &b){return (a<b?a=b,1:0);}
auto rng=bind(uniform_int_distribution<int>(1,1e9),mt19937(time(0)));
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifndef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
*this << "[";
for (auto it = d.b; it != d.e; ++it)
*this << ", " + 2 * (it == d.b) << *it;
ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
template <class T> using oset=tree <T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const int N=1e5+3;
struct segtree{
pii t[4*N];
segtree(){
fill(t,t+4*N,m_p(-2e9,1));
}
void upd(int id,pii x,int v=1,int tl=0,int tr=N-1){
if(tl==tr){
umax(t[v],x);
// debug()<<imie(t[v])imie(x);
return;
}
int tm=(tl+tr)>>1;
if(tm>=id)
upd(id,x,2*v,tl,tm);
else
upd(id,x,2*v+1,tm+1,tr);
t[v]=max(t[2*v],t[2*v+1]);
}
pii get(int l,int r,int v=1,int tl=0,int tr=N-1){
if(tl>=l&&tr<=r) return t[v];
if(tl>r||tr<l) return {-2e9,1};
int tm=(tl+tr)>>1;
return max(get(l,r,2*v,tl,tm),get(l,r,2*v+1,tm+1,tr));
}
}seg;
struct fenwick{
int fnw[N];
fenwick(){
fill(fnw,fnw+N,0);
}
vec<pii>up;
void upd(int i,int x,bool t=1){
if(t) up.pb({i,-x});
while(i<N){
fnw[i]+=x;
i+=i&-i;
}
}
int get(int i){
int ans=0;
while(i>0){
ans+=fnw[i];
i-=i&-i;
}
return ans;
}
int get(int l,int r){
return get(r)-get(l-1);
}
void clr(){
// debug()<<imie(up);
for(auto &z : up)
upd(z.f,z.s,0);
up.clear();
}
}fnw;
vec<int> g[N];
int up[N][20],tin[N],tout[N],tt=1;
void dfs(int v,int p){
up[v][0]=p;
tin[v]=tt++;
// debug()<<imie()
for(int i=1;i<20;i++) up[v][i]=up[up[v][i-1]][i-1];
for(auto &z : g[v]){
if(z==p) continue;
dfs(z,v);
}
tout[v]=tt-1;
// debug()<<imie(v)imie(tin[v])imie(tout[v]);
}
signed main(){
fast_ioi;
// return 0;
int n;
cin>>n;
vec<int>a(n);
for(auto &z : a) cin>>z;
vec<int> kek=a;
sort(all(kek));kek.erase(unique(all(kek)),kek.end());
for(auto &z : a) z=lower_bound(all(kek),z)-kek.begin()+1;
vec<pii>e;
for(int i=1;i<n;i++){
int v, u;
cin >> v >> u;//--v; --u;
e.pb({v,u});
g[v].pb(u);
}
g[0].pb(1);
dfs(0,0);
seg.upd(tin[1],{-1,a[0]});
// return 0;
for(int i=0;i<n-1;i++){
ll ans=0;
int v=e[i].f;
vec<pii>arr;
while(v){
/// find max u
int cnt=0;
for(int j=19;j>=0;j--){
if(up[v][j]!=0
&&
seg.get(tin[up[v][j]],tout[up[v][j]])==seg.get(tin[v],tout[v]))
v=up[v][j],cnt+=(1<<j);
}
arr.pb({seg.get(tin[v],tout[v]).s,cnt+1});
v=up[v][0];
}
// debug()<<imie(arr);
for(auto &z : arr){
ans+=1ll*z.s*fnw.get(z.f-1);
fnw.upd(z.f,z.s);
}
fnw.clr();
cout<<ans<<'\n';
seg.upd(tin[e[i].s],{i,a[e[i].s-1]});
// if(i==1) return 0;
// for(int i=0;i<=n;i++)
// debug()<<imie(i)imie(seg.get(tin[i],tout[i]));
// debug()<<imie(*max_element(fnw.fnw,fnw.fnw+N));
// debug()<<imie()
}
return 0;
}
/*
5
4
0
0
0
1
2
3
0
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6092 KB |
Output is correct |
2 |
Correct |
3 ms |
6092 KB |
Output is correct |
3 |
Correct |
3 ms |
6200 KB |
Output is correct |
4 |
Correct |
5 ms |
6220 KB |
Output is correct |
5 |
Correct |
5 ms |
6220 KB |
Output is correct |
6 |
Correct |
7 ms |
6220 KB |
Output is correct |
7 |
Correct |
5 ms |
6220 KB |
Output is correct |
8 |
Correct |
5 ms |
6220 KB |
Output is correct |
9 |
Correct |
4 ms |
6220 KB |
Output is correct |
10 |
Correct |
4 ms |
6220 KB |
Output is correct |
11 |
Correct |
5 ms |
6220 KB |
Output is correct |
12 |
Correct |
5 ms |
6220 KB |
Output is correct |
13 |
Correct |
4 ms |
6220 KB |
Output is correct |
14 |
Correct |
5 ms |
6220 KB |
Output is correct |
15 |
Correct |
7 ms |
6220 KB |
Output is correct |
16 |
Correct |
7 ms |
6220 KB |
Output is correct |
17 |
Correct |
7 ms |
6208 KB |
Output is correct |
18 |
Correct |
8 ms |
6204 KB |
Output is correct |
19 |
Correct |
5 ms |
6220 KB |
Output is correct |
20 |
Correct |
5 ms |
6220 KB |
Output is correct |
21 |
Correct |
7 ms |
6220 KB |
Output is correct |
22 |
Correct |
5 ms |
6200 KB |
Output is correct |
23 |
Correct |
4 ms |
6220 KB |
Output is correct |
24 |
Correct |
5 ms |
6220 KB |
Output is correct |
25 |
Correct |
5 ms |
6220 KB |
Output is correct |
26 |
Correct |
4 ms |
6200 KB |
Output is correct |
27 |
Correct |
5 ms |
6220 KB |
Output is correct |
28 |
Correct |
4 ms |
6220 KB |
Output is correct |
29 |
Correct |
5 ms |
6196 KB |
Output is correct |
30 |
Correct |
7 ms |
6272 KB |
Output is correct |
31 |
Correct |
7 ms |
6200 KB |
Output is correct |
32 |
Correct |
5 ms |
6220 KB |
Output is correct |
33 |
Correct |
5 ms |
6220 KB |
Output is correct |
34 |
Correct |
4 ms |
6220 KB |
Output is correct |
35 |
Correct |
5 ms |
6220 KB |
Output is correct |
36 |
Correct |
5 ms |
6276 KB |
Output is correct |
37 |
Correct |
7 ms |
6220 KB |
Output is correct |
38 |
Correct |
5 ms |
6200 KB |
Output is correct |
39 |
Correct |
5 ms |
6200 KB |
Output is correct |
40 |
Correct |
5 ms |
6220 KB |
Output is correct |
41 |
Correct |
5 ms |
6220 KB |
Output is correct |
42 |
Correct |
5 ms |
6220 KB |
Output is correct |
43 |
Correct |
5 ms |
6220 KB |
Output is correct |
44 |
Correct |
4 ms |
6196 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6092 KB |
Output is correct |
2 |
Correct |
3 ms |
6092 KB |
Output is correct |
3 |
Correct |
3 ms |
6200 KB |
Output is correct |
4 |
Correct |
5 ms |
6220 KB |
Output is correct |
5 |
Correct |
5 ms |
6220 KB |
Output is correct |
6 |
Correct |
7 ms |
6220 KB |
Output is correct |
7 |
Correct |
5 ms |
6220 KB |
Output is correct |
8 |
Correct |
5 ms |
6220 KB |
Output is correct |
9 |
Correct |
4 ms |
6220 KB |
Output is correct |
10 |
Correct |
4 ms |
6220 KB |
Output is correct |
11 |
Correct |
5 ms |
6220 KB |
Output is correct |
12 |
Correct |
5 ms |
6220 KB |
Output is correct |
13 |
Correct |
4 ms |
6220 KB |
Output is correct |
14 |
Correct |
5 ms |
6220 KB |
Output is correct |
15 |
Correct |
7 ms |
6220 KB |
Output is correct |
16 |
Correct |
7 ms |
6220 KB |
Output is correct |
17 |
Correct |
7 ms |
6208 KB |
Output is correct |
18 |
Correct |
8 ms |
6204 KB |
Output is correct |
19 |
Correct |
5 ms |
6220 KB |
Output is correct |
20 |
Correct |
5 ms |
6220 KB |
Output is correct |
21 |
Correct |
7 ms |
6220 KB |
Output is correct |
22 |
Correct |
5 ms |
6200 KB |
Output is correct |
23 |
Correct |
4 ms |
6220 KB |
Output is correct |
24 |
Correct |
5 ms |
6220 KB |
Output is correct |
25 |
Correct |
5 ms |
6220 KB |
Output is correct |
26 |
Correct |
4 ms |
6200 KB |
Output is correct |
27 |
Correct |
5 ms |
6220 KB |
Output is correct |
28 |
Correct |
4 ms |
6220 KB |
Output is correct |
29 |
Correct |
5 ms |
6196 KB |
Output is correct |
30 |
Correct |
7 ms |
6272 KB |
Output is correct |
31 |
Correct |
7 ms |
6200 KB |
Output is correct |
32 |
Correct |
5 ms |
6220 KB |
Output is correct |
33 |
Correct |
5 ms |
6220 KB |
Output is correct |
34 |
Correct |
4 ms |
6220 KB |
Output is correct |
35 |
Correct |
5 ms |
6220 KB |
Output is correct |
36 |
Correct |
5 ms |
6276 KB |
Output is correct |
37 |
Correct |
7 ms |
6220 KB |
Output is correct |
38 |
Correct |
5 ms |
6200 KB |
Output is correct |
39 |
Correct |
5 ms |
6200 KB |
Output is correct |
40 |
Correct |
5 ms |
6220 KB |
Output is correct |
41 |
Correct |
5 ms |
6220 KB |
Output is correct |
42 |
Correct |
5 ms |
6220 KB |
Output is correct |
43 |
Correct |
5 ms |
6220 KB |
Output is correct |
44 |
Correct |
4 ms |
6196 KB |
Output is correct |
45 |
Correct |
8 ms |
6244 KB |
Output is correct |
46 |
Correct |
24 ms |
6752 KB |
Output is correct |
47 |
Correct |
28 ms |
6732 KB |
Output is correct |
48 |
Correct |
24 ms |
6892 KB |
Output is correct |
49 |
Correct |
15 ms |
6976 KB |
Output is correct |
50 |
Correct |
15 ms |
6988 KB |
Output is correct |
51 |
Correct |
16 ms |
7008 KB |
Output is correct |
52 |
Correct |
22 ms |
6852 KB |
Output is correct |
53 |
Correct |
21 ms |
6864 KB |
Output is correct |
54 |
Correct |
22 ms |
6892 KB |
Output is correct |
55 |
Correct |
23 ms |
6860 KB |
Output is correct |
56 |
Correct |
22 ms |
6844 KB |
Output is correct |
57 |
Correct |
49 ms |
6732 KB |
Output is correct |
58 |
Correct |
48 ms |
6744 KB |
Output is correct |
59 |
Correct |
52 ms |
6752 KB |
Output is correct |
60 |
Correct |
51 ms |
6744 KB |
Output is correct |
61 |
Correct |
25 ms |
6860 KB |
Output is correct |
62 |
Correct |
26 ms |
6852 KB |
Output is correct |
63 |
Correct |
26 ms |
6860 KB |
Output is correct |
64 |
Correct |
26 ms |
6720 KB |
Output is correct |
65 |
Correct |
24 ms |
6728 KB |
Output is correct |
66 |
Correct |
26 ms |
6732 KB |
Output is correct |
67 |
Correct |
23 ms |
6744 KB |
Output is correct |
68 |
Correct |
15 ms |
6976 KB |
Output is correct |
69 |
Correct |
22 ms |
6780 KB |
Output is correct |
70 |
Correct |
21 ms |
6836 KB |
Output is correct |
71 |
Correct |
25 ms |
6848 KB |
Output is correct |
72 |
Correct |
50 ms |
6692 KB |
Output is correct |
73 |
Correct |
52 ms |
6732 KB |
Output is correct |
74 |
Correct |
25 ms |
6844 KB |
Output is correct |
75 |
Correct |
21 ms |
6732 KB |
Output is correct |
76 |
Correct |
20 ms |
6780 KB |
Output is correct |
77 |
Correct |
20 ms |
6788 KB |
Output is correct |
78 |
Correct |
21 ms |
6776 KB |
Output is correct |
79 |
Correct |
20 ms |
6732 KB |
Output is correct |
80 |
Correct |
19 ms |
6760 KB |
Output is correct |
81 |
Correct |
24 ms |
6804 KB |
Output is correct |
82 |
Correct |
30 ms |
6780 KB |
Output is correct |
83 |
Correct |
21 ms |
6720 KB |
Output is correct |
84 |
Correct |
23 ms |
6788 KB |
Output is correct |
85 |
Correct |
22 ms |
6772 KB |
Output is correct |
86 |
Correct |
26 ms |
6732 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6092 KB |
Output is correct |
2 |
Correct |
3 ms |
6092 KB |
Output is correct |
3 |
Correct |
3 ms |
6200 KB |
Output is correct |
4 |
Correct |
5 ms |
6220 KB |
Output is correct |
5 |
Correct |
5 ms |
6220 KB |
Output is correct |
6 |
Correct |
7 ms |
6220 KB |
Output is correct |
7 |
Correct |
5 ms |
6220 KB |
Output is correct |
8 |
Correct |
5 ms |
6220 KB |
Output is correct |
9 |
Correct |
4 ms |
6220 KB |
Output is correct |
10 |
Correct |
4 ms |
6220 KB |
Output is correct |
11 |
Correct |
5 ms |
6220 KB |
Output is correct |
12 |
Correct |
5 ms |
6220 KB |
Output is correct |
13 |
Correct |
4 ms |
6220 KB |
Output is correct |
14 |
Correct |
5 ms |
6220 KB |
Output is correct |
15 |
Correct |
7 ms |
6220 KB |
Output is correct |
16 |
Correct |
7 ms |
6220 KB |
Output is correct |
17 |
Correct |
7 ms |
6208 KB |
Output is correct |
18 |
Correct |
8 ms |
6204 KB |
Output is correct |
19 |
Correct |
5 ms |
6220 KB |
Output is correct |
20 |
Correct |
5 ms |
6220 KB |
Output is correct |
21 |
Correct |
7 ms |
6220 KB |
Output is correct |
22 |
Correct |
5 ms |
6200 KB |
Output is correct |
23 |
Correct |
4 ms |
6220 KB |
Output is correct |
24 |
Correct |
5 ms |
6220 KB |
Output is correct |
25 |
Correct |
5 ms |
6220 KB |
Output is correct |
26 |
Correct |
4 ms |
6200 KB |
Output is correct |
27 |
Correct |
5 ms |
6220 KB |
Output is correct |
28 |
Correct |
4 ms |
6220 KB |
Output is correct |
29 |
Correct |
5 ms |
6196 KB |
Output is correct |
30 |
Correct |
7 ms |
6272 KB |
Output is correct |
31 |
Correct |
7 ms |
6200 KB |
Output is correct |
32 |
Correct |
5 ms |
6220 KB |
Output is correct |
33 |
Correct |
5 ms |
6220 KB |
Output is correct |
34 |
Correct |
4 ms |
6220 KB |
Output is correct |
35 |
Correct |
5 ms |
6220 KB |
Output is correct |
36 |
Correct |
5 ms |
6276 KB |
Output is correct |
37 |
Correct |
7 ms |
6220 KB |
Output is correct |
38 |
Correct |
5 ms |
6200 KB |
Output is correct |
39 |
Correct |
5 ms |
6200 KB |
Output is correct |
40 |
Correct |
5 ms |
6220 KB |
Output is correct |
41 |
Correct |
5 ms |
6220 KB |
Output is correct |
42 |
Correct |
5 ms |
6220 KB |
Output is correct |
43 |
Correct |
5 ms |
6220 KB |
Output is correct |
44 |
Correct |
4 ms |
6196 KB |
Output is correct |
45 |
Correct |
8 ms |
6244 KB |
Output is correct |
46 |
Correct |
24 ms |
6752 KB |
Output is correct |
47 |
Correct |
28 ms |
6732 KB |
Output is correct |
48 |
Correct |
24 ms |
6892 KB |
Output is correct |
49 |
Correct |
15 ms |
6976 KB |
Output is correct |
50 |
Correct |
15 ms |
6988 KB |
Output is correct |
51 |
Correct |
16 ms |
7008 KB |
Output is correct |
52 |
Correct |
22 ms |
6852 KB |
Output is correct |
53 |
Correct |
21 ms |
6864 KB |
Output is correct |
54 |
Correct |
22 ms |
6892 KB |
Output is correct |
55 |
Correct |
23 ms |
6860 KB |
Output is correct |
56 |
Correct |
22 ms |
6844 KB |
Output is correct |
57 |
Correct |
49 ms |
6732 KB |
Output is correct |
58 |
Correct |
48 ms |
6744 KB |
Output is correct |
59 |
Correct |
52 ms |
6752 KB |
Output is correct |
60 |
Correct |
51 ms |
6744 KB |
Output is correct |
61 |
Correct |
25 ms |
6860 KB |
Output is correct |
62 |
Correct |
26 ms |
6852 KB |
Output is correct |
63 |
Correct |
26 ms |
6860 KB |
Output is correct |
64 |
Correct |
26 ms |
6720 KB |
Output is correct |
65 |
Correct |
24 ms |
6728 KB |
Output is correct |
66 |
Correct |
26 ms |
6732 KB |
Output is correct |
67 |
Correct |
23 ms |
6744 KB |
Output is correct |
68 |
Correct |
15 ms |
6976 KB |
Output is correct |
69 |
Correct |
22 ms |
6780 KB |
Output is correct |
70 |
Correct |
21 ms |
6836 KB |
Output is correct |
71 |
Correct |
25 ms |
6848 KB |
Output is correct |
72 |
Correct |
50 ms |
6692 KB |
Output is correct |
73 |
Correct |
52 ms |
6732 KB |
Output is correct |
74 |
Correct |
25 ms |
6844 KB |
Output is correct |
75 |
Correct |
21 ms |
6732 KB |
Output is correct |
76 |
Correct |
20 ms |
6780 KB |
Output is correct |
77 |
Correct |
20 ms |
6788 KB |
Output is correct |
78 |
Correct |
21 ms |
6776 KB |
Output is correct |
79 |
Correct |
20 ms |
6732 KB |
Output is correct |
80 |
Correct |
19 ms |
6760 KB |
Output is correct |
81 |
Correct |
24 ms |
6804 KB |
Output is correct |
82 |
Correct |
30 ms |
6780 KB |
Output is correct |
83 |
Correct |
21 ms |
6720 KB |
Output is correct |
84 |
Correct |
23 ms |
6788 KB |
Output is correct |
85 |
Correct |
22 ms |
6772 KB |
Output is correct |
86 |
Correct |
26 ms |
6732 KB |
Output is correct |
87 |
Correct |
75 ms |
7632 KB |
Output is correct |
88 |
Correct |
227 ms |
10432 KB |
Output is correct |
89 |
Correct |
1094 ms |
20360 KB |
Output is correct |
90 |
Correct |
1061 ms |
20280 KB |
Output is correct |
91 |
Correct |
1097 ms |
20360 KB |
Output is correct |
92 |
Correct |
484 ms |
26492 KB |
Output is correct |
93 |
Correct |
497 ms |
26552 KB |
Output is correct |
94 |
Correct |
497 ms |
26772 KB |
Output is correct |
95 |
Correct |
761 ms |
23428 KB |
Output is correct |
96 |
Correct |
846 ms |
23824 KB |
Output is correct |
97 |
Correct |
759 ms |
23864 KB |
Output is correct |
98 |
Correct |
786 ms |
23812 KB |
Output is correct |
99 |
Correct |
784 ms |
22748 KB |
Output is correct |
100 |
Execution timed out |
2100 ms |
20400 KB |
Time limit exceeded |
101 |
Halted |
0 ms |
0 KB |
- |