Submission #712742

#TimeUsernameProblemLanguageResultExecution timeMemory
712742noeditMagic Tree (CEOI19_magictree)C++17
34 / 100
404 ms1048576 KiB
#include <bits/stdc++.h> //#include <quadmath.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> //#define sz(x) (int)x.size() //#define sqr(x) x*x //#pragma GCC optimize("-O3") //#pragma GCC optimize("Ofast") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,popcnt,abm,mmx,avx,avx2,tune=native") //#pragma GCC optimize("unroll-loops") //#pragma GCC optimize("no-stack-protector") //#pragma GCC optimize("fast-math") using namespace std; using namespace __gnu_pbds; //#define int long long //#define ld long double template <class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; typedef long long ll; const int INF = 2e9; vector<vector<int> > g; vector<pair<int, int> > a; vector<vector<ll> > dp; int k; void dfs(int v, int p) { for (auto& u : g[v]) { if (u != p) { dfs(u, v); } } ll bebra = 0; if (a[v].first != INF){ for (auto& u : g[v]) { if (u != p) bebra += dp[u][a[v].first]; } } for (int i = 1; i <= k; i++) { ll res = 0; for (auto& u : g[v]){ if (u != p) res += dp[u][i]; } dp[v][i] = res; if (a[v].first <= i) { dp[v][i] = max(dp[v][i], bebra + a[v].second); } } } void solve() { int n, m; cin >> n >> m >> k; g.resize(n); a.resize(n, {INF, 0}); dp.resize(n, vector<ll>(k + 1)); for (int i = 1; i < n; i++) { int p; cin >> p; p--; g[p].push_back(i); } for (int i = 0; i < m; i++) { int v, d, w; cin >> v >> d >> w; v--; a[v] = {d, w}; } dfs(0, 0); ll ans = 0; // for (int i = 0; i < n; i++) // { // cout << i << ": "; // for (int j = 1; j <= k; j++) // { // cout << dp[i][j] << ' '; // } // cout << endl; // } for (int i = 1; i <= k; i++) ans = max(ans, dp[0][i]); cout << ans; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t; t = 1; //cin >> t; while (t--) { solve(); } return 0; } /* 6 4 10 1 2 1 4 4 3 4 5 4 7 2 5 4 1 6 9 3 */ /* 4 1 3 2 4 2 3 3 4 */ /* 1 1 1 100000000 1 1 1 1 1 */ /* ???????????????????????????????????????????????? */
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...