#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, 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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Incorrect |
2 ms |
4948 KB |
Output isn't correct |
3 |
Incorrect |
2 ms |
4948 KB |
Output isn't correct |
4 |
Incorrect |
5 ms |
5412 KB |
Output isn't correct |
5 |
Incorrect |
5 ms |
5548 KB |
Output isn't correct |
6 |
Incorrect |
5 ms |
5460 KB |
Output isn't correct |
7 |
Incorrect |
36 ms |
13908 KB |
Output isn't correct |
8 |
Incorrect |
40 ms |
13908 KB |
Output isn't correct |
9 |
Incorrect |
37 ms |
15424 KB |
Output isn't correct |
10 |
Incorrect |
86 ms |
16396 KB |
Output isn't correct |