# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
333844 | limabeans | 바이오칩 (IZhO12_biochips) | C++17 | 2084 ms | 15292 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
template<typename T>
void out(T x) { cout << x << endl; exit(0); }
#define watch(x) cout << (#x) << " is " << (x) << endl
using ll = long long;
const int maxn = 2e5+10;
int n, m;
vector<int> g[maxn];
int a[maxn];
vector<int> dp[maxn];
vector<int> merge(vector<int> v, vector<int> w) {
int N = v.size();
int M = w.size();
vector<int> res(N+M,0);
for (int i=0; i<N; i++) {
for (int j=0; j<M; j++) {
res[i+j] = max(res[i+j], v[i]+w[j]);
}
}
return res;
}
void dfs(int at) {
dp[at].push_back(0);
for (int to: g[at]) {
dfs(to);
dp[at] = merge(dp[at], dp[to]);
}
while ((int)dp[at].size()<2) dp[at].push_back(0);
dp[at][1] = max(dp[at][1], a[at]);
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin>>n>>m;
int root = -1;
for (int i=1; i<=n; i++) {
int p; cin>>p;
if (p==0) {
root=i;
} else {
g[p].push_back(i);
}
cin>>a[i];
}
dfs(root);
cout<<dp[root][m]<<endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |