import java.math.BigInteger;
import java.util.*;//vector
public class solution14_2
{
// instance variables - replace the example below with your own
public static Vector table = new Vector();
public static BigInteger zero = new BigInteger("0");
public static BigInteger one = new BigInteger("1");
public static BigInteger two = new BigInteger("2");
public static BigInteger three = new BigInteger("3");
public static BigInteger million = new BigInteger("1000000");
// public static int table;
public static void main (String [] args){
/*int i = 0;
int chainlength = 0;
int longest = 0;
int longestchain = 0;
int value = 0
*/
BigInteger chainlength = zero;
BigInteger longest = zero;
BigInteger longestchain = zero;
BigInteger i = zero;
BigInteger value = zero;
table.setSize(1000000);
while (i.compareTo(million)<0){
table.set(i.intValue(),0);
i = i.add(one);
}
for (i=one; i.compareTo(million)<0; i = i.add(one))
{
//System.out.println(i);
chainlength = zero;
value = i;
while (!value.equals(one)){
chainlength=chainlength.add(one);
value = NextNumber(value);
if (value.compareTo(million)>0)
continue;
else if (value.equals(million))
continue;
else if (! ((table.get(value.intValue())).equals(zero))){
//chainlength += table.get(value);
// (BigInteger)table.get(value.intValue())
int a = value.intValue();
int b = (Integer) table.get(a);
chainlength = chainlength.add(BigInteger.valueOf(B));
break;
}
}
table.set(i.intValue(),chainlength.intValue());
// table.set(i,chainlength);
//System.out.println(i+"->"+chainlength);
if (longestchain.compareTo(chainlength)<0){
longest = i;
longestchain = chainlength;
}
}
System.out.printf("\n%d: %d\n", longest, longestchain);
}
public static BigInteger NextNumber (BigInteger value){
//System.out.println("Value: " + value);
if ((value.remainder(two)).compareTo(zero)==0)
{
value = value.divide(two);
// System.out.println("New Value: " + value);
}
else
{
value = value.multiply(three).add(one);
// System.out.println("New Value: " + value+"\n***********");
}
if (value.compareTo(zero)<0)
System.err.println("ERROR:Negative value!");
return value;
}
}