#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 |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Incorrect |
2 ms |
4960 KB |
Output isn't correct |
3 |
Incorrect |
3 ms |
4948 KB |
Output isn't correct |
4 |
Incorrect |
5 ms |
5460 KB |
Output isn't correct |
5 |
Incorrect |
6 ms |
5468 KB |
Output isn't correct |
6 |
Incorrect |
5 ms |
5472 KB |
Output isn't correct |
7 |
Incorrect |
36 ms |
13920 KB |
Output isn't correct |
8 |
Incorrect |
36 ms |
13936 KB |
Output isn't correct |
9 |
Incorrect |
42 ms |
15460 KB |
Output isn't correct |
10 |
Incorrect |
95 ms |
16404 KB |
Output isn't correct |