Collecting Chocolates

You are given a 0-indexed integer array nums of size n representing the cost of collecting different chocolates. The cost of collecting the chocolate at index i is nums[i]. Each chocolate is of a different type, and initially, the chocolate at index i is of i^th type.

In one operation, you can do the following with an incurred cost of x:

  • Simultaneously change the chocolate of i^th type to ((i + 1) mod n)^th type for all chocolates.

Return the minimum cost to collect chocolates of all types, given that you can perform as many operations as you would like.

Example 1
Inputnums = [20,1,15], x = 5
Output13
By buying one chocolate after each of two operations, the total cost is 1 + 5 + 1 + 5 + 1 = 13, which is optimal.
Example 2
Inputnums = [1,2,3], x = 4
Output6
All three types can be collected at their own prices without performing any operations, for a total cost of 1 + 2 + 3 = 6.

Constraints

  • 1 <= nums.length <= 1000
  • 1 <= nums[i] <= 10^9
  • 1 <= x <= 10^9

Asked at 2 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