11 • 3个月前
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <string>
#include <queue>
#include <map>
#include <bitset>
#include <vector>
void fre() { system("clear"), freopen("A.txt", "r", stdin); freopen("Ans.txt","w",stdout); }
void Fre() { system("clear"), freopen("A.txt", "r", stdin);}
#define ios ios::sync_with_stdio(false)
#define Pi acos(-1)
#define pb push_back
#define fi first
#define se second
#define ll long long
#define ull unsigned long long
#define db double
#define Pir pair<int, int>
#define m_p make_pair
#define INF 0x3f3f3f3f
#define esp 1e-7
#define for_(i, s, e) for(int i = (ll)(s); i <= (ll)(e); i ++)
#define rep_(i, e, s) for(int i = (ll)(e); i >= (ll)(s); i --)
#define sc scanf
#define pr printf
#define sd(a) scanf("%d", &a)
#define ss(a) scanf("%s", a)
#define size() size() * 1LL
#define mod (ll)(10007)
#define Min(a, b, c) min(a, min(b, c))
#define Max(a, b, c) max(a, max(b, c))
using namespace std;
const int mxn = 150;
int dp[mxn][mxn];
vector<int> e[mxn];
int w[mxn];
int n, m;
void dfs(int u)
{
for_(i, 0, e[u].size() - 1)
{
int v = e[u][i];
dfs(v);
rep_(i, m, 0)
{
for_(j, 0, i)
{
dp[u][i] = max(dp[u][i], dp[v][j] + dp[u][i - j]);
}
}
}
rep_(i, m, 1)
dp[u][i] = dp[u][i - 1] + w[u];
}
int main()
{
sc("%d %d", &n, &m);
m ++;
int u;
for_(i, 1, n)
{
sc("%d %d", &u, &w[i]);
e[u].pb(i);
}
dfs(0);
pr("%d\n", dp[0][m]);
return 0;
}
评论:
请先登录,才能进行评论