Problem:
Treasure Map
Mr. Kobou found a bundle of old paper when he was cleaning his family home. On each paper, two series of numbers are written. Strange as it appeared to him, Mr. Kobou further went through the storehouse and found out a note his ancestor left. According to it, the bundle of paper is a treasure map, in which the two sequences of numbers seem to give a clue to the whereabouts of the treasure the ancestor buried.
Mr. Kobou’s ancestor divided the area where he buried his treasure in a reticular pattern and used only some of the grid sections. The two series of numbers indicate the locations: the $i$-th member of the first series indicates the number of locations in the $i$-th column (form left) of the grid sections where a part of the treasure is buried, and the $j$-th member of the second indicates the same information regarding the $j$-th row from the top. No more than one piece of treasure is buried in one grid section. An example of a 5 × 4 case is shown below. If the pieces of treasure are buried in the grid sections noted as "
#
" the two series of numbers become "0,2,2,1,1" and "1,1,1,3".
0
2
2
1
1
1
#
1
#
1
#
3
#
#
#
Mr. Kobou’s ancestor seems to be a very careful person. He slipped some pieces of paper with completely irrelevant information into the bundle. For example, a set of number series "3,2,3,0,0" and "4,2,0,0,2" does not match any combination of 5 × 5 matrixes. So, Mr. Kobou has first to exclude these pieces of garbage information.
Given the set of information written on the pieces of paper, make a program to judge if the information is relevant.
Input
The input is given in the following format.
$W$ $H$
$a_1$ $a_2$ $...$ $a_W$
$b_1$ $b_2$ $...$ $b_H$
The first line provides the number of horizontal partitions $W$ ($1 \leq W \leq 1000$) and vertical partitions $H$ ($1 \leq H \leq 1000$). The second line provides the $i$-th member of the first number series $a_i$ ($0 \leq a_i \leq H$) written on the paper, and the third line the $j$-th member of the second series $b_j$ ($0 \leq b_j \leq W$).
Output
Output "
1
" if the information written on the paper is relevant, or "
0
" otherwise.
Sample Input 1
5 4
0 2 2 1 1
1 1 1 3
Sample Output 1
1
Sample Input 2
5 5
3 2 3 0 0
4 2 0 0 2
Sample Output 2
0

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, e) for (int i = (int)(s); i < (int)(e); ++i)

int main() {
	cin.tie(nullptr);
	ios_base::sync_with_stdio(false);
	
	int W, H;
	cin >> W >> H;
	vector<int> A(W), B(H);
	int sumA = 0, sumB = 0;
	rep(i, 0, W) {
		cin >> A[i];
		sumA += A[i];
	}
	rep(i, 0, H) {
		cin >> B[i];
		sumB += B[i];
	}
	
	if (sumA != sumB) {
		cout << 0 << '\n';
		return 0;
	}
	
	priority_queue<int> que;
	queue<int> tmp;
	rep(i, 0, W) if (A[i] != 0) que.push(A[i]);
	bool can = true;
	
	rep(i, 0, H) {
		if (que.size() < B[i]) {
			can = false;
			break;
		}
		
		rep(itr, 0, B[i]) {
			tmp.push(que.top() - 1);
			que.pop();
		}
		rep(itr, 0, B[i]) {
			if (tmp.front() != 0) que.push(tmp.front());
			tmp.pop();
		}
	}
	
	cout << (can ? 1 : 0) << '\n';
}
accuracy: 1
time_ms: 10
memory_kb: 3448
final_rank: 3

code_snippet: #include <bits/stdc++.h>
using namespace std; 
/*#include <boost/multiprecision/cpp_dec_float.hpp>
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/rational.hpp>
using namespace boost::multiprecision;*/
/*#include <atcoder/all>
using namespace atcoder;
using mint = modint;*/
//long long型=int64_tという認識で大丈夫です。
//#define int long long
#define ll long long
#define ull unsigned long long
#define vec vector
#define vi vector<int>
#define vll vector<ll>
#define pq priority_queue
#define pb push_back
#define rep(i, l, r) for(ll i=(ll)(l); i<(ll)(r); i++)
#define ld long double

const ll INF = 2e9;
const ll inf = 5e18;
const ld ep = 1e-15;

struct Edge {
    int to;
    ll cost;
};

struct SEdge {
    int to;
    pair<ll, ll> cost;
};

template<class T>
using spq = priority_queue<T, vec<T>, greater<T>>; 

const vector<ll> di = {-1, 0, 1, 0}, dj = {0, -1, 0, 1};

template<class T>
bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}

template<class T>
bool chmax(T& a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}

template<class T, class U> 
istream &operator>>(istream &is, pair<T, U> &p) {
    is >> p.first >> p.second;
    return is;
}

template<class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << p.first << " " << p.second;
    return os;
}
template <class T>
istream &operator>>(istream &is, vector<T> &v) {
    for(auto &x : v) {
        is >> x;
    }
    return is;
}

template <class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
    for(int i = 0; i < (int)v.size(); i++) {
        if(i != (int)v.size() - 1)
            os << v[i] << " ";
        else
            os << v[i];
    }
    return os;
}

string strplus(string a, string b) {
    reverse(a.begin(), a.end()); reverse(b.begin(), b.end());
    bool ch = false;
    if(a.size() < b.size()) {
        swap(a, b);
        ch = true;
    }
    string ret;
    int cnt = 0;
    rep(i, 0, a.size()) {
        if(i >= b.size()) {
            ret += (char)(((int)(a[i] - '0') + cnt) % 10 + '0');
            cnt = (((int)(a[i] - '0') + cnt) / 10);
        }
        else {
            ret += (char)(((int)(a[i] - '0' + b[i] - '0') + cnt) % 10 + '0');
            cnt = ((int)(a[i] - '0' + b[i] - '0') + cnt) / 10;
        }
    }
    if(cnt != 0) ret += "1";
    reverse(ret.begin(), ret.end());
    return ret;
}

