#include <bits/stdc++.h>
#ifdef NON_SUBMIT
#define TEST(n) (n)
#define tout cerr
#else
#define TEST(n) ((void)0)
#define tout cin
#endif
using namespace std;
vector<pair<int,int>> adj[300000];
int parent[300000];
tuple<long long,long long,long long> D[300000];
void dfs(int c)
{
vector<long long> x(1);
vector<int> C;
long long A=0, B=0, m=0;
for(auto[n,w]: adj[c]) {
dfs(n);
m=max(m,get<0>(D[n]));
x.push_back(get<0>(D[n])+=w);
x.push_back(get<1>(D[n])+=w);
}
get<2>(D[c])=0x7fffffffffffffffLL;
sort(x.begin(),x.end());
x.resize(unique(x.begin(),x.end())-x.begin());
C.resize(x.size());
for(auto[n,w]: adj[c]) {
auto[l,r,v]=D[n];
A--; B+=v+l;
C[lower_bound(x.begin(),x.end(),l)-x.begin()]++;
C[lower_bound(x.begin(),x.end(),r)-x.begin()]++;
}
for(int i=0;i<x.size();i++) if(x[i]>=m) {
A+=C[i];
B-=x[i]*C[i];
if(A*x[i]+B<get<2>(D[c])) {
get<2>(D[c])=A*x[i]+B;
get<0>(D[c])=get<1>(D[c])=x[i];
}
else if(A*x[i]+B==get<2>(D[c])) get<1>(D[c])=x[i];
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
TEST(freopen("input.txt","r",stdin));
TEST(freopen("output.txt","w",stdout));
TEST(freopen("debug.txt","w",stderr));
int N, M;
cin>>N>>M; N+=M;
for(int i=1;i<N;i++) {
int c;
cin>>parent[i]>>c;
adj[--parent[i]].emplace_back(i,c);
}
dfs(0);
cout<<get<2>(D[0])<<'\n';
return 0;
}
Compilation message
fireworks.cpp: In function 'void dfs(int)':
fireworks.cpp:32:14: warning: unused variable 'w' [-Wunused-variable]
for(auto[n,w]: adj[c]) {
^
fireworks.cpp:38:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<x.size();i++) if(x[i]>=m) {
~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
7424 KB |
Output is correct |
2 |
Correct |
8 ms |
7424 KB |
Output is correct |
3 |
Correct |
9 ms |
7456 KB |
Output is correct |
4 |
Correct |
10 ms |
7424 KB |
Output is correct |
5 |
Correct |
8 ms |
7424 KB |
Output is correct |
6 |
Correct |
9 ms |
7424 KB |
Output is correct |
7 |
Correct |
8 ms |
7424 KB |
Output is correct |
8 |
Correct |
9 ms |
7424 KB |
Output is correct |
9 |
Correct |
8 ms |
7424 KB |
Output is correct |
10 |
Correct |
8 ms |
7424 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
8 ms |
7424 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
7424 KB |
Output is correct |
2 |
Correct |
8 ms |
7424 KB |
Output is correct |
3 |
Correct |
9 ms |
7456 KB |
Output is correct |
4 |
Correct |
10 ms |
7424 KB |
Output is correct |
5 |
Correct |
8 ms |
7424 KB |
Output is correct |
6 |
Correct |
9 ms |
7424 KB |
Output is correct |
7 |
Correct |
8 ms |
7424 KB |
Output is correct |
8 |
Correct |
9 ms |
7424 KB |
Output is correct |
9 |
Correct |
8 ms |
7424 KB |
Output is correct |
10 |
Correct |
8 ms |
7424 KB |
Output is correct |
11 |
Incorrect |
8 ms |
7424 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
7424 KB |
Output is correct |
2 |
Correct |
8 ms |
7424 KB |
Output is correct |
3 |
Correct |
9 ms |
7456 KB |
Output is correct |
4 |
Correct |
10 ms |
7424 KB |
Output is correct |
5 |
Correct |
8 ms |
7424 KB |
Output is correct |
6 |
Correct |
9 ms |
7424 KB |
Output is correct |
7 |
Correct |
8 ms |
7424 KB |
Output is correct |
8 |
Correct |
9 ms |
7424 KB |
Output is correct |
9 |
Correct |
8 ms |
7424 KB |
Output is correct |
10 |
Correct |
8 ms |
7424 KB |
Output is correct |
11 |
Incorrect |
8 ms |
7424 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |