Language: C++

#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef vector<long long> vl;
typedef pair<int,int> pii;
typedef pair<long,long> pll;
typedef long long ll;
typedef vector<pii> vpii;
#define reps(i, x, n) for(int i = x; i < n; i++)
#define rep(i,n) reps(i,0,n)
#define all(g) (g).begin(),(g).end()
#define pb push_back
const int mod = 1e9 + 7;
#define INF 1<<25
/*
input case
output case
*/

// ll W,H;
// const ll max_s = 100100;
// const ll max_v = max_s * max_s;
// int color[max_s][max_s];
// ll M[max_s][max_s];
int W, H;
const int max_s = 100100;
int color[max_s][max_s];
ll p[max_s];
ll q[max_s];
ll d[max_s][max_s];
pii z[max_s][max_s];
int tee,u,s;

void solve(){
    ll mincost;
    d[0][0] = 0;
    z[0][0] = make_pair(0,-1);
    while(true){
        mincost = INF;
        rep(i,W) rep(j,H){
            if ((color[i][j] != 2) and(d[i][j] < mincost)){
                mincost = d[i][j];
                u = i;
                s = j;
            }
        }
        if (mincost == INF) break;
        color[u][s] = 2;

        rep(i,W) rep(j,H){
            if ((color[i][j] != 2) and ((i == u and abs(j-s) == 1) or (abs(i-u) == 1 and j == s))){
                if ((i == u) and (abs(j-s) == 1)){ tee = q[max(j,s)];}
                if ((abs(i-u) == 1) and (s == j)){ tee = p[max(i,u)];}
                d[i][j] = tee;
                z[i][j] = make_pair(u,s);
                color[i][j] = 1;
            }
        }
    }
    int sum = 0;
    rep(i,W) rep(j,H){
        if ((z[i][j].first != -1) or (z[i][j].second != -1)) {
            if (abs(z[i][j].first - i) == 1){
                sum += p[z[i][j].first];
            }else{
                sum += q[z[i][j].second];
            }
        }
    }
    cout << sum << endl;
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> W >> H;
    rep(i,W) cin >> p[i];
    rep(i,H) cin >> q[i];
    rep(i,W) rep(j,H) color[i][j] = 0;
    rep(i,W) rep(j,H) d[i][j] = INF;

    solve();
}

Compiler output:
/tmp/ccnr0B6E.o: in function `solve()':
a.cc:(.text+0xb): relocation truncated to fit: R_X86_64_PC32 against symbol `d' defined in .bss section in /tmp/ccnr0B6E.o
a.cc:(.text+0x42): relocation truncated to fit: R_X86_64_PC32 against symbol `z' defined in .bss section in /tmp/ccnr0B6E.o
a.cc:(.text+0xba): relocation truncated to fit: R_X86_64_PC32 against symbol `d' defined in .bss section in /tmp/ccnr0B6E.o
a.cc:(.text+0xe8): relocation truncated to fit: R_X86_64_PC32 against symbol `d' defined in .bss section in /tmp/ccnr0B6E.o
a.cc:(.text+0xf9): relocation truncated to fit: R_X86_64_PC32 against symbol `u' defined in .bss section in /tmp/ccnr0B6E.o
a.cc:(.text+0x102): relocation truncated to fit: R_X86_64_PC32 against symbol `s' defined in .bss section in /tmp/ccnr0B6E.o
a.cc:(.text+0x13c): relocation truncated to fit: R_X86_64_PC32 against symbol `u' defined in .bss section in /tmp/ccnr0B6E.o
a.cc:(.text+0x142): relocation truncated to fit: R_X86_64_PC32 against symbol `s' defined in .bss section in /tmp/ccnr0B6E.o
a.cc:(.text+0x1b8): relocation truncated to fit: R_X86_64_PC32 against symbol `u' defined in .bss section in /tmp/ccnr0B6E.o
a.cc:(.text+0x1c5): relocation truncated to fit: R_X86_64_PC32 against symbol `s' defined in .bss section in /tmp/ccnr0B6E.o
a.cc:(.text+0x1dc): additional relocation overflows omitted from the output
collect2: error: ld returned 1 exit status