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

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define f first
#define s second
#define mp make_pair

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define print(x) printf("%d\\n", x)

using namespace std;

pair<int, pair<int, int>> cat[1000];
int catx[1000];
int caty[1000];
int catc[1000];
int esa[1000];
int n, m;
int acc;

bool can(int lim) {
  vector<int> e(esa, esa + m);
  while (true) {
    if (e.size() <= 1) {
      if (lim >= acc)
        break;
      else
        return false;
    }

    int mm = e.size();
    vector<int> counts(mm);
    int ep = 0;
    int next = e[1];
    int maxc = -1;
    int maxp;

    REP(i, n) {
      if (ep == mm - 1) {
        counts[ep] += catc[i];
      } else {
        if (catx[i] <= e[ep + 1]) {
#define ABS(x) ((x) < 0 ? -(x) : (x))
          int da = ABS(e[ep] - catx[i]);
          int db = ABS(e[ep + 1] - catx[i]);
          if (da <= db) {
            counts[ep] += catc[i];
          } else {
            counts[ep + 1] += catc[i];
          }
        } else {
          while (ep != mm - 1 && catx[i] > e[ep + 1])
            ep++;
          i--;
        }
      }
      if (maxc < counts[ep]) {
        maxc = counts[ep];
        maxp = ep;
      }
      if (ep != mm - 1 && maxc < counts[ep + 1]) {
        maxc = counts[ep + 1];
        maxp = ep + 1;
      }
    }

    // printf("counts: ");
    // REP(i,mm) printf("(%d):%d ",e[i],counts[i]); puts("");
    if (maxc <= lim)
      break;
    if (maxp == 0 || maxp == mm - 1)
      return false;
    // if(counts[maxp - 1] > lim && counts[maxp + 1] > lim) return false;
    e.erase(e.begin() + maxp);
  }
  return true;
}

int main() {
  while (scanf("%d%d", &n, &m), n + m) {
    int tmp;
    REP(i, n) scanf("%d%d%d", &cat[i].f, &cat[i].s.f, &cat[i].s.s);
    REP(i, m) scanf("%d", esa + i);
    if (n == 0) {
      puts("0");
      continue;
    }

    sort(cat, cat + n);
    sort(esa, esa + m);
    REP(i, n) catx[i] = cat[i].f;
    REP(i, n) caty[i] = cat[i].s.f * cat[i].s.f;
    REP(i, n) catc[i] = cat[i].s.s;

    int high = acc = accumulate(catc, catc + n, 0);
    int low = *max_element(catc, catc + n);

    while (high >= low) {
      int mid = (high + low) / 2;
      // printf("%d %d %d\\n",low,mid,high);
      if (can(mid)) {
        high = mid - 1;
      } else {
        low = mid + 1;
      }
    }
    low = max(*max_element(catc, catc + n), low - 2);
    while (!can(low))
      low++;
    print(low);
  }
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define f first
#define s second
#define mp make_pair

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define print(x) printf("%d\\n", x)

using namespace std;

pair<int, pair<int, int>> cat[1000];
int catx[1000];
int caty[1000];
int catc[1000];
int esa[1000];
int n, m;
int acc;

bool can(int lim) {
  vector<int> e(esa, esa + m);
  while (true) {
    if (e.size() <= 1) {
      if (lim >= acc)
        break;
      else
        return false;
    }

    int mm = e.size();
    vector<int> counts(mm);
    int ep = 0;
    int next = e[1];
    int maxc = -1;
    int maxp;

    REP(i, n) {
      if (ep == n - 1) {
        counts[ep] += catc[i];
      } else {
        if (catx[i] <= e[ep + 1]) {
          int da = caty[i] + (e[ep] - catx[i]) * (e[ep] - catx[i]);
          int db = caty[i] + (e[ep] - catx[i]) * (e[ep] - catx[i]);
          if (da <= db) {
            counts[ep] += catc[i];
          } else {
            counts[ep + 1] += catc[i];
          }
        } else {
          while (ep != n - 1 && catx[i] > e[ep + 1])
            ep++;
          i--;
        }
      }
      if (maxc < counts[ep]) {
        maxc = counts[ep];
        maxp = ep;
      }
      if (ep != n - 1 && maxc < counts[ep + 1]) {
        maxc = counts[ep + 1];
        maxp = ep + 1;
      }
    }

    // printf("counts: ");
    // REP(i,mm) printf("(%d):%d ",e[i],counts[i]); puts("");
    if (maxc <= lim)
      break;
    if (maxp == 0 || maxp == mm - 1)
      return false;
    // if(counts[maxp - 1] > lim && counts[maxp + 1] > lim) return false;
    e.erase(e.begin() + maxp);
  }
  return true;
}

int main() {
  while (scanf("%d%d", &n, &m), n + m) {
    int tmp;
    REP(i, n) scanf("%d%d%d", &cat[i].f, &cat[i].s.f, &cat[i].s.s);
    REP(i, m) scanf("%d", esa + i);

    sort(cat, cat + n);
    sort(esa, esa + m);
    REP(i, n) catx[i] = cat[i].f;
    REP(i, n) caty[i] = cat[i].s.f * cat[i].s.f;
    REP(i, n) catc[i] = cat[i].s.s;

    int high = acc = accumulate(catc, catc + n, 0);
    int low = *max_element(catc, catc + n);

    while (high >= low) {
      int mid = (high + low) / 2;
      // printf("%d %d %d\\n",low,mid,high);
      if (can(mid)) {
        high = mid - 1;
      } else {
        low = mid + 1;
      }
    }
    low = max(*max_element(catc, catc + n), low - 2);
    while (!can(low))
      low++;
    print(low);
  }
  return 0;
}