#include <bits/stdc++.h>
// author : aykhn
using namespace std;
typedef long long ll;
#define pb push_back
#define ins insert
#define mpr make_pair
#define all(v) v.begin(), v.end()
#define bpc __builtin_popcount
#define pii pair<int, int>
#define pll pair<ll, ll>
#define fi first
#define se second
#define int ll
#define infll 0x3F3F3F3F3F3F3F3F
#define inf 0x3F3F3F3F
const int MXN = 1e4 + 5;
const int MXM = 1e2 + 5;
int n, m, r;
vector<int> adj[MXN];
int p[MXN], c[MXN];
int dp[MXN][MXM];
void dfs(int a)
{
for (int v : adj[a]) dfs(v);
int n1 = adj[a].size();
vector<vector<int>> dp1(n1 + 1, vector<int> (m + 1, 0));
for (int i = 1; i <= adj[a].size(); i++)
{
int v = adj[a][i - 1];
for (int j = 0; j <= m; j++)
{
for (int k = 0; k <= j; k++)
{
dp1[i][j] = max(dp1[i][j], dp1[i - 1][j - k] + dp[v][k]);
}
}
}
dp[a][1] = max(c[a], dp1[n1][1]);
for (int i = 2; i <= m; i++) dp[a][i] = dp1[n1][i];
}
signed main()
{
cin >> n >> m;
for (int i = 1; i <= n; i++)
{
cin >> p[i] >> c[i];
if (!p[i])
{
r = i;
continue;
}
adj[p[i]].pb(i);
}
dfs(r);
cout << dp[r][m] << '\n';
}
Compilation message
biochips.cpp: In function 'void dfs(ll)':
biochips.cpp:34:23: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
34 | for (int i = 1; i <= adj[a].size(); i++)
| ~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2480 KB |
Output isn't correct |
3 |
Correct |
1 ms |
2648 KB |
Output is correct |
4 |
Correct |
21 ms |
9308 KB |
Output is correct |
5 |
Correct |
34 ms |
9308 KB |
Output is correct |
6 |
Correct |
69 ms |
9292 KB |
Output is correct |
7 |
Runtime error |
3 ms |
4952 KB |
Execution killed with signal 11 |
8 |
Runtime error |
4 ms |
4956 KB |
Execution killed with signal 11 |
9 |
Runtime error |
9 ms |
5724 KB |
Execution killed with signal 11 |
10 |
Runtime error |
6 ms |
5724 KB |
Execution killed with signal 11 |