Transparent Crocodiles
Time limit1sMemory limit1024 MB
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 (), the coordinate range of the swamp. The swamp spans the range of the line.
The second line contains integers separated by spaces. The -th integer is the number of claw marks at position . Every integer is at least and less than .
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)