\documentclass[11pt]{article}
\usepackage[margin=1in]{geometry}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{amsmath,amssymb,amsthm}
\usepackage{enumitem}

\title{ICPC World Finals 2025\\H. Score Values}
\author{}
\date{}

\begin{document}
\maketitle

\section*{Problem Summary}

We can increase the score by any of the given point values, and whenever the score would exceed $m$ it
is capped to exactly $m$. We must determine, for every digit $0$ through $8$, the maximum number of
times that digit can appear in any reachable score. Digit $9$ is merged into digit $6$ because a $6$ sign
can be turned upside down.

\section*{Key Observations}

\begin{itemize}[leftmargin=*]
    \item Ignoring the cap for a moment, the reachable scores form a numerical semigroup generated by the
    point values. Let $g$ be their gcd. Only multiples of $g$ can appear before the cap is hit.
    \item Divide all point values by $g$, and let $a$ be the smallest reduced value.
    Using Dijkstra on residues modulo $a$, we can compute for every residue the smallest reachable value
    with that residue.
    \item From the classical semigroup criterion, once we know these smallest values, every sufficiently
    large number with the correct residue is reachable. Therefore there is a finite exceptional prefix, and
    after that all multiples of $g$ are reachable.
    \item The finite prefix can be enumerated directly.
    On the long suffix interval, the problem becomes:
    among all numbers in $[L,m]$ that are congruent to $0 \pmod g$, maximize the weighted digit count.
    This is a standard digit-DP problem.
    \item The cap makes the value $m$ reachable even if $m$ itself is not a multiple of $g$, so we must also
    test $m$ explicitly.
\end{itemize}

\section*{Algorithm}

\begin{enumerate}[leftmargin=*]
    \item Compute $g=\gcd(p_1,\dots,p_n)$ and divide all scores by $g$.
    \item Let $a$ be the smallest reduced score.
    Run Dijkstra on the graph of residues modulo $a$, where adding a reduced score is one edge.
    This gives the smallest reachable reduced value \texttt{dist[r]} for every residue $r$.
    \item Let $B=\max_r \texttt{dist[r]}$.
    Then every reduced value $y \ge B$ is reachable iff $y \equiv 0 \pmod g$ after scaling back, so every
    multiple of $g$ in $[gB,m]$ is reachable.
    \item Enumerate all reachable values below $gB$ explicitly and update the best digit counts.
    \item For each digit $d \in \{0,\dots,8\}$, run a digit DP on the interval $[gB,m]$ with the modulus
    condition ``number is divisible by $g$'' and weight $1$ on digit $d$ (or on both digits $6$ and $9$ for
    the merged case).
    \item Also evaluate the digits of $m$ itself and take the maximum.
\end{enumerate}

\section*{Correctness Proof}

We prove that the algorithm returns the correct answer.

\paragraph{Lemma 1.}
For every residue $r$ modulo the smallest reduced score $a$, \texttt{dist[r]} computed by Dijkstra is the
smallest reachable reduced score congruent to $r$.

\paragraph{Proof.}
The residue graph has one vertex for each residue modulo $a$. Traversing an edge labeled by a reduced
score $q$ changes residue by $q$ and increases the total reduced score by exactly $q$.
Thus every walk corresponds to a reachable reduced score with the resulting residue, and vice versa.
All edge weights are positive, so Dijkstra returns the shortest path to every residue, i.e.\ the smallest
reachable reduced score with that residue. \qed

\paragraph{Lemma 2.}
Every multiple of $g$ whose reduced value is at least $B=\max_r \texttt{dist[r]}$ is reachable.

\paragraph{Proof.}
Take any reduced value $y \ge B$.
Let $r=y \bmod a$.
By definition, \texttt{dist[r]} is reachable and has the same residue as $y$, and
\texttt{dist[r]} \le B \le y$.
Therefore $y-\texttt{dist[r]}$ is a nonnegative multiple of $a$, so by adding the smallest reduced score
$a$ enough times we can reach $y$. Scaling back by $g$ proves the claim. \qed

\paragraph{Lemma 3.}
The digit DP computes, for each digit weight function, the largest total weight among all reachable scores
in the long suffix interval.

\paragraph{Proof.}
By Lemma 2, the long suffix contains exactly the multiples of $g$ in that interval.
The digit DP enumerates precisely those integers in the interval that satisfy the modulus condition
``value $\equiv 0 \pmod g$'' and maximizes the chosen digit weight. Therefore it returns the correct best
digit count on the suffix. \qed

\paragraph{Theorem.}
The algorithm outputs the correct answer for every valid input.

\paragraph{Proof.}
Every reachable score is either in the finite exceptional prefix, in the suffix covered by Lemma 3, or is
the capped score $m$ itself. The program checks all three possibilities. By Lemma 1 the prefix boundary
is computed correctly, by Lemma 2 no reachable suffix value is missed, and by Lemma 3 the best digit
count on the suffix is computed exactly. Therefore the maximum reported for each digit is correct. \qed

\section*{Complexity Analysis}

The residue Dijkstra uses $a \le 1000$ states and $n \le 10$ transitions per state, so it is tiny.
The digit DP has at most $19 \cdot 1000 \cdot 2 \cdot 2 \cdot 2$ states and is run once for each of the
$9$ reported digits. The memory usage is dominated by the DP table and is also small.

\section*{Implementation Notes}

\begin{itemize}[leftmargin=*]
    \item The DP handles leading zeros separately so that the score $0$ contributes one zero digit.
    \item For digit $6$, the weight is assigned to both digits $6$ and $9$ because the same sign can display
    either one.
\end{itemize}

\end{document}
