Language: C++

#include <bits/stdc++.h>
#define FOR(i,n) for(int i=0;i<(int)(n);i++)
#define FORR(i,m,n) for(int i=(int)(m);i<(int)(n);i++)
#define pb push_back
#define mp make_pair
#define ALL(a) a.begin(),a.end()
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,-1,sizeof(a))
#define len(a) sizeof(a)
#define ll long long
#define pint pair<int,int>
#define INF 1<<29
#define MAX 501
using namespace std;
 
int R,W[2],H[2],X[2],Y[2],L[2][MAX][MAX],d[2][MAX][MAX],dd[4]={0,-1,0,1};
map<int,int> m[2];
map<int,int>::iterator ite;
vector<int> lev,sum;
 
void solve(){
 for(;;){
  cin>>R;
  if(!R) return;
  FOR(i,2){
   cin>>W[i]>>H[i]>>X[i]>>Y[i];
   X[i]--;
   Y[i]--;
   FOR(j,H[i]) FOR(k,W[i]) cin>>L[i][j][k];
   FOR(j,H[i]) FOR(k,W[i]) d[i][j][k]=INT_MAX/3;
   d[i][X[i]][Y[i]]=1;
   priority_queue<pint,vector<pint>,greater<pint>> pque;
   pque.push(mp(1,X[i]*W[i]+Y[i]));
   while(!pque.empty()){
    pint p=pque.top(); pque.pop();
    int x=p.second/W[i],y=p.second%W[i];
    if(d[i][x][y]<p.first) continue;
    FOR(j,4){
     int nx=x+dd[j],ny=y+dd[j^1];
     if(nx<0||ny<0||nx>=H[i]||ny>=W[i]) continue;
     if(d[i][nx][ny]>max(d[i][x][y],L[i][nx][ny])){
      d[i][nx][ny]=max(d[i][x][y],L[i][nx][ny]);
      pque.push(mp(d[i][nx][ny],nx*W[i]+ny));
     }
    }
   }
   FOR(j,H[i]) FOR(k,W[i]) m[i][d[i][j][k]]++;
   int tot=0;
   lev[i].pb(0);
   sum[i].pb(0);
   for(ite=m[0].begin();ite!=m[0].end();ite++){
    lev[i].pb(ite->first);
    tot+=ite->second;
    sum[i].pb(tot);
   }
  }
  int mi=1<<30;
  FOR(i,lev[0].size()){
   int ind=lower_bound(ALL(sum[1]),R-sum[0])-sum[1].begin();
   mi=min(mi,lev[ind]+lev[i]);
  }
  cout<<mi<<endl;
  FOR(i,2){
   m[i].clear();
   lev[i].clear();
   sum[i].clear();
  }
 }
}
 
int main(){
 solve();
 return 0;
}

Compiler output:
a.cc: In function 'void solve()':
a.cc:4:12: error: request for member 'push_back' in 'lev.std::vector<int>::operator[](((std::vector<int>::size_type)i))', which is of non-class type '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'}
    4 | #define pb push_back
      |            ^~~~~~~~~
a.cc:49:11: note: in expansion of macro 'pb'
   49 |    lev[i].pb(0);
      |           ^~
a.cc:4:12: error: request for member 'push_back' in 'sum.std::vector<int>::operator[](((std::vector<int>::size_type)i))', which is of non-class type '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'}
    4 | #define pb push_back
      |            ^~~~~~~~~
a.cc:50:11: note: in expansion of macro 'pb'
   50 |    sum[i].pb(0);
      |           ^~
a.cc:4:12: error: request for member 'push_back' in 'lev.std::vector<int>::operator[](((std::vector<int>::size_type)i))', which is of non-class type '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'}
    4 | #define pb push_back
      |            ^~~~~~~~~
a.cc:52:12: note: in expansion of macro 'pb'
   52 |     lev[i].pb(ite->first);
      |            ^~
a.cc:4:12: error: request for member 'push_back' in 'sum.std::vector<int>::operator[](((std::vector<int>::size_type)i))', which is of non-class type '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'}
    4 | #define pb push_back
      |            ^~~~~~~~~
a.cc:54:12: note: in expansion of macro 'pb'
   54 |     sum[i].pb(tot);
      |            ^~
a.cc:58:16: error: request for member 'size' in 'lev.std::vector<int>::operator[](0)', which is of non-class type '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'}
   58 |   FOR(i,lev[0].size()){
      |                ^~~~
a.cc:2:38: note: in definition of macro 'FOR'
    2 | #define FOR(i,n) for(int i=0;i<(int)(n);i++)
      |                                      ^
a.cc:6:18: error: request for member 'begin' in 'sum.std::vector<int>::operator[](1)', which is of non-class type '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'}
    6 | #define ALL(a) a.begin(),a.end()
      |                  ^~~~~
a.cc:59:24: note: in expansion of macro 'ALL'
   59 |    int ind=lower_bound(ALL(sum[1]),R-sum[0])-sum[1].begin();
      |                        ^~~
a.cc:6:28: error: request for member 'end' in 'sum.std::vector<int>::operator[](1)', which is of non-class type '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'}
    6 | #define ALL(a) a.begin(),a.end()
      |                            ^~~
a.cc:59:24: note: in expansion of macro 'ALL'
   59 |    int ind=lower_bound(ALL(sum[1]),R-sum[0])-sum[1].begin();
      |                        ^~~
a.cc:59:53: error: request for member 'begin' in 'sum.std::vector<int>::operator[](1)', which is of non-class type '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'}
   59 |    int ind=lower_bound(ALL(sum[1]),R-sum[0])-sum[1].begin();
      |                                                     ^~~~~
a.cc:65:11: error: request for member 'clear' in 'lev.std::vector<int>::operator[](((std::vector<int>::size_type)i))', which is of non-class type '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'}
   65 |    lev[i].clear();
      |           ^~~~~
a.cc:66:11: error: request for member 'clear' in 'sum.std::vector<int>::operator[](((std::vector<int>::size_type)i))', which is of non-class type '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'}
   66 |    sum[i].clear();
      |           ^~~~~