#include <bits/stdc++.h>
#define pii pair<int, int>
#define x first
#define y second
using namespace std;
const int N = 1e5 + 10;
long long high[N], slope[N], par[N];
int sz[N], hv[N], n, m;
priority_queue<long long> F;
vector<pii> g[N];
void dfs( int u, int p ) {
pii hh( 0, -1 );
sz[u] = 1;
for( pii v : g[u] ) if( v.x != p ) {
dfs( v.x, u );
par[v.x] = v.y;
sz[u] += sz[v.x];
hh = max( hh, pii( sz[v.x], v.x ) );
}
hv[u] = hh.y;
return ;
}
void sack( int u, int p, priority_queue<long long> &Q ) {
//printf("U : %d P : %d\n",u,p);
if( sz[u] == 1 ) {
high[u] = par[u], slope[u] = -1LL;
Q.emplace( par[u] ), Q.emplace( par[u] );
return ;
}
sack( hv[u], u, Q );
high[u] = high[hv[u]], slope[u] = slope[hv[u]];
priority_queue<long long> S;
for( pii v : g[u] ) if( v.x != p && v.x != hv[u] ) {
sack( v.x, u, S );
high[u] += high[v.x], slope[u] += slope[v.x];
while( !S.empty() ) {
Q.emplace( S.top() );
S.pop();
}
}
if( !p ) {
//printf("%d %d\n",u,p);
vector<long long> v;
while( !Q.empty() ) {
v.emplace_back( Q.top() );
Q.pop();
}
reverse( v.begin(), v.end() );
long long pv = 0, no = 0;
for( int i = 0 ; slope[u] < 0 ; slope[u]++, i++ ) {
no += ( v[i] - pv ) * slope[u];
pv = v[i];
}
printf("%lld\n",high[u]+no);
}
else {
long long a, b;
while( slope[u] + (int)Q.size() >= 0 ) {
swap( a, b );
a = Q.top();
//printf("%lld %lld %d\n",a,b,(int)Q.size());
Q.pop();
}
Q.emplace( a + par[u] ), Q.emplace( b + par[u] );
/*printf("%d %d\nTEMP : ",u,p);
priority_queue<long long> temp = Q;
while( !temp.empty() ) {
printf("%d ",temp.top());
temp.pop();
}
printf("\n");*/
high[u] += par[u];
}
}
int main()
{
scanf("%d %d",&n,&m); n += m;
for( int i = 2, a, b ; i <= n ; i++ ) {
scanf("%d %d",&a,&b);
g[i].emplace_back( pii( a, b ) );
g[a].emplace_back( pii( i, b ) );
}
dfs( 1, 0 );
sack( 1, 0, F );
return 0;
}
Compilation message
fireworks.cpp: In function 'int main()':
fireworks.cpp:84:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&n,&m); n += m;
~~~~~^~~~~~~~~~~~~~~
fireworks.cpp:86:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&a,&b);
~~~~~^~~~~~~~~~~~~~~
fireworks.cpp: In function 'void sack(int, int, std::priority_queue<long long int>&)':
fireworks.cpp:70:47: warning: 'b' may be used uninitialized in this function [-Wmaybe-uninitialized]
Q.emplace( a + par[u] ), Q.emplace( b + par[u] );
~~^~~~~~~~
fireworks.cpp:70:22: warning: 'a' may be used uninitialized in this function [-Wmaybe-uninitialized]
Q.emplace( a + par[u] ), Q.emplace( b + par[u] );
~~^~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
2688 KB |
Output is correct |
2 |
Correct |
6 ms |
2688 KB |
Output is correct |
3 |
Correct |
6 ms |
2688 KB |
Output is correct |
4 |
Correct |
6 ms |
2688 KB |
Output is correct |
5 |
Correct |
6 ms |
2688 KB |
Output is correct |
6 |
Correct |
6 ms |
2688 KB |
Output is correct |
7 |
Correct |
6 ms |
2688 KB |
Output is correct |
8 |
Correct |
6 ms |
2688 KB |
Output is correct |
9 |
Correct |
6 ms |
2688 KB |
Output is correct |
10 |
Correct |
6 ms |
2688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
2688 KB |
Output is correct |
2 |
Correct |
6 ms |
2688 KB |
Output is correct |
3 |
Correct |
6 ms |
2688 KB |
Output is correct |
4 |
Correct |
6 ms |
2688 KB |
Output is correct |
5 |
Correct |
8 ms |
2688 KB |
Output is correct |
6 |
Correct |
6 ms |
2688 KB |
Output is correct |
7 |
Correct |
6 ms |
2688 KB |
Output is correct |
8 |
Correct |
7 ms |
2688 KB |
Output is correct |
9 |
Correct |
6 ms |
2688 KB |
Output is correct |
10 |
Correct |
6 ms |
2688 KB |
Output is correct |
11 |
Correct |
6 ms |
2688 KB |
Output is correct |
12 |
Correct |
6 ms |
2688 KB |
Output is correct |
13 |
Correct |
7 ms |
2688 KB |
Output is correct |
14 |
Correct |
6 ms |
2688 KB |
Output is correct |
15 |
Correct |
6 ms |
2688 KB |
Output is correct |
16 |
Correct |
6 ms |
2688 KB |
Output is correct |
17 |
Correct |
6 ms |
2688 KB |
Output is correct |
18 |
Correct |
6 ms |
2688 KB |
Output is correct |
19 |
Correct |
6 ms |
2688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
2688 KB |
Output is correct |
2 |
Correct |
6 ms |
2688 KB |
Output is correct |
3 |
Correct |
6 ms |
2688 KB |
Output is correct |
4 |
Correct |
6 ms |
2688 KB |
Output is correct |
5 |
Correct |
6 ms |
2688 KB |
Output is correct |
6 |
Correct |
6 ms |
2688 KB |
Output is correct |
7 |
Correct |
6 ms |
2688 KB |
Output is correct |
8 |
Correct |
6 ms |
2688 KB |
Output is correct |
9 |
Correct |
6 ms |
2688 KB |
Output is correct |
10 |
Correct |
6 ms |
2688 KB |
Output is correct |
11 |
Correct |
6 ms |
2688 KB |
Output is correct |
12 |
Correct |
6 ms |
2688 KB |
Output is correct |
13 |
Correct |
6 ms |
2688 KB |
Output is correct |
14 |
Correct |
6 ms |
2688 KB |
Output is correct |
15 |
Correct |
8 ms |
2688 KB |
Output is correct |
16 |
Correct |
6 ms |
2688 KB |
Output is correct |
17 |
Correct |
6 ms |
2688 KB |
Output is correct |
18 |
Correct |
7 ms |
2688 KB |
Output is correct |
19 |
Correct |
6 ms |
2688 KB |
Output is correct |
20 |
Correct |
6 ms |
2688 KB |
Output is correct |
21 |
Correct |
6 ms |
2688 KB |
Output is correct |
22 |
Correct |
6 ms |
2688 KB |
Output is correct |
23 |
Correct |
7 ms |
2688 KB |
Output is correct |
24 |
Correct |
6 ms |
2688 KB |
Output is correct |
25 |
Correct |
6 ms |
2688 KB |
Output is correct |
26 |
Correct |
6 ms |
2688 KB |
Output is correct |
27 |
Correct |
6 ms |
2688 KB |
Output is correct |
28 |
Correct |
6 ms |
2688 KB |
Output is correct |
29 |
Correct |
6 ms |
2688 KB |
Output is correct |
30 |
Correct |
6 ms |
2688 KB |
Output is correct |
31 |
Correct |
7 ms |
2816 KB |
Output is correct |
32 |
Correct |
7 ms |
2816 KB |
Output is correct |
33 |
Correct |
8 ms |
2944 KB |
Output is correct |
34 |
Correct |
8 ms |
2944 KB |
Output is correct |
35 |
Correct |
8 ms |
3072 KB |
Output is correct |
36 |
Correct |
9 ms |
3072 KB |
Output is correct |
37 |
Correct |
10 ms |
3072 KB |
Output is correct |
38 |
Correct |
10 ms |
3200 KB |
Output is correct |
39 |
Correct |
10 ms |
3200 KB |
Output is correct |
40 |
Correct |
9 ms |
3968 KB |
Output is correct |
41 |
Correct |
9 ms |
3968 KB |
Output is correct |
42 |
Correct |
8 ms |
3072 KB |
Output is correct |
43 |
Correct |
10 ms |
3584 KB |
Output is correct |
44 |
Correct |
9 ms |
3456 KB |
Output is correct |
45 |
Correct |
10 ms |
3456 KB |
Output is correct |
46 |
Correct |
10 ms |
3328 KB |
Output is correct |
47 |
Correct |
9 ms |
3328 KB |
Output is correct |
48 |
Correct |
9 ms |
3200 KB |
Output is correct |
49 |
Correct |
11 ms |
3328 KB |
Output is correct |
50 |
Correct |
9 ms |
3200 KB |
Output is correct |
51 |
Correct |
9 ms |
3200 KB |
Output is correct |
52 |
Correct |
9 ms |
3280 KB |
Output is correct |
53 |
Correct |
9 ms |
3200 KB |
Output is correct |
54 |
Correct |
9 ms |
3456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
2688 KB |
Output is correct |
2 |
Correct |
6 ms |
2688 KB |
Output is correct |
3 |
Correct |
6 ms |
2688 KB |
Output is correct |
4 |
Correct |
6 ms |
2688 KB |
Output is correct |
5 |
Correct |
6 ms |
2688 KB |
Output is correct |
6 |
Correct |
6 ms |
2688 KB |
Output is correct |
7 |
Correct |
6 ms |
2688 KB |
Output is correct |
8 |
Correct |
6 ms |
2688 KB |
Output is correct |
9 |
Correct |
6 ms |
2688 KB |
Output is correct |
10 |
Correct |
6 ms |
2688 KB |
Output is correct |
11 |
Correct |
6 ms |
2688 KB |
Output is correct |
12 |
Correct |
6 ms |
2688 KB |
Output is correct |
13 |
Correct |
6 ms |
2688 KB |
Output is correct |
14 |
Correct |
6 ms |
2688 KB |
Output is correct |
15 |
Correct |
8 ms |
2688 KB |
Output is correct |
16 |
Correct |
6 ms |
2688 KB |
Output is correct |
17 |
Correct |
6 ms |
2688 KB |
Output is correct |
18 |
Correct |
7 ms |
2688 KB |
Output is correct |
19 |
Correct |
6 ms |
2688 KB |
Output is correct |
20 |
Correct |
6 ms |
2688 KB |
Output is correct |
21 |
Correct |
6 ms |
2688 KB |
Output is correct |
22 |
Correct |
6 ms |
2688 KB |
Output is correct |
23 |
Correct |
7 ms |
2688 KB |
Output is correct |
24 |
Correct |
6 ms |
2688 KB |
Output is correct |
25 |
Correct |
6 ms |
2688 KB |
Output is correct |
26 |
Correct |
6 ms |
2688 KB |
Output is correct |
27 |
Correct |
6 ms |
2688 KB |
Output is correct |
28 |
Correct |
6 ms |
2688 KB |
Output is correct |
29 |
Correct |
6 ms |
2688 KB |
Output is correct |
30 |
Correct |
6 ms |
2688 KB |
Output is correct |
31 |
Correct |
7 ms |
2816 KB |
Output is correct |
32 |
Correct |
7 ms |
2816 KB |
Output is correct |
33 |
Correct |
8 ms |
2944 KB |
Output is correct |
34 |
Correct |
8 ms |
2944 KB |
Output is correct |
35 |
Correct |
8 ms |
3072 KB |
Output is correct |
36 |
Correct |
9 ms |
3072 KB |
Output is correct |
37 |
Correct |
10 ms |
3072 KB |
Output is correct |
38 |
Correct |
10 ms |
3200 KB |
Output is correct |
39 |
Correct |
10 ms |
3200 KB |
Output is correct |
40 |
Correct |
9 ms |
3968 KB |
Output is correct |
41 |
Correct |
9 ms |
3968 KB |
Output is correct |
42 |
Correct |
8 ms |
3072 KB |
Output is correct |
43 |
Correct |
10 ms |
3584 KB |
Output is correct |
44 |
Correct |
9 ms |
3456 KB |
Output is correct |
45 |
Correct |
10 ms |
3456 KB |
Output is correct |
46 |
Correct |
10 ms |
3328 KB |
Output is correct |
47 |
Correct |
9 ms |
3328 KB |
Output is correct |
48 |
Correct |
9 ms |
3200 KB |
Output is correct |
49 |
Correct |
11 ms |
3328 KB |
Output is correct |
50 |
Correct |
9 ms |
3200 KB |
Output is correct |
51 |
Correct |
9 ms |
3200 KB |
Output is correct |
52 |
Correct |
9 ms |
3280 KB |
Output is correct |
53 |
Correct |
9 ms |
3200 KB |
Output is correct |
54 |
Correct |
9 ms |
3456 KB |
Output is correct |
55 |
Correct |
16 ms |
3968 KB |
Output is correct |
56 |
Correct |
50 ms |
7380 KB |
Output is correct |
57 |
Correct |
86 ms |
10588 KB |
Output is correct |
58 |
Runtime error |
60 ms |
14200 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
59 |
Halted |
0 ms |
0 KB |
- |