#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define sz(x) (int)x.size()
#define ALL(v) v.begin(),v.end()
#define MASK(k) (1LL << (k))
#define BIT(x, i) (((x) >> (i)) & 1)
#define oo (ll)1e18
#define INF (ll)1e9
#define MOD (ll)(998244353)
using namespace std;
template<class T1, class T2>
bool maximize(T1 &a, T2 b){if(a < b){a = b; return true;} return false;}
template<class T1, class T2>
bool minimize(T1 &a, T2 b){if(a > b){a = b; return true;} return false;}
template<class T1, class T2>
void add(T1 &a, T2 b){a += b; if(a >= MOD) a -= MOD;}
template<class T1, class T2>
void sub(T1 &a, T2 b){a -= b; if(a < 0) a += MOD;}
template<class T>
void cps(T &v){sort(ALL(v)); v.resize(unique(ALL(v)) - v.begin());}
const int MAXN = 2e5 + 10, MAXK = 503;
int N, K;
vector<int> adj[MAXN];
int value[MAXN], cnt[MAXN];
int f[MAXN][MAXK];
void dfs(int u){
cnt[u] = 0;
for(int v: adj[u]){
dfs(v);
for(int i=cnt[u]; i>=0; i--){
for(int j=min(cnt[v], K - i); j>=0; j--){
maximize(f[u][i + j], f[v][j] + f[u][i]);
}
}
cnt[u] += cnt[v];
minimize(cnt[u], K);
}
maximize(f[u][1], value[u]);
cnt[u]++;
}
void solve(){
cin >> N >> K;
memset(f, -0x3f, sizeof f);
int root = 0;
for(int i=1; i<=N; i++){
int p; cin >> p >> value[i];
if(p != 0) adj[p].push_back(i);
else root = i;
f[i][0] = 0;
}
dfs(root);
cout << f[root][K] << '\n';
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
// freopen("numtable.inp","r",stdin);
// freopen("numtable.out","w",stdout);
solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
171 ms |
398676 KB |
Output is correct |
2 |
Correct |
203 ms |
398708 KB |
Output is correct |
3 |
Correct |
176 ms |
398676 KB |
Output is correct |
4 |
Correct |
179 ms |
399188 KB |
Output is correct |
5 |
Correct |
197 ms |
399184 KB |
Output is correct |
6 |
Correct |
183 ms |
399164 KB |
Output is correct |
7 |
Correct |
272 ms |
402516 KB |
Output is correct |
8 |
Correct |
280 ms |
402616 KB |
Output is correct |
9 |
Correct |
340 ms |
402768 KB |
Output is correct |
10 |
Correct |
409 ms |
403284 KB |
Output is correct |