# Fruit Feast

From USACO 2015 December Contest, Gold:

Bessie has broken into Farmer John’s house again! She has discovered a pile of lemons and a pile of oranges in the kitchen (effectively an unlimited number of each), and she is determined to eat as much as possible. Bessie has a maximum fullness of . Eating an orange increases her fullness by , and eating a lemon increases her fullness by (with ). Additionally, if she wants, Bessie can drink water at most one time, which will instantly decrease her fullness by half (and will round down).

Help Bessie determine the maximum fullness she can achieve!

INPUT FORMAT:The first (and only) line has three integers , , and .

OUTPUT FORMAT:A single integer, representing the maximum fullness Bessie can achieve.

## Solution

If Bessie can reach a fullness value of , then she can also reach fullness values of and . Since Bessie starts at a fullness of , she can immediately reach fullness values of and , and then continue from there.

Since we get unlimited uses of oranges and lemons, this is an unbounded knapsack problem. We can solve knapsack problems using dynamic programming. However, we also need to consider whether Bessie drinks water, which will allow her to reach a fullness value of , but unable to drink water again.

We define our subproblem to be whether Bessie can achieve a fullness of , with without drinking water and having drunk water. Then our recurrence relation is

But what order should we solve our subproblems in? Notice that all subproblems of the form depend only on subproblems with fullness values less than . This means we can solve subproblems by iterating over increasing values of . What about ? Like in the case of , depends on subproblems with fullness values less than , but it also depends on the special cases when Bessie drinks water and halves her fullness. Fortunately, these special cases fall under the case, which means we can solve the subproblems if we solve the first.

Here is an iterative dynamic programming solution in Java:

```
int fruitFeast(int T, int A, int B) {
boolean[][] full = new boolean[T + 1][2];
full[0][0] = true;
// solve F(t, 0) subproblems first
for (int t = 0; t <= T; t++) {
// F(t, 0) = F(t - A, 0) or F(t - B, 0)
if (t - A >= 0 && full[t - A][0])
full[t][0] = true;
if (t - B >= 0 && full[t - B][0])
full[t][0] = true;
}
// solve F(t, 1) subproblems
// we'll ignore bounds checks for simplicity
for (int t = 0; t <= T; t++) {
full[t][1] = full[t - A][1] || full[t - B][1]
|| full[2 * t][0] || full[2 * t + 1][0];
}
// return the largest value of t that Bessie can reach
for (int t = T; t >= 0; t--)
if (full[t][0] || full[t][1])
return t;
return 0;
}
```

Here is my original, full recursive dynamic programming solution in C++:

```
#include <iostream>
using namespace std;
int T, A, B;
bool full[5000010];
void dp(int i, bool drank) {
if (i < 0 || i > T || full[i])
return;
full[i] = true;
dp(i+A, drank);
dp(i+B, drank);
if (!drank)
dp(i / 2, true);
}
int main() {
cin >> T >> A >> B;
dp(0, false);
for (int i = T; i >= 0; i--) {
if (full[i]) {
cout << i << endl;
break;
}
}
}
```