Language: C++

#include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>

using namespace std;

long long int par[10000000010] = {0};
long long int rrank[10000000010] = {0};

void init(long long int n, long long int par[], long long int rank[]) {
	for (int i = 0; i < n; i++) {
		par[i] = i;
		rank[i] = 0;
	}
}

int find(long long int x, long long int par[]) {
	if (par[x] == x) {
		return x;
	} else {
		return par[x] = find(par[x], par);
	}
}

void unite(long long int x, long long int y, long long int par[], long long int rank[]) {
	x = find(x, par);
	y = find(y, par);
	if (x == y) {
		return ;
	}

	if (rank[x] < rank[y]) {
		par[x] = y;
	} else {
		par[y] = x;
		if (rank[x] == rank[y]) rank[x]++;
	}
}


bool same(long long int x, long long int y, long long int par[]) {
	return find(x, par) == find(y, par);
}

struct edge {
	long long int u, v, cost;
};

bool comp(const edge& e1, const edge& e2) {
	return e1.cost < e2.cost;
}

vector<edge> es;
long long int V, E;

long long int kruskal(long long int par[], long long int rank[]) {
	sort(es.begin(), es.end(), comp);
	init(V, par, rank);
	long long int res = 0;
	for (long long int i = 0; i < E; i++) {
		edge e = es[i];
		if (same(e.u, e.v, par)) {
			unite(e.u, e.v, par, rank);
			res += e.cost;
		}
	}
	return res;

}

int main() {
	long long int w, h;
	cin >> w >> h;
	V = w*h;
	E = (w-1)*h+w*(h-1);
	vector<long long int> p(w);
	vector<long long int> q(h);
	for (long long int i = 0; i < w; i++) {
		cin >> p[i];
	}
	for (long long int i = 0; i < h; i++) {
		cin >> q[i];
	}
	for (long long int i = 0; i < w-1; i++) {
		for (long long int j = 0; j < h; j++) {
			edge e;
			e.u = i*w+j;
			e.v = (i+1)*w+j;
			e.cost = p[i];
			es.push_back(e);
		}
	}
	for (long long int i = 0; i < h-1; i++) {
		for (long long int j = 0; j < w; j++) {
			edge e;
			e.u = j*w+i;
			e.v = j*w+i+1;
			e.cost = q[i];
			es.push_back(e);
		}
	}
	cout << kruskal(par, rrank) << endl;
	return 0;
}

Compiler output:
/tmp/ccNEyOAj.o: in function `kruskal(long long*, long long*)':
a.cc:(.text+0x26c): relocation truncated to fit: R_X86_64_PC32 against symbol `es' defined in .bss section in /tmp/ccNEyOAj.o
a.cc:(.text+0x27e): relocation truncated to fit: R_X86_64_PC32 against symbol `es' defined in .bss section in /tmp/ccNEyOAj.o
a.cc:(.text+0x2a5): relocation truncated to fit: R_X86_64_PC32 against symbol `V' defined in .bss section in /tmp/ccNEyOAj.o
a.cc:(.text+0x2d8): relocation truncated to fit: R_X86_64_PC32 against symbol `es' defined in .bss section in /tmp/ccNEyOAj.o
a.cc:(.text+0x341): relocation truncated to fit: R_X86_64_PC32 against symbol `E' defined in .bss section in /tmp/ccNEyOAj.o
/tmp/ccNEyOAj.o: in function `main':
a.cc:(.text+0x398): relocation truncated to fit: R_X86_64_PC32 against symbol `V' defined in .bss section in /tmp/ccNEyOAj.o
a.cc:(.text+0x3c2): relocation truncated to fit: R_X86_64_PC32 against symbol `E' defined in .bss section in /tmp/ccNEyOAj.o
a.cc:(.text+0x523): relocation truncated to fit: R_X86_64_PC32 against symbol `es' defined in .bss section in /tmp/ccNEyOAj.o
a.cc:(.text+0x5d1): relocation truncated to fit: R_X86_64_PC32 against symbol `es' defined in .bss section in /tmp/ccNEyOAj.o
a.cc:(.text+0x606): relocation truncated to fit: R_X86_64_PC32 against symbol `rrank' defined in .bss section in /tmp/ccNEyOAj.o
/tmp/ccNEyOAj.o: in function `__static_initialization_and_destruction_0()':
a.cc:(.text+0x6c7): additional relocation overflows omitted from the output
collect2: error: ld returned 1 exit status