All problems
0241MediumArrayDynamic Programming

Grade the Orchard Rows

Tracked in this browser only
Write code

Trains the technique from

LeetCode 1043Partition Array for Maximum Sum

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

An orchard is planted as one long line of rows. You are given yields, where yields[i] is the crop weight recorded for row i, and an integer span.

At grading time the manager cuts the line into consecutive blocks. Every row belongs to exactly one block, and a block holds at least one row and at most span rows. Each row is then written into the season report with the heaviest weight found anywhere in its own block, and the season figure is the sum of the written weights over all rows.

Return the largest season figure that some cut into blocks produces.

Examples

Example 1

Input
yields = [4, 1, 1, 12, 2, 6, 3], span = 3
Output
58

Cutting into the blocks [4], [1, 1, 12] and [2, 6, 3] writes 4, then 12 three times, then 6 three times, giving 4 + 36 + 18 = 58.

Example 2

Input
yields = [1, 1, 9], span = 2
Output
19

Cutting into the blocks [1] and [1, 9] writes 1, then 9 twice, giving 1 + 18 = 19.

Example 3

Input
yields = [6, 2, 5, 1, 8, 3, 4], span = 2
Output
42

Cutting into the blocks [6, 2], [5, 1], [8, 3] and [4] writes 12 + 10 + 16 + 4 = 42.

Constraints

  • 1 <= yields.length <= 500
  • 0 <= yields[i] <= 10^9
  • 1 <= span <= yields.length

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def max_graded_total(yields: list[int], span: int) -> int:
Java
public int maxGradedTotal(int[] yields, int span)
September 7
Apply