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.
Examples
Input
1 3
Output
9
Input
3 2
Output
8
Input
5 0
Output
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 9blocks. 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.
题意:
有高度2和3的砖,有n个学生用高度为2的砖建塔和m个学生用高度3的砖建塔,并且要求每个学生塔的高度大于0,求满足条件的最低塔中最高的塔的高度。
分析:
开始用树状数组和count函数结合处理的,不可以。
正确思路:由于重复的肯定是6的倍数的塔高,可以先忽略重复的塔分别计算出砖高为2和砖高为3的塔的高度n,m,判断低于n和m时6的倍数出现时将高度为2的砖(不能加高度为3的砖,想想为什么)加在n和m低的踏上,最后求出n和m的最大值。
代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<vector>
#include<queue>
#include<vector>
#include<map>
#define mem(a,b) memset(a,b,sizeof(a))
#define LL long long
#include<set>
using namespace std;
int main()
{
int n,m,x=6;
scanf("%d%d",&n,&m);
n*=2;
m*=3;
while(x<=min(n,m))///6的倍数出现的次数
{
if(n<=m)///如果2的小于3的,先用2的补充重复的那个数字,因为这样塔低
{
n+=2;
}
else
{
m+=3;
}
x+=6;
}
int ans=0;
ans=max(n,m);
printf("%d\n",ans);
return 0;
}