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

#include <iostream>
#include <limits.h>
using namespace std;

int main(void) {
  int n, now = 0, flag[101];
  long ans = 0, an[100][100], min[100];

  cin >> n;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> an[i][j];
    }
    min[i] = LONG_MAX;
    flag[i] = 0;
  }

  /* ???????則?????????即?????属????????? */
  do {
    flag[now] = 1;
    flag[100] = 0;
    /*
                    cout << "\\n??? " << now << " ????則???????????????????" <<
       endl; cout << "?????贈?????? : "; for(int i = 0; i < n; i++) { if(flag[i]
       == 1) { cout << i << " ";
                            }
                    }
                    cout << endl;
                    cout << "?????即???????属?????????則" << endl;
                    for(int i = 0; i < n; i++) {
                            if(flag[i] == 0) {
                                    cout << "??? " << i << " : " << min[i] <<
       endl;
                            }
                    }
    */
    /* min?????卒??属 */
    for (int j = 0; j < n; j++) {
      if (an[now][j] != -1 && an[now][j] < min[j] && flag[j] == 0) {
        /*				cout << "??? " << j <<"
         * ???????属?????????卒??属??????????????? : "<< min[j] << " ??? " <<
         * an[now][j] << endl; */
        min[j] = an[now][j];
      }
    }
    /*
                    cout << "?????即???????属?????????則" << endl;
                    for(int i = 0; i < n; i++) {
                            if(flag[i] == 0) {
                                    cout << "??? " << i << " : " << min[i] <<
       endl;
                            }
                    }
    */
    /* ?測??????蔵?卒蔵???min????属???? */
    long key = LONG_MAX, key_j;

    for (int j = 0; j < n; j++) {
      if (flag[j] == 0 && min[j] < key) {
        key = min[j];
        key_j = j;
      }
    }
    /*
                    cout << "?則???????????賊??????????????????? : ??? " <<
       key_j << endl;
    */
    /* ?則?????????? */
    now = key_j;
    ans += key;

    /* ??????????????贈????????? */
    for (int i = 0; i < n; i++) {
      if (flag[i] == 1) {
        flag[100]++;
      }
    }

  } while (flag[100] != n - 1);

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <limits.h>
using namespace std;

int main(void) {
  int n, now = 0, flag[101];
  long ans = 0, an[100][100], min[100];

  cin >> n;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> an[i][j];
    }
    min[i] = LONG_MAX - 1;
    flag[i] = 0;
  }

  /* ???????則?????????即?????属????????? */
  do {
    flag[now] = 1;
    flag[100] = 0;
    /*
                    cout << "\\n??? " << now << " ????則???????????????????" <<
       endl; cout << "?????贈?????? : "; for(int i = 0; i < n; i++) { if(flag[i]
       == 1) { cout << i << " ";
                            }
                    }
                    cout << endl;
                    cout << "?????即???????属?????????則" << endl;
                    for(int i = 0; i < n; i++) {
                            if(flag[i] == 0) {
                                    cout << "??? " << i << " : " << min[i] <<
       endl;
                            }
                    }
    */
    /* min?????卒??属 */
    for (int j = 0; j < n; j++) {
      if (an[now][j] != -1 && an[now][j] < min[j] && flag[j] == 0) {
        /*				cout << "??? " << j <<"
         * ???????属?????????卒??属??????????????? : "<< min[j] << " ??? " <<
         * an[now][j] << endl; */
        min[j] = an[now][j];
      }
    }
    /*
                    cout << "?????即???????属?????????則" << endl;
                    for(int i = 0; i < n; i++) {
                            if(flag[i] == 0) {
                                    cout << "??? " << i << " : " << min[i] <<
       endl;
                            }
                    }
    */
    /* ?測??????蔵?卒蔵???min????属???? */
    int key = LONG_MAX - 1, key_j;

    for (int j = 0; j < n; j++) {
      if (flag[j] == 0 && min[j] < key) {
        key = min[j];
        key_j = j;
      }
    }
    /*
                    cout << "?則???????????賊??????????????????? : ??? " <<
       key_j << endl;
    */
    /* ?則?????????? */
    now = key_j;
    ans += key;

    /* ??????????????贈????????? */
    for (int i = 0; i < n; i++) {
      if (flag[i] == 1) {
        flag[100]++;
      }
    }

  } while (flag[100] != n - 1);

  cout << ans << endl;

  return 0;
}