def getMinimumSum(arr): n = len(arr) if n < 3: return -1 left_min = [None] * n min_so_far = float('inf') # Populate left_min for j in range(n): if min_so_far < arr[j]: left_min[j] = min_so_far else: left_min[j] = None if arr[j] < min_so_far: min_so_far = arr[j] right_min = [None] * n min_so_far = float('inf') # Populate right_min for j in range(n-1, -1, -1): if min_so_far < arr[j]: right_min[j] = min_so_far else: right_min[j] = None if arr[j] < min_so_far: min_so_far = arr[j] min_sum = float('inf') for j in range(n): if left_min[j] is not None and right_min[j] is not None: current_sum = left_min[j] + arr[j] + right_min[j] if current_sum < min_sum: min_sum = current_sum return min_sum if min_sum != float('inf') else -1 # Example Usage: if __name__ == "__main__": arr = [3, 4, 5, 1, 2, 3, 1] result = getMinimumSum(arr) print(result) # Output: 4