Skip to main content
Back to problems
Leetcode
Medium
Arrays
Greedy
Sorting
Maximize Happiness Of Selected Children

Choose up to kk children to maximize the total happiness gained, where selecting a child reduces the happiness contribution of later selections.

Acceptance 0%
Problem Statement

Problem

You are given an array happiness, where happiness[i] is the initial happiness value of the ii-th child, and an integer k.

You may select at most k children, one per round. In each round, choose a child who has not been selected before. If the child currently has happiness x, you gain max(x, 0) happiness from that child, and then every unselected child's happiness effectively decreases by 1 for the next round.

Return the maximum total happiness you can collect.

Notes

  • Once a child is selected, it cannot be selected again.
  • If a child's effective happiness becomes non-positive, selecting it contributes nothing useful.
  • The goal is to maximize the sum over all selected rounds.

Input Format

  • happiness: an array of integers
  • k: the maximum number of children you may select

Output Format

  • Return a single integer: the maximum possible total happiness.

Constraints

  • 1 <= happiness.length
  • k >= 1
  • Each round decreases the remaining unselected children's effective happiness by 1
  • A selected child's contribution is max(current_happiness, 0)
Examples
Sample cases returned by the problem API.

Example 1

Input

happiness = [1, 2, 3], k = 2

Output

5

Explanation

Pick the child with happiness 3 first, then the child with happiness 2. The total is 3 + 2 = 5.

Example 2

Input

happiness = [1, 1, 1], k = 3

Output

1

Explanation

The best choice is to pick any child first for 1 point. After that, the remaining effective happiness values are not positive, so taking more children adds no benefit.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.