Codeforces 1136B

B. Nastya Is Playing Computer Games

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.

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.

 

In one turn, Nastya can do one of the following:

  • 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).
  • go to a neighboring manhole;
  • 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).

 

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).

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.

Note one time more that Nastya can open a manhole only when there are no stones onto it.

 

Input

The first and only line contains two integers ? and ?, separated by space (2≤?≤50002≤n≤5000, 1≤?≤?1≤k≤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 ? manholes.

Output

Print a single integer — minimum number of moves which lead Nastya to pick all the coins.

Examples

input

Copy

2 2

output

Copy

6

input

Copy

4 2

output

Copy

13

input

Copy

5 1

output

Copy

15

Note

Let's consider the example where ?=2n=2, ?=2k=2. Nastya should play as follows:

  • At first she throws the stone from the second manhole to the first. Now there are two stones on the first manhole.
  • Then she opens the second manhole and pick the coin from it.
  • 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.

So, 66 moves are required to win.

 

题意:

Nastya 需要将所有石井下的硬币取出才可以通关,每一步可以采用以下三种任意一种方式:

1、当前的石井上至少有一个石子时,需要将这个石子扔向其他的石井上;

2、移动到相邻的石井上;

3、如果当前的石子上没有石子,那么Nastya可以取出硬币,并把井关闭

求最少需要多少步能把所有硬币取出(起初每个石井上只有一个石子)

 

思路:

步数分为 : 扔石子、移动到相邻的、取出石子

画一下移动图可以看出

(1)、第 k 个石井和相邻的石井 总共需要 6 步可以将所有石子取出

(2)、其余的 n - 2 个井各需要 3 步 ( 从相邻移动到当前井,扔石子,取出)

            此时的石井上都各有一个石子(因为在第一步中已经空出来一个石井并且硬币也取出,那么以后的就可以扔一次)

(3)、考虑一开始往左右走的问题,因为到达边界之后,还需要拐回来

            当起初在 k 时,一开始往左需要多移动 k - 1 步,相反需要 n - k 步

所以 ans = 3 × n + min(k-1,n-k)

 

CODE:

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <sstream>
#include <cstdio>
#include <vector>
#include <string>
#include <cmath>
#include <stack>
#include <queue>
#include <map>
#include <set>
#define INF 0x3f3f3f3f
#define memset(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long LL;
const double PI = acos(-1);
const int M=1e4+10;

int main()
{
    int n,k;
    cin >> n >> k;
    int ans = 0;
    ans = n*3 + min(k-1,n-k);
    cout << ans << endl;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值