#include <iostream>
#include <vector>
#include <algorithm>
#include <deque>
#include <set>
#include <utility>
#include <array>
using namespace std;
#define ALL(x) x.begin(), x.end()
#define ShinLena cin.tie(nullptr)->sync_with_stdio(false);
#define N 200050
using ll = long long;
int n, m, a[N], timer, dp[2][N];
vector<int> g[N];
vector<tuple<int, int, int>> v;
void dfs(int u)
{
int in = timer++;
for (auto v : g[u]) dfs(v);
int out = timer++;
v.emplace_back(out, in, a[u]);
}
void schedule()
{
for (int j = 1; j <= m; ++j)
{
for (auto i = 1; i <= n; ++i)
{
auto [e, s, w] = v[i];
int l = 0, r = i-1;
dp[j&1][i] = dp[j&1][i-1];
while (l <= r)
{
int y = (l+r)/2;
if (get<0>(v[y]) < s)
dp[j&1][i] = max(dp[j&1][i], dp[!(j&1)][y] + w), l = y + 1;
else r = y - 1;
}
}
}
}
int main()
{
ShinLena;
cin >> n >> m;
for (int p, i = 1; i <= n; ++i) cin >> p >> a[i], g[p].push_back(i);
v={{-1e9,0,0}};dfs(0);
sort(ALL(v));
schedule();
cout << dp[m&1][n];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6748 KB |
Output is correct |
2 |
Correct |
1 ms |
6748 KB |
Output is correct |
3 |
Correct |
1 ms |
6748 KB |
Output is correct |
4 |
Correct |
17 ms |
7188 KB |
Output is correct |
5 |
Correct |
21 ms |
7000 KB |
Output is correct |
6 |
Correct |
27 ms |
7004 KB |
Output is correct |
7 |
Correct |
1571 ms |
12396 KB |
Output is correct |
8 |
Correct |
1896 ms |
11476 KB |
Output is correct |
9 |
Execution timed out |
2013 ms |
13004 KB |
Time limit exceeded |
10 |
Execution timed out |
2044 ms |
11796 KB |
Time limit exceeded |