• + 0 comments
    import os
    
    def minimumLoss(price):
        # Create a list of (price, original index) tuples
        price_with_indices = [(price[i], i) for i in range(len(price))]
        
        # Sort the list by price
        price_with_indices.sort()
        
        min_loss = float('inf')
        
        # Iterate through the sorted prices and calculate the minimum loss
        for i in range(1, len(price_with_indices)):
            current_price, current_index = price_with_indices[i]
            previous_price, previous_index = price_with_indices[i - 1]
            
            # Ensure that we buy before we sell
            if current_index < previous_index:
                min_loss = min(min_loss, current_price - previous_price)
        
        return min_loss
    
    if __name__ == '__main__':
        import os  # Ensure os is imported
    
        fptr = open(os.environ['OUTPUT_PATH'], 'w')
    
        n = int(input().strip())
    
        price = list(map(int, input().rstrip().split()))
    
        result = minimumLoss(price)
    
        fptr.write(str(result) + '\n')
    
        fptr.close()