ILP Part 57 – SPOJ Wpłaty

This is the fifty seventh part of the ILP series. For your convenience you can find other parts in the table of contents in Part 1 – Boolean algebra

Today we are going to solve RNR_01_04 – Wpłaty from SPOJ. The history is as follows.
We want to get some money for charity. We go from door to door and ask each homeowner to donate. Each person can either donate automatically if at least d_i persons donated already. We can either convince the person explicitly (if that’s needed) or move on. The task is to get at least m donations and to convince as few people as possible. We are given number of people, m, and next line with d values.

Example:

Output:

Solution:

We create binary variables in line 6 to indicate whether we had to convince person explicitly. Then in loop in line 9 we calculate whether person is convinced – either automatically or explicitly.
In line 16 we add constraint to have at least m payments.
Then in line 18 we want to count people we had to convince explicitly and then minimize the value.

Bonus chatter: Do you know how to solve this in O(n) time and O(n) memory?