2020 Jan Silver Problem 2 Loan Repayment
Official Problem Statement[edit]
Problem Statement:
USACO Contest 991: Loan Repayment
You are given a loan of N dollars, and you must repay it over the course of M months. Each month, you must pay a certain amount of money (A_i) towards the loan. You can choose to pay more than the required amount each month, but you cannot pay less.
At the end of each month, the remaining loan balance is reduced by the amount you paid. If the remaining balance is less than the amount you paid, the extra money is not returned to you.
Your task is to determine the minimum amount of money you need to pay each month in order to pay off the loan in M months.
Solution:
We can solve this problem using dynamic programming. We create an array dp[i] that stores the minimum amount of money we need to pay in order to pay off the loan in i months.
We start with dp[0] = N, since that is the initial loan amount. Then, for each month i, we calculate dp[i] = min(dp[i-1], A_i) + (N - dp[i-1]). This is because we need to pay the minimum amount of money in order to pay off the loan in i months, which is either the amount we paid in the previous month (dp[i-1]) or the amount required for this month (A_i). We then add the remaining loan balance (N - dp[i-1]) to get the total amount we need to pay in this month.
Finally, we return dp[M], which is the minimum amount of money we need to pay each month in order to pay off the loan in M months.
int dp[M+1]; dp[0] = N; for (int i = 1; i <= M; i++) { dp[i] = min(dp[i-1], A_i) + (N - dp[i-1]); } return dp[M];