It might be tempting at first to do a 2-dimensional DP with the number of computer purchases and the number of customer orders, but that would result in a nasty and inefficient transition.
Instead, let's try grouping the two into a single array of "transactions". Each transation has three attributes: the change in the number of cores, the associated clock rate of the cores, and the change in the total profits. For example, the example case would have a transaction array that looks like this:
| Core change | Clock rate | Profit change |
| 4 | 2200 | -700 |
| 2 | 1800 | -10 |
| 20 | 2550 | -9999 |
| 4 | 2000 | -750 |
| -1 | 1500 | 300 |
| -6 | 1900 | 1500 |
| -3 | 2400 | 4550 |
Now we sort the array by clock rate in reverse, resulting in this:
| Core change | Clock rate | Profit change |
| 20 | 2550 | -9999 |
| -3 | 2400 | 4550 |
| 4 | 2200 | -700 |
| 4 | 2000 | -750 |
| -6 | 1900 | 1500 |
| 2 | 1800 | -10 |
| -1 | 1500 | 300 |
Notice that in this order, as long as a customer order comes after a computer purchase, the computer will be able to satisfy the order as long as there are enough cores.
This drastically simplifies the problem. Now we can simply define as the maximum profit we can gain from the first transactions given that we have cores left. Given this, the rest of the problem is simple knapsack.
C++
#include <algorithm>#include <iostream>#include <vector>using std::cout;using std::endl;using std::vector;struct Transaction {int cores;
Join the USACO Forum!
Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!