This is a problem that comes up often in my job. Not as transparently as this (it's not a heavily-math focused job, haha), but at a fundamental level it does, and I was wondering if it can be generalised in any way.
I'm not a very math-savvy person, so please bear with my phrasing of this like a textbook question:
You have a linear sequence of 10 spaces labelled A through J. Into each space you may place 1 and only 1 counter. You have 4 types of counter: four counters marked "2", three counters marked "3", two counters marked "4", and two counters marked "5". You may place counters into each space as you see fit, as long as you respect the following rule:
- A counter marked with $x$ cannot be placed within $x$ spaces of another counter marked with $x$.
For example, if a "2" counter is placed in space A, no "2" counters can be placed in spaces B or C. If a "2" counter is placed in space C, no "2" counters can be placed in spaces A, B, D, or E.
Finally, you are not required to place a counter in every space.
Given the above constraints, what is the greatest possible value for the sum of the numbers marked on the counters placed in spaces A through J?
Like I said, the problem in practice is not usually that transparent, so I'm more interested in whether the answer can be generalised in some way (like into a formula of some kind), or if it's too complex for that. Thank you for your time, and apologies in advance if I've mis-tagged this question.
You can solve the problem via integer linear programming as follows. Let binary decision variable $x_{sc}$ indicate whether space $s$ contains counter $c$. Let $u_c$ be the upper bound on the number of times counter $c$ can appear (for your example, $u_2=4, u_3=3, u_4=u_5=2$). The problem is to maximize $\sum_{s,c} c x_{sc}$ subject to linear constraints \begin{align} \sum_c x_{sc} &\le 1 &&\text{for all $s$} \tag1\label1\\ \sum_s x_{sc} &\le u_c &&\text{for all $c$} \tag2\label2\\ x_{sc} + x_{tc} &\le 1 &&\text{for all $c$, $s$, $t$ such that $s<t$ and $|s-t| \le c$} \tag3\label3 \end{align}
Constraint \eqref{1} assigns at most one counter per space. Constraint \eqref{2} enforces the upper bound for each counter. Constraint \eqref{3} prevents assigning the same counter to spaces that are too close together.
The maximum is $31$, attained as follows:
It turns out that there are $36$ such optimal solutions.
Here is the SAS code I used: