#include <bits/stdc++.h>
using namespace std;
#define m_p make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
typedef long long ll;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
mt19937 rnf(2106);
const int N = 200005, M = 503, INF = 1000000009;
int n, m;
int a[N];
vector<int> g[N];
int tin[N], tout[N], ti;
void dfs(int x)
{
tin[x] = ++ti;
for (int i = 0; i < g[x].size(); ++i)
{
int h = g[x][i];
dfs(h);
}
tout[x] = ti;
}
vector<pair<int, int> > v[N];
int dp[N][M];
void solv()
{
cin >> n >> m;
for (int x = 1; x <= n; ++x)
{
int p;
cin >> p;
g[p].push_back(x);
cin >> a[x];
}
dfs(g[0][0]);
for (int x = 1; x <= n; ++x)
v[tout[x]].push_back(m_p(tin[x], a[x]));
for (int i = 0; i <= n; ++i)
{
for (int j = 0; j <= m; ++j)
{
dp[i][j] = -INF;
}
dp[i][0] = 0;
}
for (int i = 1; i <= n; ++i)
{
for (int q = 1; q <= m; ++q)
{
for (int j = 0; j < sz(v[i]); ++j)
{
dp[i][q] = max(dp[i][q], dp[v[i][j].fi - 1][q - 1] + v[i][j].se);
}
dp[i + 1][q] = max(dp[i + 1][q], dp[i][q]);
}
}
cout << dp[n][m] << "\n";
}
int main()
{
#ifdef SOMETHING
freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
#endif // SOMETHING
ios_base::sync_with_stdio(false), cin.tie(0);
int tt = 1;
//cin >> tt;
while (tt--)
{
solv();
}
return 0;
}
Compilation message
biochips.cpp: In function 'void dfs(int)':
biochips.cpp:21:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
21 | for (int i = 0; i < g[x].size(); ++i)
| ~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9676 KB |
Output is correct |
2 |
Correct |
6 ms |
9676 KB |
Output is correct |
3 |
Correct |
6 ms |
9932 KB |
Output is correct |
4 |
Correct |
20 ms |
27724 KB |
Output is correct |
5 |
Correct |
23 ms |
29960 KB |
Output is correct |
6 |
Correct |
24 ms |
29972 KB |
Output is correct |
7 |
Correct |
383 ms |
308904 KB |
Output is correct |
8 |
Correct |
409 ms |
308932 KB |
Output is correct |
9 |
Correct |
502 ms |
374248 KB |
Output is correct |
10 |
Correct |
615 ms |
412556 KB |
Output is correct |