#include <bits/stdc++.h>
#define _CRT_SECURE_NO_WARNINGS
using namespace std;
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define uniq(x) x.resize(unique(all(x)) - x.begin());
#define sort_uniq(x) sort(all(x)), uniq(x);
#define ll long long
#define ld long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define V vector
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define eb emplace_back
#define FASTIO ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
#define INF INT32_MAX
#define blt __builtin_popcount
#define clr(x) x.clear()
#define ff first
#define ss second
#define popf pop_front
#define popb pop_back
#define sz(x) int(x.size())
#define rep(a, b, c, d) for (int a = b; a <= c; a += d)
#define repl(a, b, c, d) for (int a = b; a >= c; a -= d)
const int N = 1e4 + 1;
const int M = 101;
V<int> adj[N];
int v[N];
static bitset<M> dp[N][M];
void dfs(int node) {
dp[node][v[node]][v[node]] = 1;
if (!sz(adj[node]))
return;
bitset<M> chld[M];
rep(i, 0, M - 1, 1)
chld[i].reset();
for (auto to : adj[node]) {
dfs(to);
rep(j, 1, M - 1, 1)
chld[j] |= dp[to][j];
}
for (int i = M - 1; i >= 1; i--) {
for (int j = i + 1; j < M; j++) {
if (chld[i][j - 1])
chld[i] |= chld[j];
}
}
rep(i, 1, v[node], 1) {
rep(j, v[node], M - 1, 1) {
if ((i == v[node] || chld[i][v[node] - 1]) &&
(j == v[node] || chld[v[node] + 1][j]))
dp[node][i][j] = 1;
}
}
}
int main() {
FASTIO
int n;
cin >> n;
rep(i, 1, n, 1)
cin >> v[i];
rep(i, 1, n - 1, 1) {
int a, b;
cin >> a >> b;
adj[a].pb(b);
}
dfs(1);
ll ans = 0;
rep(i, 1, M - 1, 1)
ans += dp[1][i].count();
cout << ans << "\n";
}