Maximum Total from Optimal Activation Order

You are given two integer arrays value and limit, both of length n.

Initially, all elements are inactive. You may activate them in any order.

  • To activate an inactive element at index i, the number of currently active elements must be strictly less than limit[i].
  • When you activate the element at index i, it adds value[i] to the total activation value, i.e. the sum of value[i] for all elements that have undergone activation operations.
  • After each activation, if the number of currently active elements becomes x, then all elements j with limit[j] <= x become permanently inactive, even if they are already active.

Return the maximum total you can obtain by choosing the activation order optimally.

Example 1
Inputvalue = [3,5,8], limit = [2,1,3]
Output16
One optimal activation order collects values 5, then 3, then 8, for a maximum possible total of 16.
Example 2
Inputvalue = [4,2,6], limit = [1,1,1]
Output6
Activating index 2 first adds 6, after which all elements become permanently inactive, so the maximum possible total is 6.

Constraints

  • 1 <= n == value.length == limit.length <= 10^5
  • 1 <= value[i] <= 10^5
  • 1 <= limit[i] <= n

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