This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/* Ignut
started: 13.08.2024
now: 13.08.2024
████████████████████████████████████████████████████████████████████
████████████████████████████████    ████████████████████████████████
██████████████████████████████        ██████████████████████████████
██████      ██████████████████        ██████████████████      ██████
██████          ██████████████        ██████████████          ██████
██████      ██    ████████████        ████████████    ██      ██████
██████      ████    ██████████        ██████████    ████      ██████
██████      ████      ██████████    ██████████      ████      ██████
██████      ████      ██████████    ██████████    ██████      ██████
██████      ██████    ██████████    ██████████    ██████      ██████
██████      ██████    ████████        ████████    ██████      ██████
██████      ██████      ██████        ██████      ██████      ██████
██████      ████        ████            ████        ████      ██████
██████            ██████████    ████    ██████████            ██████
██████      ██      ██████    ████████    ██████      ██      ██████
██████      ██████            ████████            ██████      ██████
██████                    ██            ██                    ██████
██████████████████████      ████    ████      ██████████████████████
████████████████████████      ██    ██      ████████████████████████
██████████████████████████                ██████████████████████████
██████████████████████████████        ██████████████████████████████
████████████████████████████████████████████████████████████████████
*/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MAXN = 2e5 + 123;
vector<pair<int, int>> tree[MAXN];
ll dx[MAXN], dy[MAXN];
void dfs(int v, int par, ll d, bool f) {
    if (f) dx[v] = d;
    else dy[v] = d;
    for (auto [to, w] : tree[v])
        if (to != par)
            dfs(to, v, d + w, f);
}
bool takeX[MAXN];
bool takeY[MAXN];
void dfsX(int v) {
    takeX[v] = true;
    for (auto [to, w] : tree[v])
        if (dx[to] < dx[v])
            dfsX(to);
}
void dfsY(int v) {
    takeY[v] = true;
    for (auto [to, w] : tree[v])
        if (dy[to] < dy[v])
            dfsY(to);
}
int max_score(int N, int X, int Y, ll K, vector<int> U, vector<int> V, vector<int> W) {
    for (int i = 0; i < N; i ++)
        tree[i].clear();
    for (int i = 0; i < N - 1; i ++) {
        tree[U[i]].push_back({V[i], W[i]});
        tree[V[i]].push_back({U[i], W[i]});
    }
    dfs(X, -1, 0, true);
    dfs(Y, -1, 0, false);
    int res = 0;
    for (int mask = 0; mask < 1 << N; mask ++) {
        for (int i = 0; i < N; i ++) takeX[i] = takeY[i] = false;
        ll sum = 0;
        int ans = 0;
        for (int i = 0; i < N; i ++) {
            if (mask & (1 << i)) {
                dfsX(i);
                dfsY(i);
            }
        }
        vector<ll> ds;
        for (int i = 0; i < N; i ++) {
            ans += takeX[i] + takeY[i];
            if (takeX[i] && takeY[i]) sum += max(dx[i], dy[i]);
            else if (takeX[i] && !takeY[i]) sum += dx[i];
            else if (!takeX[i] && takeY[i]) sum += dy[i];
            if (!takeX[i]) ds.push_back(dx[i]);
            if (!takeY[i]) ds.push_back(dy[i]);
        }
        sort(ds.begin(), ds.end());
        for (int i = 0; i < ds.size(); i ++) {
            if (sum + ds[i] <= K) {
                sum += ds[i];
                ans ++;
            }
        }
        if (sum <= K) res = max(res, ans);
    }
    return res;
}
Compilation message (stderr)
closing.cpp: In function 'int max_score(int, int, int, ll, std::vector<int>, std::vector<int>, std::vector<int>)':
closing.cpp:96:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   96 |         for (int i = 0; i < ds.size(); i ++) {
      |                         ~~^~~~~~~~~~~| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |