#include // #pragma GCC optimize("O3") // #pragma GCC optimize("unroll-loops") #include using namespace std; using namespace atcoder; using ll=long long; using LL=__int128; using ld=long double; using uint=unsigned int; using ull=unsigned long long; using pii=pair; using pll=pair; template using vc=vector; template using vvc=vector>; template using vvvc=vector>; using vi=vc; using vvi=vvc; using vd=vc; using vvd=vvc; using vl=vc; using vvl=vvc; using vs=vc; using vp=vc; using vvp=vvc; #define overload3(_1,_2,_3,name,...) name #define rep1(n) for(ll _=0;_ll(a);) #define rrep(...) overload3(__VA_ARGS__,rrep3,rrep2,rrep1)(__VA_ARGS__) #define ALL(a) (a).begin(),(a).end() #define RALL(a) (a).rbegin(),(a).rend() template ostream& operator<<(ostream& os,static_modint v){ os<>(istream& is,__int128& v){ string s; is>>s; v=0; rep(i,s.size())if(isdigit(s[i])) v=v*10+s[i]-'0'; if(s[0]=='-') v*=-1; return is; } ostream& operator<<(ostream& os,__int128 v){ ostream::sentry s(os); if(s){ __uint128_t t=v<0?-v:v; char buf[128]; char* d=end(buf); do{ d--; *d="0123456789"[t%10]; t/=10; }while(t); if(v<0){ d--; *d='-'; } int len=end(buf)-d; if(os.rdbuf()->sputn(d,len)!=len) os.setstate(ios_base::badbit); } return os; } template ostream& operator<<(ostream& os,pair a){ os< ostream& operator<<(ostream& os,vector a){ if(a.empty()) return os; os< ostream& operator<<(ostream& os,vector> a){ if(a.empty()) return os; os< void output(vector a,bool next_line=0){ if(next_line){ for(auto x:a) output(x); } else cout<y) x=y;} void chmax(auto &x,auto y){if(x void read(vector &a,int n,int off=0){ a=vector(n); for(auto &i:a){ cin>>i; i-=off; } } void read(vs &a,int n){ a=vs(n); for(auto &i:a) cin>>i; } template void read(vector> &a,int n,int off=0){ a=vector>(n); for(auto &[x,y]:a){ cin>>x>>y; x-=off,y-=off; } } template void read(vector> &a,int n,int m,int off=0){ a=vector>(n,vector(m)); for(auto &i:a) for(auto &j:i){ cin>>j; j-=off; } } template void readGraph(vector> &g,int n,int m,bool rv=1,int off=1){ g=vector>(n); for(int i=0;i>u>>v; u-=off,v-=off; g[u].emplace_back(v); if(rv) g[v].emplace_back(u); } } template void readGraph(vector>> &g,int n,int m,bool id=0,bool rv=1,int off=1){ g=vector>>(n); for(int i=0;i>u>>v; u-=off,v-=off; g[u].emplace_back(v,i); if(rv) g[v].emplace_back(u,i); } else{ T u,v,w; cin>>u>>v>>w; u-=off,v-=off; g[u].emplace_back(v,w); if(rv) g[v].emplace_back(u,w); } } } string toBinary(ll x,ll n,bool rev=0){ assert(0<=x&&x<1LL<; using vm=vc; using vvm=vvc; void main2(); int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); ll q=1; // cin>>q; while(q--) main2(); } void main2(){ ll n,m; cin>>n>>m; vm a(n); rep(i,n){ ll x; cin>>x; a[i]=x; } const vm a_=a; vp ed; read(ed,m,1); dsu uft(2*n); ll id_=-1; vvp g(n); rep(i,m){ auto [u,v]=ed[i]; if(uft.same(u,v)) id_=i; else if(!uft.same(u,v+n)){ uft.merge(u,v+n); uft.merge(u+n,v); g[u].emplace_back(v,i); g[v].emplace_back(u,i); } } mint sm=0; rep(i,n) sm+=(uft.same(i,0)?a[i]:-a[i]); if(sm==0||id_>=0){ vm rs(m); if(sm!=0){ auto [u,v]=ed[id_]; mint t=(uft.same(0,v)?sm:-sm)/2; rs[id_]=t; a[u]-=t,a[v]-=t; } sm=0; auto f=[&](auto sf,ll v,ll p)->void { for(auto [u,i]:g[v])if(u!=p){ sf(sf,u,v); mint t=a[u]; rs[i]=t; a[u]-=t,a[v]-=t; } }; f(f,0,-1); output(rs); } else output(-1); }