#include <bits/stdc++.h>
#define fr first
#define sc second
#define mk make_pair
#define pb emplace_back
#define all(s) s.begin(), s.end()
using namespace std;
const int N = 2e5 + 5;
int n, m, x, y, root, a[N];
int dp[N][505], tin[N], tiktak, inf = 1e9 + 7, tout[N], ans, last;
vector < vector <int> > g;
vector < pair <int, int> > vec;
void dfs (int v)
{
tin[v] = ++tiktak;
for (auto to : g[v])
dfs(to);
tout[v] = tiktak;
vec.pb( mk( tout[v], v ) );
}
main()
{
cin >> n >> m;
g.resize(n + 1);
for (int i = 1; i <= n; i++){
scanf("%d%d", &x, &a[i]);
if (x == 0)
root = i;
else
g[x].pb(i);
}
dfs(root);
sort( all(vec) );
for (int i = 0; i <= n; i++)
for (int j = 0; j <= m; j++)
dp[i][j] = -inf;
dp[0][0] = 0;
for (auto it : vec)
{
for (int l = last + 1; l <= it.fr; l++)
for (int j = 0; j <= m; j++)
dp[l][j] = max( dp[l][j], dp[l - 1][j] );
for (int j = 1; j <= m; j++)
dp[it.fr][j] = max( dp[ tin[it.sc] - 1 ][j - 1] + a[it.sc], dp[it.fr][j] );
last = it.fr;
}
for (int j = 1; j <= n; j++)
ans = max(dp[j][m], ans);
cout << ans << endl;
}
Compilation message
biochips.cpp:33:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main()
^
biochips.cpp: In function 'int main()':
biochips.cpp:39:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &x, &a[i]);
~~~~~^~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
508 KB |
Output is correct |
3 |
Correct |
2 ms |
508 KB |
Output is correct |
4 |
Correct |
20 ms |
18720 KB |
Output is correct |
5 |
Correct |
23 ms |
21020 KB |
Output is correct |
6 |
Correct |
27 ms |
21292 KB |
Output is correct |
7 |
Correct |
418 ms |
301308 KB |
Output is correct |
8 |
Correct |
442 ms |
302888 KB |
Output is correct |
9 |
Correct |
586 ms |
369744 KB |
Output is correct |
10 |
Correct |
589 ms |
409256 KB |
Output is correct |