#include<bits/stdc++.h>
using namespace std;
int f[201],w[201],c[201];
int m,n,k,ans;
bool mapp[201][201];
int main()
{ freopen("landmine.in","r",stdin);
freopen("landmine.out","w",stdout);
scanf("%d",&n);
for (int i=1;i<=n;i++)
{ scanf("%d",&w[i]);
}
f[n]=w[n];
int x,y;
scanf("%d%d",&x,&y);
while (true)
{ if(!x&&!y) break;
mapp[x][y]=true;
scanf("%d%d",&x,&y);
}
for (int i=n-1;i>=1;i--)
{ int l=0,k=0;
for (int j=i+1;j<=n;j++)
if (mapp[i][j]) if (f[j]>l) {l=f[j];k=j;}
f[i]=w[i]+l;
c[i]=k;
}
for (int i=1;i<=n;i++)
if (f[i]>ans){ans=f[i];k=i;}
cout<<k;
k=c[k];
while (k!=0)
{ cout<<"-"<<k;
k=c[k];
}
cout<<endl;
printf("%d",ans);
return 0;
}