public class RiverRecords {

   
public static int maxTrailing(List<Integer> arr) {
       
if (arr.isEmpty()) return -1; // If the list is empty, return -1

       
int maxTrailingDifference = -1; // Initialize the maximum trailing difference to -1
       
int minSoFar = arr.get(0); // Initialize the minimum element seen so far to the first element

       
for (int i = 1; i < arr.size(); i++) {
           
int current = arr.get(i);
           
int difference = current - minSoFar;

           
if (difference > 0 && (maxTrailingDifference == -1 || difference > maxTrailingDifference)) {
               
maxTrailingDifference = difference;
            }

           
if (current < minSoFar) {
               
minSoFar = current; // Update the minimum element seen so far
           
}
        }

       
return maxTrailingDifference;
    }

   
public static void main(String[] args) {
       
// Example usage:
       
List<Integer> arr1 = List.of(2, 3, 10, 2, 4, 8, 1);
       
System.out.println(maxTrailing(arr1)); // Output: 8

       
List<Integer> arr2 = List.of(4, 3, 2, 1);
       
System.out.println(maxTrailing(arr2)); // Output: -1
   
}
}