Learn DSADSAContests
coding75 logo
LeetCode POTDPOTDSheets
coding75 Pro
coding75 logo
Dashboard
Learn DSA
Course RoadmapFlow
DSA Topic TreeNew πŸš€
Contest SolutionsPractice
Practice Sheets
MasterclassesLive πŸ‘¨πŸ»β€πŸ’»

coding75 ProPRO

Live Classes & Placement Guidance

coding75 logo

The premier developer platform for mastering Data Structures & Algorithms, exploring contest editorials, building ATS-ready resumes, and accelerating your tech career.

Connect & Community

DSA & Contests

  • Learn DSA
  • Contest Solutions
  • LeetCode POTDDaily
  • Practice Sheets
  • MasterclassesSoon

Interview Prep

  • Portfolio Projects
  • CS Fundamentals
  • System DesignSoon
  • Interview ExperiencesSoon
  • Mock InterviewsSoon

Career & Pro

  • Jobs & Internships
  • Resume BuilderATS
  • coding75 Pro
  • Submit Feedback
Β© 2026coding75β€’crackDSAβ„’β€’Maa Lalita Edtech Private Limited. All Rights Reserved.
Privacy PolicyTerms & ConditionsContact Support
Registered Office: Kanpur 208021β€’Regional Office: Indiranagar, Bangalore, 560008
Maa Lalita Edtech Private Limited
Contests/Codeforces/Codeforces Round 957/B. Angry Monk
PrevNext
CodeforcesCodeforces
Pupil
Codeforces Round 957

B. Angry Monk

MathGreedySortingArray
Loading...
Solve on Codeforces

Problem Statement

Official algorithmic problem description and constraints.

Open on Codeforces

To celebrate his recovery, k1o0n has baked an enormous π‘› metres long potato casserole.

Turns out, Noobish_Monk just can't stand potatoes, so he decided to ruin k1o0n's meal. He has cut it into π‘˜ pieces, of lengths π‘Ž1,π‘Ž2,…,π‘Žπ‘˜ meters.

k1o0n wasn't keen on that. Luckily, everything can be fixed. In order to do that, k1o0n can do one of the following operations:

  • Pick a piece with length π‘Žπ‘–β‰₯2 and divide it into two pieces with lengths 1 and π‘Žπ‘–βˆ’1. As a result, the number of pieces will increase by 1;
  • Pick a slice π‘Žπ‘– and another slice with length π‘Žπ‘—=1 (𝑖≠𝑗) and merge them into one piece with length π‘Žπ‘–+1. As a result, the number of pieces will decrease by 1.

Help k1o0n to find the minimum number of operations he needs to do in order to merge the casserole into one piece with length π‘›.

For example, if π‘›=5, π‘˜=2 and π‘Ž=[3,2], it is optimal to do the following:

  1. Divide the piece with length 2 into two pieces with lengths 2βˆ’1=1 and 1, as a result π‘Ž=[3,1,1].
  2. Merge the piece with length 3 and the piece with length 1, as a result π‘Ž=[4,1].
  3. Merge the piece with length 4 and the piece with length 1, as a result π‘Ž=[5].


Solutions & Walkthrough

Detailed video explanations, mathematical intuition, and clean C++ implementation code.

Connecting secure classroom stream...
Back to Codeforces Round 957
Previous ProblemNext Problem