#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#define fo(i,a,b) for(i=a;i<=b;i++)
#define fod(i,a,b) for(i=a;i>=b;i--)
#define rep(i,a) for(i=first[a];i;i=next[i])
using namespace std;
const int maxn=1e6+7;
int i,j,k,l,t,n,m,ans,head,tail,x,y,z,fu;
int first[maxn*2],last[maxn*2],next[maxn*2],num,a[maxn];
int f[maxn],d[maxn],fa[maxn],in[maxn],out[maxn];
int b[maxn],c[maxn];
void add(int x,int y){
last[++num]=y,next[num]=first[x],first[x]=num;
}
int main(){
// freopen("fan.in","r",stdin);
// freopen("fan.out","w",stdout);
scanf("%d",&n);
fo(i,1,n)scanf("%d",&a[i]);
fo(i,1,n-1)scanf("%d%d",&k,&l),add(k,l),add(l,k);
head=0,d[tail=1]=1;
while(head<tail){
rep(i,d[++head]){
if(last[i]==fa[d[head]])continue;
fa[last[i]]=d[head];d[++tail]=last[i];
}
}
fod(k,n,1){
x=d[k];
rep(i,x){
y=last[i];if(y==fa[x])continue;
z=a[y]-f[y];
if(z>0){
if(x>y)in[x]+=z,out[y]+=z,f[x]+=z;
else out[x]+=z,in[y]+=z,f[x]+=z;
}
else{
z=-z;
if(x>y)in[y]+=z,out[x]+=z,f[x]-=z;
else out[y]+=z,in[x]+=z,f[x]-=z;
}
}
}
fo(i,1,n){
j=min(in[i],out[i]),in[i]-=j,out[i]-=j;
while(in[i])in[i]--,c[++c[0]]=i;
while(out[i])out[i]--,b[++b[0]]=i;
}
printf("%d\n",b[0]);
fo(i,1,b[0])printf("%d %d\n",b[i],c[i]);
}