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

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int d[50][50], p[50][50], M, n, m;
vector<int> dp[50][50];
int g[50][50];
bool Dainari(vector<int> a, vector<int> b) {
  if (a.size() < b.size())
    return true;
  if (b.size() > a.size())
    return false;
  for (int i = 0; i < a.size(); i++) {
    if (a[i] < b[i])
      return true;
    if (a[i] > b[i])
      return false;
  }
  return false;
}
int main() {
  cin >> n >> m;
  int S = 0;
  for (int j = 0; j < n; j++) {
    for (int i = 0; i < m; i++) {
      cin >> d[i][j];
      M = min(M, d[i][j]);
    }
  }
  for (int i = 0; i < m; i++) {
    M = 1 << 30;
    for (int j = 0; j < n; j++) {
      M = min(d[i][j], M);
    }
    for (int j = 0; j < n; j++) {
      if (d[i][j] == M)
        p[i][j] = 1;
    }
    S += M;
  }
  for (int i = 0; i < 50; i++) {
    for (int j = 0; j < 50; j++)
      g[i][j] = 1 << 30;
  }
  for (int i = 0; i < n; i++) {
    if (p[0][i] == 1) {
      dp[0][i].push_back(i);
      g[0][i] = 0;
    }
  }
  for (int i = 1; i < m; i++) {
    for (int j = 0; j < n; j++) {
      if (p[i][j] == 0)
        continue;
      for (int k = 0; k < n; k++) {
        if (p[i - 1][k] == 0)
          continue;
        vector<int> D = dp[i - 1][k];
        D.push_back(j);
        int L = 1;
        if (j == k)
          L = 0;
        if (g[i - 1][k] + L < g[i][j]) {
          g[i][j] = g[i - 1][k] + L;
          dp[i][j] = D;
        } else if (g[i - 1][k] + L == g[i][j] && Dainari(D, dp[i][j]))
          dp[i][j] = D;
      }
    }
  }
  vector<pair<int, vector<int>>> X;
  for (int i = 0; i < n; i++) {
    X.push_back(make_pair(g[m - 1][i], dp[m - 1][i]));
  }
  sort(X.begin(), X.end());
  vector<int> Y = X[0].second;
  cout << S << ' ' << X[0].first << endl;
  for (int i = 0; i < m; i++)
    cout << Y[i] + 1 << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int d[50][50], p[50][50], M, n, m;
vector<int> dp[50][50];
int g[50][50];
int main() {
  cin >> n >> m;
  int S = 0;
  for (int j = 0; j < n; j++) {
    for (int i = 0; i < m; i++) {
      cin >> d[i][j];
      M = min(M, d[i][j]);
    }
  }
  for (int i = 0; i < m; i++) {
    M = 1 << 30;
    for (int j = 0; j < n; j++) {
      M = min(d[i][j], M);
    }
    for (int j = 0; j < n; j++) {
      if (d[i][j] == M)
        p[i][j] = 1;
    }
    S += M;
  }
  for (int i = 0; i < 50; i++) {
    for (int j = 0; j < 50; j++)
      g[i][j] = 1 << 30;
  }
  for (int i = 0; i < n; i++) {
    if (p[0][i] == 1)
      dp[0][i].push_back(i);
    g[0][i] = 0;
  }
  for (int i = 1; i < m; i++) {
    for (int j = 0; j < n; j++) {
      if (p[i][j] == 0)
        continue;
      for (int k = 0; k < n; k++) {
        if (p[i - 1][k] == 0)
          continue;
        vector<int> D = dp[i - 1][k];
        D.push_back(j);
        int L = 1;
        if (j == k)
          L = 0;
        if (g[i - 1][k] + L < g[i][j]) {
          g[i][j] = g[i - 1][k] + L;
          dp[i][j] = D;
        } else if (g[i - 1][k] + L < g[i][j] && D < dp[i][j])
          dp[i][j] = D;
      }
    }
  }
  vector<pair<int, vector<int>>> X;
  for (int i = 0; i < n; i++)
    X.push_back(make_pair(g[m - 1][i], dp[m - 1][i]));
  sort(X.begin(), X.end());
  vector<int> Y = X[0].second;
  cout << S << ' ' << X[0].first << endl;
  for (int i = 0; i < m; i++)
    cout << Y[i] + 1 << endl;
  return 0;
}