Skip to main content
Back to problems
Codeforces
Medium
Arrays
Greedy
Math
Painting Pebbles

Rearrange the pebbles in each step so that as many piles as possible are painted, subject to a fixed per-step painting limit.

Acceptance 0%
Also Available On
Other platform versions and source mappings for the same problem.
Problem Statement

Problem

You are given several piles of pebbles. In one operation, you may choose a fixed amount of pebbles to paint from the available piles, but each pile can contribute only a limited number of pebbles to the painted set in that step. The goal is to determine the minimum number of operations needed to paint all pebbles, or equivalently the maximum progress possible per operation under the restriction.

More concretely, each pile has a size, and in every step you can reduce the sizes of some piles while keeping the process valid. You must decide the best order of selecting piles so that the total number of required steps is minimized.

This is an implementation-heavy greedy problem: the key is to always use the most useful available piles first and keep track of how many piles remain active after each step.

Input Format

  • The input consists of one test case.
  • The first line contains an integer nn — the number of piles.
  • The second line contains nn integers describing the pile sizes.

Output Format

  • Print one integer: the minimum number of operations needed to finish the process described in the problem.
  • If multiple optimal strategies exist, any one of them is acceptable as long as the count is minimal.

Constraints

  • 1n21051 \le n \le 2 \cdot 10^5
  • Pile sizes are positive integers
  • Time complexity should be close to linear or O(nlogn)O(n \log n)
Examples
Sample cases returned by the problem API.

Example 1

Input

5
1 2 2 3 3

Output

3

Explanation

A valid greedy schedule can reduce the tallest useful piles first. The total process finishes in 3 steps under the given restriction.

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.