#include <bits/stdc++.h>
#define mp make_pair
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define inf 2e9
#define ff first
#define ss second
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
const int N = 200000 + 1, M = 500 + 1;
int n, m, sz[N], w[N], dp[N][M];
vector <int> v[N], ord;
void dfs(int x){
ord.pb(x);
for (auto u: v[x]){
dfs(u);
sz[x] += sz[u];
}
sz[x]++;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
// freopen("input.txt", "r", stdin);
cin >> n >> m;
int st;
for (int i = 0; i < n; i++){
int x;
cin >> x >> w[i];
if (x)
v[x-1].pb(i);
else st = i;
}
dfs(st);
memset(dp, -1, sizeof dp);
for (int i = 0; i <= ord.size(); i++)
dp[i][0] = 0;
int ans = 0;
for (int i = ord.size()-1; i >= 0; i--){
for (int j = 1; j <= m; j++){
if (dp[i+sz[ord[i]]][j-1] != -1)
dp[i][j] = dp[i+sz[ord[i]]][j-1] + w[ord[i]];
dp[i][j] = max(dp[i][j], dp[i + 1][j]);
}
ans = max(ans, dp[i][m]);
}
cout << ans;
return 0;
}
Compilation message
beauty.cpp: In function 'int main()':
beauty.cpp:41:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
41 | for (int i = 0; i <= ord.size(); i++)
| ~~^~~~~~~~~~~~~
beauty.cpp:39:8: warning: 'st' may be used uninitialized in this function [-Wmaybe-uninitialized]
39 | dfs(st);
| ~~~^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
29 ms |
9964 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |