#include #include typedef struct Day Day; struct Day { int win; int best_with_first; int best_without_first; }; static const int wins[] = { 158, 44, 196, 399, 47, 2, -158, -197, 375, 121, 806, 44, 953, 7, 20, 1, 7, 88, 191, 33, 654, 156, 321, 784, -111, 159, 88, 49, 25, 366, 861, 869, 380 }; static int max (int a, int b) { if (a > b) return a; else return b; } static void maximize (int n, Day *days) { if (n == 1) { days->best_with_first = days->win; days->best_without_first = 0; } else { maximize (n - 1, days + 1); days->best_with_first = days[0].win + days[1].best_without_first; days->best_without_first = max (days[1].best_with_first, days[1].best_without_first); } } int main () { int n_days = sizeof wins / sizeof wins[0]; Day *days = malloc (n_days * sizeof (Day)); int gambled_on_previous_day; int total; int i; for (i = 0; i < n_days; ++i) { days[i].win = wins[i]; days[i].best_with_first = 0; days[i].best_without_first = 0; } maximize (n_days, days); gambled_on_previous_day = 0; total = 0; for (i = 0; i < n_days; ++i) { Day *day = &(days[i]); if (day->best_with_first > day->best_without_first && !gambled_on_previous_day) { printf ("gamble on day %2d => $%d\n", i, day->win); total += day->win; gambled_on_previous_day = 1; } else { printf ("don't gamble on day %2d => $%d\n", i, day->win); gambled_on_previous_day = 0; } } printf ("total => $%d\n", total); return 0; }