#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;
typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;
typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;
#define FOR(i, a, b) for (int i=a; i<(b); i++)
#define F0R(i, a) for (int i=0; i<(a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
const int MOD = 1000000007;
const ll INF = 1e18;
const int MX = 300001;
pl operator+(const pl& l, const pl& r) {
return {l.f+r.f,l.s+r.s};
}
struct dat {
pl st = {0,0};
priority_queue<ll> p;
};
void comb(dat& a, dat& b) {
if (sz(a.p) < sz(b.p)) swap(a.p,b.p);
a.st = a.st+b.st;
while (sz(b.p)) a.p.push(b.p.top()), b.p.pop();
}
int N,M,P[MX],C[MX];
dat tmp[MX];
void finish() {
vl z;
while (sz(tmp[1].p)) {
z.pb(tmp[1].p.top());
tmp[1].p.pop();
}
z.pb(0); reverse(all(z));
ll cans = tmp[1].st.f;
F0R(i,sz(z)-1) {
if (tmp[1].st.s+i >= 0) break;
cans += (z[i+1]-z[i])*(tmp[1].st.s+i);
}
cout << cans;
}
void prin(priority_queue<ll> p) {
while (sz(p)) {
cout << p.top() << " ";
p.pop();
}
cout << "\n----\n\n";
}
void process(int x) {
if (x > N) {
tmp[x].st = {C[x],-1};
tmp[x].p.push(C[x]); tmp[x].p.push(C[x]);
} else {
while (sz(tmp[x].p) > -tmp[x].st.s+1) tmp[x].p.pop();
tmp[x].st.f += C[x];
ll z1 = tmp[x].p.top(); tmp[x].p.pop();
ll z0 = tmp[x].p.top(); tmp[x].p.pop();
tmp[x].p.push(z0+C[x]), tmp[x].p.push(z1+C[x]);
}
/*cout << x << " " << tmp[x].st.f << " " << tmp[x].st.s << "\n";
prin(tmp[x].p);*/
comb(tmp[P[x]],tmp[x]);
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> N >> M;
FOR(i,2,N+M+1) cin >> P[i] >> C[i];
FORd(i,2,N+M+1) process(i);
finish();
}
/* Look for:
* the exact constraints (multiple sets are too slow for n=10^6 :( )
* special cases (n=1?)
* overflow (ll vs int?)
* array bounds
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
14456 KB |
Output is correct |
2 |
Correct |
15 ms |
14456 KB |
Output is correct |
3 |
Correct |
20 ms |
14512 KB |
Output is correct |
4 |
Correct |
15 ms |
14548 KB |
Output is correct |
5 |
Correct |
17 ms |
14680 KB |
Output is correct |
6 |
Correct |
14 ms |
14688 KB |
Output is correct |
7 |
Correct |
20 ms |
14688 KB |
Output is correct |
8 |
Correct |
19 ms |
14688 KB |
Output is correct |
9 |
Correct |
20 ms |
14688 KB |
Output is correct |
10 |
Correct |
22 ms |
14784 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
14784 KB |
Output is correct |
2 |
Correct |
19 ms |
14784 KB |
Output is correct |
3 |
Correct |
18 ms |
14784 KB |
Output is correct |
4 |
Correct |
19 ms |
14784 KB |
Output is correct |
5 |
Correct |
16 ms |
14784 KB |
Output is correct |
6 |
Correct |
16 ms |
14784 KB |
Output is correct |
7 |
Correct |
16 ms |
14792 KB |
Output is correct |
8 |
Correct |
16 ms |
14820 KB |
Output is correct |
9 |
Correct |
17 ms |
14820 KB |
Output is correct |
10 |
Correct |
19 ms |
14820 KB |
Output is correct |
11 |
Correct |
17 ms |
14820 KB |
Output is correct |
12 |
Correct |
19 ms |
14820 KB |
Output is correct |
13 |
Correct |
17 ms |
14860 KB |
Output is correct |
14 |
Correct |
18 ms |
14860 KB |
Output is correct |
15 |
Correct |
19 ms |
14860 KB |
Output is correct |
16 |
Correct |
16 ms |
14860 KB |
Output is correct |
17 |
Correct |
18 ms |
14964 KB |
Output is correct |
18 |
Correct |
18 ms |
14964 KB |
Output is correct |
19 |
Correct |
18 ms |
14964 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
14456 KB |
Output is correct |
2 |
Correct |
15 ms |
14456 KB |
Output is correct |
3 |
Correct |
20 ms |
14512 KB |
Output is correct |
4 |
Correct |
15 ms |
14548 KB |
Output is correct |
5 |
Correct |
17 ms |
14680 KB |
Output is correct |
6 |
Correct |
14 ms |
14688 KB |
Output is correct |
7 |
Correct |
20 ms |
14688 KB |
Output is correct |
8 |
Correct |
19 ms |
14688 KB |
Output is correct |
9 |
Correct |
20 ms |
14688 KB |
Output is correct |
10 |
Correct |
22 ms |
14784 KB |
Output is correct |
11 |
Correct |
15 ms |
14784 KB |
Output is correct |
12 |
Correct |
19 ms |
14784 KB |
Output is correct |
13 |
Correct |
18 ms |
14784 KB |
Output is correct |
14 |
Correct |
19 ms |
14784 KB |
Output is correct |
15 |
Correct |
16 ms |
14784 KB |
Output is correct |
16 |
Correct |
16 ms |
14784 KB |
Output is correct |
17 |
Correct |
16 ms |
14792 KB |
Output is correct |
18 |
Correct |
16 ms |
14820 KB |
Output is correct |
19 |
Correct |
17 ms |
14820 KB |
Output is correct |
20 |
Correct |
19 ms |
14820 KB |
Output is correct |
21 |
Correct |
17 ms |
14820 KB |
Output is correct |
22 |
Correct |
19 ms |
14820 KB |
Output is correct |
23 |
Correct |
17 ms |
14860 KB |
Output is correct |
24 |
Correct |
18 ms |
14860 KB |
Output is correct |
25 |
Correct |
19 ms |
14860 KB |
Output is correct |
26 |
Correct |
16 ms |
14860 KB |
Output is correct |
27 |
Correct |
18 ms |
14964 KB |
Output is correct |
28 |
Correct |
18 ms |
14964 KB |
Output is correct |
29 |
Correct |
18 ms |
14964 KB |
Output is correct |
30 |
Correct |
18 ms |
14964 KB |
Output is correct |
31 |
Correct |
17 ms |
14964 KB |
Output is correct |
32 |
Correct |
19 ms |
15008 KB |
Output is correct |
33 |
Correct |
19 ms |
15144 KB |
Output is correct |
34 |
Correct |
19 ms |
15144 KB |
Output is correct |
35 |
Correct |
20 ms |
15144 KB |
Output is correct |
36 |
Correct |
20 ms |
15340 KB |
Output is correct |
37 |
Correct |
20 ms |
15396 KB |
Output is correct |
38 |
Correct |
23 ms |
15464 KB |
Output is correct |
39 |
Correct |
21 ms |
15600 KB |
Output is correct |
40 |
Correct |
20 ms |
15600 KB |
Output is correct |
41 |
Correct |
23 ms |
15600 KB |
Output is correct |
42 |
Correct |
20 ms |
15688 KB |
Output is correct |
43 |
Correct |
21 ms |
15888 KB |
Output is correct |
44 |
Correct |
19 ms |
15920 KB |
Output is correct |
45 |
Correct |
21 ms |
16032 KB |
Output is correct |
46 |
Correct |
25 ms |
16360 KB |
Output is correct |
47 |
Correct |
22 ms |
16424 KB |
Output is correct |
48 |
Correct |
23 ms |
16488 KB |
Output is correct |
49 |
Correct |
24 ms |
16544 KB |
Output is correct |
50 |
Correct |
23 ms |
16544 KB |
Output is correct |
51 |
Correct |
19 ms |
16544 KB |
Output is correct |
52 |
Correct |
25 ms |
16576 KB |
Output is correct |
53 |
Correct |
24 ms |
16640 KB |
Output is correct |
54 |
Correct |
23 ms |
16836 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
14456 KB |
Output is correct |
2 |
Correct |
15 ms |
14456 KB |
Output is correct |
3 |
Correct |
20 ms |
14512 KB |
Output is correct |
4 |
Correct |
15 ms |
14548 KB |
Output is correct |
5 |
Correct |
17 ms |
14680 KB |
Output is correct |
6 |
Correct |
14 ms |
14688 KB |
Output is correct |
7 |
Correct |
20 ms |
14688 KB |
Output is correct |
8 |
Correct |
19 ms |
14688 KB |
Output is correct |
9 |
Correct |
20 ms |
14688 KB |
Output is correct |
10 |
Correct |
22 ms |
14784 KB |
Output is correct |
11 |
Correct |
15 ms |
14784 KB |
Output is correct |
12 |
Correct |
19 ms |
14784 KB |
Output is correct |
13 |
Correct |
18 ms |
14784 KB |
Output is correct |
14 |
Correct |
19 ms |
14784 KB |
Output is correct |
15 |
Correct |
16 ms |
14784 KB |
Output is correct |
16 |
Correct |
16 ms |
14784 KB |
Output is correct |
17 |
Correct |
16 ms |
14792 KB |
Output is correct |
18 |
Correct |
16 ms |
14820 KB |
Output is correct |
19 |
Correct |
17 ms |
14820 KB |
Output is correct |
20 |
Correct |
19 ms |
14820 KB |
Output is correct |
21 |
Correct |
17 ms |
14820 KB |
Output is correct |
22 |
Correct |
19 ms |
14820 KB |
Output is correct |
23 |
Correct |
17 ms |
14860 KB |
Output is correct |
24 |
Correct |
18 ms |
14860 KB |
Output is correct |
25 |
Correct |
19 ms |
14860 KB |
Output is correct |
26 |
Correct |
16 ms |
14860 KB |
Output is correct |
27 |
Correct |
18 ms |
14964 KB |
Output is correct |
28 |
Correct |
18 ms |
14964 KB |
Output is correct |
29 |
Correct |
18 ms |
14964 KB |
Output is correct |
30 |
Correct |
18 ms |
14964 KB |
Output is correct |
31 |
Correct |
17 ms |
14964 KB |
Output is correct |
32 |
Correct |
19 ms |
15008 KB |
Output is correct |
33 |
Correct |
19 ms |
15144 KB |
Output is correct |
34 |
Correct |
19 ms |
15144 KB |
Output is correct |
35 |
Correct |
20 ms |
15144 KB |
Output is correct |
36 |
Correct |
20 ms |
15340 KB |
Output is correct |
37 |
Correct |
20 ms |
15396 KB |
Output is correct |
38 |
Correct |
23 ms |
15464 KB |
Output is correct |
39 |
Correct |
21 ms |
15600 KB |
Output is correct |
40 |
Correct |
20 ms |
15600 KB |
Output is correct |
41 |
Correct |
23 ms |
15600 KB |
Output is correct |
42 |
Correct |
20 ms |
15688 KB |
Output is correct |
43 |
Correct |
21 ms |
15888 KB |
Output is correct |
44 |
Correct |
19 ms |
15920 KB |
Output is correct |
45 |
Correct |
21 ms |
16032 KB |
Output is correct |
46 |
Correct |
25 ms |
16360 KB |
Output is correct |
47 |
Correct |
22 ms |
16424 KB |
Output is correct |
48 |
Correct |
23 ms |
16488 KB |
Output is correct |
49 |
Correct |
24 ms |
16544 KB |
Output is correct |
50 |
Correct |
23 ms |
16544 KB |
Output is correct |
51 |
Correct |
19 ms |
16544 KB |
Output is correct |
52 |
Correct |
25 ms |
16576 KB |
Output is correct |
53 |
Correct |
24 ms |
16640 KB |
Output is correct |
54 |
Correct |
23 ms |
16836 KB |
Output is correct |
55 |
Correct |
30 ms |
17148 KB |
Output is correct |
56 |
Correct |
58 ms |
19768 KB |
Output is correct |
57 |
Correct |
86 ms |
22516 KB |
Output is correct |
58 |
Correct |
110 ms |
25656 KB |
Output is correct |
59 |
Correct |
156 ms |
29472 KB |
Output is correct |
60 |
Correct |
176 ms |
33984 KB |
Output is correct |
61 |
Correct |
240 ms |
38532 KB |
Output is correct |
62 |
Correct |
254 ms |
43216 KB |
Output is correct |
63 |
Correct |
271 ms |
49568 KB |
Output is correct |
64 |
Correct |
335 ms |
54116 KB |
Output is correct |
65 |
Correct |
126 ms |
54116 KB |
Output is correct |
66 |
Correct |
128 ms |
54116 KB |
Output is correct |
67 |
Correct |
134 ms |
57596 KB |
Output is correct |
68 |
Correct |
270 ms |
71348 KB |
Output is correct |
69 |
Correct |
265 ms |
76712 KB |
Output is correct |
70 |
Correct |
289 ms |
81384 KB |
Output is correct |
71 |
Correct |
331 ms |
103232 KB |
Output is correct |
72 |
Correct |
352 ms |
108120 KB |
Output is correct |
73 |
Correct |
312 ms |
108324 KB |
Output is correct |
74 |
Correct |
294 ms |
112848 KB |
Output is correct |
75 |
Correct |
302 ms |
115180 KB |
Output is correct |
76 |
Correct |
327 ms |
119128 KB |
Output is correct |
77 |
Correct |
294 ms |
122236 KB |
Output is correct |
78 |
Correct |
318 ms |
126196 KB |
Output is correct |
79 |
Correct |
339 ms |
134164 KB |
Output is correct |