string strminus(string a, string b) {
    reverse(a.begin(), a.end()); reverse(b.begin(), b.end());
    bool ch = false;
    if(a.size() < b.size()) {
        swap(a, b);
        ch = true;
    }
    else if(a.size() == b.size() && a < b) {
        swap(a, b);
        ch = true;
    }
    string ret;
    int cnt = 0;
    rep(i, 0, a.size()) {
        if(i >= b.size()) {
            ret += (char)(((int)(a[i] - '0') + cnt + 10) % 10 + '0');
            cnt = ((int)(a[i] - '0') + cnt + 10) / 10 - 1;
        }
        else {
            ret += (char)(((int)(a[i] - b[i]) + cnt + 10) % 10 + '0');
            cnt = ((int)(a[i] - b[i]) + cnt + 10) / 10 - 1;
        }
    }
    while(ret[ret.size() - 1] == '0' && ret.size() >= 1) ret.erase(--ret.end());
    if(ret != "0" && ch) ret += '-';
    reverse(ret.begin(), ret.end());
    return ret;
}

template <class T>
bool jf(pair<T, T> a, pair<T, T> b) {
    if(a.second > b.second) return false;
    else return true;
}

using S = ll;

S op(S a, S b){ return max(a, b); }
S e(){ return 0;}

