public int[] recurse(int node, int[] max) {
    int[] newmax = max.clone();
    int[] childmax = max.clone();

    for (int child : children[node]) {
      childmax = recurse(child, childmax);
    }

    for (int x = max.length - 1; x >= 0; x--) {
      if (x != 0 && newmax[x - 1] > 0) {
        newmax[x] = Math.max(newmax[x - 1] + worths[node], childmax[x]);
      } else if (x == 0) {
        newmax[x] = Math.max(worths[node], childmax[x]);
      } else {
        newmax[x] = childmax[x];
      }
    }

    return newmax;
  }