#include <iostream>
#include <vector>
#include <algorithm>
#include <assert.h>
#include <queue>
#define pub push_back
#define ll long long
#define vi vector<ll>
#define pii pair<int, int>
#define fst first
#define snd second
#define vpi vector<pii>
using namespace std;
const ll MX = 1e15;
int n, m;
vector<vpi> adj;
ll dp[301][301] = {}, res = MX, prec[301] = {};
struct slopeDS
{
priority_queue<ll> pqL;
priority_queue<ll, vi, greater<ll>> pqR;
ll sft = 0, opt = 0;
slopeDS(int x)
{
pqL.push(0); pqL.push(x);
pqR.push(x); pqR.push(MX);
}
ll getL() {return pqL.top();}
ll getR() {return pqR.top() + sft;}
void pushL(ll val) {pqL.push(val);}
void pushR(ll val) {pqR.push(val - sft);}
void popL() {pqL.pop();}
void popR() {pqR.pop();}
int getSZ() {return pqL.size() + pqR.size();}
void debugOutput(int id)
{
priority_queue<ll> cpqL = pqL;
priority_queue<ll, vi, greater<ll>> cpqR = pqR;
cout << "-------------------------------------------------------------------------\n";
cout << "Slope DS ID : " << id << "\n";
cout << "Left Slopes : ";
while (cpqL.size()) {cout << cpqL.top() << " "; cpqL.pop();}
cout << "\n";
cout << "Right Slopes : ";
while (cpqR.size()) {cout << cpqR.top() + sft << " "; cpqR.pop();}
cout << "\n";
cout << "OPT VAL : " << opt << "\n";
cout << "-------------------------------------------------------------------------\n";
}
};
inline void merge(slopeDS *&L, slopeDS *&R)
{
if (L == nullptr) {L = R;}
else if (R == nullptr) {R = L;}
else
{
if (L -> getSZ() < R -> getSZ()) swap(L, R);
L -> opt += R -> opt;
while (R -> pqL.size())
{
ll pos = R -> getL(); R -> popL();
if (pos <= L -> getR()) {L -> pushL(pos);}
else
{
L -> opt += pos - L -> getR();
L -> pushL(L -> getR()); L -> popR();
L -> pushR(pos);
}
}
while (R -> pqR.size())
{
ll pos = R -> getR(); R -> popR();
if (pos >= L -> getL()) {L -> pushR(pos);}
else
{
L -> opt += L -> getL() - pos;
L -> pushR(L -> getL()); L -> popL();
L -> pushL(pos);
}
}
}
}
slopeDS* dfs(int u, int p, int val)
{
if (adj[u].size() == 0) {return new slopeDS(val);}
else
{
slopeDS* cur = nullptr;
for (auto v : adj[u])
{
if (v.fst != p)
{
slopeDS* tmp = dfs(v.fst, u, v.snd);
merge(cur, tmp);
}
}
if (p != -1)
{
ll prev; prev = cur -> getL();
cur -> sft += val;
cur -> popL();
cur -> pushL(prev + val);
prev = cur -> getR();
while (cur -> pqR.size()) cur -> popR();
cur -> pushR(prev);
}
return cur;
}
}
int main()
{
ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> m;
for (int i = 0; i < n + m; i++) {adj.pub(vpi());}
for (int i = 1; i < n + m; i++)
{
int p, v; cin >> p >> v;
adj[p - 1].push_back({i, v});
}
slopeDS* ret = dfs(0, -1, 0);
cout << ret -> opt << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
6 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
6 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
436 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
436 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
6 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
6 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
5 ms |
384 KB |
Output is correct |
24 |
Correct |
5 ms |
436 KB |
Output is correct |
25 |
Correct |
5 ms |
384 KB |
Output is correct |
26 |
Correct |
5 ms |
384 KB |
Output is correct |
27 |
Correct |
5 ms |
436 KB |
Output is correct |
28 |
Correct |
5 ms |
384 KB |
Output is correct |
29 |
Correct |
5 ms |
384 KB |
Output is correct |
30 |
Correct |
5 ms |
384 KB |
Output is correct |
31 |
Correct |
5 ms |
512 KB |
Output is correct |
32 |
Correct |
5 ms |
640 KB |
Output is correct |
33 |
Correct |
6 ms |
640 KB |
Output is correct |
34 |
Correct |
6 ms |
768 KB |
Output is correct |
35 |
Correct |
7 ms |
896 KB |
Output is correct |
36 |
Correct |
7 ms |
896 KB |
Output is correct |
37 |
Correct |
8 ms |
1088 KB |
Output is correct |
38 |
Correct |
8 ms |
1088 KB |
Output is correct |
39 |
Correct |
8 ms |
1088 KB |
Output is correct |
40 |
Correct |
7 ms |
1344 KB |
Output is correct |
41 |
Correct |
7 ms |
1344 KB |
Output is correct |
42 |
Correct |
8 ms |
832 KB |
Output is correct |
43 |
Correct |
8 ms |
1472 KB |
Output is correct |
44 |
Correct |
8 ms |
1344 KB |
Output is correct |
45 |
Correct |
8 ms |
1472 KB |
Output is correct |
46 |
Correct |
9 ms |
1608 KB |
Output is correct |
47 |
Correct |
9 ms |
1728 KB |
Output is correct |
48 |
Correct |
9 ms |
1472 KB |
Output is correct |
49 |
Correct |
9 ms |
1600 KB |
Output is correct |
50 |
Correct |
8 ms |
1344 KB |
Output is correct |
51 |
Correct |
8 ms |
1344 KB |
Output is correct |
52 |
Correct |
8 ms |
1472 KB |
Output is correct |
53 |
Correct |
9 ms |
1600 KB |
Output is correct |
54 |
Correct |
8 ms |
1472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
6 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
6 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
5 ms |
384 KB |
Output is correct |
24 |
Correct |
5 ms |
436 KB |
Output is correct |
25 |
Correct |
5 ms |
384 KB |
Output is correct |
26 |
Correct |
5 ms |
384 KB |
Output is correct |
27 |
Correct |
5 ms |
436 KB |
Output is correct |
28 |
Correct |
5 ms |
384 KB |
Output is correct |
29 |
Correct |
5 ms |
384 KB |
Output is correct |
30 |
Correct |
5 ms |
384 KB |
Output is correct |
31 |
Correct |
5 ms |
512 KB |
Output is correct |
32 |
Correct |
5 ms |
640 KB |
Output is correct |
33 |
Correct |
6 ms |
640 KB |
Output is correct |
34 |
Correct |
6 ms |
768 KB |
Output is correct |
35 |
Correct |
7 ms |
896 KB |
Output is correct |
36 |
Correct |
7 ms |
896 KB |
Output is correct |
37 |
Correct |
8 ms |
1088 KB |
Output is correct |
38 |
Correct |
8 ms |
1088 KB |
Output is correct |
39 |
Correct |
8 ms |
1088 KB |
Output is correct |
40 |
Correct |
7 ms |
1344 KB |
Output is correct |
41 |
Correct |
7 ms |
1344 KB |
Output is correct |
42 |
Correct |
8 ms |
832 KB |
Output is correct |
43 |
Correct |
8 ms |
1472 KB |
Output is correct |
44 |
Correct |
8 ms |
1344 KB |
Output is correct |
45 |
Correct |
8 ms |
1472 KB |
Output is correct |
46 |
Correct |
9 ms |
1608 KB |
Output is correct |
47 |
Correct |
9 ms |
1728 KB |
Output is correct |
48 |
Correct |
9 ms |
1472 KB |
Output is correct |
49 |
Correct |
9 ms |
1600 KB |
Output is correct |
50 |
Correct |
8 ms |
1344 KB |
Output is correct |
51 |
Correct |
8 ms |
1344 KB |
Output is correct |
52 |
Correct |
8 ms |
1472 KB |
Output is correct |
53 |
Correct |
9 ms |
1600 KB |
Output is correct |
54 |
Correct |
8 ms |
1472 KB |
Output is correct |
55 |
Correct |
13 ms |
2300 KB |
Output is correct |
56 |
Correct |
41 ms |
7528 KB |
Output is correct |
57 |
Correct |
76 ms |
12644 KB |
Output is correct |
58 |
Correct |
91 ms |
16096 KB |
Output is correct |
59 |
Correct |
130 ms |
21464 KB |
Output is correct |
60 |
Correct |
159 ms |
26712 KB |
Output is correct |
61 |
Correct |
185 ms |
30808 KB |
Output is correct |
62 |
Correct |
219 ms |
33624 KB |
Output is correct |
63 |
Correct |
263 ms |
40264 KB |
Output is correct |
64 |
Correct |
263 ms |
41928 KB |
Output is correct |
65 |
Correct |
136 ms |
59208 KB |
Output is correct |
66 |
Correct |
141 ms |
59208 KB |
Output is correct |
67 |
Correct |
134 ms |
21852 KB |
Output is correct |
68 |
Correct |
203 ms |
61512 KB |
Output is correct |
69 |
Correct |
235 ms |
59080 KB |
Output is correct |
70 |
Correct |
232 ms |
59080 KB |
Output is correct |
71 |
Correct |
329 ms |
84424 KB |
Output is correct |
72 |
Correct |
327 ms |
84296 KB |
Output is correct |
73 |
Correct |
293 ms |
78024 KB |
Output is correct |
74 |
Correct |
288 ms |
78280 KB |
Output is correct |
75 |
Correct |
298 ms |
76104 KB |
Output is correct |
76 |
Correct |
309 ms |
76232 KB |
Output is correct |
77 |
Correct |
307 ms |
73124 KB |
Output is correct |
78 |
Correct |
298 ms |
71624 KB |
Output is correct |
79 |
Correct |
232 ms |
72008 KB |
Output is correct |