Similar Problems

Similar Problems not available

Minimum Penalty For A Shop - Leetcode Solution

Companies:

LeetCode:  Minimum Penalty For A Shop Leetcode Solution

Difficulty: Medium

Topics: string prefix-sum  

Problem:

You are working in a shop which has a number of customers. Each customer is holding a list of items they want to buy from the shop. The list consists of N items.

You are responsible for prioritizing and packing the orders of these customers. The shop operates as follows:

  1. All customers place their orders at the beginning of the day.
  2. You then arrange the orders of all the customers in the order in which they arrived at the shop.
  3. For each customer, you process their order from their list of items, one by one in the order in which they appear in the list.
  4. Each customer has a penalty associated with each item in their list. This penalty is a non-negative integer value.
  5. The penalty for a customer's entire order is the maximum penalty for any item in their list.
  6. You must pack each customer's order as soon as it is completed.
  7. The shop closes at the end of the day, and any remaining orders will not be processed.

Your task is to arrange the orders of the customers in such a way that the total penalty for all customers is minimized.

Write a function to calculate the minimum penalty for the shop.

Input: The first line of input contains the integer N (1 ≤ N ≤ 1000) indicating the number of items. The second line contains a list of N integers A1 to AN (1 ≤ Ai ≤ 104), indicating the order in which the customers arrived at the shop. The remaining N lines each contain a list of N integers Bi,1 to Bi,N (0 ≤ Bi,j ≤ 104), where Bi,j indicates the penalty for the j-th item in the i-th customer's order.

Output: Output a single integer, the minimum total penalty for the shop for the given order of customers.

Example: Input: 3 1 3 2 1 3 4 2 2 2 3 2 1 4 3 5 Output: 5

Explanation: The customer arriving first (customer 1) has an order with penalties 4, 3, and 1 The customer arriving second (customer 3) has an order with penalties 2, 2, and 2 The customer arriving third (customer 2) has an order with penalties 3,4, and 5

If we process the orders in the order that they arrive, total penalty will be 4 (for customer 1) + 2 (for customer 3) + 5 (for customer 2) = 11 If we process the orders in the order (1,2,3), total penalty will be 4 (for customer 1) + 2 (for customer 3) + 2 (for customer 2) = 8 If we process the orders in the order (1,3,2), total penalty will be 4 (for customer 1) + 2 (for customer 3) + 5 (for customer 2) = 11 If we process the orders in the order (2,1,3), total penalty will be 5 (for customer 1) + 2 (for customer 3) + 5 (for customer 2) = 12 If we process the orders in the order (2,3,1), total penalty will be 5 (for customer 1) + 2 (for customer 3) + 5 (for customer 2) = 12 If we process the orders in the order (3,1,2), total penalty will be 4 (for customer 1) + 3 (for customer 3) + 5 (for customer 2) = 12 If we process the orders in the order (3,2,1), total penalty will be 5 (for customer 1) + 3 (for customer 3) + 5 (for customer 2) = 13

So, the minimum penalty will be 8.

Minimum Penalty For A Shop Solution Code

1