#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <sstream>
#include <map>
#include <stack>
#include <set>
#include <queue>
#include <unordered_set>
#include <unordered_map>
#include <math.h>
#include <cmath>
#include <vector>
#include <iomanip>
#include <random>
#include <chrono>
using namespace std;
#define ll long long
#define fr first
#define sc second
#define pb push_back
#define US freopen("milkvisits.in", "r", stdin); freopen("milkvisits.out", "w", stdout);
ll gcd(ll a, ll b)
{
if (a == 0 || b == 0) {
return max(a, b);
}
if (a <= b) {
return gcd(a, b % a);
}
else {
return gcd(a % b, b);
}
}
ll lcm(ll a, ll b) {
return (a / gcd(a, b)) * b;
}
const int N = 300005;
const ll oo = 1000000000000000, MOD = 1000000007;
vector<int> g[N];
int dp[N][505], n, m, weight[N], sz[N];
void dfs(int u, int p) {
sz[u] = 1;
for (int i = 0; i < g[u].size(); ++i) {
int h = g[u][i];
if (h != p) {
dfs(h, u);
sz[u] += sz[h];
for (int j = min(sz[u], m); j >= 1; --j) {
for (int k = 1; k <= min(sz[h], j); ++k) {
dp[u][j] = max(dp[u][j], dp[h][k] + dp[u][j - k]);
}
}
}
}
dp[u][1] = max(dp[u][1], weight[u]);
}
void solve() {
cin >> n >> m;
int r = 1;
for (int i = 1; i <= n; ++i) {
int u, w;
cin >> u >> w;
if (u == 0) {
r = i;
}
else {
g[i].pb(u);
g[u].pb(i);
}
weight[i] = w;
}
dfs(r, -1);
cout << dp[r][m] << endl;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
//US
int tt = 1;
//cin >> tt;
while (tt--) {
solve();
}
}
Compilation message
biochips.cpp: In function 'void dfs(int, int)':
biochips.cpp:52:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
52 | for (int i = 0; i < g[u].size(); ++i) {
| ~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
11612 KB |
Output is correct |
2 |
Correct |
6 ms |
11612 KB |
Output is correct |
3 |
Correct |
3 ms |
11612 KB |
Output is correct |
4 |
Correct |
12 ms |
27508 KB |
Output is correct |
5 |
Correct |
13 ms |
29788 KB |
Output is correct |
6 |
Correct |
13 ms |
29804 KB |
Output is correct |
7 |
Correct |
344 ms |
306920 KB |
Output is correct |
8 |
Correct |
259 ms |
306824 KB |
Output is correct |
9 |
Correct |
408 ms |
372456 KB |
Output is correct |
10 |
Correct |
499 ms |
410336 KB |
Output is correct |