#include
using namespace std;
int main()
{
int n,max, min, pos1, pos2, mem, sum;
cin >> n;
pos1 = 0;
pos2 = 0;
int *A = new int[n];
for (int i = 0; i < n; i++)
{
cin >> A[i];
}
min = A[0];
max = A[0];
for (int i = 0; i < n; i++)
{
if (A[i] > max)
{
max = A[i];
pos1 = i;
}
if (A[i] < min)
{
min = A[i];
pos2 = i;
}
}
if (pos2 > pos1)
{
mem = pos2;
pos2 = pos1;
pos1 = mem;
}
sum = 0;
for (pos2; pos2 <= pos1; pos2++)<br>{
sum = sum + A[pos2];
}
cout << sum;<br>system("pause");
return 0;
}