#include<bits/stdc++.h>
#define pb push_back
#define x first
#define y second
#define all(a) (a).begin(), (a).end()
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
const int maxn = 3e5 + 5;
struct Data {
int idx;
ll lo, hi, ans;
};
int n, m;
ll depth[maxn];
Data dp[maxn];
vector<pair<int, ll>> adj[maxn];
void dfs(int x) {
for(auto p : adj[x])
depth[p.x] = p.y + depth[x], dfs(p.x);
}
Data solve(int x) {
if(adj[x].size() == 0)
return dp[x] = {x, depth[x], depth[x], 0};
vector<Data> ch;
for(auto p : adj[x])
ch.pb(solve(p.x));
int sz = 0;
ll sum_l = 0, sum_r = 0;
int cnt_l = 0, cnt_r = 0;
for(Data D : ch)
sum_r += D.lo, sz++, cnt_r++;
map<ll, vector<int>> L, R;
set<ll> vals;
ll sum = 0;
for(Data D : ch) {
sum += D.ans;
L[D.lo].pb(D.idx);
R[D.hi].pb(D.idx);
vals.insert(D.lo), vals.insert(D.hi);
}
vector<ll> v;
ll ans = 1e18;
for(ll val : vals) {
for(int y : L[val])
sum_r -= dp[y].lo, cnt_r--;
ll ansi = sum_r - sum_l + (ll)(cnt_l - cnt_r) * val;
if(abs(cnt_r - cnt_l) <= sz - cnt_r - cnt_l) {
if(ansi < ans) v.clear(), ans = ansi;
if(ansi == ans) v.pb(val);
}
for(int y : R[val])
sum_l += dp[y].hi, cnt_l++;
}
Data ret;
ret.idx = x;
ret.ans = ans + sum;
ret.lo = v[0];
ret.hi = v.back();
//cout << x + 1 << " -> " << ret.lo << " " << ret.hi << " " << ret.ans << "\n";
return dp[x] = ret;
}
int main() {
scanf("%d%d", &n, &m);
for(int i = 1;i < n + m;i++) {
int p, c;
scanf("%d%d", &p, &c);
p--;
adj[p].pb({i, c});
}
dfs(0);
Data ans = solve(0);
printf("%lld\n", ans.ans);
return 0;
}
Compilation message
fireworks.cpp: In function 'int main()':
fireworks.cpp:80:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
80 | scanf("%d%d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~
fireworks.cpp:83:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
83 | scanf("%d%d", &p, &c);
| ~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7252 KB |
Output is correct |
2 |
Correct |
4 ms |
7252 KB |
Output is correct |
3 |
Correct |
3 ms |
7380 KB |
Output is correct |
4 |
Correct |
4 ms |
7364 KB |
Output is correct |
5 |
Correct |
3 ms |
7380 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
4 ms |
7380 KB |
Output is correct |
8 |
Correct |
4 ms |
7380 KB |
Output is correct |
9 |
Correct |
3 ms |
7352 KB |
Output is correct |
10 |
Correct |
3 ms |
7380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7380 KB |
Output is correct |
2 |
Correct |
3 ms |
7380 KB |
Output is correct |
3 |
Incorrect |
4 ms |
7380 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7252 KB |
Output is correct |
2 |
Correct |
4 ms |
7252 KB |
Output is correct |
3 |
Correct |
3 ms |
7380 KB |
Output is correct |
4 |
Correct |
4 ms |
7364 KB |
Output is correct |
5 |
Correct |
3 ms |
7380 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
4 ms |
7380 KB |
Output is correct |
8 |
Correct |
4 ms |
7380 KB |
Output is correct |
9 |
Correct |
3 ms |
7352 KB |
Output is correct |
10 |
Correct |
3 ms |
7380 KB |
Output is correct |
11 |
Correct |
5 ms |
7380 KB |
Output is correct |
12 |
Correct |
3 ms |
7380 KB |
Output is correct |
13 |
Incorrect |
4 ms |
7380 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7252 KB |
Output is correct |
2 |
Correct |
4 ms |
7252 KB |
Output is correct |
3 |
Correct |
3 ms |
7380 KB |
Output is correct |
4 |
Correct |
4 ms |
7364 KB |
Output is correct |
5 |
Correct |
3 ms |
7380 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
4 ms |
7380 KB |
Output is correct |
8 |
Correct |
4 ms |
7380 KB |
Output is correct |
9 |
Correct |
3 ms |
7352 KB |
Output is correct |
10 |
Correct |
3 ms |
7380 KB |
Output is correct |
11 |
Correct |
5 ms |
7380 KB |
Output is correct |
12 |
Correct |
3 ms |
7380 KB |
Output is correct |
13 |
Incorrect |
4 ms |
7380 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |