{* 
 * Solution to Problem 4 (Pascal)
 * 11th Annual CS Programming Contest
 * WCU Dept of Math and CS
 * Copyright 2000, All Rights Reserved
 *
 * Problem Description: Stock Market Problem from page 190- 196 of
 * Kamin98. Read in the values of a stock for a sequence of
 * consecutive days and find the best pair of days to have bought and
 * sold the stock to maximize your profit. There is only one buy day
 * and one sell day.
 *}

program problem4(input,output);

var
   
{biggest profit possible so far	: starts at 0}
   big_diff			: integer;
   
{sell price so far}
   sell_at  : integer;
   
{ sell day so far: starts at 1.}
   sell_day  : integer;
   
{ maximum profit for the current day: starts at 0. }
   current_diff  : integer;
   
  { lowest price so far}
   low  : integer;
   
  { lowest price day so far: starts at 1.}
   low_day  : integer;
   
  { buy price so far}
   buy_at  : integer;

  { buy day so far: starts at 1.}
   buy_day  : integer;

 {these hold the current price and the current day }
   input, day  : integer;

begin

  {* I initialize the big_diff to 0 because the profit for buying and
   * selling on the first day is 0.
   * 
   * The buy- sell- and low- days are initialized to 1 because that is
   * what they should be on the first day.
   *}

   big_diff := 0;
   sell_day := 1;
   current_diff := 0;
   low_day := 1;
   buy_day := 1;
   input := 0;
   day := 1;


   { The first day input is treated specially. }
   Writeln("Enter the value of the stock on day 1: ");
   Readln(input);
   
   { On the first day, input is the lowest AND the highest value
    entered so far:}
   
   low := input;
   buy_at := input;
   sell_at := input;

   if input <> -1 then
   begin
      Writeln("Enter the value of the stock on day 2");
      Readln(input);
      day := 2;
   end;
   
  while input <> -1 do
  begin
     { Update the value of low and low_day.}
     if input < low then
     begin
	low := input;
	low_day := day;
     end;

    { Calculate maximum profit for selling today.  The max profit for
     selling today will be the difference between today's price and
     the lowest price so far. }
   current_diff := input - low;

    { If today's max profit is bigger than the biggest so far, }
    if current_diff > big_diff then
       begin
      { then change the buy and sell data }
	  big_diff := current_diff;
	  sell_at := input;
	  sell_day := day;
	  buy_at := low;
	  buy_day := low_day;
       end;

   day := day + 1;
   Write("Enter the value of the stock on day ");
   Write(day);
   Writeln(":");
   Readln (input);
  end;

  { Report the results}
  
  { If sell_at is -1, that can only mean that no data was entered.}
   if sell_at = -1 then
   begin
      Writeln( "No data entered.  Goodbye.\n");
   end
   else
   begin
      Write("Maximum profit comes from buying on day ");
      Write(buy_day);
      Write(" and selling on day ");
      Writeln(sell_day);
      Write("The maximum profit is $");
      Writeln(big_diff);
   end;
End.
