Skip to main content
Back to problems
Codeforces
Easy
Arrays
Sorting
Greedy
Minimum Difficulty

Choose an adjacent pair to remove from an array of task difficulties until one task remains, and minimize the difficulty of the remaining task.

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

You are given a sequence of task difficulties. Repeatedly, you may choose any adjacent pair of tasks and remove the task with the larger difficulty from that pair. Keep doing this until only one task remains.

Your goal is to determine the minimum possible difficulty of the final remaining task.

Think about what values can never be removed by this operation and how that affects the answer.

Input Format

The input contains a single array of integers representing task difficulties.

Output Format

Print one integer — the minimum possible difficulty of the last remaining task.

Constraints

  • The array contains at least one element.
  • Each difficulty value is a positive integer.
  • Use 64-bit integers if needed.
Examples
Sample cases returned by the problem API.

Example 1

Input

[5, 3, 2, 4]

Output

2

Explanation

The smallest value is 2, and it can never be removed because every operation removes only the larger of an adjacent pair. So the minimum possible final difficulty is 2.

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.