#include
#include
#include
#include
#include
using namespace std;
vector<vector> vec;
vector vis,dur,et,ft,path={-1};
int n,m,x,y;
stack sta;
void topuDFS(int e)
{
vis[e]=1;
for (auto t : vec[e])
{
if(!vis[t])
{
topuDFS(t);
}
if(ft[t]>=et[e])
{
et[e]=ft[t];
path[e]=t;
}
}
ft[e]=et[e]+dur[e];
}
void pathcount(int t)
{
cout<<t<<" ";
if(path[t]!=-1)
{
pathcount(path[t]);
}
}
int main ()
{
cin>>n;
dur.resize(n);
et.resize(n);
ft.resize(n);
path.resize(n);
for (int i=0;i<n;i++)//这里i从什么开始要看题目要求
{
cin>>dur[i];
}
vec.resize(n+1);
vis.resize(n+1);
cin>>m;
for (int i=0;i<m;i++)
{
cin>>x>>y;
vec[x].push_back(y);
}
for(int i=1;i<=n;i++)
{
if(!vis[i])
topuDFS(i);
}
auto res=max_element(ft.begin(),ft.end());
cout<<*res<<endl;
auto res_id=res-ft.begin();
pathcount(res_id);
cout<<endl;
//输出路径
cout<<path[0]<<endl;
return 0;
}