/*
Online Java - IDE, Code Editor, Compiler
Online Java is a quick and easy tool that helps you to build, compile, test your programs online.
*/
import java.io.*;
import java.util.*;
public class Main
{
    public static void main(String[] args) {
System.out.println("Welcome to Online IDE!! Happy Coding :)");
//According to this problem you can buy the stock at 0 and sell it for a profit.
// For example the solution to this problem states
// for stocks 5,2,4,0,1 (4-2) + (1-0) = 3
// However for this code I have prevented this.
// Here are some scenarios created to test this problem. All seem to function.
// the value of k has not been implemented
//int[] stock = new int[]{1,3,2,8,4,10}; //CORRECT (3-1) + (8-2) + (10-4) = 14
//int[] stock = new int[]{5,2,4,0,1}; // CORRECT (4-2) = 2
 //int[] stock = new int[]{1,1,8,12,15}; // CORRECT (15-12) = 3
 //int[] stock = new int[]{1,3,2,8,10}; //PASSES (3-1) + (10-8) = 4
//int[] stock = new int[]{5,4,8}; //PASSES (8-4)
//int[] stock = new int[]{0,4,6,3,2}; //PASSES (6-4)
//int[] stock = new int[]{5,9,8,2}; // PASSES (9-5)
//int[] stock = new int[]{1,2,3,4,5,6,7,8}; // PASSES
//int[] stock = new int[]{19,5,32,7,15}; //PASSES (32-5) + (15-7) = 35
//EXTRA TESTS
//int[] stock = new int[]{5,2,15,4,18,24,9,1,1,2}; //FAILS IT BUYS (18-4) even though 24 is higher than 18
int[] stock = new int[]{0,1,0,1,0,2,0,4,2,3}; // PASSES 0
//int[] stock = new int[]{16,1,8,12,17}; // CORRECT
//int[] stock = new int[]{1,0,9,7,5,2,6,3}; // PASSES (9-1)
int[][] difference = new int[stock.length][20];

int count=0;
int temp=0;
int k;
boolean profitPossible=false;
int tempStore=0;
int runningTotal=0;
int num=20;
int [] profit = new int[num];
num=0;
int max=0;
int penultimateProfit=0;
boolean previousProfit=false;
boolean bypassPenultimate=false;

for (int i=0; i<stock.length;i++)
{
    System.out.println("\nStocks bought");
    System.out.println(Arrays.toString(stock));
    System.out.println("\n**************************");
    System.out.println("**************************");
    //System.out.println("current running total:" + runningTotal);
    System.out.println(stock[i]);
    int used=9999999;
    count=0;
    runningTotal=0;
    previousProfit=false;
    bypassPenultimate=false;
    
    for (int j=i+1; j<stock.length;j++) // this ensures stock is not compared against itself
    {
        if (j!=stock.length-1)
        {
            if (j==i)
            {
                j++;
            }
            
            // this checks if next stock is greater or higher than examined stock
            // and also if one after next is lower. These conditions will allow point of sale and rebuy at new value
            // this is to try and fix circumstance for 1, 3 ,2, 8, 10
            //currently it is doing 10 - 8 and skipping 10-2
            // this will try to accomodate for this condition.
            
            System.out.println("THIS POSITION***************");
            System.out.println("value of stock j:" + stock[j]);
            System.out.println("value of stock i:" + stock[i]);
            
            
            //if (stock[j]>=stock[i]&& stock[j+1]>stock[j] && stock[i]!=0 && j+1==stock.length-1 && !bypassPenultimate /*&& used!=stock[j]*/)
            if (stock[j+1]<stock[j] && /*stock[i]!=0 &&*/ j+1==stock.length-1 && !bypassPenultimate /*&& used!=stock[j]*/)
            
            {
                // this is to ensure that in stocks as below, it does not process.... 10-8 on two instances
                //int[] stock = new int[]{1,3,2,8,10};
                
                if (temp==stock.length-1 || temp+1 == stock.length-1)
                {
                    System.out.println("GETTTTT OUT!!!!");
                    break;
                    
                }
                System.out.println("ENTERRRRRRRRRRRRRRRRRRRRRRRRRRRRRR");
                profitPossible=true;
                System.out.println("Next highest stock000 is:" + stock[j+1] + " after " + stock[j]);
tempStore=difference[i][count];
difference[i][count]=stock[j+1]-stock[j];
runningTotal=runningTotal + difference[i][count];
//System.out.println("this is the profit so far1: " + difference[i][count] + "("+stock[j+1] + "-" + stock[j]+")");
System.out.println("this is the profit so far1: " + runningTotal + "("+stock[j+1] + "-" + stock[j] + "+"
+ tempStore+")");
profit[num]=runningTotal;
previousProfit=true;
System.out.println("This is the RUNNING SCENARIO TOTAL: " + profit[num]);
bypassPenultimate=true;
}


System.out.println("This is used: " + used);System.out.println("This is i: " + i);


//******* GETTING RID OF  stock[j+1]!=0  allows buying at 0
if (stock[j]>=stock[i]&& stock[j+1]<stock[j] && /*stock[i]!=0*/ temp!=j && !bypassPenultimate &&
used!=i)
{
    System.out.println("value of stockj: " + stock[j]);
    System.out.println("value of stockj+1: " + stock[j+1]);
    
profitPossible=true;
System.out.println("USSSSSSSSSSSSEDDD value: " + used);
System.out.println("Next highest stock222 is:" + stock[j] + " after " + stock[i]);
difference[i][count]=stock[j]-stock[i];
runningTotal=runningTotal + difference[i][count];
System.out.println("this is the profit so far123: " + difference[i][count] + "("+stock[j] + "-" +
stock[i]+")");
profit[num]=runningTotal;
used=i;
System.out.println("USED VALUEEEEEEE: " + used);
previousProfit=true;
System.out.println("This is the RUNNING SCENARIO TOTAL: " + profit[num]);
// this will check all other occurrences from the current point if there is scope to sell and buy
//conditions are similar
for (temp=j+1;temp<stock.length;temp++)
{
System.out.println("What is stock j:" + stock[j]);
System.out.println("What is stock i:" + stock[i]);
System.out.println("What is stock temp:" + stock[temp]);
// conditions are similar.. if next item is same or greater.. And following stock is lower than previous
System.out.println("What is temp:" + temp);

if (temp!=stock.length-1)
{
if (stock[temp]>stock[j+1] &&  stock[temp+1]<stock[temp] && !bypassPenultimate /*&& stock[temp-1]!=0
&& stock[j+1]!=0*/)
{
if (temp+1==stock.length-1)
// this is checking if last stock in list and to avoid any exceptions, it breaks out of loop
{
    
tempStore=difference[i][count];
difference[i][count]=difference[i][count] + (stock[temp+1]-stock[temp]);
if (previousProfit)
{
runningTotal=difference[i][count];
}
if (!previousProfit)
{
runningTotal=runningTotal + difference[i][count];
}
//runningTotal=runningTotal + difference[i][count];
System.out.println("this is the profit so far2: " + difference[i][count] + "("+ stock[temp+1]
+ "-" + stock[temp] +"+"+ tempStore + ")");
System.out.println("this is the stock: " + stock[i]);
System.out.println("this is the num: " + num);
profit[num]=runningTotal;
System.out.println("This is the RUNNING SCENARIO TOTAL: " + profit[num]);
num++;
break;
}
System.out.println("The next highest stock111 is: " + stock[temp] + " after " + stock[temp-
1]);
tempStore=difference[i][count];
difference[i][count]=difference[i][count] + (stock[temp]-stock[temp-1]);
System.out.println("this is the profit so far3: " + difference[i][count] + "("+ stock[temp] + "-"
+ stock[temp-1] +"+"+ tempStore + ")");
//positionCompared=temp;
if (previousProfit)
{
runningTotal=difference[i][count];
}
if (!previousProfit)
{
runningTotal=runningTotal + difference[i][count];
}
//runningTotal=runningTotal + difference[i][count];
profit[num]=runningTotal;
//System.
System.out.println("This is the RUNNING SCENARIO TOTAL: " + profit[num]);
System.out.println("this is the stock: " + stock[i]);
System.out.println("this is the num: " + num);
num++;
break;
}
}

}
}
}
// this is now creating a scenario to examine if there has been a buy and resell
// it also checks scenario such as 2,4,0,1 since the current logic above
// does not account for a buy and sell as last two stocks
if (stock[j]>stock[j-1] && profitPossible && j==stock.length-1 && i==stock.length-2 &&
bypassPenultimate)
{
System.out.println("This should be penultimate stock:" + stock[i]);
System.out.println("This should be last stock:" + stock[j]);
tempStore=difference[i][count];
runningTotal = runningTotal + (stock[j]-stock[j-1]);
// It is capturing value here
penultimateProfit=(stock[j]-stock[j-1]);
num++;
profit[num]=runningTotal;
System.out.println("This is the RUNNING SCENARIO TOTAL: " + profit[num]);
System.out.println("this is the stock: " + stock[i]);
System.out.println("this is the num: " + num);
System.out.println("this is the profit so far44: " + runningTotal + "("+stock[j] +"-"+ stock[j-1]+")");
profit[num]=runningTotal;
System.out.println("************REACH********");
break;
}
// this now covers scenario such as 1 , 1, 8 , 15
// without this logic, there will be no buy sell and buy
// checks if stock is higher than examined stock.// it will only enter this scenario also if no other profits have been analysed
// otherwise totals will be incorect.. For instance, it would process 8 => 15 when it
// would be completed as part of normal logic.
System.out.println("WHY NOT HERE!!!!!!!!!!!");
System.out.println(stock[j]);
System.out.println(stock[i]);
System.out.println("value of j:" + j);
System.out.println("length:" + (stock.length-1));
if (stock[j]>stock[i] && !profitPossible && j==stock.length-1 /*&& stock[i]!=0*/)
{
//System.out.println("Rare instance of all increasing");
System.out.println("This stock:" + stock[i]);
System.out.println("This should be last stock:" + stock[j]);
tempStore=difference[i][count];
if (previousProfit)
{
runningTotal = runningTotal + (stock[j]-stock[i]);
}
if (!previousProfit)
{
runningTotal=(stock[j]-stock[i]);
}
//runningTotal = runningTotal + (stock[j]-stock[i]);
profit[num]=runningTotal;
System.out.println("This is the RUNNING SCENARIO TOTAL: " + profit[num]);
num++;
System.out.println("this is the profit so far55: " + runningTotal + "("+stock[j] +"-"+ stock[i]+")");
System.out.println("************REACH AGAIN********");
break;
}
}
System.out.println("This is the stock:" + stock[i]);
System.out.println("This is all possible routes to make profit:" + difference[i][count]);
//profit[num]=runningTotal;
System.out.println("VALUE HERE:");
//System.out.println(profit[num]);
num++;
count++;
}
// this is simply assigning the highest profitable route
for (int p=0;p<profit.length;p++)
{
System.out.println("ALL VALUES");
System.out.println(profit[p]);
if (profit[p]>max)
{
max=profit[p];
}
}
// this gets the profit total if shares are not as such:
//5,2,4,0,1
// this now checks if scenario such as above
if (penultimateProfit!=0 && previousProfit)
{
System.out.println("This is the highest profit possible1: " + (max + penultimateProfit));
System.out.println(max);
System.out.println(penultimateProfit);
}
else
{System.out.println("This is the highest profit possible: " + max);
}
}
}