signed main(){
    int w, h; cin >> w >> h;
    vi a(w), b(h); rep(i, 0, w) cin >> a[i]; rep(i, 0, h) cin >> b[i];
    sort(b.rbegin(), b.rend());
    pq<int> Q; rep(i, 0, w) if(a[i] != 0) Q.push(a[i]);
    rep(i, 0, h) {
        if(Q.size() < b[i]) {
            cout << 0 << endl;
            return 0;
        }
        vi ret;
        rep(j, 0, b[i]) {
            int cnt = Q.top(); Q.pop();
            if(cnt == 1) continue;
            ret.emplace_back(--cnt);
        }
        rep(j, 0, ret.size()) Q.push(ret[j]);
    }
    cout << 1 << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 3440
final_rank: 2

code_snippet: #include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
typedef long long ll;
#define mod 1000000007

int main(){
  ll w,h; cin>>w>>h;
  ll a[1010],b[1010];
  ll vl=0,vr=0;
  for(int i=0;i<w;i++){
      cin>>a[i];
      vl+=a[i];
  }
  for(int j=0;j<h;j++){
      cin>>b[j];
      vr+=b[j];
  }
  if(vl!=vr){
      cout<<0<<endl;
      return 0;
  }
  for(int j=0;j<h;j++){
      pair<ll,ll> c[1010];
      for(int i=0;i<w;i++){
	  c[i]=make_pair(a[i],i);
      }
      sort(c,c+w,greater<pair<ll,ll> >());
      for(int i=0;i<b[j];i++){
	  a[c[i].second]--;
      }
  }
  bool ok=1;
  for(int i=0;i<w;i++)ok&=(a[i]==0);
  cout<<ok<<endl;
  return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 3500
final_rank: 18

code_snippet: #include <iostream>
#include <vector>
#include <math.h>
#include <algorithm>
#include <set>
#include <map>
#include <unordered_map>
#include <queue>
#include <deque>
#include <stack>
#include <string>
#include <bitset>
#include <iomanip>
using namespace std;
using ll = long long;
using VVI = vector<vector<int>>;
using VVL = vector<vector<ll>>;
using VI = vector<int>;
using VL = vector<ll>;
using VS = vector<string>;
using VC = vector<char>;
using VP = vector<pair<int, int>>;
using Graph0 = vector<vector<int>>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define drep(i, a, b) for (int i = (int)(a);i >= (int)(b);i--)
#define urep(i, a, b) for (int i = (int)(a);i <= (int)(b);i++)
#define lrep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define ldrep(i, a, b) for (ll i = (ll)(a);i >= (ll)(b);i--)
#define lurep(i, a, b) for (ll i = (ll)(a);i <= (ll)(b);i++)
#define arep(i, v) for (auto i : v)
#define all(a) (a).begin(), (a).end()
#define yes cout << "Yes" << endl
#define no cout << "No" << endl
#define eyes cout << "Yes" << endl;exit(0);
#define eno cout << "No" << endl;exit(0);
template <typename T>
bool chmax(T &a, const T& b) {
  if (a < b) {
    a = b;  
    return true;
  }
  return false;
}
template <typename T>
bool chmin(T &a, const T& b) {
  if (a > b) {
    a = b; 
    return true;
  }
  return false;
}
template<typename T>
void excout(T A) {
  cout << A << endl;
  exit(0);
}
constexpr long long INF = (1LL << 60); // INFにちゅういい！


struct Edge
{
  int to;
  int cost;
};
using Graph = std::vector<std::vector<Edge>>;
using Pair = std::pair<long long, int>;
void Dijkstra(const Graph& graph, std::vector<long long>& distances, int startIndex)
{
  std::priority_queue<Pair, std::vector<Pair>, std::greater<Pair>> q;
  q.emplace((distances[startIndex] = 0), startIndex);

  while (!q.empty())
  {
    const long long distance = q.top().first;
    const int from = q.top().second;
    q.pop();
    if (distances[from] < distance)
    {
      continue;
    }
    for (const auto& edge : graph[from])
    {
      const long long d = (distances[from] + edge.cost);
      if (d < distances[edge.to])
      {
        q.emplace((distances[edge.to] = d), edge.to);
      }
    }
  }
}
template<typename T>
T MODS(T a, T mods) {
  return ((((((a + mods) % mods) + mods) % mods)));
}
VVL comb(int n, int r) {
  VVL v(n + 1, VL (n + 1, 0));
  for (int i = 0; i < v.size(); i++) {
    v[i][0] = 1;
    v[i][i] = 1;
  }
  for (int j = 1; j < v.size(); j++) {
    for (int k = 1; k < j; k++) {
      v[j][k] = (v[j - 1][k - 1] + v[j - 1][k]);
    }
  }
  return v;
}
vector<pair<long long, long long> > prime_factorize(long long N) {
    vector<pair<long long, long long> > res;
    for (long long p = 2; p * p <= N; ++p) {
        if (N % p != 0) {
            continue;
        }
        int e = 0;
        while (N % p == 0) {
            ++e;
            N /= p;
        }
        res.emplace_back(p, e);
    }
    if (N != 1) {
        res.emplace_back(N, 1);
    }
    return res;
}

struct UnionFind {
    vector<int> par, siz;
    UnionFind(int n) : par(n, -1), siz(n, 1) {}
    int root(int x) {
        if (par[x] == -1) return x;
        else return par[x] = root(par[x]);
    }
    bool issame(int x, int y) {
        return root(x) == root(y);
    }
    bool unite(int x, int y) {
        x = root(x);y = root(y);
        if (x == y) return false;
        if (siz[x] < siz[y]) swap(x, y);
        par[y] = x;
        siz[x] += siz[y];
        return true;
    }
    int size(int x) {
        return siz[root(x)];
    }
};
template<class Abel> struct WUnionFind {
  vector<int> par;
  vector<int> rank;
  vector<Abel> diff_weight;

  WUnionFind(int n = 1, Abel SUM_UNITY = 0) {
    init(n, SUM_UNITY);
  }

  void init(int n = 1, Abel SUM_UNITY = 0) {
    par.resize(n); rank.resize(n); diff_weight.resize(n);
    for (int i = 0; i < n; ++i) par[i] = i, rank[i] = 0, diff_weight[i] = SUM_UNITY;
  }

  int root(int x) {
    if (par[x] == x) {
      return x;
    }
    else {
      int r = root(par[x]);
      diff_weight[x] += diff_weight[par[x]];
      return par[x] = r;
    }
  }

  Abel weight(int x) {
    root(x);
    return diff_weight[x];
  }

  bool issame(int x, int y) {
    return root(x) == root(y);
  }

  bool merge(int x, int y, Abel w) { //W(y)=W(x)+w
    w += weight(x); w -= weight(y);
    x = root(x); y = root(y);
    if (x == y) return false;
    if (rank[x] < rank[y]) swap(x, y), w = -w;
    if (rank[x] == rank[y]) ++rank[x];
    par[y] = x;
    diff_weight[y] = w;
    return true;
  }

  Abel diff(int x, int y)  { // W(y) - W(x)
    return weight(y) - weight(x);
  }
};
VI topo_sort(Graph0& G) {
  int N = G.size();
  VI IND(N, 0);
  rep(v, N) {
    arep(nv, G[v]) {
      IND[nv]++;
    }
  }
  queue<int> que;
  rep(v, N) {
    if (IND[v] == 0) {
      que.push(v);
    }
  }
  VI ANS;
  while (!que.empty()) {
    int v = que.front();
    ANS.push_back(v);
    que.pop();
    arep(nv, G[v]) {
      IND[nv]--;
      if (IND[nv] == 0) {
        que.push(nv);
      }
    }
  }
  return ANS;
}
void ADD(int a, int b, Graph0& G) {
  G[a].push_back(b);
  G[b].push_back(a);
}
VP near(int i, int j, int H, int W) {
  VP ans;
  VP cand = {{i - 1, j}, {i + 1, j}, {i, j - 1}, {i, j + 1}};
  arep(v, cand) {
    if (v.first < 0 or v.first >= H) continue;
    if (v.second < 0 or v.second >= W) continue;
    ans.push_back(v);
  }
  return ans;
}
int cast(int i, int j, int H, int W) {
  return ((W * i) + j);
}
ll pows(ll x, ll n, ll mod) {
    if (!n) return 1;
    x %= mod;
    ll r = pows(x, n / 2, mod);
    (r *= r) %= mod;
    if (n % 2) (r *=x) %= mod;
    return r;
}
struct COMB_MOD {
  ll mod;
  int MAX;
  VL fac, finv, inv;
  COMB_MOD(int max, ll m) {
    fac.assign(max, 0);
    finv.assign(max, 0);
    inv.assign(max, 0);
    mod = m;
    MAX = max;
  }
  void solve() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i % mod;

        inv[i] = mod - inv[mod%i] * (mod / i) % mod;
        finv[i] = finv[i - 1] * inv[i] % mod;
    }
    
  }
  ll comb(int n, int k) {
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
  }
};

