J - Block Towers

Students in a class are making towers of blocks. Each student makes a (non-zero) tower by stacking pieces lengthwise on top of each other. n of the students use pieces made of two blocks and m of the students use pieces made of three blocks.

The students don’t want to use too many blocks, but they also want to be unique, so no two students’ towers may contain the same number of blocks. Find the minimum height necessary for the tallest of the students' towers.

Input

The first line of the input contains two space-separated integers n and m (0 ≤ n, m ≤ 1 000 000, n + m > 0) — the number of students using two-block pieces and the number of students using three-block pieces, respectively.

Output

Print a single integer, denoting the minimum possible height of the tallest tower.

Sample 1

Inputcopy Outputcopy
1 3
9

Sample 2

Inputcopy Outputcopy
3 2
8

Sample 3

Inputcopy Outputcopy
5 0
10

Note

In the first case, the student using two-block pieces can make a tower of height 4, and the students using three-block pieces can make towers of height 3, 6, and 9 blocks. The tallest tower has a height of 9 blocks.

In the second case, the students can make towers of heights 2, 4, and 8 with two-block pieces and towers of heights 3 and 6 with three-block pieces, for a maximum height of 8 blocks.

题意翻译

一群人在搭建积木塔。 有N个人2块2块往上搭,有M个人3块3块往上搭,每个人塔的高度都要大于0,且高度各不相同,问最高的塔高度的最小值。(0<=n,m<=1000000)

#include
#include
#include
 using namespace std;
 typedef long long ll;

int main()
{
	int n, m; cin >> n >> m;
	int t2 = 2 * n, t3 = 3 * m;
	while (n >= 3 && m >= 2)  //循环继续的条件是当塔2和塔3均存在6的倍数时
	{
		if (t2 <= t3)
		{
			t2 += 2;
			n++;
		}
		else
		{
			t3 += 3;
			m++;
		}
		n -= 3;  //每次从塔2和塔3分别拿走一个6的倍数
		m -= 2;

		//每次循环结束,我们默认把刚从两个塔拿出来的6的倍数加入未增加高度的塔中
		//这样就可以保证在没有重复高度下得到最高高度的最小值了
	}
	cout << max(t2, t3) << endl;
}

 

你可能感兴趣的:(算法)