#include <bits/stdc++.h>
using namespace std;
#define FAST_IO ios_base::sync_with_stdio(0); cin.tie(0)
#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
#define REP(n) FOR(O, 1, (n))
#define pb push_back
#define f first
#define s second
typedef long double ld;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
typedef vector<int> vi;
typedef vector<pii> vii;
typedef vector<ll> vl;
typedef vector<piii> viii;
const int MAXN = 300100, MAXK = 30;
//const ll MOD = 998244353;
const ll INF = 4e18;
const ld PI = asin(1) * 2;
priority_queue<ll> q[MAXN];
int p[MAXN];
ll c[MAXN];
vi adj[MAXN];
int n, m;
void print (int i) {
cout << " queue i=" << i << ": ";
priority_queue<ll> tmp;
while (!q[i].empty()) {
ll x = q[i].top();
q[i].pop();
tmp.push(x);
cout << " " << x;
}
while (!tmp.empty()) {
ll x = tmp.top();
tmp.pop();
q[i].push(x);
//cout << " " << x;
}
cout << endl;
}
void dfs (int v) {
// cout << " v = " << v << endl;
if (n+1 <= v) {
REP(2) q[v].push(c[v]);
//print(v);
return;
}
for (int u : adj[v]) {
dfs(u);
if ((int)q[u].size() > (int)q[v].size()) swap(q[u], q[v]);
while (!q[u].empty()) {
q[v].push(q[u].top());
q[u].pop();
}
}
int sz = (int)adj[v].size();
REP(sz-1) q[v].pop();
int fr, to;
to = q[v].top();
q[v].pop();
fr = q[v].top();
q[v].pop();
q[v].push(fr+c[v]);
q[v].push(to+c[v]);
// print(v);
}
int main()
{
FAST_IO;
cin >> n >> m;
FOR(i, 2, n+m) {
cin >> p[i] >> c[i];
adj[p[i]].pb(i);
}
/*FOR(i, 1, n+m) {
cout << " i = " << i << " adj: ";
for (int x : adj[i]) cout << x << " ";
cout << endl;
}*/
dfs (1);
ll cur = 0;
FOR(i, 1, n+m) cur += c[i];
q[1].pop();
while (!q[1].empty()) {
cur -= q[1].top();
q[1].pop();
}
cout << cur << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
16716 KB |
Output is correct |
2 |
Correct |
11 ms |
16716 KB |
Output is correct |
3 |
Correct |
13 ms |
16756 KB |
Output is correct |
4 |
Correct |
11 ms |
16764 KB |
Output is correct |
5 |
Correct |
14 ms |
16716 KB |
Output is correct |
6 |
Correct |
13 ms |
16716 KB |
Output is correct |
7 |
Correct |
11 ms |
16708 KB |
Output is correct |
8 |
Correct |
11 ms |
16764 KB |
Output is correct |
9 |
Correct |
14 ms |
16692 KB |
Output is correct |
10 |
Correct |
11 ms |
16664 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
16728 KB |
Output is correct |
2 |
Correct |
11 ms |
16716 KB |
Output is correct |
3 |
Correct |
15 ms |
16716 KB |
Output is correct |
4 |
Correct |
13 ms |
16716 KB |
Output is correct |
5 |
Correct |
13 ms |
16716 KB |
Output is correct |
6 |
Correct |
14 ms |
16760 KB |
Output is correct |
7 |
Correct |
14 ms |
16708 KB |
Output is correct |
8 |
Correct |
13 ms |
16684 KB |
Output is correct |
9 |
Correct |
14 ms |
16760 KB |
Output is correct |
10 |
Correct |
12 ms |
16752 KB |
Output is correct |
11 |
Correct |
11 ms |
16716 KB |
Output is correct |
12 |
Correct |
12 ms |
16716 KB |
Output is correct |
13 |
Correct |
13 ms |
16716 KB |
Output is correct |
14 |
Correct |
14 ms |
16716 KB |
Output is correct |
15 |
Correct |
14 ms |
16716 KB |
Output is correct |
16 |
Correct |
14 ms |
16764 KB |
Output is correct |
17 |
Correct |
14 ms |
16764 KB |
Output is correct |
18 |
Correct |
14 ms |
16716 KB |
Output is correct |
19 |
Correct |
12 ms |
16696 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
16716 KB |
Output is correct |
2 |
Correct |
11 ms |
16716 KB |
Output is correct |
3 |
Correct |
13 ms |
16756 KB |
Output is correct |
4 |
Correct |
11 ms |
16764 KB |
Output is correct |
5 |
Correct |
14 ms |
16716 KB |
Output is correct |
6 |
Correct |
13 ms |
16716 KB |
Output is correct |
7 |
Correct |
11 ms |
16708 KB |
Output is correct |
8 |
Correct |
11 ms |
16764 KB |
Output is correct |
9 |
Correct |
14 ms |
16692 KB |
Output is correct |
10 |
Correct |
11 ms |
16664 KB |
Output is correct |
11 |
Correct |
11 ms |
16728 KB |
Output is correct |
12 |
Correct |
11 ms |
16716 KB |
Output is correct |
13 |
Correct |
15 ms |
16716 KB |
Output is correct |
14 |
Correct |
13 ms |
16716 KB |
Output is correct |
15 |
Correct |
13 ms |
16716 KB |
Output is correct |
16 |
Correct |
14 ms |
16760 KB |
Output is correct |
17 |
Correct |
14 ms |
16708 KB |
Output is correct |
18 |
Correct |
13 ms |
16684 KB |
Output is correct |
19 |
Correct |
14 ms |
16760 KB |
Output is correct |
20 |
Correct |
12 ms |
16752 KB |
Output is correct |
21 |
Correct |
11 ms |
16716 KB |
Output is correct |
22 |
Correct |
12 ms |
16716 KB |
Output is correct |
23 |
Correct |
13 ms |
16716 KB |
Output is correct |
24 |
Correct |
14 ms |
16716 KB |
Output is correct |
25 |
Correct |
14 ms |
16716 KB |
Output is correct |
26 |
Correct |
14 ms |
16764 KB |
Output is correct |
27 |
Correct |
14 ms |
16764 KB |
Output is correct |
28 |
Correct |
14 ms |
16716 KB |
Output is correct |
29 |
Correct |
12 ms |
16696 KB |
Output is correct |
30 |
Incorrect |
14 ms |
16844 KB |
Output isn't correct |
31 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
16716 KB |
Output is correct |
2 |
Correct |
11 ms |
16716 KB |
Output is correct |
3 |
Correct |
13 ms |
16756 KB |
Output is correct |
4 |
Correct |
11 ms |
16764 KB |
Output is correct |
5 |
Correct |
14 ms |
16716 KB |
Output is correct |
6 |
Correct |
13 ms |
16716 KB |
Output is correct |
7 |
Correct |
11 ms |
16708 KB |
Output is correct |
8 |
Correct |
11 ms |
16764 KB |
Output is correct |
9 |
Correct |
14 ms |
16692 KB |
Output is correct |
10 |
Correct |
11 ms |
16664 KB |
Output is correct |
11 |
Correct |
11 ms |
16728 KB |
Output is correct |
12 |
Correct |
11 ms |
16716 KB |
Output is correct |
13 |
Correct |
15 ms |
16716 KB |
Output is correct |
14 |
Correct |
13 ms |
16716 KB |
Output is correct |
15 |
Correct |
13 ms |
16716 KB |
Output is correct |
16 |
Correct |
14 ms |
16760 KB |
Output is correct |
17 |
Correct |
14 ms |
16708 KB |
Output is correct |
18 |
Correct |
13 ms |
16684 KB |
Output is correct |
19 |
Correct |
14 ms |
16760 KB |
Output is correct |
20 |
Correct |
12 ms |
16752 KB |
Output is correct |
21 |
Correct |
11 ms |
16716 KB |
Output is correct |
22 |
Correct |
12 ms |
16716 KB |
Output is correct |
23 |
Correct |
13 ms |
16716 KB |
Output is correct |
24 |
Correct |
14 ms |
16716 KB |
Output is correct |
25 |
Correct |
14 ms |
16716 KB |
Output is correct |
26 |
Correct |
14 ms |
16764 KB |
Output is correct |
27 |
Correct |
14 ms |
16764 KB |
Output is correct |
28 |
Correct |
14 ms |
16716 KB |
Output is correct |
29 |
Correct |
12 ms |
16696 KB |
Output is correct |
30 |
Incorrect |
14 ms |
16844 KB |
Output isn't correct |
31 |
Halted |
0 ms |
0 KB |
- |