#include #define rep(i,n) for(int i = 0; i < (n); ++i) #define rep1(i,n) for(int i = 1; i <= (n); ++i) #define drep(i,n) for(int i = (n)-1; i >= 0; --i) #define srep(i,s,t) for (int i = s; i < (t); ++i) #define rng(a) a.begin(),a.end() #define rrng(a) a.rbegin(),a.rend() #define fi first #define se second #define pb push_back #define eb emplace_back #define em emplace #define pob pop_back #define sz(x) (int)(x).size() #define pcnt __builtin_popcountll #define snuke srand((unsigned)clock()+(unsigned)time(NULL)); #define newline puts("") #define vc vector using namespace std; template using vv = vc>; template using PQ = priority_queue,greater>; using uint = unsigned; using ull = unsigned long long; using vi = vc; using vvi = vv; using vvvi = vv; using ll = long long; using vl = vc; using vvl = vv; using vvvl = vv; using P = pair; using vp = vc

; using vvp = vv

; int geti(){int x;scanf("%d",&x);return x;} vi pm(int n, int s=0) { vi a(n); iota(rng(a),s); return a;} templateistream& operator>>(istream&i,pair&v){return i>>v.fi>>v.se;} templateostream& operator<<(ostream&o,const pair&v){return o<istream& operator>>(istream&i,vc&v){rep(j,sz(v))i>>v[j];return i;} templatestring join(const T&v,const string& d=""){stringstream s;rep(i,sz(v))(i?s<ostream& operator<<(ostream&o,const vc&v){if(sz(v))o<void vin(vv& a){int n,m;cin>>n>>m;a=vv(n,vc(m));cin>>a;} templatevoid vin(vc& a){int n;cin>>n;a=vc(n);cin>>a;} templatevoid operator--(pair&a,int){a.fi--;a.se--;} templatevoid operator++(pair&a,int){a.fi++;a.se++;} templatevoid operator--(vc&a,int){for(T&x:a)x--;} templatevoid operator++(vc&a,int){for(T&x:a)x++;} templatevoid operator+=(vc&a,T b){for(T&x:a)x+=b;} templatevoid operator-=(vc&a,T b){for(T&x:a)x-=b;} templatevoid operator*=(vc&a,T b){for(T&x:a)x*=b;} templatevoid operator/=(vc&a,T b){for(T&x:a)x/=b;} templatevoid operator+=(vc&a,const vc&b){a.insert(a.end(),rng(b));} templatebool mins(T1& x,const T2&y){if(ybool maxs(T1& x,const T2&y){if(xT min(const vector& a){return *min_element(rng(a));} templateT max(const vector& a){return *max_element(rng(a));} templateTx dup(Tx x, Ty y){return (x+y-1)/y;} templatell suma(const vc&a){ll res(0);for(auto&&x:a)res+=x;return res;} templatell suma(const vv&a){ll res(0);for(auto&&x:a)res+=suma(x);return res;} templatevoid uni(T& a){sort(rng(a));a.erase(unique(rng(a)),a.end());} templatevoid prepend(vc&a,const T&x){a.insert(a.begin(),x);} const double eps = 1e-10; const ll LINF = 1001002003004005006ll; const int INF = 1001001001; #define dame { puts("-1"); return 0;} #define yes { puts("Yes"); return 0;} #define no { puts("No"); return 0;} #define rtn(x) { cout<<(x)<; void vin(vs& s){int n,m;cin>>n>>m;s=vs(n);cin>>s;} // Max flow // !! Be care of double and INF !! struct Maxflow { typedef int TT; int n; vi to, next, head, dist, it; vector lim; Maxflow(){} Maxflow(int n):n(n),head(n,-1),it(n){} void add(int a, int b, TT c=1) { next.pb(head[a]); head[a] = sz(to); to.pb(b); lim.pb(c); next.pb(head[b]); head[b] = sz(to); to.pb(a); lim.pb(0); } void bfs(int sv) { dist = vi(n,INF); queue q; dist[sv] = 0; q.push(sv); while (!q.empty()){ int v = q.front(); q.pop(); for (int i = head[v]; i != -1; i = next[i]) { if (lim[i] && dist[to[i]] == INF) { // double !! dist[to[i]] = dist[v]+1; q.push(to[i]); } } } } TT dfs(int v, int tv, TT nf=INF) { // INF !! if (v == tv) return nf; for (; it[v] != -1; it[v] = next[it[v]]) { int u = to[it[v]]; TT f; if (!lim[it[v]] || dist[v] >= dist[u]) continue; // double !! if (f = dfs(u, tv, min(nf, lim[it[v]])), f) { // double !! lim[it[v]] -= f; lim[it[v]^1] += f; return f; } } return 0; } TT solve(int sv, int tv) { TT flow = 0, f; while (1) { bfs(sv); if (dist[tv] == INF) return flow; rep(i,n) it[i] = head[i]; while (f = dfs(sv,tv), f) flow += f; } } }; // // coordinate compression struct X { typedef int T; vector d; X() {} // X(vector& a): d(a) { // init(); // for (T& na : a) na = (*this)(na); // } void add(const T& x) { d.pb(x);} void init() { sort(rng(d)); d.erase(unique(rng(d)), d.end()); } int size() const { return sz(d);} T operator[](int i) const { return d[i];} int operator()(const T& x) const { return upper_bound(rng(d),x)-d.begin()-1;} bool in(const T& x) const { return binary_search(rng(d), x);} }; // int main() { int h,w; scanf("%d%d",&h,&w); vi a(h), b(w); cin>>a>>b; int n; scanf("%d",&n); vp p(n); cin>>p; rep(i,n) p[i].fi--; rep(i,n) p[i].se--; // X xs, ys; // rep(i,n) xs.add(p[i].fi); // rep(i,n) ys.add(p[i].se); // xs.init(); // ys.init(); // int eh = sz(xs), dh = h-eh; // int ew = sz(ys), dw = w-ew; vs ans(h,string(w,'.')); rep(i,n) ans[p[i].fi][p[i].se] = 'x'; // int sv = h+w, tv = sv+1; // Maxflow g(tv+1); // rep(ei,eh) { // int i = xs[ei]; // rep(j,w) { // if (ys.in(j)) continue; // g.add(i,h+j); // } // } // rep(ej,ew) { // int j = ys[ej]; // rep(i,h) { // if (xs.in(i)) continue; // g.add(i,h+j); // } // } // rep(i,h) { // if (xs.in(i)) g.add(sv,i,a[i]); // else { // int now = max(0,a[i]-dw); // g.add(sv,i,now); // } // } // rep(j,w) { // if (ys.in(j)) g.add(h+j,tv,b[j]); // else { // int now = max(0,b[j]-dh); // g.add(h+j,tv,now); // } // } // g.solve(sv,tv); // vi ta = a, tb = b; // int e = 0; // rep(ei,eh) { // int i = xs[ei]; // rep(j,w) { // if (ys.in(j)) continue; // if (!g.lim[e]) tb[j]--; // e += 2; // } // } // rep(ej,ew) { // int j = ys[ej]; // rep(i,h) { // if (xs.in(i)) continue; // if (!g.lim[e]) ta[i]--; // e += 2; // } // } // rep(i,h) if (!xs.in(i) && ta[i] > dw) dame; // rep(j,w) if (!ys.in(j) && tb[j] > dh) dame; // int ne = sz(g.lim); // rep(ei,eh) { // int i = xs[ei]; // rep(ej,ew) { // int j = ys[ej]; // if (ans[i][j] == 'x') continue; // g.add(i,h+j); // } // } // rep(i,h) { // if (xs.in(i)) continue; // g.add(sv,i, min(a[i],dw)); // } // rep(j,w) { // if (ys.in(j)) continue; // g.add(h+j,tv, min(b[j],dh)); // } // g.solve(sv,tv); auto add = [&](int i, int j) { ans[i][j] = 'o'; a[i]--; b[j]--; }; // e = 0; // rep(ei,eh) { // int i = xs[ei]; // rep(j,w) { // if (ys.in(j)) continue; // if (!g.lim[e]) add(i,j); // e += 2; // } // } // rep(ej,ew) { // int j = ys[ej]; // rep(i,h) { // if (xs.in(i)) continue; // if (!g.lim[e]) add(i,j); // e += 2; // } // } // e = ne; // rep(ei,eh) { // int i = xs[ei]; // rep(ej,ew) { // int j = ys[ej]; // if (ans[i][j] == 'x') continue; // if (!g.lim[e]) add(i,j); // e += 2; // } // } // rep(ei,eh) if (a[xs[ei]]) dame; // rep(ej,ew) if (b[ys[ej]]) dame; // vi is, js; // rep(i,h) if (!xs.in(i)) is.pb(i); // rep(j,w) if (!ys.in(j)) js.pb(j); // for (int i : is) { // vp nj; // for (int j : js) nj.eb(b[j],j); // sort(rrng(nj)); // int na = a[i]; // rep(k,na) add(i,nj[k].se); // } int sv = h+w, tv = sv+1; Maxflow g(tv+1); vi oa = a, ob = b; vvi ng(h,vi(w)); rep(i,h) { vp nj; rep(j,w) nj.eb(b[j],j); sort(rrng(nj)); rep(k,a[i]) { int j = nj[k].se; ng[i][j] = 1; b[j]--; } a[i] = 0; } rep(j,w) if (b[j]) dame; int e = 0; rep(i,h)rep(j,w) { if (ans[i][j] == 'x') { if (!ng[i][j]) continue; a[i]++; b[j]++; } else { g.add(i,h+j); if (ng[i][j]) { g.lim[e] = 0; g.lim[e^1] = 1; } e += 2; } } rep(i,h) if (a[i]) g.add(sv,i,a[i]); rep(j,w) if (b[j]) g.add(h+j,tv,b[j]); g.solve(sv,tv); a = oa; b = ob; e = 0; rep(i,h)rep(j,w) { if (ans[i][j] == 'x') continue; if (!g.lim[e]) add(i,j); e += 2; } rep(i,h) if (a[i]) dame; rep(j,w) if (b[j]) dame; cout<<"Yay!"<