#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, pref[N][505];
vector < vector <int> > g;
vector < pair <int, int> > vec;
void dfs (int v)
{
if (g[v].empty() )
tin[v] = ++tiktak;
for (auto to : g[v]){
dfs(to);
if (!tin[v])
tin[v] = tin[to];
else
tin[v] = min( tin[v], tin[to] );
}
vec.pb( mk( tiktak, 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, pref[i][j] = -inf;
dp[0][0] = 0;
pref[0][0] = 0;
for (auto it : vec)
{
for (int j = 1; j <= m; j++)
dp[it.fr][j] = pref[ tin[it.sc] - 1 ][j - 1] + a[it.sc];
for (int j = 0; j <= m; j++)
pref[it.fr][j] = max( dp[it.fr][j], max( pref[it.fr - 1][j], pref[it.fr][j] ) );
}
cout << pref[vec.back().fr][m] << endl;
}
/**
7 2
2 100
0 1000
2 150
3 100
3 5
5 100
2 50
**/
Compilation message
biochips.cpp:37:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main()
^
biochips.cpp: In function 'int main()':
biochips.cpp:43:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &x, &a[i]);
~~~~~^~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
500 KB |
Output is correct |
3 |
Correct |
2 ms |
932 KB |
Output is correct |
4 |
Correct |
34 ms |
36380 KB |
Output is correct |
5 |
Correct |
34 ms |
40728 KB |
Output is correct |
6 |
Correct |
35 ms |
40848 KB |
Output is correct |
7 |
Runtime error |
443 ms |
525312 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
8 |
Halted |
0 ms |
0 KB |
- |