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

#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

class Solver {
  typedef vector<int> V;
  typedef vector<V> VV;

private:
  string i2s(int n) {
    stringstream ss;
    ss << n;
    return ss.str();
  }

public:
  int mn, tn, pn, rn;
  int m[2000], t[2000], p[2000], j[2000];
  string solve() {
    string retval;
    VV last_solved(tn, V(pn, -1));
    VV answer(tn, V(pn, 0));
    VV score(tn, V(3, 0));
    for (int i = 0; i < rn; ++i) {
      last_solved[t[i] - 1][p[i] - 1] = (j[i] == 0 ? m[i] : -1);
      ++answer[t[i] - 1][p[i] - 1];
    }
    for (int ti = 0; ti < tn; ++ti) {
      score[ti][2] = -ti - 1;
      for (int pi = 0; pi < pn; ++pi) {
        if (last_solved[ti][pi] >= 0) {
          --score[ti][0];
          score[ti][1] += last_solved[ti][pi] + 20 * (answer[ti][pi] - 1);
        }
      }
    }
    sort(score.begin(), score.end());
    retval = i2s(-score[0][2]);
    for (int i = 1; i < tn; ++i) {
      if (score[i][0] == score[i - 1][0] && score[i][1] == score[i - 1][1]) {
        retval += "=";
      } else {
        retval += ",";
      }
      retval += i2s(-score[i][2]);
    }
    return retval;
  }
};

int main() {
  while (true) {
    Solver s;
    cin >> s.mn >> s.tn >> s.pn >> s.rn;
    if (s.mn == 0) {
      return 0;
    }
    for (int i = 0; i < s.rn; ++i) {
      cin >> s.m[i] >> s.t[i] >> s.p[i] >> s.j[i];
    }
    string answer = s.solve();
    cout << answer << endl;
  }
}

Error: -11
FLAG0
FLAG1
FLAG2
FLAG3
FLAG4
3,5,1,10=9=8=7=6=4=2
FLAG4b
FLAG5a
FLAG5
FLAG0
FLAG1
FLAG2
FLAG3
FLAG4
2,1,3,4,5
FLAG4b
FLAG5a
FLAG5
FLAG0
FLAG1
FLAG2
FLAG3
FLAG4
2,1,3
FLAG4b
FLAG5a
FLAG5
FLAG0
FLAG1


Fixed submission:

#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

class Solver {
  typedef vector<int> V;
  typedef vector<V> VV;

private:
  string i2s(int n) {
    stringstream ss;
    ss << n;
    return ss.str();
  }

public:
  int mn, tn, pn, rn;
  int m[2000], t[2000], p[2000], j[2000];
  string solve() {
    string retval;
    VV last_solved(tn, V(pn, -1));
    VV answer(tn, V(pn, 0));
    VV score(tn, V(3, 0));
    cerr << "FLAG1" << endl;
    for (int i = 0; i < rn; ++i) {
      last_solved[t[i] - 1][p[i] - 1] = (j[i] == 0 ? m[i] : -1);
      ++answer[t[i]][p[i]];
    }
    for (int ti = 0; ti < tn; ++ti) {
      score[ti][2] = -ti - 1;
      for (int pi = 0; pi < pn; ++pi) {
        if (last_solved[ti][pi] >= 0) {
          --score[ti][0];
          score[ti][1] = max(score[ti][1], last_solved[ti][pi]);
        }
      }
    }
    cerr << "FLAG2" << endl;
    sort(score.begin(), score.end());
    retval = i2s(-score[0][2]);
    cerr << "FLAG3" << endl;
    for (int i = 1; i < tn; ++i) {
      if (score[i][0] == score[i - 1][0] && score[i][1] == score[i - 1][1]) {
        retval += "=";
      } else {
        retval += ",";
      }
      retval += i2s(-score[i][2]);
    }
    cerr << "FLAG4" << endl;
    cerr << retval << endl;
    cerr << "FLAG4b" << endl;
    return retval;
  }
};

int main() {
  Solver s;
  while (true) {
    string answer;
    cin >> s.mn >> s.tn >> s.pn >> s.rn;
    if (s.mn == 0) {
      return 0;
    }
    for (int i = 0; i < s.rn; ++i) {
      cin >> s.m[i] >> s.t[i] >> s.p[i] >> s.j[i];
    }
    cerr << "FLAG0" << endl;
    answer = s.solve();
    cerr << "FLAG5a" << endl;
    cout << answer << endl;
    cerr << "FLAG5" << endl;
  }
}