Submission #490788

# Submission time Handle Problem Language Result Execution time Memory
490788 2021-11-29T09:05:47 Z ntabc05101 Biochips (IZhO12_biochips) C++14
0 / 100
63 ms 8980 KB
#include<bits/stdc++.h>
using namespace std;

#define taskname ""

const long long inf = 1e18;
const int mxN = 10002;
const int mxM = 102;

int n, m;
int a[mxN];
vector<int> adj[mxN];
long long dp[mxN][mxM];

void dfs(int u, int p = -1) {
	for (int i = 1; i <= m; i++) {
		dp[u][i] = -inf;
	}
	for (auto &to: adj[u]) {
		if (to != p) {
			dfs(to, u);
			for (int k = m; k; k--) {
				for (int j = 1; j <= k; j++) {
					dp[u][k] = max(dp[u][k], dp[u][k - j] + dp[to][j]);
				}
			}
		}
	}
	dp[u][1] = max(dp[u][1], (long long)a[u]);	
	
	/*cout << 1 + u << ":\n";
	for (int i = 0; i <= m; i++) {
		cout << dp[u][i] << " ";
	}
	cout << "\n";*/
}

int main() {
	if (fopen(taskname".inp", "r")) {
		freopen(taskname".inp", "r", stdin);
		freopen(taskname".out", "w", stdout);
	}
	else {
		if (fopen(taskname".in", "r")) {
			freopen(taskname".in", "r", stdin);
			freopen(taskname".out", "w", stdout);
		}
	}

	cin.tie(0)->sync_with_stdio(0);

	cin >> n >> m;
	int root = -1;
	for (int i = 0, x, y; i < n; i++) {
		cin >> x >> y; x--;
		a[i] = y;
		if (!(~x)) {
			root = i; continue;
		}
		adj[x].push_back(i);
		adj[i].push_back(x);
	}

	dfs(root);
	cout << dp[root][m] << "\n";

	return 0;
}

Compilation message

biochips.cpp: In function 'int main()':
biochips.cpp:40:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   40 |   freopen(taskname".inp", "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
biochips.cpp:41:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   41 |   freopen(taskname".out", "w", stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
biochips.cpp:45:11: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   45 |    freopen(taskname".in", "r", stdin);
      |    ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
biochips.cpp:46:11: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   46 |    freopen(taskname".out", "w", stdout);
      |    ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 460 KB Output is correct
2 Correct 1 ms 460 KB Output is correct
3 Correct 1 ms 588 KB Output is correct
4 Correct 26 ms 7956 KB Output is correct
5 Correct 39 ms 8980 KB Output is correct
6 Correct 63 ms 8920 KB Output is correct
7 Runtime error 1 ms 972 KB Execution killed with signal 11
8 Halted 0 ms 0 KB -