Language: C++

#include <iostream>
#include <list>
#include <vector>

using namespace std;
const int FIELD_SIZE = 502 * 502; // ÔºÌ½ß+2·é
const int FIELD_WIDTH = 502;
int field[FIELD_SIZE];	// ìÆpÌæ

typedef struct LevelRoomRelation {
	int level, room;	// FØx, ®
} LevelRoomRelation;

int fill(int r, int p, list<int> *nexts){
	int count = 1;
	field[p] = 0;
	if(field[p + 1] == 0){			// ÊßÏÝ
	}else if(field[p + 1] <= r){	// »Ì®Éüé
		count += fill(r, p + 1, nexts);
	}else{							// ãÅüéXgÖÇÁ
		list<int>::iterator it = nexts->begin();
		while(it != nexts->end()){
			if(field[*it] >= field[p + 1]){ break; }
			it++;
		}
		nexts->insert(it, p + 1);
	}
	if(field[p - 1] == 0){			// ÊßÏÝ
	}else if(field[p - 1] <= r){	// »Ì®Éüé
		count += fill(r, p - 1, nexts);
	}else{							// ãÅüéXgÖÇÁ
		list<int>::iterator it = nexts->begin();
		while(it != nexts->end()){
			if(field[*it] >= field[p - 1]){ break; }
			it++;
		}
		nexts->insert(it, p - 1);
	}
	if(field[p + FIELD_WIDTH] == 0){		// ÊßÏÝ
	}else if(field[p + FIELD_WIDTH] <= r){	// »Ì®Éüé
		count += fill(r, p + FIELD_WIDTH, nexts);
	}else{									// ãÅüéXgÖÇÁ
		list<int>::iterator it = nexts->begin();
		while(it != nexts->end()){
			if(field[*it] >= field[p + FIELD_WIDTH]){ break; }
			it++;
		}
		nexts->insert(it, p + FIELD_WIDTH);
	}
	if(field[p - FIELD_WIDTH] == 0){		// ÊßÏÝ
	}else if(field[p - FIELD_WIDTH] <= r){	// »Ì®Éüé
		count += fill(r, p - FIELD_WIDTH, nexts);
	}else{									// ãÅüéXgÖÇÁ
		list<int>::iterator it = nexts->begin();
		while(it != nexts->end()){
			if(field[*it] >= field[p - FIELD_WIDTH]){ break; }
			it++;
		}
		nexts->insert(it, p - FIELD_WIDTH);
	}
	return count;
}

void calculateLevel(int r, list<LevelRoomRelation> *answer){
	// âèÌüÍ
	int w, h, x, y;
	scanf("%d%d%d%d", &w, &h, &x, &y);
	memset(field, 0, sizeof(int) * FIELD_SIZE);
	for(int i = 1; i <= h; i++){
		for(int j = 1; j <= w; j++){
			scanf("%d", &(field[i * FIELD_WIDTH + j]));
		}
	}
	// vZ·é
	int currentLevel = 0, currentRoomCount = 0, remains = w * h;
	list<int> nexts;
	nexts.push_back(x + y * FIELD_WIDTH);
	list<int>::iterator front = nexts.begin();
	while(currentRoomCount < r && remains > 0){
		while(front != nexts.end()){
			currentLevel = field[*front];
			if(currentLevel != 0){ break; }
			front++;
		}
		while(front != nexts.end() && field[*front] <= currentLevel){
			if(field[*front] != 0){	// Ü¾ÊßµÄ¢È¢ê
				int fillResult = fill(currentLevel, (*front), &nexts);
				currentRoomCount += fillResult;
				remains -= fillResult;
			}
			front++;
		}
		LevelRoomRelation lrr;
		lrr.level = currentLevel;
		lrr.room = currentRoomCount;
		answer->push_back(lrr);
	}
}

int main(){
	while(true){
		int r;
		scanf("%d", &r);
		if(r == 0){ break; }

		LevelRoomRelation lrr;
		lrr.level = 0;
		lrr.room = 0;

		list<LevelRoomRelation> office1;
		calculateLevel(r, &office1);
		office1.insert(office1.begin(), lrr);
		{
//			cout << "Office1 dump:" << endl;
			list<LevelRoomRelation>::iterator it = office1.begin();
			while(it != office1.end()){
//				cout << "Level: " << (*it).level << " / " << "Room: " << (*it).room << endl;
				it++;
			}
		}
		list<LevelRoomRelation> office2;
		calculateLevel(r, &office2);
		office2.insert(office2.begin(), lrr);
		{
//			cout << "Office2 dump:" << endl;
			list<LevelRoomRelation>::iterator it = office2.begin();
			while(it != office2.end()){
//				cout << "Level: " << (*it).level << " / " << "Room: " << (*it).room << endl;
				it++;
			}
		}

		int minLevel = 0x7fffffff;
		list<LevelRoomRelation>::iterator it1 = office1.begin(), it2 = office2.end();
		int level2, level1 = 0;
		int roomCount2, roomCount1 = 0, roomCount;
		while(true){
			if(it2 == office2.begin()){
				if(it1 == office1.end()){ break; }
			}else{
				it2--;
			}
			level2 = (*it2).level;
			roomCount2 = (*it2).room;
			roomCount = roomCount2 + roomCount1;
			while(it1 != office1.end() && roomCount < r){
				roomCount1 = (*it1).room;
				roomCount = roomCount2 + roomCount1;
				level1 = (*it1).level;
				it1++;
			}
//			cout << "office1: " << level1 << " / office2: " << level2 << endl;
			if(minLevel > (level2 + level1)){
				minLevel = level2 + level1;
			}
		}
		cout << minLevel << endl;
	}
	return 0;
}

Compiler output:
a.cc: In function 'void calculateLevel(int, std::__cxx11::list<LevelRoomRelation>*)':
a.cc:68:9: error: 'memset' was not declared in this scope
   68 |         memset(field, 0, sizeof(int) * FIELD_SIZE);
      |         ^~~~~~
a.cc:4:1: note: 'memset' is defined in header '<cstring>'; this is probably fixable by adding '#include <cstring>'
    3 | #include <vector>
  +++ |+#include <cstring>
    4 |