Many loans at once
transcript
A lender has a balance. Each loan has a minimum. Take the largest profit that is affordable. One line often goes wrong.
All three loans are affordable. An if moves only one loan. It takes profit one. The balance is six. A while moves all three. It takes nine. The balance is fourteen.
Loans wait, sorted by minimum. Ready loans sit in a heap of profits. Each round takes the largest profit. Then the balance grows.
Minimums are zero, one and two. Profits are one, two and three. Balance zero affords the first loan. Its profit makes the second loan ready. Two rounds end with a balance of three.
Test a start where no loan is affordable. Test more rounds than there are loans. Test totals past two billion. Use sixty-four bits for the balance, to be safe.
Use a while, not an if. Then take the best. Full breakdown in the lecture.