#include #include #include #include using namespace std; // Function to find the largest index j such that locations[j] <= locations[i] - 5 int p(int i, vector& locations) { for (int j = i - 1; j >= 0; j--) { if (locations[j] <= locations[i] - 5) { return j; } } return -1; } // Function to compute the maximum total bonus and record the choices pair, vector> computeMaxBonusAndChoices(vector& locations, vector& bonuses) { int n = locations.size(); vector T(n + 1), R(n + 1); T[0] = 0; for (int i = 1; i <= n; i++) { int pi = p(i - 1, locations); int bonus = (pi != -1) ? bonuses[i - 1] + T[pi + 1] : bonuses[i - 1]; if (bonus > T[i - 1]) { T[i] = bonus; R[i] = i; } else { T[i] = T[i - 1]; R[i] = R[i - 1]; } } return {T, R}; } int main() { vector locations = {8, 10, 15, 22, 26}; vector bonuses = {15, 5, 25, 15, 5}; pair, vector> result = computeMaxBonusAndChoices(locations, bonuses); vector T = result.first; vector R = result.second; ofstream outfile("bonus_pc_out.txt"); for (int i = 0; i < T.size(); i++) { outfile << "T[" << i << "] = " << T[i] << ", R[" << i << "] = " << R[i] << endl; } outfile << "Maximum total bonus = " << T.back() << endl; outfile.close(); return 0; }