#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template<typename T>
using ordered_set = tree<T, null_type, less < T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T>
using normal_queue = priority_queue <T, vector<T>, greater<>>;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
#define ll long long
#define trace(x) cout << #x << ": " << (x) << endl;
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define uniq(x) x.resize(unique(all(x)) - begin(x))
#define ld long double
#define sz(s) ((int) size(s))
#define pii pair<int, int>
#define mp(x, y) make_pair(x, y)
#define int128 __int128
#define pb push_back
#define eb emplace_back
template<typename T>
bool ckmin(T &x, T y) {
if (x > y) {
x = y;
return true;
}
return false;
}
template<typename T>
bool ckmax(T &x, T y) {
if (x < y) {
x = y;
return true;
}
return false;
}
int bit(int x, int b) {
return (x >> b) & 1;
}
int rand(int l, int r) { return (int) ((ll) rnd() % (r - l + 1)) + l; }
//soryan za musor sverhu
const ll infL = 3e18;
const int infI = 1e9 + 7;
const int N = 300001;
const ll mod = 1e9 + 7;
const ld eps = 1e-9;
priority_queue<ll> pq[N];
int p[N], c[N];
ll k[N], b[N];
//это уравнение прямой при больших X
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
//https://codeforces.com/blog/entry/47821
int n, m;
cin >> n >> m;
auto mrg = [](int i) {
if (sz(pq[i]) > sz(pq[p[i]])) swap(pq[i], pq[p[i]]);
k[p[i]] += k[i];
b[p[i]] += b[i];
while (!pq[i].empty()) {
auto v = pq[i].top();
pq[i].pop();
pq[p[i]].push(v);
}
};
for (int i = 1; i < n + m; ++i) {
cin >> p[i] >> c[i];
--p[i];
}
for (int i = n; i < n + m; ++i) {
k[i] = 1;
b[i] = -c[i];
pq[i].push(c[i]);
pq[i].push(c[i]);
mrg(i);
}
for (int i = n - 1; i > 0; --i) {
//we don't need slopes with k > 1 because we just can increase c[i[ by one all the time
while (k[i] > 1) {
--k[i];
b[i] += pq[i].top(); // y = kx+b = (a-1)x + (b + x) at slope changing point
pq[i].pop();
}
auto R = pq[i].top();
pq[i].pop();
auto L = pq[i].top();
pq[i].pop();
L += c[i];
R += c[i];
//moving slopes with K = 0, 1
b[i] -= c[i];
//we shifted to the right by c[i] and k = 1 so b[i] decreases by c[i]
pq[i].push(L);
pq[i].push(R);
mrg(i);
}
while (k[0] > 0) {
--k[0];
b[0] += pq[0].top();// y = kx+b = (a-1)x + (b + x) at slope changing point
pq[0].pop();
}
cout << b[0];
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9748 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
6 ms |
9816 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Correct |
5 ms |
9728 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9728 KB |
Output is correct |
2 |
Correct |
6 ms |
9684 KB |
Output is correct |
3 |
Correct |
6 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9724 KB |
Output is correct |
5 |
Correct |
5 ms |
9728 KB |
Output is correct |
6 |
Correct |
5 ms |
9732 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Correct |
5 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9660 KB |
Output is correct |
11 |
Correct |
5 ms |
9724 KB |
Output is correct |
12 |
Correct |
5 ms |
9684 KB |
Output is correct |
13 |
Correct |
5 ms |
9684 KB |
Output is correct |
14 |
Correct |
5 ms |
9728 KB |
Output is correct |
15 |
Correct |
5 ms |
9684 KB |
Output is correct |
16 |
Correct |
6 ms |
9728 KB |
Output is correct |
17 |
Correct |
6 ms |
9728 KB |
Output is correct |
18 |
Correct |
5 ms |
9684 KB |
Output is correct |
19 |
Correct |
5 ms |
9708 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9748 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
6 ms |
9816 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Correct |
5 ms |
9728 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
11 |
Correct |
6 ms |
9728 KB |
Output is correct |
12 |
Correct |
6 ms |
9684 KB |
Output is correct |
13 |
Correct |
6 ms |
9684 KB |
Output is correct |
14 |
Correct |
5 ms |
9724 KB |
Output is correct |
15 |
Correct |
5 ms |
9728 KB |
Output is correct |
16 |
Correct |
5 ms |
9732 KB |
Output is correct |
17 |
Correct |
5 ms |
9684 KB |
Output is correct |
18 |
Correct |
5 ms |
9684 KB |
Output is correct |
19 |
Correct |
5 ms |
9684 KB |
Output is correct |
20 |
Correct |
5 ms |
9660 KB |
Output is correct |
21 |
Correct |
5 ms |
9724 KB |
Output is correct |
22 |
Correct |
5 ms |
9684 KB |
Output is correct |
23 |
Correct |
5 ms |
9684 KB |
Output is correct |
24 |
Correct |
5 ms |
9728 KB |
Output is correct |
25 |
Correct |
5 ms |
9684 KB |
Output is correct |
26 |
Correct |
6 ms |
9728 KB |
Output is correct |
27 |
Correct |
6 ms |
9728 KB |
Output is correct |
28 |
Correct |
5 ms |
9684 KB |
Output is correct |
29 |
Correct |
5 ms |
9708 KB |
Output is correct |
30 |
Correct |
6 ms |
9684 KB |
Output is correct |
31 |
Correct |
5 ms |
9812 KB |
Output is correct |
32 |
Correct |
5 ms |
9812 KB |
Output is correct |
33 |
Correct |
6 ms |
9888 KB |
Output is correct |
34 |
Correct |
6 ms |
9868 KB |
Output is correct |
35 |
Correct |
6 ms |
9940 KB |
Output is correct |
36 |
Correct |
7 ms |
9940 KB |
Output is correct |
37 |
Correct |
8 ms |
10068 KB |
Output is correct |
38 |
Correct |
9 ms |
10068 KB |
Output is correct |
39 |
Correct |
7 ms |
9996 KB |
Output is correct |
40 |
Correct |
7 ms |
9940 KB |
Output is correct |
41 |
Correct |
6 ms |
9940 KB |
Output is correct |
42 |
Correct |
6 ms |
9940 KB |
Output is correct |
43 |
Correct |
8 ms |
9992 KB |
Output is correct |
44 |
Correct |
7 ms |
9992 KB |
Output is correct |
45 |
Correct |
7 ms |
10028 KB |
Output is correct |
46 |
Correct |
7 ms |
10224 KB |
Output is correct |
47 |
Correct |
7 ms |
10196 KB |
Output is correct |
48 |
Correct |
8 ms |
10196 KB |
Output is correct |
49 |
Correct |
7 ms |
10248 KB |
Output is correct |
50 |
Correct |
8 ms |
10196 KB |
Output is correct |
51 |
Correct |
7 ms |
10068 KB |
Output is correct |
52 |
Correct |
9 ms |
10196 KB |
Output is correct |
53 |
Correct |
8 ms |
10132 KB |
Output is correct |
54 |
Correct |
7 ms |
10104 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9748 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
6 ms |
9816 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Correct |
5 ms |
9728 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
11 |
Correct |
6 ms |
9728 KB |
Output is correct |
12 |
Correct |
6 ms |
9684 KB |
Output is correct |
13 |
Correct |
6 ms |
9684 KB |
Output is correct |
14 |
Correct |
5 ms |
9724 KB |
Output is correct |
15 |
Correct |
5 ms |
9728 KB |
Output is correct |
16 |
Correct |
5 ms |
9732 KB |
Output is correct |
17 |
Correct |
5 ms |
9684 KB |
Output is correct |
18 |
Correct |
5 ms |
9684 KB |
Output is correct |
19 |
Correct |
5 ms |
9684 KB |
Output is correct |
20 |
Correct |
5 ms |
9660 KB |
Output is correct |
21 |
Correct |
5 ms |
9724 KB |
Output is correct |
22 |
Correct |
5 ms |
9684 KB |
Output is correct |
23 |
Correct |
5 ms |
9684 KB |
Output is correct |
24 |
Correct |
5 ms |
9728 KB |
Output is correct |
25 |
Correct |
5 ms |
9684 KB |
Output is correct |
26 |
Correct |
6 ms |
9728 KB |
Output is correct |
27 |
Correct |
6 ms |
9728 KB |
Output is correct |
28 |
Correct |
5 ms |
9684 KB |
Output is correct |
29 |
Correct |
5 ms |
9708 KB |
Output is correct |
30 |
Correct |
6 ms |
9684 KB |
Output is correct |
31 |
Correct |
5 ms |
9812 KB |
Output is correct |
32 |
Correct |
5 ms |
9812 KB |
Output is correct |
33 |
Correct |
6 ms |
9888 KB |
Output is correct |
34 |
Correct |
6 ms |
9868 KB |
Output is correct |
35 |
Correct |
6 ms |
9940 KB |
Output is correct |
36 |
Correct |
7 ms |
9940 KB |
Output is correct |
37 |
Correct |
8 ms |
10068 KB |
Output is correct |
38 |
Correct |
9 ms |
10068 KB |
Output is correct |
39 |
Correct |
7 ms |
9996 KB |
Output is correct |
40 |
Correct |
7 ms |
9940 KB |
Output is correct |
41 |
Correct |
6 ms |
9940 KB |
Output is correct |
42 |
Correct |
6 ms |
9940 KB |
Output is correct |
43 |
Correct |
8 ms |
9992 KB |
Output is correct |
44 |
Correct |
7 ms |
9992 KB |
Output is correct |
45 |
Correct |
7 ms |
10028 KB |
Output is correct |
46 |
Correct |
7 ms |
10224 KB |
Output is correct |
47 |
Correct |
7 ms |
10196 KB |
Output is correct |
48 |
Correct |
8 ms |
10196 KB |
Output is correct |
49 |
Correct |
7 ms |
10248 KB |
Output is correct |
50 |
Correct |
8 ms |
10196 KB |
Output is correct |
51 |
Correct |
7 ms |
10068 KB |
Output is correct |
52 |
Correct |
9 ms |
10196 KB |
Output is correct |
53 |
Correct |
8 ms |
10132 KB |
Output is correct |
54 |
Correct |
7 ms |
10104 KB |
Output is correct |
55 |
Correct |
11 ms |
10696 KB |
Output is correct |
56 |
Correct |
25 ms |
13524 KB |
Output is correct |
57 |
Correct |
43 ms |
16068 KB |
Output is correct |
58 |
Correct |
62 ms |
17864 KB |
Output is correct |
59 |
Correct |
72 ms |
20512 KB |
Output is correct |
60 |
Correct |
105 ms |
23080 KB |
Output is correct |
61 |
Correct |
93 ms |
25416 KB |
Output is correct |
62 |
Correct |
105 ms |
26848 KB |
Output is correct |
63 |
Correct |
126 ms |
30036 KB |
Output is correct |
64 |
Correct |
151 ms |
31032 KB |
Output is correct |
65 |
Correct |
70 ms |
21604 KB |
Output is correct |
66 |
Correct |
68 ms |
21580 KB |
Output is correct |
67 |
Correct |
66 ms |
21656 KB |
Output is correct |
68 |
Correct |
122 ms |
28328 KB |
Output is correct |
69 |
Correct |
134 ms |
28928 KB |
Output is correct |
70 |
Correct |
125 ms |
28992 KB |
Output is correct |
71 |
Correct |
170 ms |
43684 KB |
Output is correct |
72 |
Correct |
167 ms |
43808 KB |
Output is correct |
73 |
Correct |
147 ms |
40256 KB |
Output is correct |
74 |
Correct |
147 ms |
40764 KB |
Output is correct |
75 |
Correct |
148 ms |
39088 KB |
Output is correct |
76 |
Correct |
146 ms |
39424 KB |
Output is correct |
77 |
Correct |
181 ms |
38832 KB |
Output is correct |
78 |
Correct |
165 ms |
38124 KB |
Output is correct |
79 |
Correct |
124 ms |
36072 KB |
Output is correct |