Language: C++

#include <bits/stdc++.h>
/* Karen {{{
  ___  ____
 |_  ||_  _|
   | |_/ /    ,--.   _ .--.  .---.  _ .--.
   |  __'.   `'_\ : [ `/'`\]/ /__\\[ `.-. |
  _| |  \ \_ // | |, | |    | \__., | | | |
 |____||____|\'-;__/[___]    '.__.'[___||__]
}}} */
/* cpp template {{{*/
using namespace std;

#define endl '\n'

#define REP(i, n) for (int i = 0; i < (n); i++)
#define REPS(i, a, n) for (int i = (a); i < (n); i++)
#define RREP(i, n) for (int i = 1; i <= (n); i++)
#define X real()
#define Y imag()
#define EQ(n,m) (abs((n)-(m)) < EPS)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ALL(v) v.begin(), v.end()

typedef double D;
typedef long long ll;
typedef long double lb;
typedef complex<D> P;
typedef vector<P> VP;
typedef vector<int> vi;
typedef vector<vector<int> > vii;
typedef pair<int, int> pii;
typedef pair<int, string> pis;
typedef pair<string, int> psi;

inline bool inside(int y,int x,int H,int W){return(y>=0&&x>=0&&y<H&&x<W);}
inline int in() {int x; std::cin >> x; return x;}

template <typename T>
void print(std::vector<T>& v, char c = ' ') {
  REP(i, v.size()) {
    if (i != 0) std::cout << c;
    std::cout << v[i];
  }
  std::cout << endl;
}
template <typename T>
void print(T x) {
  std::cout << x << '\n';
}
/* }}} */
/* 定数 {{{*/
const ll MOD = 1e9 + 7;
const ll INF = 1e9 + 9;
const D EPS  = 1e-9;
const int dx[] = {0, 1, 0, -1, 1, -1, 1, -1};
const int dy[] = {1, 0, -1, 0, 1, -1, -1, 1};
/* }}} */
// {{{
const ll MAX_H = 1e5, MAX_W = 1e5;
const ll MAX_N = MAX_H * MAX_W;

struct UnionFind
{
  ll par[MAX_N];

  /*　初期化　*/
  void init(int n)
  {
    for (int i = 0; i < n; i++) par[i] = i;
  }

  /*　root(一番上)を返す　*/
  int root(int x)
  {
    return ((x == par[x]) ? (x) : (par[x] = root(par[x])));
  }

  /*　yの下にxをぶら下げる　*/
  void unite(int x, int y)
  {
    x = root(x);
    y = root(y);
    if (x == y) return;
    par[x] = y;
  }

  /*　xとyのrootが同じであればtrueを返す　*/
  bool same(int x, int y)
  {
    return root(x) == root(y);
  }
}uf;
// }}}

int ans;

#define int long long
signed main()
{
  cin.tie(0);
  ios::sync_with_stdio(false);

  int w = in(), h = in();
  vector<pair<int, pair<bool, int> > > cost;

  for (int i = 0; i < w; i++){
    int p = in();
    cost.pb(mp(p, mp(true, i)));
  }
  for (int i = 0; i < h; i++){
    int q = in();
    cost.pb(mp(q, mp(false, i)));
  }

  h++; w++;

  uf.init(h * w);
  sort(ALL(cost));

  for (int i = 0; i < h + w - 2; i++){
    //cout << cost[i].first << endl;
    bool f = cost[i].se.fi;
    for (int j = 0; j < (f?w:h) + 1; ++j){
      if (f){
        if (!uf.same(cost[i].se.se * w + j + 1, (cost[i].se.se + 1) * w + j + 1)){
          cout << cost[i].se.se * w + j + 1 << ":" << (cost[i].se.se + 1) * w + j + 1 << " " << cost[i].fi << endl;
          ans += cost[i].fi;
          uf.unite(cost[i].se.se * w + j + 1, (cost[i].se.se + 1) * w + j + 1);
        }
      } else {
        if (!uf.same(j * w + cost[i].se.se + 1, j * w + cost[i].se.se + 2)){
          cout << j * w + cost[i].se.se + 1 << ":" << j * w + cost[i].se.se + 2 << " " << cost[i].fi << endl;
          ans += cost[i].fi;
          uf.unite(j * w + cost[i].se.se + 1, j * w + cost[i].se.se + 2);
        }
      }
    }
  }

  cout << ans << endl;
  return (0);
}



Compiler output:
/tmp/ccxPnRCF.o: in function `main':
a.cc:(.text+0x395): relocation truncated to fit: R_X86_64_PC32 against symbol `ans' defined in .bss section in /tmp/ccxPnRCF.o
a.cc:(.text+0x39d): relocation truncated to fit: R_X86_64_PC32 against symbol `ans' defined in .bss section in /tmp/ccxPnRCF.o
a.cc:(.text+0x58a): relocation truncated to fit: R_X86_64_PC32 against symbol `ans' defined in .bss section in /tmp/ccxPnRCF.o
a.cc:(.text+0x592): relocation truncated to fit: R_X86_64_PC32 against symbol `ans' defined in .bss section in /tmp/ccxPnRCF.o
a.cc:(.text+0x650): relocation truncated to fit: R_X86_64_PC32 against symbol `ans' defined in .bss section in /tmp/ccxPnRCF.o
collect2: error: ld returned 1 exit status