//In the name of God
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int maxn = 2e5 + 100;
const int mod = 1e9 + 7;
const int inf = 1e9;
#define fast_io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io freopen("input.txt", "r+", stdin);freopen("output.txt", "w+", stdout);
#define pb push_back
#define Mp make_pair
#define F first
#define S second
#define Sz(x) ll((x).size())
#define all(x) (x).begin(), (x).end()
ll pw(ll a, ll b){
ll c = 1;
while(b){
if(b & 1) c = c * a % mod;
a = a * a % mod;
b >>= 1;
}
return c;
}
ll n, a[maxn], h[maxn], c[maxn], pp[maxn], ptr;
vector<int> g[maxn];
vector<int> vec;
set<pll> s[maxn];
void solve(int v){
for(int u : g[v]){
solve(u);
if(Sz(s[u]) > Sz(s[v])) s[v].swap(s[u]);
for(auto x : s[u]){
s[v].insert(x);
}
}
/* cout << "# " << v << "\n";
for(auto i : s[v]){
cout << " " << i.F << " " << i.S << "\n";
}
cout << "! " << h[v] << " " << c[v] << "\n";*/
auto it = s[v].lower_bound(Mp(h[v], inf));
ll x = c[v];
while(it != s[v].end() && pp[it -> S] <= x){
x -= pp[it->S];
it = s[v].erase(it);
}
if(it != s[v].end()){
pp[it->S] -= x;
}
pp[ptr] = c[v];
s[v].insert(Mp(h[v], ptr++));
/*for(auto i : s[v]){
cout << " " << i.F << " " << i.S << "\n";
}
cout << "\n";*/
return;
}
int main(){
fast_io;
cin >> n;
ll sc = 0;
for(int i = 1; i <= n; i++){
cin >> a[i] >> h[i] >> c[i];
vec.pb(h[i]);
if(i != 1) g[a[i]].pb(i);
sc += c[i];
}
sort(all(vec));
vec.resize(unique(all(vec)) - vec.begin());
for(int i = 1; i <= n; i++){
h[i] = Sz(vec) - 1 - (lower_bound(all(vec), h[i]) - vec.begin());
}
solve(1);
for(auto i : s[1]){
// cout << i.F << " " << i.S << "\n";
sc -= pp[i.S];
}
cout << sc << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
14412 KB |
Output is correct |
2 |
Correct |
8 ms |
14408 KB |
Output is correct |
3 |
Correct |
9 ms |
14412 KB |
Output is correct |
4 |
Correct |
9 ms |
14412 KB |
Output is correct |
5 |
Correct |
13 ms |
15764 KB |
Output is correct |
6 |
Correct |
13 ms |
15784 KB |
Output is correct |
7 |
Correct |
13 ms |
15700 KB |
Output is correct |
8 |
Correct |
15 ms |
15828 KB |
Output is correct |
9 |
Correct |
19 ms |
15840 KB |
Output is correct |
10 |
Correct |
13 ms |
15824 KB |
Output is correct |
11 |
Correct |
12 ms |
16044 KB |
Output is correct |
12 |
Correct |
11 ms |
15564 KB |
Output is correct |
13 |
Correct |
11 ms |
15588 KB |
Output is correct |
14 |
Correct |
11 ms |
15184 KB |
Output is correct |
15 |
Correct |
11 ms |
15156 KB |
Output is correct |
16 |
Correct |
22 ms |
16084 KB |
Output is correct |
17 |
Correct |
17 ms |
16212 KB |
Output is correct |
18 |
Correct |
16 ms |
16596 KB |
Output is correct |
19 |
Correct |
12 ms |
15444 KB |
Output is correct |
20 |
Correct |
13 ms |
15448 KB |
Output is correct |
21 |
Correct |
11 ms |
15480 KB |
Output is correct |
22 |
Correct |
11 ms |
15316 KB |
Output is correct |
23 |
Correct |
12 ms |
15316 KB |
Output is correct |
24 |
Correct |
12 ms |
15444 KB |
Output is correct |
25 |
Correct |
17 ms |
15464 KB |
Output is correct |
26 |
Correct |
10 ms |
15584 KB |
Output is correct |
27 |
Correct |
11 ms |
15444 KB |
Output is correct |
28 |
Correct |
11 ms |
15576 KB |
Output is correct |
29 |
Correct |
15 ms |
15700 KB |
Output is correct |
30 |
Correct |
14 ms |
15572 KB |
Output is correct |
31 |
Correct |
11 ms |
15688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
14412 KB |
Output is correct |
2 |
Correct |
8 ms |
14408 KB |
Output is correct |
3 |
Correct |
9 ms |
14412 KB |
Output is correct |
4 |
Correct |
9 ms |
14412 KB |
Output is correct |
5 |
Correct |
13 ms |
15764 KB |
Output is correct |
6 |
Correct |
13 ms |
15784 KB |
Output is correct |
7 |
Correct |
13 ms |
15700 KB |
Output is correct |
8 |
Correct |
15 ms |
15828 KB |
Output is correct |
9 |
Correct |
19 ms |
15840 KB |
Output is correct |
10 |
Correct |
13 ms |
15824 KB |
Output is correct |
11 |
Correct |
12 ms |
16044 KB |
Output is correct |
12 |
Correct |
11 ms |
15564 KB |
Output is correct |
13 |
Correct |
11 ms |
15588 KB |
Output is correct |
14 |
Correct |
11 ms |
15184 KB |
Output is correct |
15 |
Correct |
11 ms |
15156 KB |
Output is correct |
16 |
Correct |
22 ms |
16084 KB |
Output is correct |
17 |
Correct |
17 ms |
16212 KB |
Output is correct |
18 |
Correct |
16 ms |
16596 KB |
Output is correct |
19 |
Correct |
12 ms |
15444 KB |
Output is correct |
20 |
Correct |
13 ms |
15448 KB |
Output is correct |
21 |
Correct |
11 ms |
15480 KB |
Output is correct |
22 |
Correct |
11 ms |
15316 KB |
Output is correct |
23 |
Correct |
12 ms |
15316 KB |
Output is correct |
24 |
Correct |
12 ms |
15444 KB |
Output is correct |
25 |
Correct |
17 ms |
15464 KB |
Output is correct |
26 |
Correct |
10 ms |
15584 KB |
Output is correct |
27 |
Correct |
11 ms |
15444 KB |
Output is correct |
28 |
Correct |
11 ms |
15576 KB |
Output is correct |
29 |
Correct |
15 ms |
15700 KB |
Output is correct |
30 |
Correct |
14 ms |
15572 KB |
Output is correct |
31 |
Correct |
11 ms |
15688 KB |
Output is correct |
32 |
Correct |
20 ms |
15708 KB |
Output is correct |
33 |
Correct |
360 ms |
84000 KB |
Output is correct |
34 |
Correct |
351 ms |
83688 KB |
Output is correct |
35 |
Correct |
401 ms |
81032 KB |
Output is correct |
36 |
Correct |
373 ms |
83772 KB |
Output is correct |
37 |
Correct |
348 ms |
85260 KB |
Output is correct |
38 |
Correct |
347 ms |
95516 KB |
Output is correct |
39 |
Correct |
205 ms |
60888 KB |
Output is correct |
40 |
Correct |
174 ms |
60996 KB |
Output is correct |
41 |
Correct |
156 ms |
63356 KB |
Output is correct |
42 |
Correct |
204 ms |
46944 KB |
Output is correct |
43 |
Correct |
162 ms |
46960 KB |
Output is correct |
44 |
Correct |
345 ms |
109004 KB |
Output is correct |
45 |
Correct |
341 ms |
109132 KB |
Output is correct |
46 |
Correct |
407 ms |
137812 KB |
Output is correct |
47 |
Correct |
318 ms |
56824 KB |
Output is correct |
48 |
Correct |
274 ms |
56148 KB |
Output is correct |
49 |
Correct |
186 ms |
57304 KB |
Output is correct |
50 |
Correct |
287 ms |
51548 KB |
Output is correct |
51 |
Correct |
240 ms |
51460 KB |
Output is correct |
52 |
Correct |
331 ms |
57376 KB |
Output is correct |
53 |
Correct |
291 ms |
56588 KB |
Output is correct |
54 |
Correct |
146 ms |
61072 KB |
Output is correct |
55 |
Correct |
219 ms |
58464 KB |
Output is correct |
56 |
Correct |
215 ms |
64292 KB |
Output is correct |
57 |
Correct |
230 ms |
67384 KB |
Output is correct |
58 |
Correct |
204 ms |
65004 KB |
Output is correct |
59 |
Correct |
196 ms |
65084 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
14412 KB |
Output is correct |
2 |
Correct |
8 ms |
14408 KB |
Output is correct |
3 |
Correct |
9 ms |
14412 KB |
Output is correct |
4 |
Correct |
9 ms |
14412 KB |
Output is correct |
5 |
Correct |
13 ms |
15764 KB |
Output is correct |
6 |
Correct |
13 ms |
15784 KB |
Output is correct |
7 |
Correct |
13 ms |
15700 KB |
Output is correct |
8 |
Correct |
15 ms |
15828 KB |
Output is correct |
9 |
Correct |
19 ms |
15840 KB |
Output is correct |
10 |
Correct |
13 ms |
15824 KB |
Output is correct |
11 |
Correct |
12 ms |
16044 KB |
Output is correct |
12 |
Correct |
11 ms |
15564 KB |
Output is correct |
13 |
Correct |
11 ms |
15588 KB |
Output is correct |
14 |
Correct |
11 ms |
15184 KB |
Output is correct |
15 |
Correct |
11 ms |
15156 KB |
Output is correct |
16 |
Correct |
22 ms |
16084 KB |
Output is correct |
17 |
Correct |
17 ms |
16212 KB |
Output is correct |
18 |
Correct |
16 ms |
16596 KB |
Output is correct |
19 |
Correct |
12 ms |
15444 KB |
Output is correct |
20 |
Correct |
13 ms |
15448 KB |
Output is correct |
21 |
Correct |
11 ms |
15480 KB |
Output is correct |
22 |
Correct |
11 ms |
15316 KB |
Output is correct |
23 |
Correct |
12 ms |
15316 KB |
Output is correct |
24 |
Correct |
12 ms |
15444 KB |
Output is correct |
25 |
Correct |
17 ms |
15464 KB |
Output is correct |
26 |
Correct |
10 ms |
15584 KB |
Output is correct |
27 |
Correct |
11 ms |
15444 KB |
Output is correct |
28 |
Correct |
11 ms |
15576 KB |
Output is correct |
29 |
Correct |
15 ms |
15700 KB |
Output is correct |
30 |
Correct |
14 ms |
15572 KB |
Output is correct |
31 |
Correct |
11 ms |
15688 KB |
Output is correct |
32 |
Correct |
20 ms |
15708 KB |
Output is correct |
33 |
Correct |
360 ms |
84000 KB |
Output is correct |
34 |
Correct |
351 ms |
83688 KB |
Output is correct |
35 |
Correct |
401 ms |
81032 KB |
Output is correct |
36 |
Correct |
373 ms |
83772 KB |
Output is correct |
37 |
Correct |
348 ms |
85260 KB |
Output is correct |
38 |
Correct |
347 ms |
95516 KB |
Output is correct |
39 |
Correct |
205 ms |
60888 KB |
Output is correct |
40 |
Correct |
174 ms |
60996 KB |
Output is correct |
41 |
Correct |
156 ms |
63356 KB |
Output is correct |
42 |
Correct |
204 ms |
46944 KB |
Output is correct |
43 |
Correct |
162 ms |
46960 KB |
Output is correct |
44 |
Correct |
345 ms |
109004 KB |
Output is correct |
45 |
Correct |
341 ms |
109132 KB |
Output is correct |
46 |
Correct |
407 ms |
137812 KB |
Output is correct |
47 |
Correct |
318 ms |
56824 KB |
Output is correct |
48 |
Correct |
274 ms |
56148 KB |
Output is correct |
49 |
Correct |
186 ms |
57304 KB |
Output is correct |
50 |
Correct |
287 ms |
51548 KB |
Output is correct |
51 |
Correct |
240 ms |
51460 KB |
Output is correct |
52 |
Correct |
331 ms |
57376 KB |
Output is correct |
53 |
Correct |
291 ms |
56588 KB |
Output is correct |
54 |
Correct |
146 ms |
61072 KB |
Output is correct |
55 |
Correct |
219 ms |
58464 KB |
Output is correct |
56 |
Correct |
215 ms |
64292 KB |
Output is correct |
57 |
Correct |
230 ms |
67384 KB |
Output is correct |
58 |
Correct |
204 ms |
65004 KB |
Output is correct |
59 |
Correct |
196 ms |
65084 KB |
Output is correct |
60 |
Correct |
8 ms |
14416 KB |
Output is correct |
61 |
Incorrect |
8 ms |
14412 KB |
Output isn't correct |
62 |
Halted |
0 ms |
0 KB |
- |