제출 #519313

#제출 시각아이디문제언어결과실행 시간메모리
519313MonarchuwuBiochips (IZhO12_biochips)C++17
100 / 100
268 ms401700 KiB
#include<iostream> #include<algorithm> #include<cstring> #include<numeric> #include<vector> using namespace std; typedef long long ll; const int N = 2e5 + 8; int n, m, a[N]; vector<int> g[N]; int tme, tin[N], tout[N]; int inv[N]; void dfs(int u) { inv[tin[u] = ++tme] = u; for (int v : g[u]) dfs(v); tout[u] = tme; } int dp[N][502]; int main() { cin.tie(NULL)->sync_with_stdio(false); cin >> n >> m; for (int i = 1, p; i <= n; ++i) { cin >> p >> a[i]; g[p].push_back(i); } dfs(g[0][0]); memset(dp[n + 1], 0x80, sizeof(dp[n + 1])); dp[n + 1][0] = 0; for (int i = n; i; --i) { copy(dp[i + 1], dp[i + 1] + m + 1, dp[i]); for (int j = 1; j <= m; ++j) dp[i][j] = max(dp[i][j], dp[tout[inv[i]] + 1][j - 1] + a[inv[i]]); } cout << (dp[1][m] < 0 ? -1 : dp[1][m]) << '\n'; } /** /\_/\ * (= ._.) * / >0 \>1 **/
#Verdict Execution timeMemoryGrader output
Fetching results...