         let j = Stack.pop stack in
         let rec loop2 subtotal =
           if Stack.is_empty stack2 then
             Stack.push (j,subtotal) stack2
           else
             let (id,area) = Stack.top stack2 in
             if id <= j then
               Stack.push (j,subtotal) stack2
             else
               begin
                 let (id,area) = Stack.pop stack2 in
                 loop2 (subtotal+area)
               end
         in
         loop2 (i-j);
         loop (total + i-j) (i+1)
       else
         loop total (i+1)
  in
  Printf.printf "%d\n" (loop 0 0);
  Printf.printf "%d" (Stack.length stack2);
  let st = Stack.create () in
  Stack.iter (fun x -> Stack.push (snd x) st) stack2;
  Stack.iter (fun x -> Printf.printf " %d" x) st;print_newline ()
;;