Language: C++

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<vector>
#include<queue>
#include<set>
#include<functional>
#define int long long
#define p pair<int,int>
#define P pair<int,p>
using namespace std;

int x[4] = { -1,0,0,1 }, y[4] = { 0,-1,1,0 };
int f[500][500];
bool i[500][500];
set<p>l,r;
signed main() {
	int a;
	while (cin >> a, a) {
		l.insert(p(0, 0)); r.insert(p(0, 0));
		int b, c, d, e; cin >> c >> b >> e >> d;
		d--; e--;
		for (int g = 0; g < b; g++) {
			for (int h = 0; h < c; h++) {
				scanf("%lld", &f[g][h]);
			}
		}
		memset(i, true, sizeof(i));
		int sum = 0, MAX = 0;
		priority_queue<P, vector<P>, greater<P>>Q;
		Q.push(P(1, p(d, e)));
		while (Q.size()) {
			P o = Q.top(); Q.pop();
			MAX = o.first; 
			if(i[o.second.first][o.second.second])sum++;
			i[o.second.first][o.second.second] = false;
			queue<p>q;
			q.push(p(o.second.first, o.second.second));
			while (q.size()) {
				p t = q.front(); q.pop();
				for (int i = 0; i < 4; i++) {
					int dx = t.first + x[i], dy = t.second + y[i];
					if (dx >= 0 && dx < b&&dy >= 0 && dy < c&&::i[dx][dy]) {
						if (f[dx][dy] <= o.first) {
							sum++;
							q.push(p(dx,dy));
							::i[dx][dy] = false;
						}
						else {
							Q.push(P(f[dx][dy],p(dx,dy)));
						}
					}
				}
			}
			l.insert(p(sum, MAX));
		}
		cin >> c >> b >> e >> d;
		d--; e--;
		for (int g = 0; g < b; g++) {
			for (int h = 0; h < c; h++) {
				scanf("%lld", &f[g][h]);
			}
		}
		memset(i, true, sizeof(i));
		sum = 0; MAX = 0;
		priority_queue<P, vector<P>, greater<P>>U;
		U.push(P(1, p(d, e)));
		while (U.size()) {
			P o = U.top(); U.pop();
			MAX = o.first;
			if (i[o.second.first][o.second.second])sum++;
			i[o.second.first][o.second.second] = false;
			queue<p>q;
			q.push(p(o.second.first, o.second.second));
			while (q.size()) {
				p t = q.front(); q.pop();
				for (int i = 0; i < 4; i++) {
					int dx = t.first + x[i], dy = t.second + y[i];
					if (dx >= 0 && dx < b&&dy >= 0 && dy < c&&::i[dx][dy]) {
						if (f[dx][dy] <= o.first) {
							sum++;
							q.push(p(dx, dy));
							::i[dx][dy] = false;
						}
						else {
							U.push(P(f[dx][dy], p(dx, dy)));
						}
					}
				}
			}
			r.insert(p(sum, MAX));
		}
		int MIN = LLONG_MAX/3;
		for (auto i = l.begin(); i != l.end(); i++) {
			auto j = r.lower_bound(p(a-(*i).first,0));
			if (j != r.end()) {
				MIN = min(MIN, (*i).second + (*j).second);
			}
		}
		cout << MIN << endl;
	}
}

Compiler output:
a.cc: In function 'int main()':
a.cc:29:17: error: 'memset' was not declared in this scope
   29 |                 memset(i, true, sizeof(i));
      |                 ^~~~~~
a.cc:9:1: note: 'memset' is defined in header '<cstring>'; this is probably fixable by adding '#include <cstring>'
    8 | #include<functional>
  +++ |+#include <cstring>
    9 | #define int long long
a.cc:94:27: error: 'LLONG_MAX' was not declared in this scope
   94 |                 int MIN = LLONG_MAX/3;
      |                           ^~~~~~~~~
a.cc:9:1: note: 'LLONG_MAX' is defined in header '<climits>'; this is probably fixable by adding '#include <climits>'
    8 | #include<functional>
  +++ |+#include <climits>
    9 | #define int long long