Space Invaders
Space Invaders
Peterwrote hisversion of thepopular game"Space Invaders".The gameis as follows.Onthe groundattackships ofSpace Invaders.Theylined upin rowsat the topof the screen.The player controls alaser gun, which is locatedat the bottom ofthe screenin one of thecolumns.Forone actiona player canmove thegunleft or right, ormake a shotstraight up.If a playermakesa shot, itdestroys thealien shipcomingin thatcolumn,in which thegun.
Unlike the originalgame,Petyaembodiment ofalien shipsarein place andcan not shoot, so theplayer can notlose.Help thePetedestroy allalien shipsin a minimumnumber of operations.
Input
The firstline containsthe numbern and p - number of columns andcolumn number, whichis initiallya gun (1 ≤ n ≤ 100, 1 ≤ p ≤ n). The second linecontainsn numbersa1, a2, ..., an, where ai - the number ofaliensin the ith column (1 ≤ ai ≤ 100).
Output
In theoutput fileoutputa single number -the minimum number ofoperationsrequiredtodestroy all thealiens.
5 4 5 3 4 1 2
20