Maximum Score from Performing Multiplication Operations

You are given two 0-indexed integer arrays nums and multipliers of size n and m respectively, where n >= m.

You begin with a score of 0. You want to perform exactly m operations. On the i^th operation (0-indexed) you will:

  • Choose one integer x from either the start or the end of the array nums.
  • Add multipliers[i] * x to your score.
  • Note that multipliers[0] corresponds to the first operation, multipliers[1] to the second operation, and so on.
  • Remove x from nums.

Return the maximum score after performing m operations.

Example 1
Inputnums = [1,2,3], multipliers = [3,2,1]
Output14
Choosing 3, then 2, then 1 from the end gives a total score of 9 + 4 + 1 = 14.
Example 2
Inputnums = [-5,-3,-3,-2,7,1], multipliers = [-10,-5,3,4,6]
Output102
An optimal sequence chooses -5, -3, and -3 from the start, then 1 and 7 from the end, for a total score of 50 + 15 - 9 + 4 + 42 = 102.

Constraints

  • n == nums.length
  • m == multipliers.length
  • 1 <= m <= 300
  • m <= n <= 10^5
  • -1000 <= nums[i], multipliers[i] <= 1000

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate