#include <bits/stdc++.h>
using namespace std;
const long double PI = acos((long double)-1.0);
template <class T>
void dbs(string str, T t) {
  cerr << str << " : " << t << "\n";
}
template <class T, class... S>
void dbs(string str, T t, S... s) {
  int idx = str.find(',');
  cerr << str.substr(0, idx) << " : " << t << ",";
  dbs(str.substr(idx + 1), s...);
}
template <class S, class T>
ostream& operator<<(ostream& os, const pair<S, T>& p) {
  return os << "(" << p.first << ", " << p.second << ")";
}
template <class T>
ostream& operator<<(ostream& os, const vector<T>& p) {
  os << "[ ";
  for (auto& it : p) os << it << " ";
  return os << "]";
}
template <class T>
ostream& operator<<(ostream& os, const set<T>& p) {
  os << "[ ";
  for (auto& it : p) os << it << " ";
  return os << "]";
}
template <class S, class T>
ostream& operator<<(ostream& os, const map<S, T>& p) {
  os << "[ ";
  for (auto& it : p) os << it << " ";
  return os << "]";
}
template <class T>
void prc(T a, T b) {
  cerr << "[";
  for (T i = a; i != b; ++i) {
    if (i != a) cerr << ", ";
    cerr << *i;
  }
  cerr << "]\n";
}
vector<pair<int, int>> nodes;
int parent1[1000010];
int child1[1000010];
vector<int> Adj[1000010];
long long sum1 = 0;
vector<pair<pair<int, int>, int>> edges;
int val1[1000010];
int parent(int v) {
  while (v != parent1[v]) {
    v = parent1[v];
  }
  return v;
}
void unite(int u, int v) {
  u = parent(u);
  v = parent(v);
  if (u == v) return;
  if (child1[u] > child1[v]) {
    child1[u] += child1[v];
    parent1[v] = parent1[u];
  } else {
    child1[v] += child1[u];
    parent1[u] = parent1[v];
  }
}
long long solve() {
  long long answer1 = 0;
  for (auto edge : edges) {
    long long weight = edge.first.first;
    int par1 = parent(edge.first.second);
    int par2 = parent(edge.second);
    answer1 += edge.first.first * 1LL * child1[par1] * 1LL * child1[par2];
    unite(par1, par2);
  }
  return answer1;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  for (int i = (int)(1); i <= (int)(n); i++) {
    int temp;
    cin >> temp;
    val1[i] = temp;
  }
  for (int i = (int)(1); i <= (int)(n - 1); i++) {
    int a1, b1;
    cin >> a1 >> b1;
    int minWeight = min(val1[a1], val1[b1]);
    edges.push_back(make_pair(make_pair(minWeight, a1), b1));
  }
  for (int i = (int)(1); i <= (int)(n); i++) parent1[i] = i, child1[i] = 1;
  sort(edges.begin(), edges.end(),
       [](const pair<pair<int, int>, int>& a,
          const pair<pair<int, int>, int>& b) {
         return a.first.first > b.first.first;
       });
  long long minValue = solve();
  for (int i = (int)(0); i <= (int)(int(edges.size()) - 1); i++) {
    edges[i].first.first =
        max(val1[edges[i].first.second], val1[edges[i].second]);
  }
  sort(edges.begin(), edges.end(),
       [](const pair<pair<int, int>, int>& a,
          const pair<pair<int, int>, int>& b) {
         return a.first.first < b.first.first;
       });
  for (int i = (int)(1); i <= (int)(n); i++) parent1[i] = i, child1[i] = 1;
  long long maxValue = solve();
  cout << maxValue - minValue << "\n";
  return 0;
}
