#3462

Maximum Sum With at Most K Elements

medium · 60.6% accepted · 109 likes · top 59%

array · greedy · sorting · heap (priority queue) · matrix

Description

You are given a 2D integer matrix grid of size n x m, an integer array limits of length n, and an integer k. The task is to find the maximum sum of at most k elements from the matrix grid such that:

-

The number of elements taken from the ith row of grid does not exceed limits[i].



Return the maximum sum.

Solution