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)/F. It Just Keeps Going Sideways
Prev
CodeforcesCodeforces
Expert
Codeforces Round 1096 (Div. 3)

F. It Just Keeps Going Sideways

ArraySortingGreedyBinary Search
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's movement distance is defined as |π‘—βˆ’π‘–|

, where π‘–

 is its original column index and π‘—

 is its column index after the gravity shift.

Find the maximum possible total movement distance (the sum of the movement distances of all remaining cubes) Yousef can achieve.

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 total movement distance (the sum of the movement distances of all remaining cubes) Yousef can achieve.

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

9
37
0
17
29

Note


In the first test case, the initial total movement distance is 5

. If Yousef removes the cube at index 5

, the array becomes [1,2,3,2,0]

. Because the fifth column is now empty, all cubes from the first four columns are able to slide further to the right than they could originally. This results in a new total distance of 9

.

In the third test case, the initial total movement distance is 0

. Even if Yousef removes any cube, no remaining cube will be able to move. This results in a total distance of 0

.


Solutions & Walkthrough

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

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