All Submissions


Given an array of N integers, you have to to sort it in non-decreasing order
The only operation allowed is to increase or decrease an array element by 1.

You have to sort the array in non-decreasing order using minimum such operations

Input:
Input contains many cases (<=50). Read till EOF
Each case gives N(<=1000), followed by N integers of the array.

Output:
For each case, print the minimum operations required.

Sample Input:
3
2 1 1
4
0 1 4 2
5
1 2 3 4 4
4
7 1 9 2
10
10 9 8 7 6 5 4 3 2 1
5
3 2 -1 2 11

Sample output:
1
2
0
13
25
4

Think DP !

Explanation for the last test case
convert it into 2 2 2 2 11
cost=4

Problem Setter : Jayant Mukherji

Languages: Brain,C,C++,Java,C#,JavaScript,Pascal,Perl,PHP,Python,Ruby,Text

Time Limit: 2 Second(s)
Score: 100 Point(s)
Input File Limit: 50000 Bytes

Submit

Login to post clarification.

No Clarifications.

Contest

Mode Judge

Passive

Online

Overall Rankings

RankNameScore
1xyz0
2Ams0
3TIP0
4team420
5xyzz0
6asdasdasd0
7abcd0
8khankhan0
9Gabriel0
10gigel0