#include <bits/stdc++.h>
#define ll long long
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define pb push_back
#define FIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
using namespace std;
ll nxt() {ll num; cin >> num; return num;}
typedef pair <ll, ll> pii;
const int N = 300007;
int n, m;
ll nag[N];
vector <pii> adj[N];
multiset <ll> dfs(int x) {
if (adj[x].empty()) return {};
multiset <ll> s, tmp;
for (auto it : adj[x]) {
pii xx = {0, 0};
tmp = dfs(it.F);
auto p = tmp.end();
if (!tmp.empty()) --p;
ll nn = nag[it.F];
nag[x] += nn;
//MODIFY
for (int i = tmp.size(); i >= nn; i--, --p) {
if (i == nn + 1) xx.S = *p;
if (i == nn) xx.F = *p;
p = tmp.erase(p);
}
tmp.insert(xx.F + it.S);
tmp.insert(xx.S + it.S);
//COMBINE
if (s.size() < tmp.size()) swap(s, tmp);
for (auto itt : tmp) s.insert(itt);
}
return s;
}
int main () {
cin >> n >> m;
ll poc = 0, nagib = -m, res, la = 0;
for (int i = 2; i <= n+m; i++) {
ll p, c; cin >> p >> c;
poc += c;
adj[p].pb({i, c});
if (i > n) nag[i] = 1;
}
res = poc;
multiset <ll> s = dfs(1);
for (auto it : s) {
poc += nagib * (it - la);
res = min(res, poc);
nagib++;
la = it;
}
cout << res;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7252 KB |
Output is correct |
2 |
Correct |
5 ms |
7252 KB |
Output is correct |
3 |
Correct |
5 ms |
7352 KB |
Output is correct |
4 |
Correct |
4 ms |
7360 KB |
Output is correct |
5 |
Correct |
4 ms |
7252 KB |
Output is correct |
6 |
Correct |
4 ms |
7352 KB |
Output is correct |
7 |
Correct |
4 ms |
7356 KB |
Output is correct |
8 |
Correct |
4 ms |
7252 KB |
Output is correct |
9 |
Correct |
4 ms |
7252 KB |
Output is correct |
10 |
Correct |
4 ms |
7252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
10 ms |
14676 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7252 KB |
Output is correct |
2 |
Correct |
5 ms |
7252 KB |
Output is correct |
3 |
Correct |
5 ms |
7352 KB |
Output is correct |
4 |
Correct |
4 ms |
7360 KB |
Output is correct |
5 |
Correct |
4 ms |
7252 KB |
Output is correct |
6 |
Correct |
4 ms |
7352 KB |
Output is correct |
7 |
Correct |
4 ms |
7356 KB |
Output is correct |
8 |
Correct |
4 ms |
7252 KB |
Output is correct |
9 |
Correct |
4 ms |
7252 KB |
Output is correct |
10 |
Correct |
4 ms |
7252 KB |
Output is correct |
11 |
Runtime error |
10 ms |
14676 KB |
Execution killed with signal 11 |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7252 KB |
Output is correct |
2 |
Correct |
5 ms |
7252 KB |
Output is correct |
3 |
Correct |
5 ms |
7352 KB |
Output is correct |
4 |
Correct |
4 ms |
7360 KB |
Output is correct |
5 |
Correct |
4 ms |
7252 KB |
Output is correct |
6 |
Correct |
4 ms |
7352 KB |
Output is correct |
7 |
Correct |
4 ms |
7356 KB |
Output is correct |
8 |
Correct |
4 ms |
7252 KB |
Output is correct |
9 |
Correct |
4 ms |
7252 KB |
Output is correct |
10 |
Correct |
4 ms |
7252 KB |
Output is correct |
11 |
Runtime error |
10 ms |
14676 KB |
Execution killed with signal 11 |
12 |
Halted |
0 ms |
0 KB |
- |