#P4963. Maximum Happiness with Pigments
Maximum Happiness with Pigments
Maximum Happiness with Pigments
You are given \(n\) different pigments numbered from \(1\) to \(n\). Each pigment can only be used once. When starting a painting, you may choose any pigment arbitrarily. After that, at every step, you must choose a pigment \(i\) (which has not been used before) such that if the already used pigments form the set \(A\), then among all unused pigments \(j\) in \([1, n]\) the following holds: if there exists some \(i, j \notin A\) satisfying \(\gcd(A \cup \{i\}) > \gcd(A \cup \{j\})\), then pigment \(j\) cannot be chosen. In other words, at each step, you are only allowed to choose a pigment that maximizes \(\gcd(A \cup \{i\})\) among all candidates.
Every time a pigment is used, you gain happiness equal to the \(\gcd\) of all the pigments used so far. Initially, when a single pigment is chosen, the happiness is equal to that pigment’s number. To maximize the total happiness when using exactly \(m\) pigments, note that if you start with a pigment \(d\) and all subsequently chosen pigments are multiples of \(d\), then the \(\gcd\) remains \(d\) for every step, yielding a total happiness of \(m \times d\). However, there is a constraint: there must be at least \(m\) multiples of \(d\) in \([1, n]\) (including \(d\) itself). This condition is equivalent to \[ \left\lfloor \frac{n}{d} \right\rfloor \ge m. \]
It turns out that to maximize the total happiness, you should choose \(d = \lfloor n/m \rfloor\) as the initial pigment and then pick any \(m-1\) other multiples of \(d\). The maximum total happiness is therefore: \[ \text{Answer} = m \times \lfloor n/m \rfloor. \]
Given \(n\) and \(m\) (with \(1 \le m \le n\)), compute the maximum total happiness you can obtain.
inputFormat
The first line contains two space-separated integers \(n\) and \(m\) \((1 \le m \le n)\).
outputFormat
Output a single integer, which is the maximum total happiness.
sample
10 3
9