Language: C++

#include <bits/stdc++.h> 
using namespace std;
typedef long long ll;
typedef pair<int,int> Pint;
typedef pair<ll, int> P;
//typedef tuple<int,int,int> T;
ll INFL = 1000000000000000010;//10^18 = 2^60
int INF = 2147483600;//10^9
ll MOD  = 1000000007;
vector<int> dy(4);
vector<int> dx(4);
dy.at(0) = 1;
dy.at(1) = -1;
dx.at(2) = 1;
dx.at(3) = -1;
vector<ll> Ans(0);
int solve(){
    
    int R;
    cin >> R;
    if(R == 0)return 0;
    int H1,W1,X1,Y1;
    cin >> W1 >> H1 >> X1 >> Y1;
    X1--;
    Y1--;
    vector<vector<ll> > mp1(H1, vector<ll>(W1));
    vector<vector<ll> > d1(H1, vector<ll>(W1, INFL));
    for(int i = 0; i < H1; i++){
        for(int j = 0; j < W1; j++){
            cin >> mp1[i][j];
        }
    }
    
    d1[Y1][X1] = 1;
    priority_queue<pair<ll, pair<int, int> > > pque1;
    pque1.push({1, {Y1, X1}});
    
    while(!pque1.empty()){
        ll c;
        int y,x;
        c = pque1.top().first;
        y = pque1.top().second.first;
        x = pque1.top().second.second;
        pque1.pop();
        if(c > d1[y][x]) continue;
        for(int i = 0; i < 4; i++){
            int ny = y + dy[i];
            int nx = x + dx[i];
            if(0 <= ny && ny < H1 && 0 <= nx && nx < W1){
                if(d1[ny][nx] > d1[y][x]){
                    if(d1[y][x] > mp1[ny][nx]){
                        d1[ny][nx] = d1[y][x];
                    }else{
                        d1[ny][nx] = mp1[ny][nx];
                    }
                    pque1.push({d1[ny][nx], {ny, nx}});
                }else if(d1[ny][nx] > d1[y][x]){
                    d1[ny][nx] = mp1[ny][nx];
                    pque1.push({d1[ny][nx], {ny, nx}});
                }
            }
        }
    }
    
    int H2,W2,X2,Y2;
    cin >> W2 >> H2 >> X2 >> Y2;
    X2--;
    Y2--;
    vector<vector<ll> > mp2(H2, vector<ll>(W2));
    vector<vector<ll> > d2(H2, vector<ll>(W2, INFL));
    for(int i = 0; i < H2; i++){
        for(int j = 0; j < W2; j++){
            cin >> mp2[i][j];
        }
    }
    
    d2[Y2][X2] = 1;
    priority_queue<pair<ll, pair<int, int> > > pque2;
    pque2.push({1, {Y2, X2}});
    
    while(!pque2.empty()){
        ll c;
        int y,x;
        c = pque2.top().first;
        y = pque2.top().second.first;
        x = pque2.top().second.second;
        pque2.pop();
        if(c > d2[y][x]) continue;
        for(int i = 0; i < 4; i++){
            int ny = y + dy[i];
            int nx = x + dx[i];
            if(0 <= ny && ny < H2 && 0 <= nx && nx < W2){
                if(d2[ny][nx] > d2[y][x]){
                    if(d2[y][x] > mp2[ny][nx]){
                        d2[ny][nx] = d2[y][x];
                    }else{
                        d2[ny][nx] = mp2[ny][nx];
                    }
                    pque2.push({d2[ny][nx], {ny, nx}});
                }else if(d2[ny][nx] > d2[y][x]){
                    d2[ny][nx] = mp2[ny][nx];
                    pque2.push({d2[ny][nx], {ny, nx}});
                }
            }
        }
    }
    
    vector<pair<ll,int> > O1(0);
    vector<ll> COST1(0);
    for(int i = 0; i < H1; i++){
        for(int j = 0; j < W1; j++){
            COST1.push_back(d1[i][j]);
        }
    }
    sort(COST1.begin(), COST1.end());
    COST1.push_back(INFL);
    ll ad1 = COST1.at(0);
    int cnt = 0;
    for(int i = 0; i < COST1.size(); i++){
        if(ad1 == COST1.at(i)){
            cnt++;
        }else{
            O1.push_back({ad1, cnt});
            ad1 = COST1.at(i);
            cnt++;
        }
    }
    
    vector<pair<ll,int> > O2(1);
    O2.at(0).first = 0;
    O2.at(0).second = 0;
    vector<ll> COST2(0);
    for(int i = 0; i < H2; i++){
        for(int j = 0; j < W2; j++){
            COST2.push_back(d2[i][j]);
        }
    }
    sort(COST2.begin(), COST2.end());
    COST2.push_back(INFL);
    ll ad2 = COST2.at(0);
    cnt = 0;
    for(int i = 0; i < COST2.size(); i++){
        if(ad2 == COST2.at(i)){
            cnt++;
        }else{
            O2.push_back({ad2, cnt});
            ad2 = COST2.at(i);
            cnt++;
        }
    }
    
    
    ll ans = INFL;
    int right = -1;
    for(int i = 0; i < O1.size(); i++){
        ll cost = O1.at(i).first;
        int rest = R - O1.at(i).second;
        
        if(rest > O2.at(O2.size()-1).second) continue;
        int left = -1;
        if(right == -1){
            right = O2.size();
        }
        int mid;
        while(right - left > 1){
            mid = (left + right) / 2;
            if(O2.at(mid).second >= rest){
                right = mid;
            }else{
                left = mid;
            }
        }
        cost += O2.at(right).first;
        ans = min(ans, cost);
    }
    
    Ans.push_back(ans);
    solve();
    return 0;
}

int main(void){
    solve();
    for(int i = 0; i < Ans.size(); i++){
        cout << Ans.at(i) << endl;
    }
}



Compiler output:
a.cc:12:1: error: 'dy' does not name a type
   12 | dy.at(0) = 1;
      | ^~
a.cc:13:1: error: 'dy' does not name a type
   13 | dy.at(1) = -1;
      | ^~
a.cc:14:1: error: 'dx' does not name a type
   14 | dx.at(2) = 1;
      | ^~
a.cc:15:1: error: 'dx' does not name a type
   15 | dx.at(3) = -1;
      | ^~