Submission #683850

# Submission time Handle Problem Language Result Execution time Memory
683850 2023-01-19T13:31:24 Z vovamr Biochips (IZhO12_biochips) C++17
100 / 100
490 ms 414236 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define fi first
#define se second
#define ll long long
#define ld long double
#define sz(x) ((int)(x).size())
#define all(x) 	(x).begin(), (x).end()
#define pb push_back
#define mpp make_pair
#define ve vector
using namespace std;
using namespace __gnu_pbds;
template<class T> using oset = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
const ll inf = 1e18; const int iinf = 1e9;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
template <typename T> inline bool chmin(T& a, T b) { return (a > b ? a = b, 1 : 0); }
template <typename T> inline bool chmax(T& a, T b) { return (a < b ? a = b, 1 : 0); }

const int N = 2e5 + 100;
const int M = 520;

int n, m;

ve<int> gr[N];
int dp[N][M];

int a[N], sz[N];

int f[M];

inline void dfs(int v) {

	sz[v] = 1;
	for (auto &to : gr[v]) {
		dfs(to);
		sz[v] += sz[to];
	}

	dp[v][0] = 0;
	dp[v][1] = a[v];

	int have = 0;
	for (int i = 0; i <= min(m, sz[v]); ++i) f[i] = 0;

	for (auto &to : gr[v]) {
		for (int cur = min(m, have); cur >= 0; --cur) {
			for (int take = 0; take <= min(sz[to], m); ++take) {
				chmax(f[cur + take], f[cur] + dp[to][take]);
			}
		}
		have += sz[to];
	}
	for (int z = 0; z <= min(m, sz[v]); ++z) chmax(dp[v][z], f[z]);
}

inline void solve() {
	cin >> n >> m;

	int root = -1;
	for (int i = 0; i < n; ++i) {
		int p;
		cin >> p >> a[i], --p;

		if (!~p) root = i;
		else gr[p].pb(i);
	}

	dfs(root);

	int ans = 0;
	for (int i = 0; i < n; ++i) {
		chmax(ans, dp[i][m]);
	}
	cout << ans;
}

signed main() {
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	int q = 1; // cin >> q;
	while (q--) solve();
	cerr << fixed << setprecision(3) << "Time execution: " << (double)clock() / CLOCKS_PER_SEC << endl;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 4 ms 5160 KB Output is correct
4 Correct 15 ms 23292 KB Output is correct
5 Correct 16 ms 25636 KB Output is correct
6 Correct 17 ms 25684 KB Output is correct
7 Correct 276 ms 308892 KB Output is correct
8 Correct 293 ms 308900 KB Output is correct
9 Correct 378 ms 375228 KB Output is correct
10 Correct 490 ms 414236 KB Output is correct