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(a) push_back(a)
#define mp(x,y) make_pair(x,y)
#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 tint tuple<int,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:15: 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(a) push_back(a)
      |               ^~~~~~~~~
a.cc:50:11: note: in expansion of macro 'pb'
   50 |    lev[i].pb(0);
      |           ^~
a.cc:4:15: 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(a) push_back(a)
      |               ^~~~~~~~~
a.cc:51:11: note: in expansion of macro 'pb'
   51 |    sum[i].pb(0);
      |           ^~
a.cc:4:15: 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(a) push_back(a)
      |               ^~~~~~~~~
a.cc:53:12: note: in expansion of macro 'pb'
   53 |     lev[i].pb(ite->first);
      |            ^~
a.cc:4:15: 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(a) push_back(a)
      |               ^~~~~~~~~
a.cc:55:12: note: in expansion of macro 'pb'
   55 |     sum[i].pb(tot);
      |            ^~
a.cc:59: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'}
   59 |   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:60:24: note: in expansion of macro 'ALL'
   60 |    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:60:24: note: in expansion of macro 'ALL'
   60 |    int ind=lower_bound(ALL(sum[1]),R-sum[0])-sum[1].begin();
      |                        ^~~
a.cc:60: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'}
   60 |    int ind=lower_bound(ALL(sum[1]),R-sum[0])-sum[1].begin();
      |                                                     ^~~~~
a.cc:66: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'}
   66 |    lev[i].clear();
      |           ^~~~~
a.cc:67: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'}
   67 |    sum[i].clear();
      |           ^~~~~