# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
684988 | saayan007 | Biochips (IZhO12_biochips) | C++14 | 95 ms | 16404 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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) {
ll add = dfs(u, cur, bit, ch);
if(add < 0)
return -inf;
ans += add;
}
}
return ans;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
fur(i, 0, n - 1) {
cin >> p[i] >> s[i];
--p[i];
if(p[i] != -1) {
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, -1, i, 1));
}
cout << res << nl;
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |