Codeforces 626C. Block Towers

Source: Internet
Author: User

C. Block Towers

Students in a class is making towers of blocks. Each student makes a (Non-zero) tower is stacking pieces lengthwise on top of each of the other. N of the students use pieces made of both blocks and m of the students use pieces made of three B Locks.

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

Input

The first line of the input contains the space-separated integers n and m (0≤ n, C6>m ≤1, 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.

Examplesinput
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 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 He Ights 3 and 6 with three-block pieces, for a maximum height of 8 blocks.

Idea: given to you is P 2 and Q 3, then this requirement of the minimum number of T->=max (2*P,3*Q); then starting with Max (2*Q,3*Q), the violent cycle determines whether the current number meets the requirements.

When the number is K, K/2 is a multiple of 2, K/3 is a multiple of 3, K/6 is a multiple of 6, wherein 6 is a multiple is to be met, because the multiples of 6 is obtained by 2, or 3, then (K/2-K/6) is a multiple of 2 and can not be removed 3;

Similarly (K/3-K/6) is a multiple of 3 but not divisible 2;x= (P (K/2-K/6)) is able to supply 6 of the number, the result <0 x=0; This means that 2 of the number is only 2 divisible by the number of the numbers are filled, so 6 is 0, when

However, there can be 2 of the number even those who can only be divisible by 2 of the number of the case, like the case of p=0 (this is because 2,3 is independent), the same x1= (P (K/3-K/6)), if X1+X==K/6, then is K;

1#include <Set>2#include <stack>3#include <queue>4#include <stdio.h>5#include <iostream>6#include <stdlib.h>7#include <string.h>8#include <math.h>9#include <algorithm>Ten#include <map> One using namespacestd; A intMainvoid) - { -     inti,j,k,p,q; the      while(SCANF ("%d%d", &p,&q)! =EOF) -     { -         intCc=2*p; -         intDd=3*Q; +         intyy=Max (CC,DD); -         intflag=0; +          for(I=YY;; i++) A         { at             if(i%2==0|| i%3==0) -             { -                 intuu=i/2; -                 intkk=i/3; -                 intzz=i/6; -                 intX1= (uu-ZZ); in                 intX2= (kk-ZZ); -x1=p-X1; tox2=q-x2; +                 if(x1<0) -                 { thex1=0; *                 } $                 if(x2<0)Panax Notoginseng                 { -X2=0; the                 }; +                 if(x1+x2==ZZ) A                 { the                      Break; +                 } -             } $         } $printf"%d\n", i); -  -     } the}
1#include <iostream>2#include <cstdio>3#include <cstring>4 intErintNintMintAintb);5 using namespacestd;6 intMain ()7 {8     intb;9scanf"%d%d",&a,&b);Ten     intR=10000000; One     intK=er (0, r,a,b); Aprintf"%d\n", k); -     return 0; - } the  - intErintNintMintAintb) - { -     intMid= (n+m)/2; +     if(n>m) -     { +         returnN; A     } at     intCnt=max (A-mid/2+mid/6,0) +max (B-mid/3+mid/6,0); -     if(Mid/6>=CNT) -     { -         returnER (n,mid-1, A, b); -     } -     Else in         returnER (mid+1, m,a,b); -}
1  while(l<=R)2     {3         intMid= (l+r) >>1;4         intRet=max (A-mid/2+mid/6,0) +max (B-mid/3+mid/6,0);5         if(Mid/6==ret)6         {7            Break;8}Else if(Mid/6>ret)9         {Tenr=mid-1; One         } A         ElseL=mid+1; -}

Codeforces 626C. Block Towers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.