I have a certain number of triplets and I want to add up numbers in such a way that the sum remains minimum and you can select only one number in a certain triplet. eg:

```
10 10 1   //minimum 1
1  10 21  //minimum 1
10 1  10  //minimum 1 Ans : 1+1+1 = 3

```

However you **cannot** select adjacent element in same column.

```
10 10 1 //select 1
10 10 1 //cannot select 1, that's adjacent!
10 10 1 //safe to select 1

```

But the problem is, in case of a tie, I need to scan the next triplet in the sequence and make sure not the select the number in previous sequence for obtaining maximum profit
eg:

```
10 10 10  // step 2.select 10 from column 1 or 2
10 10 1   // step 1.make sure not to select element in column 3 in previous sequence
          // step 3. select 1 from column 3

```

Since we cannot replace the position of Scanner so I cannot move the scanner's readLine to the previous position, how can I scan ahead of time?

My take : (Since the range of number is large I'm using BigInt)

```
for(long j=0; j<noOfTriples ; j++){
    firstNumber = in.nextBigInteger();
    secNumber = in.nextBigInteger();
    thirdNumber = in.nextBigInteger();

    if(areEqual(firstNumber, secNumber, thirdNumber)){
        while(true){
             firstNumber2 = in.nextBigInteger();
             secNumber2 = in.nextBigInteger();
             thirdNumber2 = in.nextBigInteger();
             noOfTriples--;
                if(areEqual(firstNumber2, secNumber2, thirdNumber2)){
                    count.add(secNumber2); // add any number to the count
                                          // since all of them are same
                }else
                    break;
                }   

    pass(firstNumber, secNumber, thirdNumber, least(firstNumber2, secNumber2, thirdNumber2));
    //pass adds up the minimum of three to the count 
    //where the last field indicates which element to NOT use while adding &
    //least returns minimum of the three
    }else
    pass(firstNumber, secNumber, thirdNumber, -1);  //-1 indicates no bias
}

```

Edit :

1. [sprinter](https://stackoverflow.com/users/3705127/sprinter) pointed out, I need to scan the entire list of triplets. Scanning only 1 ahead in sequence won't help.
2. The number of inputs may be upto a million, hence exhaustive search won't help.