Problem p01417 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

template <typename Iterator>
inline bool next_combination(const Iterator first, Iterator k,
                             const Iterator last) {
  if ((first == last) || (first == k) || (last == k))
    return false;
  Iterator itr1 = first;
  Iterator itr2 = last;
  ++itr1;
  if (last == itr1)
    return false;
  itr1 = last;
  --itr1;
  itr1 = k;
  --itr2;
  while (first != itr1) {
    if (*--itr1 < *itr2) {
      Iterator j = k;
      while (!(*itr1 < *j))
        ++j;
      std::iter_swap(itr1, j);
      ++itr1;
      ++j;
      itr2 = k;
      std::rotate(itr1, j, last);
      while (last != j) {
        ++j;
        ++itr2;
      }
      std::rotate(k, itr2, last);
      return true;
    }
  }
  std::rotate(first, k, last);
  return false;
}

int main() {
  int n, m;
  cin >> n >> m;

  vector<vector<double>> v;
  for (int i = 0; i < n; i++) {
    vector<double> vv(3);
    for (int j = 0; j < 3; j++)
      cin >> vv[j];
    v.push_back(vv);
  }

  vector<int> num(n);
  for (int i = 0; i < n; i++)
    num[i] = i;

  double ret = 0;

  if (m == 1) {
    printf("%.20f\n", ret);
    return 0;
  }

  do {
    vector<int> c;
    for (int i = 0; i < m; i++)
      c.push_back(num[i]);
    double sum = 0;
    do {
      sum += pow(v[c[0]][0] - v[c[1]][0], 2) + pow(v[c[0]][1] - v[c[1]][1], 2) +
             pow(v[c[0]][2] - v[c[1]][2], 2);
    } while (next_combination(c.begin(), c.begin() + 2, c.end()));
    ret = max(ret, sum);
  } while (next_combination(num.begin(), num.begin() + m, num.end()));
  printf("%.20f\n", ret);
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

template <typename Iterator>
inline bool next_combination(const Iterator first, Iterator k,
                             const Iterator last) {
  if ((first == last) || (first == k) || (last == k))
    return false;
  Iterator itr1 = first;
  Iterator itr2 = last;
  ++itr1;
  if (last == itr1)
    return false;
  itr1 = last;
  --itr1;
  itr1 = k;
  --itr2;
  while (first != itr1) {
    if (*--itr1 < *itr2) {
      Iterator j = k;
      while (!(*itr1 < *j))
        ++j;
      std::iter_swap(itr1, j);
      ++itr1;
      ++j;
      itr2 = k;
      std::rotate(itr1, j, last);
      while (last != j) {
        ++j;
        ++itr2;
      }
      std::rotate(k, itr2, last);
      return true;
    }
  }
  std::rotate(first, k, last);
  return false;
}

int main() {
  int n, m;
  cin >> n >> m;

  vector<vector<double>> v;
  for (int i = 0; i < n; i++) {
    vector<double> vv(3);
    for (int j = 0; j < 3; j++)
      cin >> vv[j];
    v.push_back(vv);
  }

  vector<int> num(n);
  for (int i = 0; i < n; i++)
    num[i] = i;

  double ret = 0;

  if (m < 2) {
    printf("%.20f\n", ret);
    return 0;
  }

  do {
    vector<int> c;
    for (int i = 0; i < m; i++)
      c.push_back(num[i]);
    double sum = 0;
    do {
      sum += pow(v[c[0]][0] - v[c[1]][0], 2) + pow(v[c[0]][1] - v[c[1]][1], 2) +
             pow(v[c[0]][2] - v[c[1]][2], 2);
    } while (next_combination(c.begin(), c.begin() + 2, c.end()));
    ret = max(ret, sum);
  } while (next_combination(num.begin(), num.begin() + m, num.end()));
  printf("%.20f\n", ret);
}