Problem p02278 (C++)

Buggy submission (status: Runtime Error):
//============================================================================
// Name        : MinCostSort.cpp
// Author      : Ken
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

const int MAX_W = 10000;

void writeVector(vector<int> &v) {
  for (int i = 0; i < v.size(); i++) {
    cout << v.at(i) << "\n";
  }
}

void writeVector(vector<bool> &v) {
  for (int i = 0; i < v.size(); i++) {
    cout << v.at(i) << "\n";
  }
}

int main() {
  int n, input, min_all = MAX_W, total_sum = 0;
  cin >> n;
  vector<int> v, inv_v(MAX_W);
  vector<bool> isIterated(n);

  for (int i = 0; i < isIterated.size(); i++) {
    isIterated.at(i) = false;
  }

  for (int i = 0; i < n; i++) {
    cin >> input;
    v.push_back(input);
    inv_v.at(input) = i;
    if (input < min_all) {
      min_all = input;
    }
  }

  sort(v.begin(), v.end());

  // cout << "v:\n";
  // writeVector(v);
  // cout << "inv_v:\n";
  // writeVector(inv_v);
  // cout << "isIterated:\n";
  // writeVector(isIterated);

  for (int i = 0; i < v.size(); i++) {
    int min_in_loop = MAX_W;
    int sum_loop = 0;
    int pos = i;
    int elem = v.at(i);
    int loop_size = 0;

    if (not isIterated.at(pos)) {
      isIterated.at(pos) = true;

      if (inv_v.at(v.at(pos)) == pos) {
        // cout << "single loop: " << v.at(pos) << "\n";
        continue;
      }

      // cout << "multi loop: ";
      while (true) {
        loop_size++;
        isIterated.at(pos) = true;
        // cout << elem << " ";

        sum_loop += elem;

        if (elem < min_in_loop) {
          min_in_loop = elem;
        }

        pos = inv_v.at(elem);
        elem = v.at(pos);

        if (isIterated.at(pos)) {
          // cout << "sum_loop: " << sum_loop << ", min_in_loop: " <<
          // min_in_loop << " "
          //	 << "loop_size: " << loop_size << "\n";
          // cout << "\n";

          int add = min(min_in_loop * (loop_size - 2),
                        min_in_loop + min_all * (loop_size + 1));

          total_sum += sum_loop + add;

          break;
        }
      }
    }
  }

  cout << total_sum << "\n";
  return 0;
}


Fixed submission:
//============================================================================
// Name        : MinCostSort.cpp
// Author      : Ken
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

const int MAX_W = 10001;

void writeVector(vector<int> &v) {
  for (int i = 0; i < v.size(); i++) {
    cout << v.at(i) << "\n";
  }
}

void writeVector(vector<bool> &v) {
  for (int i = 0; i < v.size(); i++) {
    cout << v.at(i) << "\n";
  }
}

int main() {
  int n, input, min_all = MAX_W, total_sum = 0;
  cin >> n;
  vector<int> v, inv_v(MAX_W);
  vector<bool> isIterated(n);

  for (int i = 0; i < isIterated.size(); i++) {
    isIterated.at(i) = false;
  }

  for (int i = 0; i < n; i++) {
    cin >> input;
    v.push_back(input);
    inv_v.at(input) = i;
    if (input < min_all) {
      min_all = input;
    }
  }

  sort(v.begin(), v.end());

  // cout << "v:\n";
  // writeVector(v);
  // cout << "inv_v:\n";
  // writeVector(inv_v);
  // cout << "isIterated:\n";
  // writeVector(isIterated);

  for (int i = 0; i < v.size(); i++) {
    int min_in_loop = MAX_W;
    int sum_loop = 0;
    int pos = i;
    int elem = v.at(i);
    int loop_size = 0;

    if (not isIterated.at(pos)) {
      isIterated.at(pos) = true;

      if (inv_v.at(v.at(pos)) == pos) {
        // cout << "single loop: " << v.at(pos) << "\n";
        continue;
      }

      // cout << "multi loop: ";
      while (true) {
        loop_size++;
        isIterated.at(pos) = true;
        // cout << elem << " ";

        sum_loop += elem;

        if (elem < min_in_loop) {
          min_in_loop = elem;
        }

        pos = inv_v.at(elem);
        elem = v.at(pos);

        if (isIterated.at(pos)) {
          // cout << "sum_loop: " << sum_loop << ", min_in_loop: " <<
          // min_in_loop << " "
          //	 << "loop_size: " << loop_size << "\n";
          // cout << "\n";

          int add = min(min_in_loop * (loop_size - 2),
                        min_in_loop + min_all * (loop_size + 1));

          total_sum += sum_loop + add;

          break;
        }
      }
    }
  }

  cout << total_sum << "\n";
  return 0;
}