Problem p00874 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

typedef ostringstream OSS;
typedef istringstream ISS;

typedef long long LL;
typedef pair<int, int> PII;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef vector<double> VD;
typedef vector<VD> VVD;
typedef vector<string> VS;
typedef vector<VS> VVS;
typedef vector<bool> VB;
typedef vector<VB> VVB;
typedef vector<PII> VPII;

#define fst first
#define snd second
// #define Y first
// #define X second
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define ALL(x) (x).begin(), (x).end()
#define RANGE(x, y, maxX, maxY)                                                \\
  (0 <= (x) && 0 <= (y) && (x) < (maxX) && (y) < (maxY))
#define DUMP(x) cerr << #x << " = " << (x) << endl
#define rep(i, N) for (int i = 0; i < (int)(N); i++)
#define REP(i, init, N) for (int i = (init); i < (int)(N); i++)

template <typename T> inline T fromString(const string &s) {
  T res;
  ISS iss(s);
  iss >> res;
  return res;
};
template <typename T> inline string toString(const T &a) {
  OSS oss;
  oss << a;
  return oss.str();
};

const int INF = 0x3f3f3f3f;
const LL INFL = 0x3f3f3f3f3f3f3f3fLL;
const double DINF = 0x3f3f3f3f;
const int DX[] = {1, 0, -1, 0}, DY[] = {0, -1, 0, 1};

int W, D;
VI WS, DS;

int solve() {
  VI ws(W), ds(D);

  int cnt = 0;

  rep(w, W) {
    int n = WS[w];
    ws[w] = n;

    int pos = -1;
    rep(d, D) {
      if (n > DS[d])
        continue;

      pos = d;

      if (DS[pos] == n && ds[pos] != DS[pos])
        break;
    }
    ds[pos] = max(ds[pos], n);
    cnt += n;
  }

  /*    for (auto w : ws) cout << " " << w;
      cout << endl;
      for (auto d : ds) cout << " " << d;
      cout << endl;
  */
  rep(d, D) {
    if (ds[d] != DS[d]) {
      cnt += DS[d];
      ds[d] = DS[d];
    }
  }

  return cnt;
}

int main(void) {
  while (cin >> W >> D, W) {
    WS = VI(W);
    DS = VI(D);
    for (auto &w : WS)
      cin >> w;
    for (auto &d : DS)
      cin >> d;

    cout << solve() << endl;
  }

  return 0;
}

Error: -6
double free or corruption (out)


Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef ostringstream OSS;
typedef istringstream ISS;

typedef long long LL;
typedef pair<int, int> PII;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef vector<double> VD;
typedef vector<VD> VVD;
typedef vector<string> VS;
typedef vector<VS> VVS;
typedef vector<bool> VB;
typedef vector<VB> VVB;
typedef vector<PII> VPII;

#define fst first
#define snd second
// #define Y first
// #define X second
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define ALL(x) (x).begin(), (x).end()
#define RANGE(x, y, maxX, maxY)                                                \\
  (0 <= (x) && 0 <= (y) && (x) < (maxX) && (y) < (maxY))
#define DUMP(x) cerr << #x << " = " << (x) << endl
#define rep(i, N) for (int i = 0; i < (int)(N); i++)
#define REP(i, init, N) for (int i = (init); i < (int)(N); i++)

template <typename T> inline T fromString(const string &s) {
  T res;
  ISS iss(s);
  iss >> res;
  return res;
};
template <typename T> inline string toString(const T &a) {
  OSS oss;
  oss << a;
  return oss.str();
};

const int INF = 0x3f3f3f3f;
const LL INFL = 0x3f3f3f3f3f3f3f3fLL;
const double DINF = 0x3f3f3f3f;
const int DX[] = {1, 0, -1, 0}, DY[] = {0, -1, 0, 1};

int W, D;
VI WS, DS;

int solve() {
  VI ws(W), ds(D);

  int cnt = 0;

  rep(w, W) {
    int n = WS[w];
    ws[w] = n;

    int pos = -1;
    rep(d, D) {
      if (ds[d] == DS[d])
        continue;
      if (n > DS[d])
        continue;

      pos = d;

      if (DS[pos] == n)
        break;
    }
    ds[pos] = n;
    cnt += n;
  }

  rep(d, D) {
    if (ds[d] != DS[d]) {
      cnt += DS[d];
      ds[d] = DS[d];
    }
  }

  return cnt;
}

int main(void) {
  while (cin >> W >> D, W) {
    WS = VI(W);
    DS = VI(D);
    for (auto &w : WS)
      cin >> w;
    for (auto &d : DS)
      cin >> d;

    cout << solve() << endl;
  }

  return 0;
}