Language: C++

#include <iostream>
#include <queue>
#include <set>
#include <utility>
#include <algorithm>
#include <cstdlib>
#include <vector>
using namespace std;
#define INF 10000000

int ary1[510][510],ary2[510][510];
bool used[510][510];
int main(){
	int R,W1,W2,H1,H2,X1,X2,Y1,Y2,imax,imay,imad,res;
	priority_queue<pair<int,pair<int,int> > ,vector<pair<int,pair<int,int> > >,greater<pair<int,pair<int,int> > > > que;
	set<int> se1,se2;
	vector<int> v1,v2;
	pair<int,pair<int,int> > pa;
	while(1){
		v1.clear();
		v2.clear();
		cin>>R;
		if(R==0) break;
		cin>>W1>>H1>>Y1>>X1;
		X1--;Y1--;
		for(int i=0;i<H1;i++){
			for(int j=0;j<W1;j++){
				cin>>ary1[i][j];
			}
		}
		cin>>W2>>H2>>Y2>>X2;
		X2--;Y2--;
		for(int i=0;i<H2;i++){
			for(int j=0;j<W2;j++){
				cin>>ary2[i][j];
			}
		}
		
		memset(used,0,sizeof(used));
		que.push(make_pair(1,make_pair(X1,Y1)));
		used[X1][Y1]=true;
		while(!que.empty()){
			pa=que.top();que.pop();
			imax=pa.second.first;
			imay=pa.second.second;
			imad=pa.first;
			ary1[imax][imay]=imad;
			if(imax>0&&(!used[imax-1][imay])){
				used[imax-1][imay]=true;
				que.push(make_pair(max(ary1[imax-1][imay],imad),make_pair(imax-1,imay)));
			}
			if(imax<H1-1&&(!used[imax+1][imay])){
				used[imax+1][imay]=true;
				que.push(make_pair(max(ary1[imax+1][imay],imad),make_pair(imax+1,imay)));
			}
			if(imay>0&&(!used[imax][imay-1])){
				used[imax][imay-1]=true;
				que.push(make_pair(max(ary1[imax][imay-1],imad),make_pair(imax,imay-1)));
			}
			if(imay<W1-1&&(!used[imax][imay+1])){
				used[imax][imay+1]=true;
				que.push(make_pair(max(ary1[imax][imay+1],imad),make_pair(imax,imay+1)));
			}
		}
		memset(used,0,sizeof(used));
		que.push(make_pair(1,make_pair(X2,Y2)));
		used[X2][Y2]=true;
		while(!que.empty()){
			pa=que.top();que.pop();
			imax=pa.second.first;
			imay=pa.second.second;
			imad=pa.first;
			ary2[imax][imay]=imad;
			if(imax>0&&(!used[imax-1][imay])){
				used[imax-1][imay]=true;
				que.push(make_pair(max(ary2[imax-1][imay],imad),make_pair(imax-1,imay)));
			}
			if(imax<H2-1&&(!used[imax+1][imay])){
				used[imax+1][imay]=true;
				que.push(make_pair(max(ary2[imax+1][imay],imad),make_pair(imax+1,imay)));
			}
			if(imay>0&&(!used[imax][imay-1])){
				used[imax][imay-1]=true;
				que.push(make_pair(max(ary2[imax][imay-1],imad),make_pair(imax,imay-1)));
			}
			if(imay<W2-1&&(!used[imax][imay+1])){
				used[imax][imay+1]=true;
				que.push(make_pair(max(ary2[imax][imay+1],imad),make_pair(imax,imay+1)));
			}
		}
		v1.push_back(0);
		for(int i=0;i<H1;i++){
			for(int j=0;j<W1;j++){
				v1.push_back(ary1[i][j]);
			}
		}
		sort(v1.begin(),v1.end());
		v2.push_back(0);
		for(int i=0;i<H2;i++){
			for(int j=0;j<W2;j++){
				v2.push_back(ary2[i][j]);
			}
		}
		sort(v2.begin(),v2.end());
		res=INF;
		for(int i=0;i<=R;i++){
			if(W1*H1>=i&&W2*H2>=R-i)res=min(res,v1.at(i)+v2.at(R-i));
		}
		cout<<res<<endl;
		//
		/*for(int i=0;i<H1;i++){
			for(int j=0;j<W1;j++){
				cout<<ary1[i][j]<<" ";
			}
			cout<<endl;
		}
		for(int i=0;i<H2;i++){
			for(int j=0;j<W2;j++){
				cout<<ary2[i][j]<<" ";
			}
			cout<<endl;
		}*/
		//
	}
	return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:39:17: error: 'memset' was not declared in this scope
   39 |                 memset(used,0,sizeof(used));
      |                 ^~~~~~
a.cc:7:1: note: 'memset' is defined in header '<cstring>'; this is probably fixable by adding '#include <cstring>'
    6 | #include <cstdlib>
  +++ |+#include <cstring>
    7 | #include <vector>