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

#include <algorithm>
#include <cstdio>
using std::printf;
using std::scanf;

const int kMaxN = 1000;
const int kMaxWi = 10000;

int BinarySearch(int A[], int n, int key) {
  int left = 0, right = n, mid;
  while (left < right) {
    mid = (left + right) / 2;
    if (A[mid] == key) {
      return mid;
    }
    if (key < A[mid]) {
      right = mid;
    }
    if (A[mid] < key) {
      left = mid + 1;
    }
  }
  return -1;
}

// w[i]?????????????????????????????????cycle???????????????????????????remain????????£?????????????¨????
// ????????????cycle?????????
int MakeCycle(int w[], int i, int n, int sorted[], int remain[], int cycle[]) {
  int j = 0;
  cycle[0] = -1; //?????????while?????????????????????
  while (w[i] != cycle[0]) {
    cycle[j] = w[i];
    ++j;
    remain[i] = 0;
    i = BinarySearch(sorted, n, w[i]);
  }
  return j;
}

int CompareCosts(int cycle[], int cycle_n, int cycle_min, int w_min) {
  if (cycle_n == 1) {
    return 0;
  } else if (cycle_n == 2) {
    return cycle[0] + cycle[1];
  } else {
    int cycle_sum, i, cost1, cost2;
    cycle_sum = 0;
    for (i = 0; i < cycle_n; ++i) {
      cycle_sum += cycle[i];
    }
    cost1 = cycle_sum + (cycle_n - 2) * cycle_min;
    cost2 = cycle_sum + cycle_min + (cycle_n + 1) * w_min;
    return std::min(cost1, cost2);
  }
}

int main() {
  int n, i, j, cycle_n, cycle_min, w_min, cnt;
  scanf("%d", &n);
  int w[n], sorted[n], remain[n], cycle[n];
  for (i = 0; i < n; ++i) {
    scanf("%d", &w[i]);
  }
  w_min = kMaxWi;
  for (i = 0; i < n; ++i) {
    w_min = std::min(w_min, w[i]);
  }
  for (i = 0; i < n; ++i) {
    sorted[i] = w[i];
  }
  std::sort(sorted, sorted + n);
  for (i = 0; i < n; ++i) {
    remain[i] = 1;
  }
  cnt = 0;
  for (i = 0; i < n; ++i) {
    if (remain[i]) {
      cycle_n = MakeCycle(w, i, n, sorted, remain, cycle);
      cycle_min = kMaxWi;
      for (j = 0; j < cycle_n; ++j) {
        cycle_min = std::min(cycle_min, cycle[j]);
      }
      cnt += CompareCosts(cycle, cycle_n, cycle_min, w_min);
    }
  }
  printf("%d\\n", cnt);
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
using std::printf;
using std::scanf;

const int kMaxN = 1000;
const int kMaxWi = 10000;

// w[i]?????????????????????????????????cycle???????????????????????????remain????????£?????????????¨????
// ????????????cycle?????????
int MakeCycle(int w[], int i, int remain[], int cycle[]) {
  int j = 0;
  cycle[0] = -1; //?????????while?????????????????????
  while (w[i] != cycle[0]) {
    cycle[j] = w[i];
    ++j;
    remain[i] = 0;
    i = w[i] - 1;
  }
  return j;
}

int CompareCosts(int cycle[], int cycle_n, int cycle_min, int w_min) {
  if (cycle_n == 1) {
    return 0;
  } else if (cycle_n == 2) {
    return cycle[0] + cycle[1];
  } else {
    int cycle_sum, i, cost1, cost2;
    cycle_sum = 0;
    for (i = 0; i < cycle_n; ++i) {
      cycle_sum += cycle[i];
    }
    cost1 = cycle_sum + (cycle_n - 2) * cycle_min;
    cost2 = cycle_sum + cycle_min + (cycle_n + 1) * w_min;
    return std::min(cost1, cost2);
  }
}

int main() {
  int n, i, j, cycle_n, cycle_min, w_min, cnt;
  scanf("%d", &n);
  int w[n], remain[n], cycle[n];
  for (i = 0; i < n; ++i) {
    scanf("%d", &w[i]);
  }
  w_min = kMaxWi;
  for (i = 0; i < n; ++i) {
    w_min = std::min(w_min, w[i]);
  }
  for (i = 0; i < n; ++i) {
    remain[i] = 1;
  }
  cnt = 0;
  for (i = 0; i < n; ++i) {
    if (remain[i]) {
      cycle_n = MakeCycle(w, i, remain, cycle);
      cycle_min = kMaxWi;
      for (j = 0; j < cycle_n; ++j) {
        cycle_min = std::min(cycle_min, cycle[j]);
      }
      cnt += CompareCosts(cycle, cycle_n, cycle_min, w_min);
    }
  }
  printf("%d\\n", cnt);
  return 0;
}