struct LCA {
    vector<vector<int>> parent;  
    vector<int> dist;           
    LCA(const Graph0 &G, int root = 0) { init(G, root); }
    void init(const Graph0 &G, int root = 0) {
        int V = G.size();
        int K = 1;
        while ((1 << K) < V) K++;
        parent.assign(K, vector<int>(V, -1));
        dist.assign(V, -1);
        dfs(G, root, -1, 0);
        for (int k = 0; k + 1 < K; k++) {
            for (int v = 0; v < V; v++) {
                if (parent[k][v] < 0) {
                    parent[k + 1][v] = -1;
                } else {
                    parent[k + 1][v] = parent[k][parent[k][v]];
                }
            }
        }
    }
    void dfs(const Graph0 &G, int v, int p, int d) {
        parent[0][v] = p;
        dist[v] = d;
        for (auto e : G[v]) {
            if (e != p) dfs(G, e, v, d + 1);
        }
    }
    int query(int u, int v) {
        if (dist[u] < dist[v]) swap(u, v);  
        int K = parent.size();
        for (int k = 0; k < K; k++) {
            if ((dist[u] - dist[v]) >> k & 1) {
                u = parent[k][u];
            }
        }
        if (u == v) return u;
        for (int k = K - 1; k >= 0; k--) {
            if (parent[k][u] != parent[k][v]) {
                u = parent[k][u];
                v = parent[k][v];
            }
        }
        return parent[0][u];
    }
    int get_dist(int u, int v) { return dist[u] + dist[v] - 2 * dist[query(u, v)]; }
};
bool solve(int W, int H, VI A, VI B) {
  int sa = 0;int sb = 0;
  priority_queue<int> pq;
  rep(i, W) {
    sa += A[i];
    if (A[i] != 0) pq.push(A[i]);
  }
  rep(i, H) {
    sb += B[i];
  }
  if (sa != sb) {
    return false;
  }
  rep(i, H) {
    if (B[i] == 0) continue;
    if (pq.size() < B[i]) {
      return false;
    }
    VI value;
    rep(t, B[i]) {
      if (pq.top() >= 2) value.push_back(pq.top()-1);
      pq.pop();
    }
    arep(v, value) pq.push(v);
  }
  return true;
}
int main(void) {
  int W, H;cin >> W >> H;
  /*
  set<pair<VI, VI>> st;
  rep(bit, 1<<(H*W)) {
    VVI A(H, VI(W, 0));
    rep(i, H*W) {
      if (bit & (1 << i)) {
        A[i/W][i%W] = 1;
      }
    }
    VI X(W, 0), Y(H, 0);
    rep(i, H) rep(j, W) {
      X[j] += A[i][j];
      Y[i] += A[i][j];
    }
   // cout << "X ";
    //rep(i, W) cout << X[i] << " ";
    //cout << "Y ";
   // rep(I, H) cout << Y[I] << " ";
    //cout << endl;
    st.insert({X, Y});
  }
    */
  while (true) {
    VI A(W), B(H);
    rep(i, W) cin >> A[i];
    rep(i, H) cin >> B[i];
    bool sl = solve(W, H, A, B);
    if (sl) {
      
      cout << 1 << endl;
    } else {
      cout << 0 << endl;
    }
    break;
  }
}
accuracy: 1
time_ms: 10
memory_kb: 3584
final_rank: 10

code_snippet: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);++i)
using namespace std;
using ll = long long;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int W,H;
    cin >> W >> H;
    vector<int> a(W),b(H);
    rep(i,W) cin >> a[i];
    rep(i,H) cin >> b[i];

    priority_queue<int> pq;
    rep(i,H) pq.push(b[i]);

    sort(a.begin(),a.end(),greater<int>());
    
    rep(i,W){
        vector<int> C;
        rep(j,a[i]){
            int t = pq.top();
            pq.pop();
            if(t==0) {
                cout << 0 << endl;
                return 0;
            }
            C.push_back(t-1);
        }

        for(int e: C) pq.push(e);
    }
    cout << (pq.top()==0) << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 3460
final_rank: 6

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <set>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)

template<class T>
using vi = vector<T>;

template<class T>
using vii = vector<vi<T>>;

template<class T>
using viii = vector<vii<T>>;

struct Dinic {
    struct edge {
        int to, rev; ll cap;
        edge(int t, int r, ll c) : to(t), rev(r), cap(c){}
    };

    int n;
    vii<edge> to;
    vi<int> dist;
    vi<int> iter;
    const ll inf = 1e18;
    Dinic(int n_) : n(n_), to(n_), dist(n_), iter(n_){}

    void addedge(int u, int v, ll cap) {
        int su = (int)to[u].size();
        int sv = (int)to[v].size();
        to[u].push_back({ v, sv, cap });
        to[v].push_back({ u, su, 0 });
    }

    void bfs(int s) {
        fill(dist.begin(), dist.end(), -1);
        //dist.assign(n, -1);
        dist[s] = 0;
        queue<int> q;
        q.push(s);
        while (!q.empty()) {
            int v = q.front();
            q.pop();
            for (edge& nv : to[v]) {
                if (nv.cap == 0 || dist[nv.to] != -1) continue;
                dist[nv.to] = dist[v] + 1;
                q.push(nv.to);
            }
        }
    }

    ll dfs(int v, int t, ll f) {
        if (v == t) return f;
        int sv = (int)to[v].size();
        for (int& i = iter[v]; i < sv; i++) {
            edge& nv = to[v][i];
            if (nv.cap == 0 || dist[nv.to] <= dist[v]) continue;
            ll nf = dfs(nv.to, t, min(f, nv.cap));
            if (nf > 0) {
                nv.cap -= nf;
                to[nv.to][nv.rev].cap += nf;
                return nf;
            }
        }
        return 0;
    }

    ll maxflow(int s, int t) {
        ll res = 0, flow = 0;
        while (true) {
            bfs(s);
            if (dist[t] == -1) break;
            iter.assign(n, 0); //here?
            do {
                flow = dfs(s, t, inf);
                res += flow;
            } while (flow);
        }
        return res;
    }
    
};

