This page is still under construction.

Parts of this page are still being built. What you see may change.

Space Invaders

Interview

Time limit2sMemory limit1024 MB

Summary
Given aliens stacked in n columns and a cannon starting at column p, find the minimum number of move and fire actions to destroy every alien.
Level

Medium5 of 10

Topics
Dynamic programming, Greedy, Array, Implementation
Solved
No attempts yet

Problem

Petya wrote his own version of the well-known game <>. The game works as follows. Ships of space invaders attack the Earth. They are lined up in rows at the top of the screen. The player controls a laser cannon, which is at the bottom edge of the screen in one of the columns. In one action the player can move the cannon left or right, or fire vertically upward. If the player fires, the shot destroys the nearest alien ship in the column where the cannon is located.

Unlike the original game, in Petya's version the alien ships stay in place and cannot shoot, so the player cannot lose. Help Petya destroy all the alien ships in the minimum number of actions.

Input

The first line of the input file contains the numbers nn and pp, the number of columns and the number of the column where the cannon is initially located (1≤n≤1001\le n\le 100, 1≤p≤n1\le p\le n). The second line contains nn numbers a1,a2,...,ana_1, a_2, ..., a_n, where aia_i is the number of aliens in the ii-th column (1≤ai≤1001\le a_i\le 100).

Output

Output to the output file a single number: the minimum number of actions needed to destroy all the aliens.

Examples1

  1. Example 1

    Input
    5 4
    5 3 4 1 2
    
    Expected output
    20