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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef pair<int, int> P;

int s[30][5];

P operator+(P a, int d) {
  rep(i, d) {
    if (!~s[a.first][a.second + 1]) {
      a.first++;
      a.second = 0;
    } else
      a.second++;
  }
  return a;
}
int main() {
  int m;
  while (scanf("%d%*c", &m), m) {
    vector<P> v;
    rep(i, m) {
      memset(s, -1, sizeof(s));
      string S;
      getline(cin, S);
      stringstream buf(S);
      int id;
      buf >> id;
      int f = 1;
      bool b = 0;
      while (f <= 9) {
        buf >> s[f][b];
        if (s[f][b] == 10) {
          f++;
          b = 0;
        } else {
          if (b)
            f++;
          b = !b;
        }
      }
      buf >> s[10][0] >> s[10][1];
      if (s[10][0] == 10 || s[10][1] == 10 || s[10][0] + s[10][1] == 10)
        buf >> s[10][2];
      P p(1, 0);
      while (p.first <= 9) {
        if (!p.second && s[p.first][p.second] == 10) {
          P a = p + 1, b = p + 2;
          s[p.first][p.second] += s[a.first][a.second] + s[b.first][b.second];
        }
        if (p.second && s[p.first][p.second - 1] + s[p.first][p.second] == 10) {
          P a = p + 1;
          s[p.first][p.second] += s[a.first][a.second];
        }
        p = p + 1;
      }
      int cnt = 0;
      for (int i = 1; i <= 10; i++)
        rep(j, 3) {
          if (~s[i][j])
            cnt += s[i][j];
        }
      v.push_back(P(cnt, id));
    }
    sort(v.begin(), v.end(), [](P a, P b) {
      if (a.first != b.first)
        return a.first > b.first;
      return a.second < b.second;
    });
    for (auto p : v) {
      printf("%d %d\\n", p.second, p.first);
    }
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef pair<int, int> P;

int s[30][5];

P operator+(P a, int d) {
  rep(i, d) {
    if (!~s[a.first][a.second + 1]) {
      a.first++;
      a.second = 0;
    } else
      a.second++;
  }
  return a;
}
int main() {
  int m;
  while (scanf("%d%*c", &m), m) {
    vector<P> v;
    rep(i, m) {
      memset(s, -1, sizeof(s));
      string S;
      getline(cin, S);
      stringstream buf(S);
      int id;
      buf >> id;
      int f = 1;
      bool b = 0;
      while (f <= 9) {
        buf >> s[f][b];
        if (s[f][b] == 10)
          f++;
        else {
          if (b)
            f++;
          b = !b;
        }
      }
      buf >> s[10][0] >> s[10][1];
      if (s[10][0] == 10 || s[10][1] == 10 || s[10][0] + s[10][1] == 10)
        buf >> s[10][2];
      int k;
      if (buf >> k)
        abort();
      P p(1, 0);
      while (p.first <= 9) {
        if (s[p.first][p.second] == 10) {
          P a = p + 1, b = p + 2;
          s[p.first][p.second] += s[a.first][a.second] + s[b.first][b.second];
        }
        if (p.second && s[p.first][p.second - 1] + s[p.first][p.second] == 10) {
          P a = p + 1;
          s[p.first][p.second] += s[a.first][a.second];
        }
        p = p + 1;
      }
      int cnt = 0;
      for (int i = 1; i <= 10; i++)
        rep(j, 3) {
          if (~s[i][j])
            cnt += s[i][j];
        }
      v.push_back(P(cnt, id));
    }
    sort(v.begin(), v.end(), [](P a, P b) {
      if (a.first != b.first)
        return a.first > b.first;
      return a.second < b.second;
    });
    for (auto p : v) {
      printf("%d %d\\n", p.second, p.first);
    }
  }
}