#C386. Maximum Pieces of Ribbon
Maximum Pieces of Ribbon
Maximum Pieces of Ribbon
You are given a ribbon of length (n) centimeters and three possible piece lengths (a), (b), and (c) centimeters. Your task is to determine the maximum number of pieces into which the ribbon can be cut such that each piece is exactly one of these lengths.
If it is not possible to cut the ribbon in a way that uses all of it, output the maximum number of pieces obtainable using the valid cuts.
The problem can be solved using dynamic programming with the recurrence relation:
[
dp[i] = \max\big( dp[i - a],\ dp[i - b],\ dp[i - c] \big) + 1, \quad \text{for } i \geq a, b, c ]
]
with the base condition (dp[0] = 0).
inputFormat
The input consists of a single line containing four space-separated integers: (n), (a), (b), and (c), where (n) is the total length of the ribbon and (a), (b), (c) are the allowed piece lengths.
outputFormat
Output a single integer representing the maximum number of pieces that can be obtained by cutting the ribbon according to the given rules.## sample
5 2 1 5
5