Observe that $\int_f w$ attains its maximum by Lemma \ref{lemma-max-int-w}. The set $Y_d$ is open by Lemma \ref{lemma-semicontinuous-int-w}. Thus the statement of the lemma makes sense. \medskip\noindent Reduction to the Noetherian case; please skip this paragraph. Recall that a finite type morphism is quasi-finite if and only if it has relative dimension $0$, see Morphisms, Lemma \ref{morphisms-lemma-locally-quasi-finite-rel-dimension-0}. By Lemma \ref{lemma-Noetherian-approximation-dimension-d} applied with $d = 0$ we can find a quasi-finite morphism $f_0 : X_0 \to Y_0$ of affine Noetherian schemes and a morphism $Y \to Y_0$ such that $f$ is the base change of $f_0$. Then we can write $Y = \lim Y_i$ as a directed limit of affine schemes of finite type over $Y_0$, see Algebra, Lemma \ref{algebra-lemma-ring-colimit-fp}. By Lemma \ref{lemma-descend-weighting} we can find an $i$ such that our weighting $w$ descends to a weighting $w_i$ of the base change $f_i : X_i \to Y_i$ of $f_0$. Now if the lemma holds for $f_i, w_i$, then it implies the lemma for $f$ as formation of $\int_f w$ commutes with base change, see Lemma \ref{lemma-weighting-check-after-etale-base-change}. \medskip\noindent Assume $X$ and $Y$ Noetherian. Let $X' \to Y'$ be the base change of $f$ by a morphism $g : Y' \to Y$. The formation of $\int_f w$ and hence the open $Y_d$ commute with base change. If $g$ is finite and surjective, then $Y'_d \to Y_d$ is finite and surjective. In this case proving that $Y_d$ is affine is equivalent to showing that $Y'_d$ is affine, see Cohomology of Schemes, Lemma \ref{coherent-lemma-image-affine-finite-morphism-affine-Noetherian}. \medskip\noindent We may choose an immersion $X \to T$ with $T$ finite over $Y$, see Lemma \ref{lemma-quasi-finite-separated-pass-through-finite}. We are going to apply Morphisms, Lemma \ref{morphisms-lemma-massage-finite} to the finite morphism $T \to Y$. This lemma tells us that there is a finite surjective morphism $Y' \to Y$ such that $Y' \times_Y T$ is a closed subscheme of a scheme $T'$ finite over $Y'$ which has a special form. By the discussion in the first paragraph, we may replace $Y$ by $Y'$, $T$ by $T'$, and $X$ by $Y' \times_Y X$. Thus we may assume there is an immersion $X \to T$ (not necessarily open or closed) and closed subschemes $T_i \subset T$, $i = 1, \ldots, n$ where \begin{enumerate} \item $T \to Y$ is finite (and locally free), \item $T_i \to Y$ is an isomorphism, and \item $T = \bigcup_{i = 1, \ldots, n} T_i$ set theoretically. \end{enumerate} Let $Y' = \coprod Y_k$ be the disjoint union of the irreducible components of $Y$ (viewed as integral closed subschemes of $Y$). Then we may base change once more by $Y' \to Y$; here we are using that $Y$ is Noetherian. Thus we may in addition assume $Y$ is integral and Noetherian. \medskip\noindent We also may and do assume that $T_i \not = T_j$ if $i \not = j$ by removing repeats. Since $Y$ and hence all $T_i$ are integral, this means that if $T_i$ and $T_j$ intersect, then they intersect in a closed subset which maps to a proper closed subset of $Y$. \medskip\noindent Observe that $V_i = X \cap T_i$ is a locally closed subset which is in addition a closed subscheme of $X$ hence affine. Let $\eta \in Y$ and $\eta_i \in T_i$ be the generic points. If $\eta \not \in Y_d$, then $Y_d = \emptyset$ and we're done. Assume $\eta \in Y_d$. Denote $I \in \{1, \ldots, n\}$ the subset of indices $i$ such that $\eta_i \in V_i$. For $i \in I$ the locally closed subset $V_i \subset T_i$ contains the generic point of the irreducible space $T_i$ and hence is open. On the other hand, since $f$ is open (Lemma \ref{lemma-weighting-universally-open}), for any $x \in X$ we can find an $i \in I$ and a specialization $\eta_i \leadsto x$. It follows that $x \in T_i$ and hence $x \in V_i$. In other words, we see that $X = \bigcup_{i \in I} V_i$ set theoretically. We claim that $Y_d = \bigcap_{i \in I} \Im(V_i \to Y)$; this will finish the proof as the intersection of affine opens $\Im(V_i \to Y)$ of $Y$ is affine. \medskip\noindent For $y \in Y$ let $f^{-1}(\{y\}) = \{x_1, \ldots, x_r\}$ in $X$. For each $i \in I$ there is at most one $j(i) \in \{1, \ldots, x_r\}$ such that $\eta_i \leadsto x_{j(i)}$. In fact, $j(i)$ exists and is equal to $j$ if and only if $x_j \in V_i$. If $i \in I$ is such that $j = j(i)$ exists, then $V_i \to Y$ is an isomorphism in a neighbourhood of $x_j \mapsto y$. Hence $\bigcup_{i \in I,\ j(i) = j} V_i \to Y$ is finite after replacing source and target by neighbourhoods of $x_j \mapsto y$. Thus the definition of a weighting tells us that $w(x_j) = \sum_{i \in I,\ j(i) = j} w(\eta_i)$. Thus we see that $$ (\textstyle{\int}_f w)(\eta) = \sum\nolimits_{i \in I} w(\eta_i) \geq \sum\nolimits_{j(i)\text{ exists}} w(\eta_i) = \sum\nolimits_j w(x_j) = (\textstyle{\int}_f w)(y) $$ Thus equality holds if and only if $y$ is contained in $\bigcap_{i \in I} \Im(V_i \to Y)$ which is what we wanted to show.