Problem p01417 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <iomanip>
#include <iostream>
#include <vector>

using std::cin;
using std::cout;
using std::endl;

int main(void) {
  int N, M;
  cin >> N >> M;

  std::vector<std::vector<double>> colors(N, std::vector<double>(3));
  for (int i = 0; i < N; i++) {
    cin >> colors[i][0];
    cin >> colors[i][1];
    cin >> colors[i][2];
  }

  std::vector<std::vector<double>> dis(N, std::vector<double>(N));
  for (int i = 0; i < N; i++) {
    for (int j = i; j < N; j++) {
      if (i == j) {
        dis[i][j] = 0;
      } else {
        dis[i][j] = pow(colors[i][0] - colors[j][0], 2) +
                    pow(colors[i][1] - colors[j][1], 2) +
                    pow(colors[i][2] - colors[j][2], 2);
        dis[j][i] = dis[i][j];
      }
    }
  }

  double max = 0;
  double now;
  bool update = false;
  std::vector<int> c(M);
  for (int i = 0; i < M; i++) {
    c[i] = i;
  }

  if (N != M && M != 1) {

    while (c[0] < M) {
      now = 0;
      for (int i = 0; i < M - 1; i++) {
        for (int j = i + 1; j < M; j++) {
          now += dis[c[i]][c[j]];
        }
      }

      if (now > max) {
        max = now;
      }

      update = false;
      int t = 0;
      for (int i = M - 1; !(i > M) && !(update); i--) {
        if (i == 0) {
          c[i]++;
          update = true;
          for (int j = 0; j < t; j++) {
            c[M - 1 - j] = c[M - 1 - t] + t - j;
          }
        } else if (c[i] < N - 1 - (M - i - 1)) {
          c[i]++;
          update = true;
          for (int j = 0; j < t; j++) {
            c[M - 1 - j] = c[M - 1 - t] + t - j;
          }
        } else {
          t++;
        }
      }
    }
  }

  cout << std::setprecision(30) << max << endl;

  return 0;
}

Fixed submission:
#include <cmath>
#include <iomanip>
#include <iostream>
#include <vector>

using std::cin;
using std::cout;
using std::endl;

int main(void) {
  int N, M;
  cin >> N >> M;

  std::vector<std::vector<double>> colors(N, std::vector<double>(3));
  for (int i = 0; i < N; i++) {
    cin >> colors[i][0];
    cin >> colors[i][1];
    cin >> colors[i][2];
  }

  std::vector<std::vector<double>> dis(N, std::vector<double>(N));
  for (int i = 0; i < N; i++) {
    for (int j = i; j < N; j++) {
      if (i == j) {
        dis[i][j] = 0;
      } else {
        dis[i][j] = pow(colors[i][0] - colors[j][0], 2) +
                    pow(colors[i][1] - colors[j][1], 2) +
                    pow(colors[i][2] - colors[j][2], 2);
        dis[j][i] = dis[i][j];
      }
    }
  }

  double max = 0;
  double now;
  bool update = false;
  std::vector<int> c(M);
  for (int i = 0; i < M; i++) {
    c[i] = i;
  }

  if (M > 1) {
    while (!(c[0] > N - M)) {
      now = 0;
      for (int i = 0; i < M - 1; i++) {
        for (int j = i + 1; j < M; j++) {
          now += dis[c[i]][c[j]];
        }
      }

      if (now > max) {
        max = now;
      }

      update = false;
      int t = 0;
      for (int i = M - 1; !(i > M) && !(update); i--) {
        if (i == 0) {
          c[i]++;
          update = true;
          for (int j = 0; j < t; j++) {
            c[M - 1 - j] = c[M - 1 - t] + t - j;
          }
        } else if (c[i] < N - 1 - (M - i - 1)) {
          c[i]++;
          update = true;
          for (int j = 0; j < t; j++) {
            c[M - 1 - j] = c[M - 1 - t] + t - j;
          }
        } else {
          t++;
        }
      }
    }
  }

  cout << std::setprecision(30) << max << endl;

  return 0;
}