Problem p03972 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define RI(X) scanf("%d", &(X))
#define RII(X, Y) scanf("%d%d", &(X), &(Y))
#define RIII(X, Y, Z) scanf("%d%d%d", &(X), &(Y), &(Z))
#define DRI(X)                                                                 \
  int(X);                                                                      \
  scanf("%d", &X)
#define DRII(X, Y)                                                             \
  int X, Y;                                                                    \
  scanf("%d%d", &X, &Y)
#define DRIII(X, Y, Z)                                                         \
  int X, Y, Z;                                                                 \
  scanf("%d%d%d", &X, &Y, &Z)
#define RS(X) scanf("%s", (X))
#define CASET                                                                  \
  int ___T, case_n = 1;                                                        \
  scanf("%d ", &___T);                                                         \
  while (___T-- > 0)
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
#define RF(x) freopen(x, "r", stdin)
#define WF(x) freopen(x, "w", stdout)
typedef long long LL;
using namespace std;
typedef pair<LL, LL> PLL;
typedef pair<int, int> PII;
const LL MOD = 1e9 + 7;
const int SIZE = 1e6 + 5;
const LL INF = 1LL << 60;
const double eps = 1e-4;
const double PI = 3.1415926535897932;
const int SZE = 3200000;

int p[10009], q[10009];
vector<PII> v;

int main() {
  DRII(w, h);
  REP(i, w) {
    RI(p[i]); // hor
    v.PB(MP(p[i], 1));
  }
  REP(i, h) {
    RI(q[i]); // ver
    v.PB(MP(q[i], 2));
  }
  sort(ALL(v));
  int hor = 0, ver = 0;
  LL ans = 0;
  for (PII a : v) {
    if (a.S == 1) {
      ans += (LL)(h + 1 - ver) * a.F;
      hor++;
    } else {
      ans += (LL)(w + 1 - hor) * a.F;
      ver++;
    }
  }
  printf("%lld", ans);
}


Fixed submission:
#include <bits/stdc++.h>
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define RI(X) scanf("%d", &(X))
#define RII(X, Y) scanf("%d%d", &(X), &(Y))
#define RIII(X, Y, Z) scanf("%d%d%d", &(X), &(Y), &(Z))
#define DRI(X)                                                                 \
  int(X);                                                                      \
  scanf("%d", &X)
#define DRII(X, Y)                                                             \
  int X, Y;                                                                    \
  scanf("%d%d", &X, &Y)
#define DRIII(X, Y, Z)                                                         \
  int X, Y, Z;                                                                 \
  scanf("%d%d%d", &X, &Y, &Z)
#define RS(X) scanf("%s", (X))
#define CASET                                                                  \
  int ___T, case_n = 1;                                                        \
  scanf("%d ", &___T);                                                         \
  while (___T-- > 0)
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
#define RF(x) freopen(x, "r", stdin)
#define WF(x) freopen(x, "w", stdout)
typedef long long LL;
using namespace std;
typedef pair<LL, LL> PLL;
typedef pair<int, int> PII;
const LL MOD = 1e9 + 7;
const int SIZE = 1e6 + 5;
const LL INF = 1LL << 60;
const double eps = 1e-4;
const double PI = 3.1415926535897932;
const int SZE = 3200000;

int p[100009], q[100009];
vector<PII> v;

int main() {
  DRII(w, h);
  REP(i, w) {
    RI(p[i]); // hor
    v.PB(MP(p[i], 1));
  }
  REP(i, h) {
    RI(q[i]); // ver
    v.PB(MP(q[i], 2));
  }
  sort(ALL(v));
  int hor = 0, ver = 0;
  LL ans = 0;
  for (PII a : v) {
    if (a.S == 1) {
      ans += (LL)(h + 1 - ver) * a.F;
      hor++;
    } else {
      ans += (LL)(w + 1 - hor) * a.F;
      ver++;
    }
  }
  printf("%lld", ans);
}