Under construction

Still being built. Things may change

Transparent Crocodiles

Time limit1sMemory limit1024 MB

Summary
Given claw mark counts at each swamp position, find the minimum total front-to-back paw distance of crocodiles that leave exactly those marks, or -1 if impossible.
Level

Hard8 of 10

Topics
Dynamic programming, Math, Implementation
Solved
No attempts yet

Problem

"Kkyaaaa!" The transparent crocodile, the strongest of all crocodiles, let out a roar. The transparent crocodile was tough and the strongest among crocodiles. But when a transparent elephant, a transparent cheetah, or a transparent anaconda attacked, it lost.

In the year ■■, the world was mostly ruined, the dimensions collapsed, and everything became a single straight line. Transparent crocodiles live in the swamp on this line. They are invisible, but you can tell a transparent crocodile is there from the claw marks it leaves on the line.

Dewk lost his father to a transparent and ultimate being, and he trained for ■■ years to get revenge. Then he came to the swamp to wipe out the herd of transparent crocodiles, said to be the weakest of all transparent beings, and to satisfy his own pride. Dewk studied the following facts about transparent crocodiles.

  • A transparent crocodile has one front paw and one back paw. The front paw has five toes, while the back paw has four toes with webbing. Every toe ends in a claw.
  • The attack power of a transparent crocodile equals the distance between its front paw and back paw. No transparent crocodile has attack power 0. In other words, every transparent crocodile has its front and back paws at different positions.
  • Transparent crocodiles never leave the swamp. They move only by stepping on integer coordinate positions.

Dewk came to the swamp full of confidence, but when he saw the countless claw marks on the line, he was a little scared. He decided to estimate the minimum total attack power of the crocodiles. If he could not even beat that, he would run away and train more.

Help Dewk by computing the minimum attack power of the transparent crocodile herd from the claw marks in the swamp.

Input

The first line contains an integer NN (1≤N≤300,0001 \le N \le 300,000), the coordinate range of the swamp. The swamp spans the range [1,N][1, N] of the line.

The second line contains NN integers separated by spaces. The ii-th integer is the number of claw marks at position ii. Every integer is at least 00 and less than 2020.

Output

Print the minimum total attack power of the transparent crocodile herd on the first line. If there are no transparent crocodiles, print 0. If the transparent crocodiles cannot leave exactly the claw marks on the swamp, print -1.

Hint

In the first example, the total attack power is minimized when the crocodiles are placed as follows.

  • A crocodile with its front paw at 2 and back paw at 1 (attack power 1)
  • A crocodile with its front paw at 4 and back paw at 1 (attack power 3)
  • A crocodile with its front paw at 4 and back paw at 5 (attack power 1)
  • A crocodile with its front paw at 5 and back paw at 6 (attack power 1)

Examples2

  1. Example 1

    Input
    6
    8 5 0 10 9 4
    
    Expected output
    6
    
  2. Example 2

    Input
    4
    7 10 4 6
    
    Expected output
    -1