description: Two players, Red and Blue, are at it again, and this time they're playing with crayons! The mischievous duo is now vandalizing a rooted tree, by coloring the nodes while playing their favorite game.
  
  The game works as follows: there is a tree of size n, rooted at node 1, where each node is initially white. Red and Blue get one turn each. Red goes first. 
  
  In Red's turn, he can do the following operation any number of times: 
  
    * Pick any subtree of the rooted tree, and color every node in the subtree red. 
  
  However, to make the game fair, Red is only allowed to color k nodes of the tree. In other words, after Red's turn, at most k of the nodes can be colored red.
  
  Then, it's Blue's turn. Blue can do the following operation any number of times: 
  
    * Pick any subtree of the rooted tree, and color every node in the subtree blue. However, he's not allowed to choose a subtree that contains a node already colored red, as that would make the node purple and no one likes purple crayon. 
  
  Note: there's no restriction on the number of nodes Blue can color, as long as he doesn't color a node that Red has already colored.
  
  After the two turns, the score of the game is determined as follows: let w be the number of white nodes, r be the number of red nodes, and b be the number of blue nodes. The score of the game is w ⋅ (r - b).
  
  Red wants to maximize this score, and Blue wants to minimize it. If both players play optimally, what will the final score of the game be?
  
  Input
  
  The first line contains two integers n and k (2 ≤ n ≤ 2 ⋅ 10^5; 1 ≤ k ≤ n) — the number of vertices in the tree and the maximum number of red nodes.
  
  Next n - 1 lines contains description of edges. The i-th line contains two space separated integers u_i and v_i (1 ≤ u_i, v_i ≤ n; u_i ≠ v_i) — the i-th edge of the tree.
  
  It's guaranteed that given edges form a tree.
  
  Output
  
  Print one integer — the resulting score if both Red and Blue play optimally.
  
  Examples
  
  Input
  
  
  4 2
  1 2
  1 3
  1 4
  
  
  Output
  
  
  1
  
  
  Input
  
  
  5 2
  1 2
  2 3
  3 4
  4 5
  
  
  Output
  
  
  6
  
  
  Input
  
  
  7 2
  1 2
  1 3
  4 2
  3 5
  6 3
  6 7
  
  
  Output
  
  
  4
  
  
  Input
  
  
  4 1
  1 2
  1 3
  1 4
  
  
  Output
  
  
  -1
  
  Note
  
  In the first test case, the optimal strategy is as follows: 
  
    * Red chooses to color the subtrees of nodes 2 and 3. 
    * Blue chooses to color the subtree of node 4. 
  
  At the end of this process, nodes 2 and 3 are red, node 4 is blue, and node 1 is white. The score of the game is 1 ⋅ (2 - 1) = 1.
  
  In the second test case, the optimal strategy is as follows: 
  
    * Red chooses to color the subtree of node 4. This colors both nodes 4 and 5. 
    * Blue does not have any options, so nothing is colored blue. 
  
  At the end of this process, nodes 4 and 5 are red, and nodes 1, 2 and 3 are white. The score of the game is 3 ⋅ (2 - 0) = 6.
  
  For the third test case:
  
  <image>
  
  The score of the game is 4 ⋅ (2 - 1) = 4.
solution: #include <bits/stdc++.h>
  using namespace std;
  const int mxN = 2e5 + 5;
  vector<int> ad[mxN];
  int in[mxN];
  int dep[mxN];
  int sz[mxN];
  int par[mxN];
  int timer;
  int tree[4 * mxN];
  int lazy[4 * mxN];
  int id[4 * mxN];
  bool removed[mxN];
  int node[mxN];
  void dfs(int u, int p) {
    par[u] = p;
    dep[u] = dep[p] + 1;
    sz[u] = 1;
    in[u] = ++timer;
    node[timer] = u;
    for (int v : ad[u]) {
      if (v == p) continue;
      dfs(v, u);
      sz[u] += sz[v];
    }
  }
  void propagate(int x, int lx, int rx) {
    if (lx == rx) return;
    lazy[x << 1] += lazy[x];
    lazy[x << 1 | 1] += lazy[x];
    tree[x << 1] += lazy[x];
    tree[x << 1 | 1] += lazy[x];
    lazy[x] = 0;
  }
  void update(int x, int lx, int rx, int l, int r, int del) {
    if (lazy[x]) propagate(x, lx, rx);
    if (l > rx || lx > r) return;
    if (lx == rx) id[x] = node[lx];
    if (l <= lx && r >= rx) {
      tree[x] += del;
      lazy[x] += del;
      return;
    }
    int mx = (lx + rx) >> 1;
    update(x << 1, lx, mx, l, r, del);
    update(x << 1 | 1, mx + 1, rx, l, r, del);
    tree[x] = max(tree[x << 1], tree[x << 1 | 1]);
    if (tree[x << 1] > tree[x << 1 | 1])
      id[x] = id[x << 1];
    else
      id[x] = id[x << 1 | 1];
  }
  pair<int, int> query(int x, int lx, int rx, int l, int r) {
    if (lazy[x]) propagate(x, lx, rx);
    if (l > rx || lx > r) return {0, 0};
    if (l <= lx && r >= rx) return {tree[x], id[x]};
    int mx = (lx + rx) >> 1;
    auto a = query(x << 1, lx, mx, l, r);
    auto b = query(x << 1 | 1, mx + 1, rx, l, r);
    if (a.first > b.first) return a;
    return b;
  }
  long long mini(long long w, long long r, long long n) {
    long long b = 2 * w + 2 * r - n;
    vector<long long> v = {0, n - w - r, -b / 2, -b / 2 + 1, -b / 2 - 1};
    long long ret = 1e18;
    for (long long x : v) {
      if (x >= 0 && x <= n - w - r) ret = min(ret, x * x + b * x);
    }
    return ret;
  }
  void testCase() {
    int n, k;
    cin >> n >> k;
    for (int i = 1; i < n; i++) {
      int u, v;
      cin >> u >> v;
      ad[u].push_back(v);
      ad[v].push_back(u);
    }
    dfs(1, 0);
    for (int i = 1; i <= n; i++) {
      update(1, 1, n, in[i], in[i], dep[i] - 1);
    }
    removed[0] = true;
    long long ans = -1e18;
    long long wp = 0;
    for (long long r = 1; r <= k; r++) {
      auto q = query(1, 1, n, 1, n);
      wp += q.first;
      int cur = q.second;
      while (!removed[cur]) {
        update(1, 1, n, in[cur], in[cur] + sz[cur] - 1, -1);
        removed[cur] = true;
        cur = par[cur];
      }
      long long val = wp * r - wp * n + wp * wp + wp * r + mini(wp, r, n);
      ans = max(ans, val);
    }
    cout << ans << "\n";
  }
  int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    testCase();
    return 0;
  }
  