//#define _GLIBCXX_DEBUG
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
4 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
7 ms |
9684 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 |
7 ms |
9676 KB |
Output is correct |
10 |
Correct |
7 ms |
9684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9812 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
4 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9640 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Correct |
4 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
11 |
Correct |
5 ms |
9684 KB |
Output is correct |
12 |
Correct |
6 ms |
9684 KB |
Output is correct |
13 |
Correct |
7 ms |
9684 KB |
Output is correct |
14 |
Correct |
6 ms |
9684 KB |
Output is correct |
15 |
Correct |
7 ms |
9684 KB |
Output is correct |
16 |
Correct |
5 ms |
9684 KB |
Output is correct |
17 |
Correct |
5 ms |
9684 KB |
Output is correct |
18 |
Correct |
7 ms |
9684 KB |
Output is correct |
19 |
Correct |
5 ms |
9684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
4 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
7 ms |
9684 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 |
7 ms |
9676 KB |
Output is correct |
10 |
Correct |
7 ms |
9684 KB |
Output is correct |
11 |
Correct |
7 ms |
9812 KB |
Output is correct |
12 |
Correct |
5 ms |
9684 KB |
Output is correct |
13 |
Correct |
5 ms |
9684 KB |
Output is correct |
14 |
Correct |
4 ms |
9684 KB |
Output is correct |
15 |
Correct |
5 ms |
9684 KB |
Output is correct |
16 |
Correct |
5 ms |
9640 KB |
Output is correct |
17 |
Correct |
5 ms |
9684 KB |
Output is correct |
18 |
Correct |
5 ms |
9684 KB |
Output is correct |
19 |
Correct |
4 ms |
9684 KB |
Output is correct |
20 |
Correct |
5 ms |
9684 KB |
Output is correct |
21 |
Correct |
5 ms |
9684 KB |
Output is correct |
22 |
Correct |
6 ms |
9684 KB |
Output is correct |
23 |
Correct |
7 ms |
9684 KB |
Output is correct |
24 |
Correct |
6 ms |
9684 KB |
Output is correct |
25 |
Correct |
7 ms |
9684 KB |
Output is correct |
26 |
Correct |
5 ms |
9684 KB |
Output is correct |
27 |
Correct |
5 ms |
9684 KB |
Output is correct |
28 |
Correct |
7 ms |
9684 KB |
Output is correct |
29 |
Correct |
5 ms |
9684 KB |
Output is correct |
30 |
Correct |
5 ms |
9684 KB |
Output is correct |
31 |
Correct |
5 ms |
9684 KB |
Output is correct |
32 |
Correct |
5 ms |
9812 KB |
Output is correct |
33 |
Correct |
5 ms |
9812 KB |
Output is correct |
34 |
Correct |
6 ms |
9812 KB |
Output is correct |
35 |
Correct |
6 ms |
9920 KB |
Output is correct |
36 |
Correct |
6 ms |
9952 KB |
Output is correct |
37 |
Correct |
7 ms |
9940 KB |
Output is correct |
38 |
Correct |
7 ms |
9940 KB |
Output is correct |
39 |
Correct |
7 ms |
10036 KB |
Output is correct |
40 |
Correct |
6 ms |
9812 KB |
Output is correct |
41 |
Correct |
6 ms |
9812 KB |
Output is correct |
42 |
Correct |
6 ms |
9812 KB |
Output is correct |
43 |
Correct |
8 ms |
9940 KB |
Output is correct |
44 |
Correct |
7 ms |
9928 KB |
Output is correct |
45 |
Correct |
9 ms |
9940 KB |
Output is correct |
46 |
Correct |
9 ms |
10196 KB |
Output is correct |
47 |
Correct |
8 ms |
10196 KB |
Output is correct |
48 |
Correct |
9 ms |
10068 KB |
Output is correct |
49 |
Correct |
8 ms |
10068 KB |
Output is correct |
50 |
Correct |
7 ms |
10068 KB |
Output is correct |
51 |
Correct |
8 ms |
10004 KB |
Output is correct |
52 |
Correct |
7 ms |
10068 KB |
Output is correct |
53 |
Correct |
7 ms |
10068 KB |
Output is correct |
54 |
Correct |
7 ms |
10068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
4 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
7 ms |
9684 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 |
7 ms |
9676 KB |
Output is correct |
10 |
Correct |
7 ms |
9684 KB |
Output is correct |
11 |
Correct |
7 ms |
9812 KB |
Output is correct |
12 |
Correct |
5 ms |
9684 KB |
Output is correct |
13 |
Correct |
5 ms |
9684 KB |
Output is correct |
14 |
Correct |
4 ms |
9684 KB |
Output is correct |
15 |
Correct |
5 ms |
9684 KB |
Output is correct |
16 |
Correct |
5 ms |
9640 KB |
Output is correct |
17 |
Correct |
5 ms |
9684 KB |
Output is correct |
18 |
Correct |
5 ms |
9684 KB |
Output is correct |
19 |
Correct |
4 ms |
9684 KB |
Output is correct |
20 |
Correct |
5 ms |
9684 KB |
Output is correct |
21 |
Correct |
5 ms |
9684 KB |
Output is correct |
22 |
Correct |
6 ms |
9684 KB |
Output is correct |
23 |
Correct |
7 ms |
9684 KB |
Output is correct |
24 |
Correct |
6 ms |
9684 KB |
Output is correct |
25 |
Correct |
7 ms |
9684 KB |
Output is correct |
26 |
Correct |
5 ms |
9684 KB |
Output is correct |
27 |
Correct |
5 ms |
9684 KB |
Output is correct |
28 |
Correct |
7 ms |
9684 KB |
Output is correct |
29 |
Correct |
5 ms |
9684 KB |
Output is correct |
30 |
Correct |
5 ms |
9684 KB |
Output is correct |
31 |
Correct |
5 ms |
9684 KB |
Output is correct |
32 |
Correct |
5 ms |
9812 KB |
Output is correct |
33 |
Correct |
5 ms |
9812 KB |
Output is correct |
34 |
Correct |
6 ms |
9812 KB |
Output is correct |
35 |
Correct |
6 ms |
9920 KB |
Output is correct |
36 |
Correct |
6 ms |
9952 KB |
Output is correct |
37 |
Correct |
7 ms |
9940 KB |
Output is correct |
38 |
Correct |
7 ms |
9940 KB |
Output is correct |
39 |
Correct |
7 ms |
10036 KB |
Output is correct |
40 |
Correct |
6 ms |
9812 KB |
Output is correct |
41 |
Correct |
6 ms |
9812 KB |
Output is correct |
42 |
Correct |
6 ms |
9812 KB |
Output is correct |
43 |
Correct |
8 ms |
9940 KB |
Output is correct |
44 |
Correct |
7 ms |
9928 KB |
Output is correct |
45 |
Correct |
9 ms |
9940 KB |
Output is correct |
46 |
Correct |
9 ms |
10196 KB |
Output is correct |
47 |
Correct |
8 ms |
10196 KB |
Output is correct |
48 |
Correct |
9 ms |
10068 KB |
Output is correct |
49 |
Correct |
8 ms |
10068 KB |
Output is correct |
50 |
Correct |
7 ms |
10068 KB |
Output is correct |
51 |
Correct |
8 ms |
10004 KB |
Output is correct |
52 |
Correct |
7 ms |
10068 KB |
Output is correct |
53 |
Correct |
7 ms |
10068 KB |
Output is correct |
54 |
Correct |
7 ms |
10068 KB |
Output is correct |
55 |
Correct |
10 ms |
10452 KB |
Output is correct |
56 |
Correct |
25 ms |
12752 KB |
Output is correct |
57 |
Correct |
45 ms |
14788 KB |
Output is correct |
58 |
Correct |
59 ms |
16240 KB |
Output is correct |
59 |
Correct |
81 ms |
18300 KB |
Output is correct |
60 |
Correct |
89 ms |
20280 KB |
Output is correct |
61 |
Correct |
95 ms |
22184 KB |
Output is correct |
62 |
Correct |
109 ms |
23440 KB |
Output is correct |
63 |
Correct |
137 ms |
25936 KB |
Output is correct |
64 |
Correct |
159 ms |
26532 KB |
Output is correct |
65 |
Correct |
73 ms |
16688 KB |
Output is correct |
66 |
Correct |
66 ms |
16700 KB |
Output is correct |
67 |
Correct |
66 ms |
16728 KB |
Output is correct |
68 |
Correct |
138 ms |
23536 KB |
Output is correct |
69 |
Correct |
132 ms |
24272 KB |
Output is correct |
70 |
Correct |
141 ms |
24192 KB |
Output is correct |
71 |
Correct |
180 ms |
39364 KB |
Output is correct |
72 |
Correct |
180 ms |
39580 KB |
Output is correct |
73 |
Correct |
177 ms |
36364 KB |
Output is correct |
74 |
Correct |
151 ms |
36744 KB |
Output is correct |
75 |
Correct |
149 ms |
35448 KB |
Output is correct |
76 |
Correct |
168 ms |
35364 KB |
Output is correct |
77 |
Correct |
176 ms |
34688 KB |
Output is correct |
78 |
Correct |
163 ms |
34100 KB |
Output is correct |
79 |
Correct |
140 ms |
32588 KB |
Output is correct |