Submission #673382

# Submission time Handle Problem Language Result Execution time Memory
673382 2022-12-20T13:01:54 Z smartmonky Biochips (IZhO12_biochips) C++14
100 / 100
450 ms 292920 KB
#include <bits/stdc++.h>

#define ff first
#define ss second
#define pb push_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define int long long

using namespace std;

void fp(string name){freopen((name+".in").c_str(),"r",stdin); freopen((name+".out").c_str(),"w",stdout);}
const int N = 200005;
vector <int> g[N];
int coast[N], dp[505][N], sz[N];
int m;
void dfs(int v){
	sz[v] = 1;
	for(auto to : g[v]){
		dfs(to);
		for(int i = min(sz[v], m); i >= 0; i--){
			for(int j = min(sz[to], m - i); j >= 0; j--){
				if(i + j <= m)
				dp[i + j][v] = max(dp[i][v] + dp[j][to], dp[i + j][v]);
			}
		}
		sz[v] += sz[to];
	}
	dp[1][v] = max(dp[1][v], coast[v]);
}
main(){
	ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
	//fp("game");
	int n;
	cin >> n >> m;
	int ind = -1;
	for(int i = 0; i < n; i++){
		int a, b;
		cin >> a >> b;
		coast[i + 1] = b;
		if(a == 0){
			ind = i + 1;
			continue;
		}
		g[a].pb(i + 1);
	}
	dfs(ind);
	cout << dp[m][ind];
}

Compilation message

biochips.cpp:31:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   31 | main(){
      | ^~~~
biochips.cpp: In function 'void fp(std::string)':
biochips.cpp:12:29: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   12 | void fp(string name){freopen((name+".in").c_str(),"r",stdin); freopen((name+".out").c_str(),"w",stdout);}
      |                      ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
biochips.cpp:12:70: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   12 | void fp(string name){freopen((name+".in").c_str(),"r",stdin); freopen((name+".out").c_str(),"w",stdout);}
      |                                                               ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 5076 KB Output is correct
4 Correct 8 ms 9940 KB Output is correct
5 Correct 10 ms 11604 KB Output is correct
6 Correct 13 ms 13632 KB Output is correct
7 Correct 339 ms 283152 KB Output is correct
8 Correct 332 ms 292920 KB Output is correct
9 Correct 325 ms 17048 KB Output is correct
10 Correct 450 ms 19020 KB Output is correct