#include<bits/stdc++.h>
using namespace std;
using ll = long long;
struct segtree {
int n;
vector<ll> t;
segtree(int m) : n(m), t(m*4) {}
void update(int l, int r, ll x, int v, int tl, int tr) {
if (l <= tl && tr <= r) {
t[v] = max(t[v], x);
} else if (tr < l || r < tl) {
return;
} else {
int mid = (tl+tr) / 2;
update(l, r, x, v*2, tl, mid);
update(l, r, x, v*2+1, mid+1, tr);
}
}
ll query(int i, int v, int tl, int tr) {
if (tl == tr) return t[v];
int mid = (tl+tr) / 2;
if (i <= mid) return max(t[v], query(i, v*2, tl, mid));
else return max(t[v], query(i, v*2+1, mid+1, tr));
}
};
int main() {
int n, m, k;
cin >> n >> m >> k;
vector<vector<int>> child(n);
vector par(n, -1);
bool line = 1;
for (int i = 1; i < n; i++) {
cin >> par[i];
child[--par[i]].push_back(i);
line &= par[i] == i-1;
}
map<int,int> comp;
vector<pair<int,int>> fruit(n);
for (int i =0; i < m;i ++) {
ll v, d, w;
cin >> v >> d >> w;
v--;
comp[d] = 0;
fruit[v] = {d, w};
}
k = 0;
for (auto& mp: comp) mp.second = ++k;
for (auto& [d, w] : fruit)
d = comp[d];
if (k <= 20 || m <= 5000) {
auto dp = [&](auto& dp, int i)->vector<ll>*{
vector<vector<ll>*> dpc;
for (int e : child[i])
dpc.push_back(dp(dp, e));
vector<ll>& res = *(dpc.empty() ? new vector<ll>(k+1) : dpc[0]);
for (int day = 1; day <= k; day++) {
for (int j = 1; j < (int)dpc.size(); j++)
res[day] += (*dpc[j])[day];
if (day == fruit[i].first)
res[day] += fruit[i].second;
}
int d = fruit[i].first;
if (d) {
res[d] = max(res[d], res[d-1]);
for (int i = d+1; i <= k; i++)
res[i] = max(res[i], res[d]);
}
return &res;
};
cout << (*dp(dp, 0))[k] << '\n';
} else if (line) {
auto dp = [&](auto& dp, int i)->segtree* {
segtree* res(nullptr);
for (int e : child[i]) {
res = dp(dp, e);
}
if (!res) res = new segtree(k+1);
int d = fruit[i].first;
res->update(d, d, res->query(d, 1, 0, k) + fruit[i].second,1, 0, k);
if (d) {
res->update(d, d, res->query(d-1, 1, 0, k),1, 0, k);
res->update(d+1, k, res->query(d, 1, 0, k),1, 0, k);
}
return res;
};
cout << dp(dp, 0)->query(k, 1, 0, k) << '\n';
} else {
ll res = 0;
for (auto [a, b] : fruit) res += b;
cout << res << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
84 ms |
6292 KB |
Output is correct |
2 |
Correct |
64 ms |
7128 KB |
Output is correct |
3 |
Correct |
149 ms |
7180 KB |
Output is correct |
4 |
Correct |
141 ms |
7104 KB |
Output is correct |
5 |
Correct |
144 ms |
7288 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
171 ms |
21196 KB |
Output is correct |
5 |
Correct |
175 ms |
21192 KB |
Output is correct |
6 |
Correct |
232 ms |
21248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
142 ms |
8504 KB |
Output is correct |
2 |
Correct |
143 ms |
8508 KB |
Output is correct |
3 |
Correct |
153 ms |
11924 KB |
Output is correct |
4 |
Correct |
115 ms |
11240 KB |
Output is correct |
5 |
Correct |
119 ms |
17828 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
151 ms |
15540 KB |
Output is correct |
11 |
Correct |
102 ms |
11596 KB |
Output is correct |
12 |
Correct |
106 ms |
14724 KB |
Output is correct |
13 |
Correct |
107 ms |
25328 KB |
Output is correct |
14 |
Correct |
109 ms |
17868 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
71 ms |
63540 KB |
Output is correct |
2 |
Correct |
307 ms |
305728 KB |
Output is correct |
3 |
Correct |
324 ms |
307900 KB |
Output is correct |
4 |
Correct |
376 ms |
397288 KB |
Output is correct |
5 |
Correct |
1959 ms |
787704 KB |
Output is correct |
6 |
Correct |
432 ms |
477988 KB |
Output is correct |
7 |
Correct |
237 ms |
167540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
171 ms |
21196 KB |
Output is correct |
14 |
Correct |
175 ms |
21192 KB |
Output is correct |
15 |
Correct |
232 ms |
21248 KB |
Output is correct |
16 |
Correct |
151 ms |
15540 KB |
Output is correct |
17 |
Correct |
102 ms |
11596 KB |
Output is correct |
18 |
Correct |
106 ms |
14724 KB |
Output is correct |
19 |
Correct |
107 ms |
25328 KB |
Output is correct |
20 |
Correct |
109 ms |
17868 KB |
Output is correct |
21 |
Incorrect |
20 ms |
1440 KB |
Output isn't correct |
22 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
84 ms |
6292 KB |
Output is correct |
11 |
Correct |
64 ms |
7128 KB |
Output is correct |
12 |
Correct |
149 ms |
7180 KB |
Output is correct |
13 |
Correct |
141 ms |
7104 KB |
Output is correct |
14 |
Correct |
144 ms |
7288 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
2 ms |
468 KB |
Output is correct |
18 |
Correct |
171 ms |
21196 KB |
Output is correct |
19 |
Correct |
175 ms |
21192 KB |
Output is correct |
20 |
Correct |
232 ms |
21248 KB |
Output is correct |
21 |
Correct |
142 ms |
8504 KB |
Output is correct |
22 |
Correct |
143 ms |
8508 KB |
Output is correct |
23 |
Correct |
153 ms |
11924 KB |
Output is correct |
24 |
Correct |
115 ms |
11240 KB |
Output is correct |
25 |
Correct |
119 ms |
17828 KB |
Output is correct |
26 |
Correct |
151 ms |
15540 KB |
Output is correct |
27 |
Correct |
102 ms |
11596 KB |
Output is correct |
28 |
Correct |
106 ms |
14724 KB |
Output is correct |
29 |
Correct |
107 ms |
25328 KB |
Output is correct |
30 |
Correct |
109 ms |
17868 KB |
Output is correct |
31 |
Correct |
71 ms |
63540 KB |
Output is correct |
32 |
Correct |
307 ms |
305728 KB |
Output is correct |
33 |
Correct |
324 ms |
307900 KB |
Output is correct |
34 |
Correct |
376 ms |
397288 KB |
Output is correct |
35 |
Correct |
1959 ms |
787704 KB |
Output is correct |
36 |
Correct |
432 ms |
477988 KB |
Output is correct |
37 |
Correct |
237 ms |
167540 KB |
Output is correct |
38 |
Incorrect |
20 ms |
1440 KB |
Output isn't correct |
39 |
Halted |
0 ms |
0 KB |
- |