Package for this problem was not updated by the problem writer or Codeforces administration after we’ve upgraded the judging servers. To adjust the time limit constraint, solution execution time will be multiplied by 2. For example, if your solution works for 400 ms on judging servers, then value 800 ms will be displayed and used to determine the verdict.

Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ACM-ICPC mode for virtual contests.
If you've seen these problems, a virtual contest is not for you - solve these problems in the archive.
If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive.
Never use someone else's code, read the tutorials or communicate with other person during a virtual contest.

No tag edit access

C. Sequence

time limit per test

1 secondmemory limit per test

64 megabytesinput

standard inputoutput

standard outputLittle Petya likes to play very much. And most of all he likes to play the following game:

He is given a sequence of *N* integer numbers. At each step it is allowed to increase the value of any number by 1 or to decrease it by 1. The goal of the game is to make the sequence non-decreasing with the smallest number of steps. Petya is not good at math, so he asks for your help.

The sequence *a* is called non-decreasing if *a*_{1} ≤ *a*_{2} ≤ ... ≤ *a*_{N} holds, where *N* is the length of the sequence.

Input

The first line of the input contains single integer *N* (1 ≤ *N* ≤ 5000) — the length of the initial sequence. The following *N* lines contain one integer each — elements of the sequence. These numbers do not exceed 10^{9} by absolute value.

Output

Output one integer — minimum number of steps required to achieve the goal.

Examples

Input

5

3 2 -1 2 11

Output

4

Input

5

2 1 1 1 1

Output

1

Codeforces (c) Copyright 2010-2017 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: Aug/24/2017 13:44:03 (c4).

Desktop version, switch to mobile version.

User lists

Name |
---|