#include <bits/stdc++.h>
using namespace std;
#define ar array
#define sz(v) int(v.size())
typedef long long ll;
const int MAXN = 2e5+10;
const ll INF = 1e18+10;
int n, p[MAXN], a[MAXN], loc[MAXN];
ll co[MAXN];
vector<int> adj[MAXN];
struct T {
int tl, tr;
T *l=nullptr, *r=nullptr;
ll lzy_ad, lzy_mx;
T(int _tl, int _tr): tl(_tl), tr(_tr) { lzy_ad = 0, lzy_mx = -INF; }
void extend(){
if (l||r||tl==tr) return;
int tm=(tl+tr)/2;
l = new T(tl, tm), r = new T(tm+1, tr);
}
void prop(){
l->lzy_ad += lzy_ad, r->lzy_ad += lzy_ad;
l->lzy_mx = max(lzy_mx, l->lzy_mx + lzy_ad);
r->lzy_mx = max(lzy_mx, r->lzy_mx + lzy_ad);
lzy_mx = -INF, lzy_ad = 0;
}
void add(int ql, int qr, ll v) {
if (qr < tl || ql > tr) return;
if (ql <= tl && tr <= qr) {
lzy_ad += v;
lzy_mx += v;
return;
}
extend(), prop();
l->add(ql, qr, v), r->add(ql, qr, v);
}
void smax(int ql, int qr, ll v) {
if (qr < tl || ql > tr) return;
if (ql <= tl && tr <= qr) {
lzy_mx = max(lzy_mx, v);
return;
}
extend(), prop();
l->smax(ql, qr, v), r->smax(ql, qr, v);
}
ll qry(int pos) {
if (tl == tr) return max(lzy_ad, lzy_mx);
extend(), prop();
if (pos <= l->tr)
return l->qry(pos);
else
return r->qry(pos);
}
};
struct S {
map<int, int> mp;
T *t;
void init() {
t = new T(0, n-1);
}
ll get(int v){
return t->qry(v);
}
void merge(S& nxt){
nxt.mp[n-1]++;
for (auto it = nxt.mp.begin(), prv = it; it != nxt.mp.end(); prv = it, it = next(it)) {
int p=-1, c=it->first;
if (it != nxt.mp.begin()) p = prv->first;
t->add(p+1, c, nxt.t->qry(c));
mp[c]++;
}
map<int, int>().swap(nxt.mp);
}
void upd(int v, ll amt){
mp[v]++;
t->smax(0, v, amt);
}
} sub[MAXN];
void dfs(int c=0){
sub[loc[c]].init();
for (auto nxt : adj[c]) {
dfs(nxt);
if (sz(sub[loc[c]].mp) < sz(sub[loc[nxt]].mp))
swap(loc[c], loc[nxt]);
sub[loc[c]].merge(sub[loc[nxt]]);
}
sub[loc[c]].upd(a[c], co[c]+sub[loc[c]].get(a[c]));
}
int main(){
ios::sync_with_stdio(false); cin.tie(0);
cin >> n;
for (int i = 0; i < n; i++) {
cin >> p[i] >> a[i] >> co[i], --p[i];
if (i) adj[p[i]].push_back(i);
}
{
map<ll, int> mp; int cc=0;
for (int i = 0; i < n; i++) mp[a[i]]++;
for (auto& v : mp) v.second=cc++;
for (int i = 0; i < n; i++) a[i] = mp[a[i]];
}
iota(loc, loc+n, 0); dfs();
ll ans = 0;
for (int i = 0; i < n; i++)
ans = max(ans, sub[loc[0]].get(i));
ans = accumulate(co, co+n, 0ll) - ans;
cout << ans << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
15980 KB |
Output is correct |
2 |
Correct |
11 ms |
15980 KB |
Output is correct |
3 |
Correct |
11 ms |
15980 KB |
Output is correct |
4 |
Correct |
11 ms |
15980 KB |
Output is correct |
5 |
Correct |
45 ms |
31980 KB |
Output is correct |
6 |
Correct |
32 ms |
26988 KB |
Output is correct |
7 |
Correct |
31 ms |
25452 KB |
Output is correct |
8 |
Correct |
45 ms |
32372 KB |
Output is correct |
9 |
Correct |
32 ms |
27116 KB |
Output is correct |
10 |
Correct |
28 ms |
25452 KB |
Output is correct |
11 |
Correct |
26 ms |
25324 KB |
Output is correct |
12 |
Correct |
27 ms |
24044 KB |
Output is correct |
13 |
Correct |
22 ms |
23788 KB |
Output is correct |
14 |
Correct |
29 ms |
23916 KB |
Output is correct |
15 |
Correct |
23 ms |
23148 KB |
Output is correct |
16 |
Correct |
55 ms |
36972 KB |
Output is correct |
17 |
Correct |
34 ms |
28012 KB |
Output is correct |
18 |
Correct |
25 ms |
25324 KB |
Output is correct |
19 |
Correct |
31 ms |
25856 KB |
Output is correct |
20 |
Correct |
27 ms |
25836 KB |
Output is correct |
21 |
Correct |
27 ms |
25836 KB |
Output is correct |
22 |
Correct |
34 ms |
27628 KB |
Output is correct |
23 |
Correct |
30 ms |
27884 KB |
Output is correct |
24 |
Correct |
31 ms |
25836 KB |
Output is correct |
25 |
Correct |
27 ms |
25836 KB |
Output is correct |
26 |
Correct |
26 ms |
24044 KB |
Output is correct |
27 |
Correct |
31 ms |
26348 KB |
Output is correct |
28 |
Correct |
30 ms |
25580 KB |
Output is correct |
29 |
Correct |
28 ms |
24812 KB |
Output is correct |
30 |
Correct |
27 ms |
23788 KB |
Output is correct |
31 |
Correct |
27 ms |
23788 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
46 ms |
31980 KB |
Output is correct |
2 |
Runtime error |
1294 ms |
524292 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
15980 KB |
Output is correct |
2 |
Correct |
11 ms |
15980 KB |
Output is correct |
3 |
Correct |
11 ms |
15980 KB |
Output is correct |
4 |
Correct |
11 ms |
15980 KB |
Output is correct |
5 |
Correct |
45 ms |
31980 KB |
Output is correct |
6 |
Correct |
32 ms |
26988 KB |
Output is correct |
7 |
Correct |
31 ms |
25452 KB |
Output is correct |
8 |
Correct |
45 ms |
32372 KB |
Output is correct |
9 |
Correct |
32 ms |
27116 KB |
Output is correct |
10 |
Correct |
28 ms |
25452 KB |
Output is correct |
11 |
Correct |
26 ms |
25324 KB |
Output is correct |
12 |
Correct |
27 ms |
24044 KB |
Output is correct |
13 |
Correct |
22 ms |
23788 KB |
Output is correct |
14 |
Correct |
29 ms |
23916 KB |
Output is correct |
15 |
Correct |
23 ms |
23148 KB |
Output is correct |
16 |
Correct |
55 ms |
36972 KB |
Output is correct |
17 |
Correct |
34 ms |
28012 KB |
Output is correct |
18 |
Correct |
25 ms |
25324 KB |
Output is correct |
19 |
Correct |
31 ms |
25856 KB |
Output is correct |
20 |
Correct |
27 ms |
25836 KB |
Output is correct |
21 |
Correct |
27 ms |
25836 KB |
Output is correct |
22 |
Correct |
34 ms |
27628 KB |
Output is correct |
23 |
Correct |
30 ms |
27884 KB |
Output is correct |
24 |
Correct |
31 ms |
25836 KB |
Output is correct |
25 |
Correct |
27 ms |
25836 KB |
Output is correct |
26 |
Correct |
26 ms |
24044 KB |
Output is correct |
27 |
Correct |
31 ms |
26348 KB |
Output is correct |
28 |
Correct |
30 ms |
25580 KB |
Output is correct |
29 |
Correct |
28 ms |
24812 KB |
Output is correct |
30 |
Correct |
27 ms |
23788 KB |
Output is correct |
31 |
Correct |
27 ms |
23788 KB |
Output is correct |
32 |
Correct |
46 ms |
31980 KB |
Output is correct |
33 |
Runtime error |
1294 ms |
524292 KB |
Execution killed with signal 9 |
34 |
Halted |
0 ms |
0 KB |
- |