int main()
{
    int w, h;
    cin >> w >> h;

    vi<int> a(w), b(h);
    rep(i, w) cin >> a[i];
    rep(j, h) cin >> b[j];

    int sa = 0, sb = 0;
    rep(i, w) sa += a[i];
    rep(j, h) sb += b[j];
    if (sa != sb) {
        cout << 0 << endl;
        return 0;
    }


    Dinic dn(w + h + 2);
    int S = w + h, T = S + 1;


    rep(i, w) dn.addedge(S, i, a[i]);
    rep(j, h) dn.addedge(j + w, T, b[j]);
    rep(i, w) rep(j, h) dn.addedge(i, j + w, 1);

    cout << (dn.maxflow(S, T) == sa ? 1 : 0) << endl;

    


    return 0;
}


/* 


    if (sa > h * w / 2) {
        rep(i, w) a[i] = h - a[i];
        rep(j, h) b[j] = w - b[j];
        sa = h * w - sa;
    }

    if (w > h) {
        swap(h, w);
        swap(a, b);
    }

    sort(a.begin(), a.end(), greater<int>{});
    sort(b.begin(), b.end(), greater<int>{});

*/
accuracy: 1
time_ms: 50
memory_kb: 35556
final_rank: 20

code_snippet: #include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

template<class T, bool directed>
class Dinic {
    void bfs(int s){
        fill(level.begin(),level.end(), -1);
        queue<int> Q;
        level[s] = 0;
        Q.emplace(s);
        while(!Q.empty()){
            int v = Q.front(); Q.pop();
            for (auto &&e : G[v]){
                if(e.cap > 0 && level[e.to] < 0){
                    level[e.to] = level[v] + 1;
                    Q.emplace(e.to);
                }
            }
        }
    }

    T dfs(int v, int t, T f){
        if(v == t) return f;
        for(int &i = iter[v]; i < G[v].size(); i++){
            edge &e = G[v][i];
            if(e.cap > 0 && level[v] < level[e.to]){
                T d = dfs(e.to, t, min(f,  e.cap));
                if(d == 0) continue;
                e.cap -= d;
                G[e.to][e.rev].cap += d;
                return d;
            }
        }
        return 0;
    }
public:
    struct edge {
        int to{}; T cap; int rev{};
        edge() = default;
        edge(int to, T cap, int rev) : to(to), cap(cap), rev(rev) {}
    };

    vector<vector<edge>> G;
    vector<int> level, iter;
    Dinic() = default;
    explicit Dinic(int n) : G(n), level(n), iter(n) {}

    void add_edge(int from, int to, int cap){
        G[from].emplace_back(to, cap, G[to].size());
        G[to].emplace_back(from, directed ? 0 : cap,  G[from].size()-1);
    }


    T flow(int s, int t, T lim = INF<T>){
        T ret = 0;
        while(true) {
            bfs(s);
            if(level[t] < 0 || lim == 0) break;
            fill(iter.begin(),iter.end(), 0);
            while(true){
                T f = dfs(s, t, lim);
                if(f == 0) break;
                ret += f;
                lim -= f;
            }
        }
        return ret;
    }
};

int main() {
    int w, h;
    cin >> w >> h;
    vector<int> a(h), b(w);
    int S = 0, T = 0;
    for (auto &&i : b) scanf("%d", &i), T += i;
    for (auto &&i : a) scanf("%d", &i), S += i;
    if(S != T) {
        puts("0");
        return 0;
    }
    Dinic<int, true> G(h+w+2);
    for (int i = 0; i < h; ++i) {
        G.add_edge(0, i+1, a[i]);
    }
    for (int i = 0; i < w; ++i) {
        G.add_edge(h+i+1, h+w+1, b[i]);
    }
    for (int i = 0; i < h; ++i) {
        for (int j = 0; j < w; ++j) {
            G.add_edge(i+1, h+j+1, 1);
        }
    }
    if(S != G.flow(0, h+w+1)) puts("0");
    else puts("1");
    return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 27428
final_rank: 19

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <set>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)

template<class T>
using vi = vector<T>;

template<class T>
using vii = vector<vi<T>>;

template<class T>
using viii = vector<vii<T>>;



