Problem hidden
|This problem was hidden by Editorial Board member probably because it has incorrect language|version or invalid test data, or description of the problem is not clear.|

TOINCSEQ - Non-decreasing sequence

You are given a sequence of non-negative integers a1, a2, …, an. You are allowed to perform some operators on the sequence. Each operator consists of 2 steps: choosing an arbitrary value, then increasing (or decreasing) all elements of the selected value in the current sequence by 1.

For example, the sequence 1 9 9 2 2 will become 1 9 9 3 3 if you decide to increase all elements of value 2.

What is the minimum number of operators does it take to make the given sequence a non-decreasing sequence?

Input

  • The first line consits of an integer N, the number of elements in the sequence.
  • The second line consists of N elements of the sequence.

Output

  • The mininum number of operators needed to make the sequence non-decreasing.

Example

Input:
5

1 9 9 2 2


Output:
7

Constraints

  • 1 ≤ N ≤ 105.
  • Each element of the sequence is a non-negative integer not exceeding 109.
  • In 50% of the test cases, N does not exceed 103.

ãy

Được gửi lên bởi:VOJ Team
Ngày:2009-07-02
Thời gian chạy:0.200s
Giới hạn mã nguồn:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Ngôn ngữ cho phép:Tất cả ngoại trừ: ERL GOSU JS-RHINO NODEJS PERL6 PYPY RUST SED VB.NET
Nguồn bài:VNOI Marathon 2009 - Warm up Round

hide comments
2014-05-12 06:35:35 an IM3 Ex-Member of Bit
Chú ý tổng chi phí để biến 1 đoạn có [min, max] thành 1 giá trị có tính chất bằng nhau với mọi giá trị trong khoảng [min, max] -> có thể tham lam thay vì QHD.
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.