# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
340153 | tengiz05 | Biochips (IZhO12_biochips) | C++17 | 395 ms | 403372 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
typedef long long ll;
using namespace std;
template<class T> bool chmin(T& a, const T& b){return a>b?a=b,true:false;}
template<class T> bool chmax(T& a, const T& b){return a<b?a=b,true:false;}
const int N = 2e5+5, M = 505;
const int inf = 1e9;
int n, m;
int dp[N][M], w[N];
vector<int> edges[N];
int timer = 1;
int v[N], l[N];
void dfs(int u){
int tmp = timer;
for(auto v : edges[u])
dfs(v);
v[timer] = w[u];
l[timer] = tmp;
timer++;
}
void Solve(){
cin >> n >> m;
int root;
for(int i=1;i<=n;i++){
int j, cost;
cin >> j >> cost;
if(j > 0)edges[j].push_back(i);
else root = i;
w[i] = cost;
}
dfs(root);
for(int i=0;i<=n;i++){
for(int j=1;j<=m;j++){
dp[i][j] = -inf;
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
dp[i][j] = max(dp[i-1][j], dp[l[i]-1][j-1]+v[i]);
}
}
cout << dp[n][m] << '\n';
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie();
int t=1;
while(t--)Solve();
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |