#C5951. Sum Excluding Minimum and Maximum

    ID: 49657 Type: Default 1000ms 256MiB

Sum Excluding Minimum and Maximum

Sum Excluding Minimum and Maximum

You are given an array of integers and its size N. Your task is to compute the sum of all elements in the array excluding all instances of the minimum and maximum elements. If N is less than or equal to 2, there is no number left after exclusions, so the answer is 0.

Note: If the minimum and maximum elements are the same, then excluding them will remove every element from the array, and thus the result will be 0.

The solution should read the input from standard input (stdin) and write the output to standard output (stdout).

Mathematical Formulation:

Let \(A = \{a_1, a_2, \dots, a_N\}\) be the array. Let \(m = \min_{1 \leq i \leq N} a_i\) and \(M = \max_{1 \leq i \leq N} a_i\). Then the required sum is given by:

[ S = \sum_{i=1}^N,,\text{if }a_i \neq m \text{ and }a_i \neq M\ a_i ]

Output the sum \(S\).

inputFormat

The first line contains an integer N (\(1 \leq N \leq 10^5\)) representing the size of the array.

The second line contains N space-separated integers representing the elements of the array.

outputFormat

Output a single integer which is the sum of all elements in the array excluding all occurrences of the minimum and maximum elements.

## sample
5
3 1 2 5 4
9