Problem p03972 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
using pll = pair<ll, ll>;
using vl = vector<ll>;
using vll = vector<vl>;
using vpll = vector<pll>;
struct Edge {
  ll to;
  ll cost;
};
using Graph = vector<vector<Edge>>;

#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) (a).begin(), (a).end()
#define RES(a, n) (a).resize(n)
#define VLCIN(a, n)                                                            \
  REP(i, n) { cin >> a[i]; }
/***** define constant start *******/
const ll MOD = 1000000007;
const double PI = 2.0 * asin(1.0);
const ll INF = 1L << 60;
string abc = "abcdefghijklmnopqrstuvwxyz";
string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const ll MAX = (ll)pow(10, 7);
/****** define constant end ********/
/****** define variable start ******/
ll w, h;
vl p, q;
/******* define variable end *******/
int solve() {
  cin >> w >> h;
  p.resize(w);
  q.resize(h);
  REP(i, w) { cin >> p[i]; }
  REP(i, h) { cin >> q[i]; }
  bool is_w = (w > h);
  ll ans = 0;
  if (is_w) {
    REP(i, h) {
      if (i == 0) {
        ans += min(p[0], q[i]) * 2 + max(p[0], q[i]);
      } else {
        if (p[0] < q[i]) {
          ans += p[0] + q[i];
        } else {
          ans += q[i] * 2;
        }
      }
    }
    REP(i, h) {
      if (i == 0) {
        if (p[w - 1] < q[i]) {
          ans += p[w - 1] * 2;
        } else {
          ans += q[i] + p[w - 1];
        }
      } else {
        ans += min(q[i], p[w - 1]);
      }
    }
    reverse(ALL(p));
    p.pop_back();
    reverse(ALL(p));
    p.pop_back();
    sort(ALL(p));
    vl sum(p.size() + 1, 0);
    REP(i, p.size()) { sum[i + 1] = sum[i] + p[i]; }
    REP(i, h) {
      if (i == 0) {
        auto ite = lower_bound(p.begin(), p.end(), q[i]);
        ans += sum[distance(p.begin(), ite)];
        ans += (p.size() - distance(p.begin(), ite)) * q[i];
        ans += sum[p.size()];
      } else {
        auto ite = lower_bound(p.begin(), p.end(), q[i]);
        ans += sum[distance(p.begin(), ite)];
        ans += (p.size() - distance(p.begin(), ite)) * q[i];
      }
    }
  } else {
    REP(i, w) {
      if (i == 0) {
        ans += min(q[0], p[i]) * 2 + max(q[0], p[i]);
      } else {
        if (q[0] < p[i]) {
          ans += q[0] + p[i];
        } else {
          ans += p[i] * 2;
        }
      }
    }
    REP(i, w) {
      if (i == 0) {
        if (q[h - 1] < p[i]) {
          ans += q[h - 1] * 2;
        } else {
          ans += p[i] + q[h - 1];
        }
      } else {
        ans += min(p[i], q[h - 1]);
      }
    }
    reverse(ALL(q));
    q.pop_back();
    reverse(ALL(q));
    q.pop_back();
    sort(ALL(q));
    vl sum(q.size() + 1, 0);
    REP(i, q.size()) { sum[i + 1] = sum[i] + q[i]; }
    REP(i, w) {
      if (i == 0) {
        auto ite = lower_bound(q.begin(), q.end(), p[i]);
        ans += sum[distance(q.begin(), ite)];
        ans += (q.size() - distance(q.begin(), ite)) * p[i];
        ans += sum[q.size()];
      } else {
        auto ite = lower_bound(q.begin(), q.end(), p[i]);
        ans += sum[distance(q.begin(), ite)];
        ans += (q.size() - distance(q.begin(), ite)) * p[i];
      }
    }
  }
  cout << ans << endl;
  return 0;
}
int main() {
  // clock_t start = clock();
  cout << std::fixed << std::setprecision(10);
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int end_solve = solve();
  /*
  clock_t end = clock();
  const double time = static_cast<double>(end - start) / CLOCKS_PER_SEC *
  1000.0; printf("time %lf[ms]\n", time);
  */

  return end_solve;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
using pll = pair<ll, ll>;
using vl = vector<ll>;
using vll = vector<vl>;
using vpll = vector<pll>;
struct Edge {
  ll to;
  ll cost;
};
using Graph = vector<vector<Edge>>;

#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) (a).begin(), (a).end()
#define RES(a, n) (a).resize(n)
#define VLCIN(a, n)                                                            \
  REP(i, n) { cin >> a[i]; }
/***** define constant start *******/
const ll MOD = 1000000007;
const double PI = 2.0 * asin(1.0);
const ll INF = 1L << 60;
string abc = "abcdefghijklmnopqrstuvwxyz";
string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const ll MAX = (ll)pow(10, 7);
/****** define constant end ********/
/****** define variable start ******/
ll w, h;
vl p, q;
/******* define variable end *******/
int solve() {
  cin >> w >> h;
  p.resize(w);
  q.resize(h);
  REP(i, w) { cin >> p[i]; }
  REP(i, h) { cin >> q[i]; }
  bool is_w = (w > h);
  ll ans = 0;
  if (w == 1) {
    if (h == 1) {
      cout << p[0] * 2 + q[0] * 2 - max(p[0], q[0]) << endl;
      return 0;
    }
    REP(i, h) {
      if (i == 0) {
        ans += min(p[0], q[i]) * 2 + max(p[0], q[i]);
      } else {
        if (p[0] < q[i]) {
          ans += p[0] + q[i];
        } else {
          ans += q[i] * 2;
        }
      }
    }
    cout << ans << endl;
    return 0;
  }
  if (h == 1) {
    REP(i, w) {
      if (i == 0) {
        ans += min(q[0], p[i]) * 2 + max(q[0], p[i]);
      } else {
        if (q[0] < p[i]) {
          ans += q[0] + p[i];
        } else {
          ans += p[i] * 2;
        }
      }
    }
    cout << ans << endl;
    return 0;
  }
  if (is_w) {
    REP(i, h) {
      if (i == 0) {
        ans += min(p[0], q[i]) * 2 + max(p[0], q[i]);
      } else {
        if (p[0] < q[i]) {
          ans += p[0] + q[i];
        } else {
          ans += q[i] * 2;
        }
      }
    }
    REP(i, h) {
      if (i == 0) {
        if (p[w - 1] < q[i]) {
          ans += p[w - 1] * 2;
        } else {
          ans += q[i] + p[w - 1];
        }
      } else {
        ans += min(q[i], p[w - 1]);
      }
    }
    reverse(ALL(p));
    p.pop_back();
    reverse(ALL(p));
    p.pop_back();
    sort(ALL(p));
    vl sum(p.size() + 1, 0);
    REP(i, p.size()) { sum[i + 1] = sum[i] + p[i]; }
    REP(i, h) {
      if (i == 0) {
        auto ite = lower_bound(p.begin(), p.end(), q[i]);
        ans += sum[distance(p.begin(), ite)];
        ans += (p.size() - distance(p.begin(), ite)) * q[i];
        ans += sum[p.size()];
      } else {
        auto ite = lower_bound(p.begin(), p.end(), q[i]);
        ans += sum[distance(p.begin(), ite)];
        ans += (p.size() - distance(p.begin(), ite)) * q[i];
      }
    }
  } else {
    REP(i, w) {
      if (i == 0) {
        ans += min(q[0], p[i]) * 2 + max(q[0], p[i]);
      } else {
        if (q[0] < p[i]) {
          ans += q[0] + p[i];
        } else {
          ans += p[i] * 2;
        }
      }
    }
    REP(i, w) {
      if (i == 0) {
        if (q[h - 1] < p[i]) {
          ans += q[h - 1] * 2;
        } else {
          ans += p[i] + q[h - 1];
        }
      } else {
        ans += min(p[i], q[h - 1]);
      }
    }
    reverse(ALL(q));
    q.pop_back();
    reverse(ALL(q));
    q.pop_back();
    sort(ALL(q));
    vl sum(q.size() + 1, 0);
    REP(i, q.size()) { sum[i + 1] = sum[i] + q[i]; }
    REP(i, w) {
      if (i == 0) {
        auto ite = lower_bound(q.begin(), q.end(), p[i]);
        ans += sum[distance(q.begin(), ite)];
        ans += (q.size() - distance(q.begin(), ite)) * p[i];
        ans += sum[q.size()];
      } else {
        auto ite = lower_bound(q.begin(), q.end(), p[i]);
        ans += sum[distance(q.begin(), ite)];
        ans += (q.size() - distance(q.begin(), ite)) * p[i];
      }
    }
  }
  cout << ans << endl;
  return 0;
}
int main() {
  // clock_t start = clock();
  cout << std::fixed << std::setprecision(10);
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int end_solve = solve();
  /*
  clock_t end = clock();
  const double time = static_cast<double>(end - start) / CLOCKS_PER_SEC *
  1000.0; printf("time %lf[ms]\n", time);
  */

  return end_solve;
}