/* code by the cute PixelCat owo */
/* as cute as nacho neko (aka. my wife)! */
//#pragma GCC optimize("O4,unroll-loops,no-stack-protector")
#include <bits/stdc++.h>
#define int LL //__int128
#define double long double
using namespace std;
using LL = long long;
using uLL = unsigned long long;
using pii = pair<int, int>;
#define For(i, a, b) for (int i = a; i <= b; i++)
#define Fors(i, a, b, s) for (int i = a; i <= b; i += s)
#define Forr(i, a, b) for (int i = a; i >= b; i--)
#define F first
#define S second
#define L(id) (id * 2 + 1)
#define R(id) (id * 2 + 2)
#define LO(x) (x & (-x))
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define sz(x) ((int)x.size())
#define mkp make_pair
#define MOD (int)(1000000007)
#define INF (int)(1000000000000000ll) // 1e15
#define EPS (1e-6)
#ifdef NYAOWO
#include "debug.h"
inline void USACO(const string &s) {
cerr << "USACO: " << s << "\n";
}
#else
#define debug(...)
inline void timer() {}
inline void USACO(const string &s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
#endif
inline void NYA() {
ios::sync_with_stdio(false);
cin.tie(0);
}
inline int gcd(int a, int b) {
return __gcd(a, b);
}
inline int lcm(int a, int b) {
return a / gcd(a, b) * b;
}
int fpow(int b, int p, const int &mod) {
int ans = 1;
while (p) {
if (p & 1) ans = ans * b % mod;
p /= 2;
b = b * b % mod;
}
return ans;
}
template <typename T>
inline void chmin(T &_a, const T &_b) {
if (_b < _a) _a = _b;
}
template <typename T>
inline void chmax(T &_a, const T &_b) {
if (_b > _a) _a = _b;
}
// mt19937_64
// rng(chrono::steady_clock::now().time_since_epoch().count());
struct Node {
int a, b;
priority_queue<int> pq;
Node() : a(0), b(0), pq() {}
void merge(Node &nd) {
a += nd.a;
b += nd.b;
if (sz(pq) < sz(nd.pq)) pq.swap(nd.pq);
while (sz(nd.pq)) {
pq.emplace(nd.pq.top());
nd.pq.pop();
}
}
void pop(int tar) {
while (a > tar) {
a--;
b += pq.top();
pq.pop();
}
}
} nd[300030];
int p[300030];
int c[300030];
int32_t main() {
NYA();
// nyaacho >/////<
// miku sama bless me >/////<
int n, m;
cin >> n >> m;
For(i, 2, n + m) cin >> p[i] >> c[i];
Forr(i, n + m, n + 1) {
nd[i].a = 1;
nd[i].b = -c[i];
nd[i].pq.emplace(c[i]);
nd[i].pq.emplace(c[i]);
nd[p[i]].merge(nd[i]);
}
Forr(i, n, 2) {
nd[i].pop(1);
int x1 = nd[i].pq.top();
nd[i].pq.pop();
int x2 = nd[i].pq.top();
nd[i].pq.pop();
nd[i].pq.emplace(x1 + c[i]);
nd[i].pq.emplace(x2 + c[i]);
nd[i].b -= c[i];
nd[p[i]].merge(nd[i]);
}
nd[1].pop(0);
cout << nd[1].b << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
14408 KB |
Output is correct |
2 |
Correct |
11 ms |
14412 KB |
Output is correct |
3 |
Correct |
11 ms |
14416 KB |
Output is correct |
4 |
Correct |
11 ms |
14684 KB |
Output is correct |
5 |
Correct |
11 ms |
14392 KB |
Output is correct |
6 |
Correct |
12 ms |
14464 KB |
Output is correct |
7 |
Correct |
10 ms |
14372 KB |
Output is correct |
8 |
Correct |
10 ms |
14392 KB |
Output is correct |
9 |
Correct |
12 ms |
14464 KB |
Output is correct |
10 |
Correct |
11 ms |
14464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
14412 KB |
Output is correct |
2 |
Correct |
9 ms |
14412 KB |
Output is correct |
3 |
Correct |
10 ms |
14548 KB |
Output is correct |
4 |
Correct |
13 ms |
14364 KB |
Output is correct |
5 |
Correct |
11 ms |
14412 KB |
Output is correct |
6 |
Correct |
11 ms |
14452 KB |
Output is correct |
7 |
Correct |
9 ms |
14424 KB |
Output is correct |
8 |
Correct |
11 ms |
14464 KB |
Output is correct |
9 |
Correct |
11 ms |
14424 KB |
Output is correct |
10 |
Correct |
11 ms |
14420 KB |
Output is correct |
11 |
Correct |
10 ms |
14416 KB |
Output is correct |
12 |
Correct |
8 ms |
14412 KB |
Output is correct |
13 |
Correct |
11 ms |
14516 KB |
Output is correct |
14 |
Correct |
9 ms |
14416 KB |
Output is correct |
15 |
Correct |
11 ms |
14340 KB |
Output is correct |
16 |
Correct |
12 ms |
14440 KB |
Output is correct |
17 |
Correct |
9 ms |
14420 KB |
Output is correct |
18 |
Correct |
12 ms |
14412 KB |
Output is correct |
19 |
Correct |
11 ms |
14416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
14408 KB |
Output is correct |
2 |
Correct |
11 ms |
14412 KB |
Output is correct |
3 |
Correct |
11 ms |
14416 KB |
Output is correct |
4 |
Correct |
11 ms |
14684 KB |
Output is correct |
5 |
Correct |
11 ms |
14392 KB |
Output is correct |
6 |
Correct |
12 ms |
14464 KB |
Output is correct |
7 |
Correct |
10 ms |
14372 KB |
Output is correct |
8 |
Correct |
10 ms |
14392 KB |
Output is correct |
9 |
Correct |
12 ms |
14464 KB |
Output is correct |
10 |
Correct |
11 ms |
14464 KB |
Output is correct |
11 |
Correct |
9 ms |
14412 KB |
Output is correct |
12 |
Correct |
9 ms |
14412 KB |
Output is correct |
13 |
Correct |
10 ms |
14548 KB |
Output is correct |
14 |
Correct |
13 ms |
14364 KB |
Output is correct |
15 |
Correct |
11 ms |
14412 KB |
Output is correct |
16 |
Correct |
11 ms |
14452 KB |
Output is correct |
17 |
Correct |
9 ms |
14424 KB |
Output is correct |
18 |
Correct |
11 ms |
14464 KB |
Output is correct |
19 |
Correct |
11 ms |
14424 KB |
Output is correct |
20 |
Correct |
11 ms |
14420 KB |
Output is correct |
21 |
Correct |
10 ms |
14416 KB |
Output is correct |
22 |
Correct |
8 ms |
14412 KB |
Output is correct |
23 |
Correct |
11 ms |
14516 KB |
Output is correct |
24 |
Correct |
9 ms |
14416 KB |
Output is correct |
25 |
Correct |
11 ms |
14340 KB |
Output is correct |
26 |
Correct |
12 ms |
14440 KB |
Output is correct |
27 |
Correct |
9 ms |
14420 KB |
Output is correct |
28 |
Correct |
12 ms |
14412 KB |
Output is correct |
29 |
Correct |
11 ms |
14416 KB |
Output is correct |
30 |
Correct |
11 ms |
14368 KB |
Output is correct |
31 |
Correct |
11 ms |
14544 KB |
Output is correct |
32 |
Correct |
10 ms |
14424 KB |
Output is correct |
33 |
Correct |
12 ms |
14552 KB |
Output is correct |
34 |
Correct |
11 ms |
14556 KB |
Output is correct |
35 |
Correct |
13 ms |
14684 KB |
Output is correct |
36 |
Correct |
11 ms |
14688 KB |
Output is correct |
37 |
Correct |
14 ms |
14740 KB |
Output is correct |
38 |
Correct |
14 ms |
14684 KB |
Output is correct |
39 |
Correct |
12 ms |
14656 KB |
Output is correct |
40 |
Correct |
8 ms |
14528 KB |
Output is correct |
41 |
Correct |
10 ms |
14596 KB |
Output is correct |
42 |
Correct |
10 ms |
14564 KB |
Output is correct |
43 |
Correct |
10 ms |
14692 KB |
Output is correct |
44 |
Correct |
13 ms |
14748 KB |
Output is correct |
45 |
Correct |
11 ms |
14692 KB |
Output is correct |
46 |
Correct |
13 ms |
14940 KB |
Output is correct |
47 |
Correct |
14 ms |
15068 KB |
Output is correct |
48 |
Correct |
12 ms |
14812 KB |
Output is correct |
49 |
Correct |
17 ms |
14812 KB |
Output is correct |
50 |
Correct |
13 ms |
14692 KB |
Output is correct |
51 |
Correct |
13 ms |
14948 KB |
Output is correct |
52 |
Correct |
13 ms |
14812 KB |
Output is correct |
53 |
Correct |
12 ms |
14812 KB |
Output is correct |
54 |
Correct |
12 ms |
14812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
14408 KB |
Output is correct |
2 |
Correct |
11 ms |
14412 KB |
Output is correct |
3 |
Correct |
11 ms |
14416 KB |
Output is correct |
4 |
Correct |
11 ms |
14684 KB |
Output is correct |
5 |
Correct |
11 ms |
14392 KB |
Output is correct |
6 |
Correct |
12 ms |
14464 KB |
Output is correct |
7 |
Correct |
10 ms |
14372 KB |
Output is correct |
8 |
Correct |
10 ms |
14392 KB |
Output is correct |
9 |
Correct |
12 ms |
14464 KB |
Output is correct |
10 |
Correct |
11 ms |
14464 KB |
Output is correct |
11 |
Correct |
9 ms |
14412 KB |
Output is correct |
12 |
Correct |
9 ms |
14412 KB |
Output is correct |
13 |
Correct |
10 ms |
14548 KB |
Output is correct |
14 |
Correct |
13 ms |
14364 KB |
Output is correct |
15 |
Correct |
11 ms |
14412 KB |
Output is correct |
16 |
Correct |
11 ms |
14452 KB |
Output is correct |
17 |
Correct |
9 ms |
14424 KB |
Output is correct |
18 |
Correct |
11 ms |
14464 KB |
Output is correct |
19 |
Correct |
11 ms |
14424 KB |
Output is correct |
20 |
Correct |
11 ms |
14420 KB |
Output is correct |
21 |
Correct |
10 ms |
14416 KB |
Output is correct |
22 |
Correct |
8 ms |
14412 KB |
Output is correct |
23 |
Correct |
11 ms |
14516 KB |
Output is correct |
24 |
Correct |
9 ms |
14416 KB |
Output is correct |
25 |
Correct |
11 ms |
14340 KB |
Output is correct |
26 |
Correct |
12 ms |
14440 KB |
Output is correct |
27 |
Correct |
9 ms |
14420 KB |
Output is correct |
28 |
Correct |
12 ms |
14412 KB |
Output is correct |
29 |
Correct |
11 ms |
14416 KB |
Output is correct |
30 |
Correct |
11 ms |
14368 KB |
Output is correct |
31 |
Correct |
11 ms |
14544 KB |
Output is correct |
32 |
Correct |
10 ms |
14424 KB |
Output is correct |
33 |
Correct |
12 ms |
14552 KB |
Output is correct |
34 |
Correct |
11 ms |
14556 KB |
Output is correct |
35 |
Correct |
13 ms |
14684 KB |
Output is correct |
36 |
Correct |
11 ms |
14688 KB |
Output is correct |
37 |
Correct |
14 ms |
14740 KB |
Output is correct |
38 |
Correct |
14 ms |
14684 KB |
Output is correct |
39 |
Correct |
12 ms |
14656 KB |
Output is correct |
40 |
Correct |
8 ms |
14528 KB |
Output is correct |
41 |
Correct |
10 ms |
14596 KB |
Output is correct |
42 |
Correct |
10 ms |
14564 KB |
Output is correct |
43 |
Correct |
10 ms |
14692 KB |
Output is correct |
44 |
Correct |
13 ms |
14748 KB |
Output is correct |
45 |
Correct |
11 ms |
14692 KB |
Output is correct |
46 |
Correct |
13 ms |
14940 KB |
Output is correct |
47 |
Correct |
14 ms |
15068 KB |
Output is correct |
48 |
Correct |
12 ms |
14812 KB |
Output is correct |
49 |
Correct |
17 ms |
14812 KB |
Output is correct |
50 |
Correct |
13 ms |
14692 KB |
Output is correct |
51 |
Correct |
13 ms |
14948 KB |
Output is correct |
52 |
Correct |
13 ms |
14812 KB |
Output is correct |
53 |
Correct |
12 ms |
14812 KB |
Output is correct |
54 |
Correct |
12 ms |
14812 KB |
Output is correct |
55 |
Correct |
18 ms |
15252 KB |
Output is correct |
56 |
Correct |
33 ms |
17740 KB |
Output is correct |
57 |
Correct |
54 ms |
20156 KB |
Output is correct |
58 |
Correct |
67 ms |
21816 KB |
Output is correct |
59 |
Correct |
85 ms |
24128 KB |
Output is correct |
60 |
Correct |
100 ms |
26408 KB |
Output is correct |
61 |
Correct |
119 ms |
28484 KB |
Output is correct |
62 |
Correct |
141 ms |
29804 KB |
Output is correct |
63 |
Correct |
156 ms |
32648 KB |
Output is correct |
64 |
Correct |
175 ms |
33500 KB |
Output is correct |
65 |
Correct |
103 ms |
23852 KB |
Output is correct |
66 |
Correct |
83 ms |
23848 KB |
Output is correct |
67 |
Correct |
90 ms |
24000 KB |
Output is correct |
68 |
Correct |
174 ms |
30688 KB |
Output is correct |
69 |
Correct |
186 ms |
31232 KB |
Output is correct |
70 |
Correct |
173 ms |
31324 KB |
Output is correct |
71 |
Correct |
233 ms |
45904 KB |
Output is correct |
72 |
Correct |
239 ms |
46104 KB |
Output is correct |
73 |
Correct |
211 ms |
42584 KB |
Output is correct |
74 |
Correct |
229 ms |
43068 KB |
Output is correct |
75 |
Correct |
227 ms |
41580 KB |
Output is correct |
76 |
Correct |
205 ms |
41644 KB |
Output is correct |
77 |
Correct |
218 ms |
41064 KB |
Output is correct |
78 |
Correct |
216 ms |
40624 KB |
Output is correct |
79 |
Correct |
179 ms |
38888 KB |
Output is correct |