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

#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define For(i, x, y) for ((i) = (x); i < (y); (i)++)
#define MAX(x, y) ((x) > (y) ? (x) : (y))
#define MIN(x, y) ((x) < (y) ? (x) : (y))
#define sz(a) (sizeof(a))
#define mem(a) (memset((a), 0, sizeof(a)))

#define fop                                                                    \\
  freopen("in.txt", "r", stdin);                                               \\
  freopen("out.txt", "w", stdout);
#define print(x) (printf("%d\\n", x))
#define scan(x) (scanf("%d", &x))
#define printc(x) (printf("%c\\n", x))
#define scanc(x) (scanf("%c", &x))
#define INF 0x7fffffff

using namespace std;
typedef long long ll;
int v[25], m, n;
double a[25], b[25], c[25], d[25][25], ans;
int dfs(int i, int j, double x) {
  int k;
  double kk = 0;
  // printf("%d %d %lf\\n",i,j,x);
  if (j >= m) {

    if (x > ans)
      ans = x;
    return 0;
  }
  if (i > n)
    return 0;
  v[i] = 1;
  For(k, 1, n + 1) if (v[k]) kk += d[i][k];
  dfs(i + 1, j + 1, x + kk);
  v[i] = 0;
  dfs(i + 1, j, x);
  return 0;
}
int main() {
  int i, j;
  scan(n);
  scan(m);
  mem(v);
  For(i, 1, n + 1) { scanf("%lf%lf%lf", &a[i], &b[i], &c[i]); }
  For(i, 1, n + 1) For(j, 1, n + 1) d[i][j] = (a[i] - a[j]) * (a[i] - a[j]) +
                                              (b[i] - b[j]) * (b[i] - b[j]) +
                                              (c[i] - c[j]) * (c[i] - c[j]);
  dfs(1, 0, 0);
  printf("%.10lf\\n", ans);
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<double> VD;
typedef pair<int, int> PII;
#define ll long long
#define PB push_back
#define MP make_pair
#define For(i, n) for (int i = 0; i < (n); ++i)

#define INF 2147483647
#define eps 1e-6
#define maxn 1005

double l[2500], a[2500], b[2500], d[2500][2500], ans[2500];

int main() {
  //  freopen("in.txt","r",stdin);

  int n, m;
  double sum, smin;

  while (scanf("%d%d", &n, &m) != EOF) {
    for (int i = 1; i <= n; i++)
      scanf("%lf%lf%lf", &l[i], &a[i], &b[i]);
    if (m == 1 || m == 0) {
      printf("0.000000\\n");
      continue;
    }
    ans[n] = 0.00;
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        d[i][j] = (l[i] - l[j]) * (l[i] - l[j]) +
                  (a[i] - a[j]) * (a[i] - a[j]) + (b[i] - b[j]) * (b[i] - b[j]);
        ans[n] += d[i][j];
      }
    }
    ans[n] /= 2.0;
    int flag;
    for (int i = n - 1; i > 1; i--) {
      smin = 999999999.0;
      for (int j = 1; j <= n; j++) {
        sum = 0;
        for (int k = 1; k <= n; k++) {
          sum += d[j][k];
        }
        if (smin > sum && sum > eps) {
          smin = sum;
          flag = j;
        }
      }
      ans[i] = ans[i + 1] - smin;
      for (int j = 1; j <= n; j++) {
        d[flag][j] = 0.00;
        d[j][flag] = 0.00;
      }
    }
    printf("%lf\\n", ans[m]);
  }
  return 0;
}