Language: C++

#include <iostream>
#include <list>
#include <map>
#include <cstring>
 
using namespace std;
const int FIELD_SIZE = 502 * 502; // 逡ェ蜈オ縺ョ縺溘ａ+2縺吶ｋ
const int FIELD_WIDTH = 502;
int field[FIELD_SIZE];  // 菴懈・ュ逕ィ鬆伜沺
 
typedef struct LevelRoomRelation {
    int level, room;    // 隱崎ィシ繝ャ繝吶Ν, 驛ィ螻区焚
} LevelRoomRelation;
 
int fill(int r, int p, multimap<int, int> *nexts){
    int count = 1;
    field[p] = 0;
    if(field[p + 1] == 0){          // 騾夐℃貂医∩
    }else if(field[p + 1] <= r){ // 縺昴&#65533;驛ィ螻九↓蜈・繧&#65533;     count += fill(r, p + 1, nexts);
    }else{                          // 蠕後〒蜈・繧九Μ繧ケ繝医∈霑ス蜉&#65533;        nexts->insert(multimap<int, int>::value_type(field[p + 1], p + 1));
    }
    if(field[p - 1] == 0){          // 騾夐℃貂医∩
    }else if(field[p - 1] <= r){ // 縺昴&#65533;驛ィ螻九↓蜈・繧&#65533;     count += fill(r, p - 1, nexts);
    }else{                          // 蠕後〒蜈・繧九Μ繧ケ繝医∈霑ス蜉&#65533;        nexts->insert(multimap<int, int>::value_type(field[p - 1], p - 1));
    }
    if(field[p + FIELD_WIDTH] == 0){        // 騾夐℃貂医∩
    }else if(field[p + FIELD_WIDTH] <= r){   // 縺昴&#65533;驛ィ螻九↓蜈・繧&#65533;     count += fill(r, p + FIELD_WIDTH, nexts);
    }else{                                  // 蠕後〒蜈・繧九Μ繧ケ繝医∈霑ス蜉&#65533;        nexts->insert(multimap<int, int>::value_type(field[p + FIELD_WIDTH], p + FIELD_WIDTH));
    }
    if(field[p - FIELD_WIDTH] == 0){        // 騾夐℃貂医∩
    }else if(field[p - FIELD_WIDTH] <= r){   // 縺昴&#65533;驛ィ螻九↓蜈・繧&#65533;     count += fill(r, p - FIELD_WIDTH, nexts);
    }else{                                  // 蠕後〒蜈・繧九Μ繧ケ繝医∈霑ス蜉&#65533;        nexts->insert(multimap<int, int>::value_type(field[p - FIELD_WIDTH], p - FIELD_WIDTH));
    }
    return count;
}
 
void calculateLevel(int r, list<LevelRoomRelation> *answer){
    // 蝠城。後&#65533;蜈・蜉&#65533;    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]));
        }
    }
    // 險育ョ励☆繧&#65533;  int currentLevel = 0, currentRoomCount = 0, remains = w * h;
    multimap<int, int> nexts;
    nexts.insert(multimap<int, int>::value_type(1, x + y * FIELD_WIDTH));
    multimap<int, int>::iterator front = nexts.begin();
    while(currentRoomCount < r && remains > 0){
        while(front != nexts.end()){
            currentLevel = field[(*front).second];
            if(currentLevel != 0){ break; }
            multimap<int, int>::iterator tmp = front;
            front++;
            nexts.erase(tmp);
        }
        while(front != nexts.end() && field[(*front).second] <= currentLevel){
            if(field[(*front).second] != 0){    // 縺セ縺&#65533;&#65533;驕弱＠縺ヲ縺&#65533;↑縺&#65533;&#65533;蜷&#65533;               int fillResult = fill(currentLevel, (*front).second, &nexts);
                currentRoomCount += fillResult;
                remains -= fillResult;
            }
            multimap<int, int>::iterator tmp = front;
            front++;
            nexts.erase(tmp);
        }
        LevelRoomRelation lrr;
        lrr.level = currentLevel;
        lrr.room = currentRoomCount;
        answer->push_back(lrr);
        //cout << "Level: " << (lrr).level << " / " << "Room: " << (lrr).room << endl;
    }
}
 
int main(){
    while(true){
        int r;
        if(!~scanf("%d", &r)){ break; }
        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:39:24: error: 'w' was not declared in this scope
   39 |     scanf("%d%d%d%d", &w, &h, &x, &y);
      |                        ^
a.cc:39:28: error: 'h' was not declared in this scope
   39 |     scanf("%d%d%d%d", &w, &h, &x, &y);
      |                            ^
a.cc:39:32: error: 'x' was not declared in this scope
   39 |     scanf("%d%d%d%d", &w, &h, &x, &y);
      |                                ^
a.cc:39:36: error: 'y' was not declared in this scope
   39 |     scanf("%d%d%d%d", &w, &h, &x, &y);
      |                                    ^
a.cc:50:11: error: 'currentRoomCount' was not declared in this scope
   50 |     while(currentRoomCount < r && remains > 0){
      |           ^~~~~~~~~~~~~~~~
a.cc:50:35: error: 'remains' was not declared in this scope
   50 |     while(currentRoomCount < r && remains > 0){
      |                                   ^~~~~~~
a.cc:52:13: error: 'currentLevel' was not declared in this scope
   52 |             currentLevel = field[(*front).second];
      |             ^~~~~~~~~~~~
a.cc:58:65: error: 'currentLevel' was not declared in this scope
   58 |         while(front != nexts.end() && field[(*front).second] <= currentLevel){
      |                                                                 ^~~~~~~~~~~~
a.cc:60:37: error: 'fillResult' was not declared in this scope
   60 |                 currentRoomCount += fillResult;
      |                                     ^~~~~~~~~~
a.cc:68:21: error: 'currentLevel' was not declared in this scope
   68 |         lrr.level = currentLevel;
      |                     ^~~~~~~~~~~~