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

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int main() {
  for (;;) {
    int n, m; // 猫の集団の数、餌入れの数
    cin >> n >> m;
    if (m == 0)
      return 0;

    vector<int> cx(n), cy(n), catNum(n);
    for (int i = 0; i < n; ++i)
      cin >> cx[i] >> cy[i] >> catNum[i];

    vector<int> x(m);
    for (int i = 0; i < m; ++i)
      cin >> x[i];

    if (n == 0) {
      cout << 0 << endl;
      continue;
    }

    vector<vector<pair<pair<int, int>, int>>> dist(
        n, vector<pair<pair<int, int>, int>>(m));
    vector<int> eat(m, 0);
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < m; ++j) {
        dist[i][j].first.first =
            (cx[i] - x[j]) * (cx[i] - x[j]) + cy[i] * cy[i];
        dist[i][j].first.second = x[j];
        dist[i][j].second = j;
      }
      sort(dist[i].begin(), dist[i].end());
      eat[dist[i][0].second] += catNum[i];
    }

    vector<int> select(n, 0);
    vector<bool> erase(m, false);
    int ret = *max_element(eat.begin(), eat.end());
    for (int i = 0; i < m - 1; ++i) {
      int k = max_element(eat.begin(), eat.end()) - eat.begin();
      erase[k] = true;
      for (int j = 0; j < n; ++j) {
        eat[dist[j][select[j]].second] -= catNum[j];
        while (erase[dist[j][select[j]].second])
          ++select[j];
        eat[dist[j][select[j]].second] += catNum[j];
      }
      ret = min(ret, *max_element(eat.begin(), eat.end()));
    }

    cout << ret << endl;
  }
}

Error: WA

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int main() {
  for (;;) {
    int n, m; // 猫の集団の数、餌入れの数
    cin >> n >> m;
    if (m == 0)
      return 0;
    if (n == 0) {
      cout << 0 << endl;
      continue;
    }

    vector<int> cx(n), cy(n), catNum(n);
    for (int i = 0; i < n; ++i)
      cin >> cx[i] >> cy[i] >> catNum[i];

    vector<int> x(m);
    for (int i = 0; i < m; ++i)
      cin >> x[i];

    vector<vector<pair<pair<long long, int>, int>>> dist(
        n, vector<pair<pair<long long, int>, int>>(m));
    vector<int> eat(m, 0);
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < m; ++j) {
        dist[i][j].first.first = (long long)(cx[i] - x[j]) * (cx[i] - x[j]) +
                                 (long long)cy[i] * cy[i];
        dist[i][j].first.second = x[j];
        dist[i][j].second = j;
      }
      sort(dist[i].begin(), dist[i].end());
      eat[dist[i][0].second] += catNum[i];
    }

    vector<int> select(n, 0);
    vector<bool> erase(m, false);
    int ret = *max_element(eat.begin(), eat.end());
    for (int i = 0; i < m - 1; ++i) {
      int k = max_element(eat.begin(), eat.end()) - eat.begin();
      erase[k] = true;
      for (int j = 0; j < n; ++j) {
        eat[dist[j][select[j]].second] -= catNum[j];
        while (erase[dist[j][select[j]].second])
          ++select[j];
        eat[dist[j][select[j]].second] += catNum[j];
      }
      ret = min(ret, *max_element(eat.begin(), eat.end()));
    }

    cout << ret << endl;
  }
}