#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();
}*/
q[1].pop(); ll a=q[1].top(); q[1].push(0); ll b=0;
while(!q[1].empty()){
cur-=(a-q[1].top())*b++;
a=q[1].top(); q[1].pop();
}
cout << cur << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
16716 KB |
Output is correct |
2 |
Correct |
13 ms |
16716 KB |
Output is correct |
3 |
Correct |
14 ms |
16680 KB |
Output is correct |
4 |
Correct |
13 ms |
16716 KB |
Output is correct |
5 |
Correct |
11 ms |
16772 KB |
Output is correct |
6 |
Correct |
11 ms |
16716 KB |
Output is correct |
7 |
Correct |
11 ms |
16716 KB |
Output is correct |
8 |
Correct |
11 ms |
16716 KB |
Output is correct |
9 |
Correct |
12 ms |
16676 KB |
Output is correct |
10 |
Correct |
11 ms |
16776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
16656 KB |
Output is correct |
2 |
Correct |
11 ms |
16716 KB |
Output is correct |
3 |
Correct |
12 ms |
16716 KB |
Output is correct |
4 |
Correct |
12 ms |
16744 KB |
Output is correct |
5 |
Correct |
14 ms |
16716 KB |
Output is correct |
6 |
Correct |
14 ms |
16732 KB |
Output is correct |
7 |
Correct |
11 ms |
16720 KB |
Output is correct |
8 |
Correct |
13 ms |
16716 KB |
Output is correct |
9 |
Correct |
11 ms |
16716 KB |
Output is correct |
10 |
Correct |
13 ms |
16716 KB |
Output is correct |
11 |
Correct |
13 ms |
16716 KB |
Output is correct |
12 |
Correct |
12 ms |
16736 KB |
Output is correct |
13 |
Correct |
11 ms |
16716 KB |
Output is correct |
14 |
Correct |
13 ms |
16700 KB |
Output is correct |
15 |
Correct |
12 ms |
16716 KB |
Output is correct |
16 |
Correct |
12 ms |
16788 KB |
Output is correct |
17 |
Correct |
11 ms |
16788 KB |
Output is correct |
18 |
Correct |
14 ms |
16724 KB |
Output is correct |
19 |
Correct |
12 ms |
16716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
16716 KB |
Output is correct |
2 |
Correct |
13 ms |
16716 KB |
Output is correct |
3 |
Correct |
14 ms |
16680 KB |
Output is correct |
4 |
Correct |
13 ms |
16716 KB |
Output is correct |
5 |
Correct |
11 ms |
16772 KB |
Output is correct |
6 |
Correct |
11 ms |
16716 KB |
Output is correct |
7 |
Correct |
11 ms |
16716 KB |
Output is correct |
8 |
Correct |
11 ms |
16716 KB |
Output is correct |
9 |
Correct |
12 ms |
16676 KB |
Output is correct |
10 |
Correct |
11 ms |
16776 KB |
Output is correct |
11 |
Correct |
13 ms |
16656 KB |
Output is correct |
12 |
Correct |
11 ms |
16716 KB |
Output is correct |
13 |
Correct |
12 ms |
16716 KB |
Output is correct |
14 |
Correct |
12 ms |
16744 KB |
Output is correct |
15 |
Correct |
14 ms |
16716 KB |
Output is correct |
16 |
Correct |
14 ms |
16732 KB |
Output is correct |
17 |
Correct |
11 ms |
16720 KB |
Output is correct |
18 |
Correct |
13 ms |
16716 KB |
Output is correct |
19 |
Correct |
11 ms |
16716 KB |
Output is correct |
20 |
Correct |
13 ms |
16716 KB |
Output is correct |
21 |
Correct |
13 ms |
16716 KB |
Output is correct |
22 |
Correct |
12 ms |
16736 KB |
Output is correct |
23 |
Correct |
11 ms |
16716 KB |
Output is correct |
24 |
Correct |
13 ms |
16700 KB |
Output is correct |
25 |
Correct |
12 ms |
16716 KB |
Output is correct |
26 |
Correct |
12 ms |
16788 KB |
Output is correct |
27 |
Correct |
11 ms |
16788 KB |
Output is correct |
28 |
Correct |
14 ms |
16724 KB |
Output is correct |
29 |
Correct |
12 ms |
16716 KB |
Output is correct |
30 |
Incorrect |
11 ms |
16800 KB |
Output isn't correct |
31 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
16716 KB |
Output is correct |
2 |
Correct |
13 ms |
16716 KB |
Output is correct |
3 |
Correct |
14 ms |
16680 KB |
Output is correct |
4 |
Correct |
13 ms |
16716 KB |
Output is correct |
5 |
Correct |
11 ms |
16772 KB |
Output is correct |
6 |
Correct |
11 ms |
16716 KB |
Output is correct |
7 |
Correct |
11 ms |
16716 KB |
Output is correct |
8 |
Correct |
11 ms |
16716 KB |
Output is correct |
9 |
Correct |
12 ms |
16676 KB |
Output is correct |
10 |
Correct |
11 ms |
16776 KB |
Output is correct |
11 |
Correct |
13 ms |
16656 KB |
Output is correct |
12 |
Correct |
11 ms |
16716 KB |
Output is correct |
13 |
Correct |
12 ms |
16716 KB |
Output is correct |
14 |
Correct |
12 ms |
16744 KB |
Output is correct |
15 |
Correct |
14 ms |
16716 KB |
Output is correct |
16 |
Correct |
14 ms |
16732 KB |
Output is correct |
17 |
Correct |
11 ms |
16720 KB |
Output is correct |
18 |
Correct |
13 ms |
16716 KB |
Output is correct |
19 |
Correct |
11 ms |
16716 KB |
Output is correct |
20 |
Correct |
13 ms |
16716 KB |
Output is correct |
21 |
Correct |
13 ms |
16716 KB |
Output is correct |
22 |
Correct |
12 ms |
16736 KB |
Output is correct |
23 |
Correct |
11 ms |
16716 KB |
Output is correct |
24 |
Correct |
13 ms |
16700 KB |
Output is correct |
25 |
Correct |
12 ms |
16716 KB |
Output is correct |
26 |
Correct |
12 ms |
16788 KB |
Output is correct |
27 |
Correct |
11 ms |
16788 KB |
Output is correct |
28 |
Correct |
14 ms |
16724 KB |
Output is correct |
29 |
Correct |
12 ms |
16716 KB |
Output is correct |
30 |
Incorrect |
11 ms |
16800 KB |
Output isn't correct |
31 |
Halted |
0 ms |
0 KB |
- |