1136B.md (2791B)
1 Source: https://codeforces.com/problemset/problem/1136/B 2 3 B. Nastya Is Playing Computer Games 4 5 time limit per test 6 7 1 second 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 Finished her homework, Nastya decided to play computer games. Passing levels one by one, Nastya eventually faced a problem. Her mission is to leave a room, where a lot of monsters live, as quickly as possible. 22 23 There are $n$ manholes in the room which are situated on one line, but, unfortunately, all the manholes are closed, and there is one stone on every manhole. There is exactly one coin under every manhole, and to win the game Nastya should pick all the coins. Initially Nastya stands near the $k$-th manhole from the left. She is thinking what to do. 24 25 In one turn, Nastya can do one of the following: 26 27 * if there is at least one stone on the manhole Nastya stands near, throw exactly one stone from it onto any other manhole (yes, Nastya is strong). 28 * go to a neighboring manhole; 29 * if there are no stones on the manhole Nastya stays near, she can open it and pick the coin from it. After it she must close the manhole immediately (it doesn't require additional moves). 30 31  The figure shows the intermediate state of the game. At the current position Nastya can throw the stone to any other manhole or move left or right to the neighboring manholes. If she were near the leftmost manhole, she could open it (since there are no stones on it). 32 33 Nastya can leave the room when she picks all the coins. Monsters are everywhere, so you need to compute the minimum number of moves Nastya has to make to pick all the coins. 34 35 Note one time more that Nastya can open a manhole only when there are no stones onto it. 36 37 Input 38 39 The first and only line contains two integers $n$ and $k$, separated by space ($2 \leq n \leq 5000$, $1 \leq k \leq n$) — the number of manholes and the index of manhole from the left, near which Nastya stays initially. Initially there is exactly one stone near each of the $n$ manholes. 40 41 Output 42 43 Print a single integer — minimum number of moves which lead Nastya to pick all the coins. 44 45 Examples 46 47 Input 48 49 ``` 50 2 2 51 ``` 52 53 Output 54 55 ``` 56 6 57 ``` 58 59 Input 60 61 ``` 62 4 2 63 ``` 64 65 Output 66 67 ``` 68 13 69 ``` 70 71 Input 72 73 ``` 74 5 1 75 ``` 76 77 Output 78 79 ``` 80 15 81 ``` 82 83 Note 84 85 Let's consider the example where $n = 2$, $k = 2$. Nastya should play as follows: 86 87 * At first she throws the stone from the second manhole to the first. Now there are two stones on the first manhole. 88 * Then she opens the second manhole and pick the coin from it. 89 * Then she goes to the first manhole, throws two stones by two moves to the second manhole and then opens the manhole and picks the coin from it. 90 91 So, $6$ moves are required to win.