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 1096 (Div. 3)/E. It All Went Sideways
PrevNext
CodeforcesCodeforces
Specialist
Codeforces Round 1096 (Div. 3)

E. It All Went Sideways

ArrayDynamic ProgrammingBinary SearchSuffix Array
Loading...
Solve on Codeforces

Problem Statement

Official algorithmic problem description and constraints.

Open on Codeforces

Yousef has π‘›

 columns of cubes standing side by side. The π‘–

-th column contains π‘Ž

𝑖

 identical unit cubes stacked vertically. Initially gravity pulls cubes downwards, so every column π‘–

 contains exactly π‘Ž

𝑖

 cubes at heights 1,2,…,π‘Ž

𝑖

.

Suddenly, gravity shifts to the right. Each cube slides horizontally as far to the right as possible. A cube cannot pass through or overlap other cubes, and it must remain at its original height. The final configuration is uniquely determined by the initial heights.

Before and after applying rightward gravity.


Before the gravity shift, Yousef may perform at most one operation: choose an index π‘–

 and decrease π‘Ž

𝑖

 by 1

 (i.e. remove one cube from that column). He may also choose to do nothing.

A cube is said to move if its column index after the gravity shift is different from its original column index.

Find the maximum possible number of cubes that will move after the gravity shift, assuming Yousef applies the single decrease optimally (or chooses not to apply it).

Input


The first line contains an integer π‘‘

 (1≀𝑑≀10

4

) β€” the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer π‘›

 (1≀𝑛≀2β‹…10

5

) β€” the length of the array.

The second line of each test case contains π‘›

 integers π‘Ž

1

,π‘Ž

2

,…,π‘Ž

𝑛

 (1β‰€π‘Ž

𝑖

≀𝑛

) β€” the elements of the array.

It is guaranteed that the sum of π‘›

 over all test cases does not exceed 2β‹…10

5

.

Output


For each test case, output a single integer β€” the maximum possible number of cubes that will move after the gravity shift, assuming Yousef applies the single decrease optimally (or chooses not to apply it).

Example

Input

Copy

5
5
1 2 3 2 1
7
5 4 1 1 1 1 3
6
1 2 3 4 5 6
6
4 1 6 3 2 6
7
1 3 2 7 2 3 1

Output

Copy

8
12
0
10
18

Note


In the first test case, it is optimal to perform the operation on index 5

, making the array π‘Ž=[1,2,3,2,0]

. After the gravity shift, every remaining cube will move. The answer is 1+2+3+2=8

. It can be proven that a greater answer does not exist.

In the second test case, we can perform the operation on index 6

, making the array π‘Ž=[5,4,1,1,1,0,3]

. After the gravity shift, 5+4+1+1+1=12

 cubes will move. Note that the cubes at index 7

 do not move, since they are already at the rightmost end of the array.

In the third test case, there is no way to make any cube move.


Solutions & Walkthrough

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

Connecting secure classroom stream...
Back to Codeforces Round 1096 (Div. 3)
Previous ProblemNext Problem