Dsa
Dynamic Programming
Easy
Rhythmic Songwriting
A composer has a sequence of N musical notes, each with a beauty value B[i]. They must group these notes into 'phrases' (contiguous sub-sequences). The beauty of a phrase is the sum of its notes' values. However, if two consecutive phrases have the exact same number of notes, the second phrase's beauty is reduced by a penalty K. Maximize the total beauty.
Input Format:
Line 1: N K
Line 2: N integers (B[i])
Output Format:
A single integer (max beauty).
Example:
Input:
4 10
5 5 5 5
Output:
10
Explanation: Group as [5,5] and [5,5]. Beauties are 10 and (10-10)=0. Total 10. Or [5,5,5,5] total 20. Best is one phrase: 20.
Key concepts
dynamic_programmingpartitioning
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now