#include <bits/stdc++.h>
using namespace std;
#define int long long
#define all(x) x.begin(), x.end()
#define size(x) (int)x.size()
template<class S, class T>
bool chmin(S &a, const T &b) {
return a > b ? (a = b) == b : false;
}
template<class S, class T>
bool chmax(S &a, const T &b) {
return a < b ? (a = b) == b : false;
}
void compress(vector<int>& a) {
vector<int> v;
map<int, int> mp;
for (int i = 1; i < size(a); ++i) {
if (mp.count(a[i])) continue;
v.push_back(a[i]);
mp[a[i]];
}
sort(all(v));
for (int i = 0; i < size(v); ++i) {
mp[v[i]] = i + 1;
}
for (int i = 1; i < size(a); ++i) {
a[i] = mp[a[i]];
}
}
const int N = 1e5 + 1;
int dp[N][1001];
vector<vector<int>> g;
vector<pair<int, int>> f;
void dfs(int v) {
for (int to : g[v]) {
dfs(to);
int Max = 0;
for (int k = 0; k <= 1000; ++k) {
chmax(Max, dp[to][k]);
dp[v][k] += Max;
}
}
if (f[v].second != -1) {
int cur = 0;
for (int to : g[v]) {
cur += dp[to][f[v].second];
}
chmax(dp[v][f[v].second], cur + f[v].first);
}
for (int k = 1; k <= 1000; ++k) {
chmax(dp[v][k], dp[v][k - 1]);
}
}
struct fenwick {
int n;
vector<int> t;
fenwick(int n) {
this -> n = n;
t.assign(n + 1, 0);
}
void upd(int i, int delta) {
for (; i <= n; i += i & -i) {
chmax(t[i], delta);
}
}
int get(int i) {
int res = 0;
for (; i > 0; i -= i & -i) {
chmax(res, t[i]);
}
return res;
}
};
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
int n, m, k;
cin >> n >> m >> k;
int p[n + 1];
p[1] = -1;
vector<int> u(m + 1), d(m + 1), w(m + 1);
g.resize(n + 1);
f.assign(n + 1, {-1, -1});
for (int i = 2; i <= n; ++i) {
cin >> p[i];
g[p[i]].push_back(i);
}
bool flag = true;
for (int i = 1; i <= m; ++i) {
cin >> u[i] >> d[i] >> w[i];
if (!g[u[i]].empty()) flag = false;
}
if (flag) {
cout << accumulate(w.begin() + 1, w.end(), 0ll);
return 0;
}
compress(d);
for (int i = 1; i <= m; ++i) {
f[u[i]] = {w[i], d[i]};
}
if (m <= 1000 || k <= 20) {
dfs(1);
cout << *max_element(dp[1], dp[1] + 1001);
return 0;
}
fenwick fenw(k);
for (int i = n; i >= 1; --i) {
if (f[i].second != -1) {
fenw.upd(f[i].second, fenw.get(f[i].second) + f[i].first);
}
}
cout << fenw.get(k);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
432 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
600 KB |
Output is correct |
7 |
Correct |
0 ms |
604 KB |
Output is correct |
8 |
Correct |
0 ms |
604 KB |
Output is correct |
9 |
Correct |
0 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
7260 KB |
Output is correct |
2 |
Correct |
15 ms |
8028 KB |
Output is correct |
3 |
Correct |
30 ms |
8528 KB |
Output is correct |
4 |
Correct |
26 ms |
8412 KB |
Output is correct |
5 |
Correct |
28 ms |
8540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8280 KB |
Output is correct |
2 |
Correct |
4 ms |
8280 KB |
Output is correct |
3 |
Correct |
4 ms |
8284 KB |
Output is correct |
4 |
Correct |
58 ms |
15052 KB |
Output is correct |
5 |
Correct |
58 ms |
14868 KB |
Output is correct |
6 |
Correct |
80 ms |
15052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
633 ms |
773508 KB |
Output is correct |
2 |
Correct |
647 ms |
775440 KB |
Output is correct |
3 |
Correct |
590 ms |
791848 KB |
Output is correct |
4 |
Correct |
614 ms |
749260 KB |
Output is correct |
5 |
Correct |
508 ms |
804776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
432 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
600 KB |
Output is correct |
7 |
Correct |
0 ms |
604 KB |
Output is correct |
8 |
Correct |
0 ms |
604 KB |
Output is correct |
9 |
Correct |
0 ms |
604 KB |
Output is correct |
10 |
Correct |
675 ms |
773792 KB |
Output is correct |
11 |
Correct |
602 ms |
774156 KB |
Output is correct |
12 |
Correct |
493 ms |
791012 KB |
Output is correct |
13 |
Correct |
616 ms |
748632 KB |
Output is correct |
14 |
Correct |
429 ms |
804972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
67 ms |
114512 KB |
Output is correct |
2 |
Correct |
384 ms |
502612 KB |
Output is correct |
3 |
Correct |
384 ms |
505240 KB |
Output is correct |
4 |
Correct |
375 ms |
501592 KB |
Output is correct |
5 |
Correct |
192 ms |
15436 KB |
Output is correct |
6 |
Correct |
385 ms |
482608 KB |
Output is correct |
7 |
Correct |
416 ms |
719008 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
432 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
600 KB |
Output is correct |
7 |
Correct |
0 ms |
604 KB |
Output is correct |
8 |
Correct |
0 ms |
604 KB |
Output is correct |
9 |
Correct |
0 ms |
604 KB |
Output is correct |
10 |
Correct |
5 ms |
8280 KB |
Output is correct |
11 |
Correct |
4 ms |
8280 KB |
Output is correct |
12 |
Correct |
4 ms |
8284 KB |
Output is correct |
13 |
Correct |
58 ms |
15052 KB |
Output is correct |
14 |
Correct |
58 ms |
14868 KB |
Output is correct |
15 |
Correct |
80 ms |
15052 KB |
Output is correct |
16 |
Correct |
675 ms |
773792 KB |
Output is correct |
17 |
Correct |
602 ms |
774156 KB |
Output is correct |
18 |
Correct |
493 ms |
791012 KB |
Output is correct |
19 |
Correct |
616 ms |
748632 KB |
Output is correct |
20 |
Correct |
429 ms |
804972 KB |
Output is correct |
21 |
Incorrect |
9 ms |
2136 KB |
Output isn't correct |
22 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
432 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
600 KB |
Output is correct |
7 |
Correct |
0 ms |
604 KB |
Output is correct |
8 |
Correct |
0 ms |
604 KB |
Output is correct |
9 |
Correct |
0 ms |
604 KB |
Output is correct |
10 |
Correct |
18 ms |
7260 KB |
Output is correct |
11 |
Correct |
15 ms |
8028 KB |
Output is correct |
12 |
Correct |
30 ms |
8528 KB |
Output is correct |
13 |
Correct |
26 ms |
8412 KB |
Output is correct |
14 |
Correct |
28 ms |
8540 KB |
Output is correct |
15 |
Correct |
5 ms |
8280 KB |
Output is correct |
16 |
Correct |
4 ms |
8280 KB |
Output is correct |
17 |
Correct |
4 ms |
8284 KB |
Output is correct |
18 |
Correct |
58 ms |
15052 KB |
Output is correct |
19 |
Correct |
58 ms |
14868 KB |
Output is correct |
20 |
Correct |
80 ms |
15052 KB |
Output is correct |
21 |
Correct |
633 ms |
773508 KB |
Output is correct |
22 |
Correct |
647 ms |
775440 KB |
Output is correct |
23 |
Correct |
590 ms |
791848 KB |
Output is correct |
24 |
Correct |
614 ms |
749260 KB |
Output is correct |
25 |
Correct |
508 ms |
804776 KB |
Output is correct |
26 |
Correct |
675 ms |
773792 KB |
Output is correct |
27 |
Correct |
602 ms |
774156 KB |
Output is correct |
28 |
Correct |
493 ms |
791012 KB |
Output is correct |
29 |
Correct |
616 ms |
748632 KB |
Output is correct |
30 |
Correct |
429 ms |
804972 KB |
Output is correct |
31 |
Correct |
67 ms |
114512 KB |
Output is correct |
32 |
Correct |
384 ms |
502612 KB |
Output is correct |
33 |
Correct |
384 ms |
505240 KB |
Output is correct |
34 |
Correct |
375 ms |
501592 KB |
Output is correct |
35 |
Correct |
192 ms |
15436 KB |
Output is correct |
36 |
Correct |
385 ms |
482608 KB |
Output is correct |
37 |
Correct |
416 ms |
719008 KB |
Output is correct |
38 |
Incorrect |
9 ms |
2136 KB |
Output isn't correct |
39 |
Halted |
0 ms |
0 KB |
- |