#P9506. Maximizing Profit from Courses
Maximizing Profit from Courses
Maximizing Profit from Courses
There are courses numbered from to and textbooks also numbered from to . If you pass course , you earn dollars. However, to pass course , you must purchase the textbooks numbered , , , (\ldots), . The -th textbook costs dollars. Note that when passing a course, you must buy a new copy of each required textbook even if it has been purchased for another course. Your goal is not to pass all courses, but to maximize your total profit. The profit for course can be expressed in (\LaTeX) as:
[ \text{profit}i = A_i - \sum{j=0}^{K-1} B_{(i+j)\bmod N} ]
You may choose any subset of courses (possibly none) so that the sum of positive profits is maximized. Write a function that, given the parameters, returns the maximum profit you can earn.
inputFormat
The function receives the following parameters:
int N
: the number of courses (and textbooks).int K
: the number of consecutive textbooks required to pass a course.int *A
: an array of lengthN
whereA[i]
is the reward for passing coursei
.int *B
: an array of lengthN
whereB[i]
is the cost of textbooki
.
For C++ the function signature is:
long long int solve(int N, int K, int *A, int *B);
For other languages, implement an equivalent function.
outputFormat
The function should return a long long
integer representing the maximum profit achievable.
The profit for each course (i) is calculated as:
[ \text{profit}i = A_i - \sum{j=0}^{K-1} B_{(i+j)\bmod N} ]
Only courses with positive profit should be considered. The answer is the sum of the profits of those courses.
sample
N = 3, K = 2, A = [40, 80, 100], B = [140, 0, 20]
60