int main()
{
    int w, h;
    cin >> w >> h;

    vi<int> a(w), b(h);
    rep(i, w) cin >> a[i];
    rep(j, h) cin >> b[j];

    int sa = 0, sb = 0;
    rep(i, w) sa += a[i];
    rep(j, h) sb += b[j];
    if (sa != sb) {
        cout << 0 << endl;
        return 0;
    }
    
    priority_queue<int> pq;
    rep(i, w) if(a[i]) pq.push(a[i]);
    sort(b.begin(), b.end(), greater<int>());

    rep(j, h) {
        if (b[j] == 0) continue;
        queue<int> q;
        rep(_, b[j]) {
            if (pq.empty()) {
                cout << 0 << endl;
                return 0;
            }
            int v = pq.top();
            pq.pop();
            if (v > 1) q.push(v - 1);
        }
        while (!q.empty()) {
            pq.push(q.front());
            q.pop();
        }                
    }
    cout << 1 << endl;
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3452
final_rank: 4

code_snippet: #include <bits/stdc++.h>
using namespace std;

int main()
{
	int w,h;
	cin >> w >> h;
	vector<int> a(w),b(h);
	for(int i = 0;i < w;i++){
		cin >> a[i];
	}
	for(int i = 0;i < h;i++){
		cin >> b[i];
	}
	priority_queue<int> q;
	for(int i = 0;i < h;i++){
		if(b[i] >= 1){
			q.push(b[i]);
		}
	}
	for(int i = 0;i < w;i++){
		if(q.size() < a[i]){
			cout << 0 << endl;
			exit(0);
		}
		queue<int> it;
		for(int j = 0;j < a[i];j++){
			int t = q.top();
			q.pop();
			t--;
			if(t >= 1){
				it.push(t);
			}
		}
		while(!it.empty()){
			q.push(it.front());
			it.pop();
		}
	}
	if(!q.empty()){
		cout << 0 << endl;
		exit(0);
	}
	cout << 1 << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 3128
final_rank: 13

code_snippet: #include <bits/stdc++.h>
using namespace std;
int main(){
    int a,b;
    cin>>a>>b;
    int c[a];
    for(int i=0;i<a;i+=1){
        cin>>c[i];
    }
    priority_queue<int> que;
    queue<int> que2;
    for(int i=0;i<b;i+=1){
        int e;
        cin>>e;
        if(e!=0){
        que.push(e);
        }
    }
    for(int i=0;i<a;i+=1){
        int r=0;
       for(int j=0;j<c[i];j+=1){
           if(que.size()==0){
               cout<<0<<endl;
               return 0;
           }else{
               r=que.top();
               que.pop();
               r-=1;
               if(r!=0){
                   que2.push(r);
               }
           }
       }
       while(que2.size()!=0){
           que.push(que2.front());
           que2.pop();
       }
    }
    if(que.size()==0){
        cout<<1<<endl;
    }else{
        cout<<0<<endl;
    }
}
accuracy: 1
time_ms: 20
memory_kb: 3184
final_rank: 15

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef long long LL;
#define INF INT64_MAX;

int main()
{
    int h,w,sum1=0,sum2=0;
    cin>>h>>w;
    priority_queue<int> st,si,ans;
    int inp;
    for(int i=0;i<h;i++){
        cin>>inp;
        if(inp!=0) st.push(inp),sum1+=inp;
    }
    for(int i=0;i<w;i++){
        cin>>inp;
        if(inp!=0) si.push(inp),sum2+=inp;
    }
    if(sum1!=sum2) {
        cout<<"0"<<endl;
        return 0;
    }
    if(st.top()<si.top()) swap(st,si);
    while(!st.empty()){
        int a=st.top();
        st.pop();
        if(si.size()<a){
            cout<<"0"<<endl;
            return 0;
        }
        for(int i=0;i<a;i++){
            int b=si.top();
            si.pop();
            b--;
            if(b!=0) ans.push(b);
        }
        while(!ans.empty()){
            si.push(ans.top());
            ans.pop();
        }
    }
    if(!st.empty() || !si.empty()) {
        cout<<"0"<<endl;
        return 0;
    }
    cout<<"1"<<endl;
}
accuracy: 1
time_ms: 40
memory_kb: 3144
final_rank: 16

code_snippet: #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
    return (ull)rng() % B;
}
inline double time() {
    return static_cast<long double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int w,h; cin >> w >> h;
    vector<int> a(w);
    for (int i = 0; i < w; ++i) {
        cin >> a[i];
    }
    priority_queue<int> pq;
    for (int i = 0; i < h; ++i) {
        int b; cin >> b; pq.push(b);
    }
    for (int i = 0; i < w; ++i) {
        vector<int> v(a[i]);
        for (int j = 0; j < a[i]; ++j) {
            int p = pq.top(); pq.pop();
            if (p == 0) {
                cout << 0 << endl;
                return 0;
            }
            v[j] = p-1;
        }
        for (int j = 0; j < a[i]; ++j) {
            pq.push(v[j]);
        }
    }
    cout << (pq.top() == 0) << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 3468
final_rank: 7

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll mod1 = 1000000007;
int main(){
    ll W,H; cin >> W >> H; 
    vector<ll> a(W),b(H);
    priority_queue<ll> pq;
    for(int i = 0; i < W; i++){
        cin >> a[i];
    }
    for(int j = 0; j < H; j++){
        cin >> b[j];
        pq.push(b[j]);
    }
    ll ans = 1;
    for(int i = 0; i < W; i++){
        vector<ll> p;
        for(int j = 0; j < a[i]; j++){
            ll x = pq.top();
            pq.pop();
            p.push_back(x-1);
            //cout << x << endl;;
        }
        for(auto z: p){
            pq.push(z);
        }
    }
    for(int i = 0; i < H; i++){
        ll x = pq.top();
        if(x != 0) ans = 0;
        pq.pop();
    }
    cout << ans << endl;
    
}
accuracy: 1
time_ms: 10
memory_kb: 3468
final_rank: 7

code_snippet: /* -*- coding: utf-8 -*-
 *
 * 0396.cc: Treasure Map
 */

#include<cstdio>
#include<vector>
#include<queue>
#include<algorithm>
 
using namespace std;

/* constant */

const int MAX_W = 1000;
const int MAX_H = 1000;

/* typedef */

typedef vector<int> vi;

/* global variables */

int as[MAX_W], bs[MAX_H];

/* subroutines */

/* main */

int main() {
  int w, h;
  scanf("%d%d", &w, &h);
  for (int i = 0; i < w; i++) scanf("%d", as + i);
  for (int i = 0; i < h; i++) scanf("%d", bs + i);

  sort(as, as + w, greater<int>());

  priority_queue<int> q;
  for (int i = 0; i < h; i++)
    if (bs[i] > 0) q.push(bs[i]);

  for (int i = 0; i < w && as[i] > 0; i++) {
    if (q.size() < as[i]) { puts("0"); return 0; }

    vi v;
    for (int j = 0; j < as[i]; j++)
      v.push_back(q.top()), q.pop();
    for (auto u: v)
      if (u > 1) q.push(u - 1);
  }

  if (q.empty()) puts("1");
  else puts("0");
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 2908
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
template<class T> using vc = vector<T>;
template<class T> using vvc = vc<vc<T>>;
template<class T> using vvvc = vc<vvc<T>>;
template<class T> using vvvvc = vvc<vvc<T>>;
template<class T> using PQ = priority_queue<T>;
template<class T> using invPQ = priority_queue<T, vector<T>, greater<T>>;
using IP = pair<int, int>;
using LP = pair<ll, ll>;
#define all(x) begin(x), end(x)
#define allr(x) rbegin(x), rend(x)
#define rep(i, n) for (int i = 0; (i) < (int)(n); i++)
#define rep3(i, m, n) for (int i = (m); (i) < (int)(n); i++)
#define repr(i, n) for (int i = (n) - 1; (i) >= 0; i--)
#define rep3r(i, m, n) for (int i = (n) - 1; (i) >= (int)(m); i--)
template<class T> inline bool chmax(T & a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T & a, T b) { if (a > b) { a = b; return 1; } return 0; }

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int W, H;
    cin >> W >> H;
    vc<int> a(W), b(H);
    rep (i, W) cin >> a[i];
    rep (i, H) cin >> b[i];

    PQ<int> que;
    rep (i, H) que.push(b[i]);
    rep (i, W) {
        queue<int> next;
        rep (j, a[i]) {
            next.push(que.top()-1);
            que.pop();
        }
        while (!next.empty()) {
            que.push(next.front());
            next.pop();
        }
    }

    bool judge = true;
    while (!que.empty()) {
        if (que.top() != 0) judge = false;
        que.pop();
    }

    cout << judge << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 3480
final_rank: 9

code_snippet: /*
このコード、と～おれ!
Be accepted!
∧＿∧　
（｡･ω･｡)つ━☆・*。
⊂　　 ノ 　　　・゜+.
　しーＪ　　　°。+ *´¨)
 　　　　　　　　　.· ´¸.·*´¨) ¸.·*¨)
		  　　　　　　　　　　(¸.·´ (¸.·'* ☆
*/

#include <cstdio>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstring>
#include <vector>
#include <numeric>
#include <iostream>
#include <random>
#include <map>
#include <unordered_map>
#include <queue>
#include <regex>
#include <functional>
#include <complex>
#include <list>
#include <cassert>
#include <iomanip>
#include <set>
#include <stack>
#include <bitset>
#include <array>
#include <chrono>

//#pragma GCC target("arch=skylake-avx512")
//#pragma GCC target("avx2")
//#pragma GCC optimize("O3")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse4")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define repeat(i, n, m) for(int i = n; i < (m); ++i)
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define printynl(a) printf(a ? "yes\n" : "no\n")
#define printyn(a) printf(a ? "Yes\n" : "No\n")
#define printYN(a) printf(a ? "YES\n" : "NO\n")
#define printim(a) printf(a ? "possible\n" : "imposible\n")
#define printdb(a) printf("%.50lf\n", a)
#define printLdb(a) printf("%.50Lf\n", a)
#define printdbd(a) printf("%.16lf\n", a)
#define prints(s) printf("%s\n", s.c_str())
#define all(x) (x).begin(), (x).end()
#define deg_to_rad(deg) (((deg)/360.0L)*2.0L*PI)
#define rad_to_deg(rad) (((rad)/2.0L/PI)*360.0L)
#define Please return
#define AC 0
#define manhattan_dist(a, b, c, d) (abs(a - c) + abs(b - d))

using ll = long long;
using ull = unsigned long long;

constexpr int INF = 1073741823;
constexpr int MINF = -1073741823;
constexpr ll LINF = ll(4661686018427387903);
constexpr ll MOD = 1e9 + 7;
constexpr ll mod = 998244353;
constexpr long double eps = 1e-6;
const long double PI = acosl(-1.0L);

using namespace std;

void scans(string& str) {
	char c;
	str = "";
	scanf("%c", &c);
	if (c == '\n')scanf("%c", &c);
	while (c != '\n' && c != -1 && c != ' ') {
		str += c;
		scanf("%c", &c);
	}
}

void scanc(char& str) {
	char c;
	scanf("%c", &c);
	if (c == -1)return;
	while (c == '\n') {
		scanf("%c", &c);
	}
	str = c;
}

double acot(double x) {
	return PI / 2 - atan(x);
}

ll LSB(ll n) { return (n & (-n)); }

template<typename T>
inline T chmin(T& a, const T& b) {
	if (a > b)a = b;
	return a;
}

template<typename T>
inline T chmax(T& a, const T& b) {
	if (a < b)a = b;
	return a;
}

//cpp_int
#if __has_include(<boost/multiprecision/cpp_int.hpp>)
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
using namespace boost::multiprecision;
#else
using cpp_int = ll;
#endif

//atcoder library
#if __has_include(<atcoder/all>)
#include <atcoder/all>
//using namespace atcoder;
#endif

/*
	random_device seed_gen;
	mt19937 engine(seed_gen());
	uniform_int_distribution dist(1, 100);
*/


/*----------------------------------------------------------------------------------*/



int main() {

	int w, h;
	scanf("%d%d", &w, &h);
	vector<int> a(w), b(h);
	rep(i, w) scanf("%d", &a[i]);
	rep(i, h) scanf("%d", &b[i]);
	priority_queue<int> que;
	rep(i, h)que.push(b[i]);
	rep(i, w) {
		vector<int> ntr;
		while (a[i]--) {
			ntr.push_back(que.top());
			que.pop();
		}
		for (const auto& aa : ntr) {
			que.push(aa - 1);
		}
	}
	printf("%d\n", que.top() == 0);

	Please AC;
}
accuracy: 1
time_ms: 10
memory_kb: 3588
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using std::string;

int main() {
  int h, w;
  std::cin >> w >> h;
  std::vector<int> a(w), b(h), a2(w), b2(h);
  int a_cnt = 0, b_cnt = 0;
  for (int i = 0; i < w; ++i) {
    std::cin >> a[i];
    a_cnt += !!a[i];
  }
  for (int i = 0; i < h; ++i) {
    std::cin >> b[i];
    b_cnt += !!b[i];
  }
  std::copy(a.begin(), a.end(), a2.begin());
  std::copy(b.begin(), b.end(), b2.begin());

  for (int i = 0; i < h; ++i) {
    std::sort(a2.begin(), a2.end(), std::greater<>());
    for (int j = 0; j < b[i]; ++j) {
      --a2[j];
    }
  }

  for (int i = 0; i < w; ++i) {
    std::sort(b2.begin(), b2.end(), std::greater<>());
    for (int j = 0; j < a[i]; ++j) {
      --b2[j];
    }
  }

  for (int a_value : a2) {
    if (a_value < 0) {
      std::cout << "0\n";
      return 0;
    }
  }
  for (int b_value : b2) {
    if (b_value < 0) {
      std::cout << "0\n";
      return 0;
    }
  }

  std::cout << "1\n";
}
accuracy: 1
time_ms: 10
memory_kb: 3452
final_rank: 4

code_snippet: #include <bits/stdc++.h>
using namespace std;
using lint = long long;
constexpr lint mod = 998244353;
#define all(x) (x).begin(), (x).end()
#define bitcount(n) __builtin_popcountl((lint)(n))
#define fcout cout << fixed << setprecision(15)
#define highest(x) (63 - __builtin_clzl(x))
#define rep(i, n) for(int i = 0; i < int(n); i++)
#define rep2(i, l, r) for(int i = int(l); i < int(r); i++)
#define repr(i, n) for(int i = int(n) - 1; i >= 0; i--)
#define repr2(i, l, r) for(int i = int(r) - 1; i >= int(l); i--)

int main(){
    int W, H;
    cin >> W >> H;
    vector<int> a(W), b(H);
    rep(i, W) cin >> a[i];
    rep(i, H) cin >> b[i];
    if(accumulate(all(a), 0) != accumulate(all(b), 0)){
        cout << 0 << endl;
        return 0;
    }
        priority_queue<int> a_que;
        rep(i, W){
            a_que.push(a[i]);
        }
        rep(i, H){
            vector<int> item;
            rep(_, b[i]){
                item.push_back(a_que.top());
                a_que.pop();
            }
            for(int &j: item){
                if(j == 0){
                    cout << 0 << endl;
                    return 0;
                }
                j--;
                a_que.push(j);
            }
        }
    cout << 1 << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 3172
final_rank: 14

code_snippet: #include <bits/stdc++.h>
using namespace std;
using lint = long long;
constexpr lint mod = 998244353;
#define all(x) (x).begin(), (x).end()
#define bitcount(n) __builtin_popcountl((lint)(n))
#define fcout cout << fixed << setprecision(15)
#define highest(x) (63 - __builtin_clzl(x))
#define rep(i, n) for(int i = 0; i < int(n); i++)
#define rep2(i, l, r) for(int i = int(l); i < int(r); i++)
#define repr(i, n) for(int i = int(n) - 1; i >= 0; i--)
#define repr2(i, l, r) for(int i = int(r) - 1; i >= int(l); i--)

int main(){
    int W, H;
    cin >> W >> H;
    vector<int> a(W), b(H);
    rep(i, W) cin >> a[i];
    rep(i, H) cin >> b[i];
    if(accumulate(all(a), 0) != accumulate(all(b), 0)){
        cout << 0 << endl;
        return 0;
    }
    rep(loop, 2){
        priority_queue<int> a_que;
        rep(i, W){
            a_que.push(a[i]);
        }
        rep(i, H){
            vector<int> item;
            rep(_, b[i]){
                item.push_back(a_que.top());
                a_que.pop();
            }
            for(int &j: item){
                if(j == 0){
                    cout << 0 << endl;
                    return 0;
                }
                j--;
                a_que.push(j);
            }
        }
        swap(a, b);
        swap(W, H);
    }
    cout << 1 << endl;
}
accuracy: 1
time_ms: 40
memory_kb: 3172
final_rank: 17

code_snippet: #include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
int main(void)
{
	int w,h,a,b[1001],flg=0,i,x,c,y[1001],cnt,j;
	priority_queue<int>q;
	scanf("%d%d",&w,&h);
	for(i=0;i<w;i++){
		scanf("%d",&a);
		q.push(a);
	}
	for(i=0;i<h;i++)	scanf("%d",&b[i]);
	sort(b,b+h);
	for(i=h-1;i>=0;i--){
		c=b[i];
		cnt=0;
		for(j=1;j<=c;j++){
			x=q.top();
			if(x<=0||q.size()==0){
				printf("0\n");
				flg=1;
				break;
			}
			else{
				x--;
				q.pop();
				y[cnt]=x;
				cnt++;
			}
		}
		for(j=0;j<cnt;j++)	q.push(y[j]);
		if(flg==1)	break;
	}
	if(flg==0)	printf("1\n");
	return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 2760
final_rank: 12