답안 #673292

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
673292 2022-12-20T05:57:54 Z Baytoro 바이오칩 (IZhO12_biochips) C++17
60 / 100
407 ms 524288 KB
#include <bits/stdc++.h>
using namespace std;
#define ios ios::sync_with_stdio(false); cin.tie(NULL);
#define pb push_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define fr first
#define sc second
#define int long long
#define endl '\n'
#define cnt(x) __builtin_popcount(x)
void fopn(string name){
	freopen((name+".in").c_str(),"r",stdin);
	freopen((name+".out").c_str(),"w",stdout);
}
const int INF=1e18,mod=998244353;
int n,m;
const int N=2e5+5;
int a[N],sz[N];
int dp[N][505];
vector<int> g[N];
void dfs(int x, int p){
	sz[x]=1;
	dp[x][0]=0;
	for(auto it: g[x]){
		if(it==p) continue;
		dfs(it,x);
		vector<int> tmp(m+1);
		for(int i=0;i<=min(sz[x],m);i++)
			for(int j=0;j<=min(m-i,sz[it]);j++)
				tmp[i+j]=max(tmp[i+j],dp[x][i]+dp[it][j]);
		sz[x]+=sz[it];
		for(int i=1;i<=min(m,sz[x]);i++)
			dp[x][i]=tmp[i];
		
	}
	dp[x][1]=max(dp[x][1],a[x]);
}
void solve(){
	cin>>n>>m;
	int r=0;
	for(int i=1;i<=n;i++){
		int p;cin>>p>>a[i];
		if(!p)r=i;
		else{
			g[p].pb(i);
			g[i].pb(p);
		}
	}
	dfs(r,r);
	cout<<dp[r][m]<<endl;
}
main(){
	//fopn("newbarn");
	ios;
	int T=1;
	//cin>>T;
	while(T--){
		solve();
	}
}

Compilation message

biochips.cpp:53:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   53 | main(){
      | ^~~~
biochips.cpp: In function 'void fopn(std::string)':
biochips.cpp:13:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   13 |  freopen((name+".in").c_str(),"r",stdin);
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
biochips.cpp:14:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   14 |  freopen((name+".out").c_str(),"w",stdout);
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 3 ms 5032 KB Output is correct
3 Correct 4 ms 5332 KB Output is correct
4 Correct 20 ms 40640 KB Output is correct
5 Correct 22 ms 45172 KB Output is correct
6 Correct 26 ms 45092 KB Output is correct
7 Runtime error 310 ms 524288 KB Execution killed with signal 9
8 Runtime error 312 ms 524288 KB Execution killed with signal 9
9 Runtime error 367 ms 524288 KB Execution killed with signal 9
10 Runtime error 407 ms 524288 KB Execution killed with signal 9