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

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

using namespace std;

typedef pair<int, int> P;

int main() {
  int n;

  while (true) {
    cin >> n;
    if (!n) {
      break;
    }
    vector<P> book(n);

    for (int i = 0; i < n; i++) {
      cin >> book[i].first >> book[i].second;
    }

    sort(book.begin(), book.end(), greater<P>());

    int ans = 0;
    if (book.size() == 1) {
      ans = book[0].first * 2 + book[0].second;
    } else {
      int total = 0;
      for (int i = 1; i < book.size(); i++) {
        total += book[i].first;
      }

      if (book[0].first > total) {
        int a = book[0].first - total;

        vector<int> b(a + 1, 0);
        vector<int> write(n - 1);
        b[0] = 1;

        for (int i = 1; i < book.size(); i++) {
          write[i - 1] = book[i].second;
        }

        for (int i = 0; i < write.size(); i++) {
          for (int j = b.size() - 1; j >= 0; j--) {
            if (b[j]) {
              if (j + write[i] < b.size()) {
                b[j + write[i]] = 1;
              }
            }
          }
        }

        int s = a;

        for (int i = b.size() - 1; i >= 0; i--) {
          if (b[i]) {
            s -= i;
            break;
          }
        }

        for (int i = 0; i < book.size(); i++) {
          ans += book[i].first + book[i].second;
        }
        ans += s;

      } else {
        for (int i = 0; i < book.size(); i++) {
          ans += book[i].first + book[i].second;
        }
      }
    }

    cout << ans << endl;
  }
}

Error: -11

Fixed submission:

#include <algorithm>
#include <climits>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

using namespace std;

typedef pair<int, int> P;
typedef pair<int, P> PP;

struct edge {
  int from, to, cost;
};

int main() {

  int n, m;

  while (true) {

    cin >> n >> m;

    if (!n && !m) {
      break;
    }

    vector<vector<edge>> G(n + 1);
    vector<vector<int>> d(28, vector<int>(n, INT_MAX / 4));

    for (int i = 0; i < m; i++) {
      int f, t, c;
      edge m;
      cin >> f >> t >> c;

      m.from = f;
      m.to = t;
      m.cost = c;

      G[f].push_back(m);

      m.from = t;
      m.to = f;
      m.cost = c;

      G[t].push_back(m);
    }

    priority_queue<PP, vector<PP>, greater<PP>> que;
    d[0][0] = 0;
    que.push(PP(0, P(0, 0)));

    while (!que.empty()) {
      PP p = que.top();
      que.pop();
      int v = p.second.second;
      int a = p.second.first;

      if (d[a][v] < p.first) {
        continue;
      }

      if (v == n - 1) {
        continue;
      }

      for (int i = 0; i < (int)G[v].size(); i++) {
        edge e = G[v][i];

        if (d[(d[a][v] + e.cost) % 28][e.to] > d[a][v] + e.cost) {
          d[(d[a][v] + e.cost) % 28][e.to] = d[a][v] + e.cost;
          que.push(PP(d[(d[a][v] + e.cost) % 28][e.to],
                      P((d[a][v] + e.cost) % 28, e.to)));
        }
      }
    }

    /*for(int i = 0; i < 28; i++) {
            for(int j = 0; j < n; j++) {
                    cout << d[i][j] << "   ";
            }
            cout << endl;
    }*/

    int ans = INT_MAX - 1;
    for (int i = 0; i < 28; i++) {
      if (i % 4 == 0 || i % 7 == 0) {
        ans = min(ans, d[i][n - 1]);
      }
    }

    cout << ans << endl;
  }
}