Submission #684986

#TimeUsernameProblemLanguageResultExecution timeMemory
684986saayan007Biochips (IZhO12_biochips)C++14
10 / 100
108 ms17912 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; using pi = pair<int, int>; using pl = pair<ll, ll>; using vi = vector<int>; using vl = vector<ll>; using vpi = vector<pi>; using vpl = vector<pl>; #define fur(i, a, b) for(ll i = a; i <= (ll)b; ++i) #define ruf(i, a, b) for(ll i = a; i >= (ll)b; --i) #define fr first #define sc second #define mp make_pair #define pb push_back #define nl "\n" #define all(x) (x).begin(),(x).end() #define rall(x) (x).rbegin(),(x).rend() const ll mxn = 200001; const ll inf = 1e10L; ll n, m; ll root; ll p[mxn + 1]; vl adj[mxn + 1]; ll s[mxn + 1]; ll dfs(ll cur, ll src, ll bit, bool ch) { ll ans = 0; if(bit & (1 << cur)) { if(!ch) return -inf; ans += s[cur]; ch = 0; } for(auto u : adj[cur]) { if(u != src) { ans += dfs(u, cur, bit, ch); } } return ans; } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> m; fur(i, 1, n) { cin >> p[i]; cin >> s[i]; if(p[i] != 0) { adj[i].pb(p[i]); adj[p[i]].pb(i); } else { root = i; } } ll res = -inf; fur(i, 0, (1 << n) - 1) { if(__builtin_popcountll(i) != m) { continue; } res = max(res, dfs(root, 0, i, 1)); } cout << res << nl; }
#Verdict Execution timeMemoryGrader output
Fetching results...