alexjercan/bugnet huggingface dataset:
Problem p01417 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
using namespace std;

double a[20], b[20], c[20];
double dist[20][32];
double squ(double x) { return x * x; }
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; i++) {
    scanf("%lf %lf %lf", a + i, b + i, c + i);
  }
  if (m == 0) {
    printf("%.5f\\n", 0.);
    return 0;
  }
  for (int i = 0; i < n; i++)
    for (int j = i + 1; j < n; j++)
      dist[i][j] = squ(a[i] - a[j]) + squ(b[i] - b[j]) + squ(c[i] - c[j]);
  double ans = 0;
  int comb = (1 << m) - 1;
  while (comb < 1 << n) {
    double sum = 0;
    for (int i = 0; i < n; i++) {
      if ((comb >> i & 1) ^ 1)
        continue;
      for (int j = i + 1; j < n; j++) {
        if (comb >> j & 1)
          sum += dist[i][j];
      }
    }
    ans = max(ans, sum);
    int x = comb & -comb, y = comb + x;
    comb = ((comb & ~y) / x >> 1) | y;
  }
  printf("%.5f\\n", ans);
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cstdio>
using namespace std;

double a[20], b[20], c[20];
double dist[20][32];
double squ(double x) { return x * x; }
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; i++) {
    scanf("%lf %lf %lf", a + i, b + i, c + i);
  }
  for (int i = 0; i < n; i++)
    for (int j = i + 1; j < n; j++)
      dist[i][j] = squ(a[i] - a[j]) + squ(b[i] - b[j]) + squ(c[i] - c[j]);
  double ans = 0;
  int comb = (1 << m) - 1;
  while (comb < 1 << n) {
    double sum = 0;
    for (int i = 0; i < n; i++) {
      if ((comb >> i & 1) ^ 1)
        continue;
      for (int j = i + 1; j < n; j++) {
        if (comb >> j & 1)
          sum += dist[i][j];
      }
    }
    ans = max(ans, sum);
    int x = comb & -comb, y = comb + x;
    comb = ((comb & ~y) / x >> 1) | y;
  }
  printf("%.5f\\n", ans);
  return 0;
}
Problem p01417 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>

#define INF 0x7fffffff
#define MOD 10000007
#define N 30

using namespace std;

struct node {
  double a, b, c;
} color[N];
int ans[N];
int n, m;
double Min;

double squr(double a, double b, double c) { return a * a + b * b + c * c; }
void dfs(int now, int tot) {
  if (tot == m) {
    double cnt = 0.0;
    for (int i = 1; i <= m; i++) {
      for (int j = i + 1; j <= m; j++) {
        double aa = fabs(color[ans[i]].a - color[ans[j]].a);
        double bb = fabs(color[ans[i]].b - color[ans[j]].b);
        double cc = fabs(color[ans[i]].c - color[ans[j]].c);
        cnt = cnt + squr(aa, bb, cc);
      }
    }
    if (cnt > Min)
      Min = cnt;
    return;
  }
  if (m - tot > n - now + 1)
    return;
  for (int i = now; i <= n; i++) {
    ans[tot + 1] = i;
    dfs(i + 1, tot + 1);
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%lf%lf%lf", &color[i].a, &color[i].b, &color[i].c);
  }
  memset(ans, 0, sizeof(ans));
  Min = 0.0;
  dfs(1, 0);
  printf("%lf\\n", Min);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <stdio.h>
#include <string.h>
using namespace std;
double a[20][4];
double b[20][20];
int c[21];
double s[10000];
int n, m;
int sss;
int ii, jj, kk;
bool cmp(double a, double b) { return a > b; }
void vs(int v) {
  if (v > m) {
    s[sss] = 0;
    for (ii = 1; ii <= v - 2; ii++)
      for (jj = ii + 1; jj <= v - 1; jj++)
        s[sss] += b[c[ii]][c[jj]];
    sss++;
    return;
  }
  int i, j, k;
  for (i = c[v - 1] + 1; i < n; i++) {
    c[v] = i;
    vs(v + 1);
  }
  return;
}

int main() {
  int i, j, k;
  scanf("%d%d", &n, &m);
  for (i = 0; i < n; i++)
    scanf("%lf%lf%lf", &a[i][1], &a[i][2], &a[i][3]);
  for (i = 0; i < n - 1; i++)
    for (j = i + 1; j < n; j++) {
      b[i][j] = 0;
      for (k = 1; k <= 3; k++)
        b[i][j] += (a[i][k] - a[j][k]) * (a[i][k] - a[j][k]);
      b[j][i] = b[i][j];
    }
  c[0] = -1;
  vs(1);
  sort(s, s + sss - 1, cmp);
  printf("%lf", s[0]);
}
Problem p01417 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <vector>

using namespace std;

typedef vector<int> Vec;

inline double distance(double dl, double da, double db) {
  return dl * dl + da * da + db * db;
}

int main() {
  int n, m, r;
  bool reverse;
  double l[20], a[20], b[20];
  double max_sum = 0;
  queue<Vec> vq;
  queue<int> iq;
  cin >> n >> m;
  for (int i = 0; i < n; ++i) {
    cin >> l[i] >> a[i] >> b[i];
  }
  r = (m <= n / 2 ? m : n - m);
  reverse = (not(m <= n / 2));
  vq.push(Vec());
  iq.push(0);
  while (not vq.empty()) {
    Vec v = vq.front();
    int begin = iq.front();
    vq.pop();
    iq.pop();
    if (int(v.size()) == r) {
      double s = 0;
      if (reverse) {
        Vec rv;
        set<int> is;
        for (size_t i = 0; i < v.size(); ++i) {
          is.insert(v[i]);
        }
        for (int i = 0; i < n; ++i) {
          if (is.count(i) == 0) {
            rv.push_back(i);
          }
        }
        v = rv;
      }
      for (int i = 0; i < m; ++i) {
        for (int j = i + 1; j < m; ++j) {
          s +=
              distance(l[v[i]] - l[v[j]], a[v[i]] - a[v[j]], b[v[i]] - b[v[j]]);
        }
      }
      max_sum = max(max_sum, s);
    } else if (begin < n) {
      vq.push(v);
      iq.push(begin + 1);
      v.push_back(begin);
      vq.push(v);
      iq.push(begin + 1);
    }
  }
  cout.precision(20);
  cout << fixed << max_sum << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <vector>

using namespace std;

typedef vector<int> Vec;

inline double distance(double dl, double da, double db) {
  return dl * dl + da * da + db * db;
}

int main() {
  int n, m, r;
  bool reverse;
  double l[20], a[20], b[20];
  double max_sum = 0;
  queue<Vec> vq;
  queue<int> iq;
  cin >> n >> m;
  for (int i = 0; i < n; ++i) {
    cin >> l[i] >> a[i] >> b[i];
  }
  r = (m <= n / 2 ? m : n - m);
  reverse = (not m <= n / 2);
  vq.push(Vec());
  iq.push(0);
  while (not vq.empty()) {
    Vec v = vq.front();
    int begin = iq.front();
    vq.pop();
    iq.pop();
    if (int(v.size()) == r) {
      double s = 0;
      if (reverse) {
        Vec rv;
        set<int> is;
        for (size_t i = 0; i < v.size(); ++i) {
          is.insert(v[i]);
        }
        for (int i = 0; i < n; ++i) {
          if (is.count(i) == 0) {
            rv.push_back(i);
          }
        }
        v = rv;
      }
      if (int(v.size()) != m) {
        int *a = new int[1000000000];
        a[100] = 1;
      }
      for (int i = 0; i < m; ++i) {
        for (int j = i + 1; j < m; ++j) {
          s +=
              distance(l[v[i]] - l[v[j]], a[v[i]] - a[v[j]], b[v[i]] - b[v[j]]);
        }
      }
      max_sum = max(max_sum, s);
    } else if (begin < n) {
      vq.push(v);
      iq.push(begin + 1);
      v.push_back(begin);
      vq.push(v);
      iq.push(begin + 1);
    }
  }
  cout.precision(20);
  cout << fixed << max_sum << endl;
  return 0;
}
Problem p01417 (C++) — buggy submission, verdict: Time Limit Exceeded

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

// #define ll long long
#define vvi vector<vector<int>>
#define All(X) X.begin(), X.end()
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define pb push_back
typedef long double ld;

ld calcds(ld l1, ld a1, ld b1, ld l2, ld a2, ld b2) {
  return (l1 - l2) * (l1 - l2) + (a1 - a2) * (a1 - a2) + (b1 - b2) * (b1 - b2);
}
int main() {
  ld ans = 0.0;
  int m, n;
  cin >> n >> m;
  vector<long double> l, a, b;
  REP(i, n) {
    long double ll, aa, bb;
    cin >> ll >> aa >> bb;
    l.pb(ll);
    a.pb(aa);
    b.pb(bb);
  }
  int tmp[1000];
  int cnt = 0;
  REP(i, n) tmp[i] = 1;
  REP(i, n - m) tmp[i] = 0;
  do {
    cnt++;
    ld sum = 0.0;
    REP(i, n) {
      FOR(j, i, n) {
        if (tmp[i] == 1 && tmp[j] == 1) {
          sum += calcds(l[i], a[i], b[i], l[j], a[j], b[j]);
        }
      }
    }
    ans = max(ans, sum);
  } while (next_permutation(tmp, tmp + n));
  cout << fixed << setprecision(25) << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
// #include <stack>
// #include <queue>
// #include <map>
#include <cmath>
// #include <string>
// #include <sstream>
#include <iomanip>
// #include <complex>
// cout << fixed << setprecision(5) << tmp << endl;
using namespace std;

#define ll long long
#define vvi vector<vector<int>>
#define All(X) X.begin(), X.end()
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define pb push_back
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
typedef long double ld;

ld calcds(ld l1, ld a1, ld b1, ld l2, ld a2, ld b2) {
  return (l1 - l2) * (l1 - l2) + (a1 - a2) * (a1 - a2) + (b1 - b2) * (b1 - b2);
}
int main() {
  ld ans = 0.0;
  int m, n;
  cin >> n >> m;
  vector<long double> l, a, b;
  REP(i, n) {
    long double lll, aa, bb;
    cin >> lll >> aa >> bb;
    l.pb(lll);
    a.pb(aa);
    b.pb(bb);
  }
  vector<int> tmp;
  REP(i, n) tmp.push_back(i);
  do {
    // tmp[0]-tmp[m-1]
    ld sum = 0.0;
    FOR(i, 1, m) {
      sum += calcds(l[tmp[i]], a[tmp[i]], b[tmp[i]], l[tmp[i - 1]],
                    a[tmp[i - 1]], b[tmp[i - 1]]);
    }
    if (m != 2)
      sum += calcds(l[tmp[m - 1]], a[tmp[m - 1]], b[tmp[m - 1]], l[tmp[0]],
                    a[tmp[0]], b[tmp[0]]);
    ans = max(ans, sum);
  } while (next_permutation(All(tmp)));

  cout << fixed << setprecision(25) << ans << endl;

  return 0;
}
Problem p01417 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<double>> lab(n, vector<double>(3, 0));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 3; j++)
      cin >> lab[i][j];
  }
  if (m == 0) {
    cout << 0 << endl;
    return 0;
  }
  double res = 0;
  unsigned int comb = (1 << m) - 1;
  while (comb < 1 << n) {
    vector<int> index;
    for (int i = 0; i < n; i++) {
      if (comb & (1 << i))
        index.push_back(i);
    }
    double sum = 0;
    for (int i = 0; i < m; i++)
      for (int j = i + 1; j < m; j++) {
        for (int r = 0; r < 3; r++) {
          double dis = lab[index[i]][r] - lab[index[j]][r];
          dis *= dis;
          sum += dis;
        }
      }
    res = max(res, sum);
    unsigned int x = comb & -comb, y = comb + x;
    comb = (((comb & ~y) / x) >> 1) | y;
  }
  cout << fixed << setprecision(10);
  cout << res << endl;
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<double>> lab(n, vector<double>(3, 0));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 3; j++)
      cin >> lab[i][j];
  }
  double res = 0;
  unsigned int comb = (1 << m) - 1;
  while (comb < 1 << n) {
    vector<int> index;
    for (int i = 0; i < n; i++) {
      if (comb & (1 << i))
        index.push_back(i);
    }
    if (index.size() != m) {
      cout << "ERROR" << endl;
      return 1;
    }
    double sum = 0;
    for (int i = 0; i < m; i++)
      for (int j = i + 1; j < m; j++) {
        for (int r = 0; r < 3; r++) {
          double dis = lab[index[i]][r] - lab[index[j]][r];
          dis *= dis;
          sum += dis;
        }
      }
    res = max(res, sum);
    unsigned int x = comb & -comb, y = comb + x;
    comb = (((comb & ~y) / x) >> 1) | y;
  }
  cout << fixed << setprecision(10);
  cout << res << endl;
  return 0;
}
Problem p01417 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;

int N, M;
double L[20][3];

double culc(int x, int y) {
  double v1 = (L[x][0] - L[y][0]) * (L[x][0] - L[y][0]);
  double v2 = (L[x][1] - L[y][1]) * (L[x][1] - L[y][1]);
  double v3 = (L[x][2] - L[y][2]) * (L[x][2] - L[y][2]);
  return v1 + v2 + v3;
}

double getSum(int b) {
  double ret = 0;
  for (int i = 0; i < N; i++) {
    if ((b >> i & 1) == 0)
      continue;
    for (int j = i + 1; j < N; j++) {
      if ((b >> j & 1) == 0)
        continue;
      ret += culc(i, j);
    }
  }

  return ret;
}

void solve() {
  double ma = 0;
  int comb = (1 << M) - 1;
  /*
  while(comb<1<<N){
      ma=max(ma,getSum(comb));
      int x=comb&-comb,y=comb+x;
      comb=((comb&~y)/x>>1)|y;
  }
  */
  for (int i = 0; i < (1 << N); i++) {
    int cnt = 0;
    for (int j = 0; j < N; j++)
      if ((i >> j & 1) == 1)
        cnt++;
    if (cnt != M)
      continue;
    ma = max(ma, getSum(i));
  }
  printf("%lf\\n", ma);
}
int main() {
  scanf("%d%d", &N, &M);

  for (int i = 0; i < N; i++)
    for (int j = 0; j < 3; j++)
      scanf("%lf", &L[i][j]);

  solve();
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;

int N, M;
double L[20][3];

double culc(int x, int y) {
  double v1 = (L[x][0] - L[y][0]) * (L[x][0] - L[y][0]);
  double v2 = (L[x][1] - L[y][1]) * (L[x][1] - L[y][1]);
  double v3 = (L[x][2] - L[y][2]) * (L[x][2] - L[y][2]);
  return v1 + v2 + v3;
}

double getSum(int b) {
  double ret = 0;
  for (int i = 0; i < N; i++) {
    if ((b >> i & 1) == 0)
      continue;
    for (int j = i + 1; j < N; j++) {
      if ((b >> j & 1) == 0)
        continue;
      ret += culc(i, j);
    }
  }

  return ret;
}

void solve() {
  double ma = 0;
  int comb = (1 << M) - 1;
  while (comb < 1 << N) {
    ma = max(ma, getSum(comb));
    int x = comb & -comb, y = comb + x;
    comb = ((comb & ~y) / x >> 1) | y;
  }

  printf("%f\\n", ma);
}
int main() {
  scanf("%d%d", &N, &M);

  for (int i = 0; i < N; i++)
    for (int j = 0; j < 3; j++)
      scanf("%lf", &L[i][j]);

  solve();
  return 0;
}
Problem p01417 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

double sq(double a) { return a * a; }

int N, M;
double L[22], a[22], b[22];

double dfs(vector<int> &idx, int n) {
  if (idx.size() == M) {
    double ret = 0.0;
    for (int i = 0; i < idx.size(); i++) {
      for (int j = i + 1; j < idx.size(); j++) {
        ret += sq(L[idx[i]] - L[idx[j]]) + sq(a[idx[i]] - a[idx[j]]) +
               sq(b[idx[i]] - b[idx[j]]);
      }
    }
    return ret;
  }
  if (n >= N)
    return 0.0;
  double res = 0.0;
  vector<int> tmp1 = idx;
  vector<int> tmp2 = idx;
  tmp1.push_back(n);
  res = max(dfs(tmp1, n + 1), dfs(tmp2, n + 1));
  return res;
}

int main() {
  cin >> N >> M;
  for (int i = 0; i < N; i++)
    cin >> L[i] >> a[i] >> b[i];
  vector<int> idx;
  printf("%.10f\\n", dfs(idx, 0));
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

double sq(double a) { return a * a; }

int N, M;
double L[22], a[22], b[22];

double dfs(vector<int> &idx) {
  if (idx.size() == M) {
    double ret = 0.0;
    for (int i = 0; i < idx.size(); i++) {
      for (int j = i + 1; j < idx.size(); j++) {
        ret += sq(L[idx[i]] - L[idx[j]]) + sq(a[idx[i]] - a[idx[j]]) +
               sq(b[idx[i]] - b[idx[j]]);
      }
    }
    return ret;
  }
  double res = 0.0;
  for (int i = 0; i < N; i++) {
    vector<int> tmp = idx;
    tmp.push_back(i);
    res = max(res, dfs(tmp));
  }
  return res;
}

int main() {
  cin >> N >> M;
  for (int i = 0; i < N; i++)
    cin >> L[i] >> a[i] >> b[i];
  vector<int> idx;
  printf("%.10f\\n", dfs(idx));
  return 0;
}
Problem p01417 (C++) — buggy submission, verdict: Runtime Error

#include <iomanip>
#include <iostream>
using namespace std;

int nr, alese, sol[25];
double total, culoare[25][10], maxim;

void back(int x) {
  if (x == alese + 1) {
    total = 0;
    for (int i = 1; i <= alese; i++) {
      for (int j = i + 1; j <= alese; j++) {
        for (x = 1; x <= 3; x++) {
          total += (culoare[sol[i]][x] - culoare[sol[j]][x]) *
                   (culoare[sol[i]][x] - culoare[sol[j]][x]);
        }
      }
    }
    maxim = max(maxim, total);
  } else {
    for (int i = sol[x - 1] + 1; i <= nr - (alese - x); i++) {
      sol[x] = i;
      back(x + 1);
      sol[x] = 0;
    }
  }
}

int main() {
  cin >> nr >> alese;
  for (int i = 1; i <= nr; i++) {
    for (int j = 1; j <= 3; j++)
      cin >> culoare[i][j];
  }

  back(1);
  cout << fixed;
  cout << setprecision(10);
  cout << maxim;
}


Error: WA

Fixed submission:

#include <iomanip>
#include <iostream>

using namespace std;

struct punct {
  double x;
  double y;
  double z;
} v[20];
int n, m;
double a[20][20], ma = 0;
int b[20];
int x[20];
void be(int k) {
  int q = 0;
  for (int i = 1; i <= k; i++)
    if (x[i] > q)
      q = x[i];
  if (k == m + 1) {
    double s = 0;
    for (int i = 1; i < m; i++)
      for (int j = i + 1; j <= m; j++)
        s += a[x[i]][x[j]];
    if (s > ma)
      ma = s;
  } else
    for (int i = 1; i <= n; i++)
      if (!b[i] && i > q) {
        x[k] = i;
        b[i] = 1;
        be(k + 1);
        b[i] = 0;
      }
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    cin >> v[i].x >> v[i].y >> v[i].z;
  for (int i = 1; i < n; i++)
    for (int j = i + 1; j <= n; j++)
      a[i][j] = a[j][i] =
          (double)(v[i].x - v[j].x) * (double)(v[i].x - v[j].x) +
          (double)(v[i].y - v[j].y) * (double)(v[i].y - v[j].y) +
          (double)(v[i].z - v[j].z) * (double)(v[i].z - v[j].z);
  be(1);
  cout << setprecision(20) << ma;
  return 0;
}
Problem p01417 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

const int N = 20;

double dis[N][N], a[N], b[N], c[N];
int num[1 << N] = {0};

int main() {
  int n, m;
  for (int i = 0; i < (1 << N); i++) {
    for (int j = 0; j < N; j++) {
      if (i & (1 << j))
        num[i]++;
    }
  }
  while (cin >> n >> m) {
    for (int i = 0; i < n; i++)
      scanf("%lf%lf%lf", &a[i], &b[i], &c[i]);
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        dis[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]);
      }
    }
    double res = 0;
    for (int i = 0; i < (1 << n); i++) {
      if (num[i] != m)
        continue;
      double sum = 0;
      for (int j = 0; j < n; j++) {
        if (((1 << j) & i) == 0)
          continue;
        for (int k = j + 1; k < n; k++) {
          if (((1 << k) & i) == 0)
            continue;
          sum += dis[j][k];
        }
      }
      res = max(res, sum);
    }
    printf("%.10f\\n", res);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define PI acos(-1)
#define LL long long
#define inf (1 << 30)
#define modn 1000000007
#define maxn 20 + 10
#define Mod 2
#define type 1
#define rep(a, b) for (i = a; i <= b; i++)
#define eps 1e-7
#define zero(x) fabs(x) < eps
#define equal(x, y) zero(x - y)
#define shift(x) (1 << x)
#define lowbit(x) (x & (-x))
#define del(S, i, j) (S ^ (1 << i) ^ (1 << j))
#define jiao(S, i) (S & (1 << i))
#define bing(S, i) (S | (1 << i))
#define bu(S, i) (S ^ (1 << i))
#define ALL_BITS (1 << n) - 1
#define BuJi(A) ALL_BITS ^ A
#define wei_del(A, bit) (A |= (1 << bit))
#define rep_all for (s = 1; s <= ALL_BITS; s++)
#define rep_zi for (s0 = s; s0; s0 = (s0 - 1) & s)
using namespace std;
double ans;
int n, m;
int vis[30];
double a[30], b[30], c[30];
double G[30][30];
int da[30];
void dfs(int step) {
  int i, j;
  if (step == m + 1) {
    double tmp = 0.0;
    for (i = 1; i <= m; i++)
      for (j = i + 1; j <= m; j++)
        tmp += G[da[i]][da[j]];
    ans = max(ans, tmp);
  } else
    for (i = 1; i <= n; i++)
      if (!vis[i]) {
        vis[i] = 1;
        da[step + 1] = i;
        dfs(step + 1);
        vis[i] = 0;
      }
}
int main() {
  int i, j;
  scanf("%d%d", &n, &m);
  for (i = 1; i <= n; i++) {
    scanf("%lf%lf%lf", &a[i], &b[i], &c[i]);
  }
  for (i = 1; i <= n; i++)
    for (j = i + 1; j <= n; j++)
      G[i][j] = G[j][i] = (a[i] - a[j]) * (a[i] - a[j]) +
                          (b[i] - b[j]) * (b[i] - b[j]) +
                          (c[i] - c[j]) * (c[i] - c[j]);
  memset(vis, 0, sizeof(vis));
  ans = 0.0;
  dfs(0);
  printf("%lf\\n", ans);
  return 0;
}
Problem p01417 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

const int MAXN = 23;
int n, m;
double dis[MAXN][MAXN];
double ans, x[MAXN], y[MAXN], z[MAXN];
bool use[MAXN];
int arr[MAXN];

void dfs(int dp) {
  if (dp == m + 1) {
    double sum = 0;
    for (int i = 1; i < dp; i++)
      for (int j = i + 1; j < dp; j++)
        sum += dis[arr[i]][arr[j]];
    ans = max(ans, sum);
    return;
  }
  for (int i = arr[dp - 1] + 1; i <= n; i++)
    if (!use[i]) {
      use[i] = 1;
      arr[dp] = i;
      dfs(dp + 1);
      use[i] = 0;
    }
}

double caldis(int i, int j) {
  double dx = x[i] - x[j], dy = y[i] - y[j], dz = z[i] - z[j];
  return dx * dx + dy * dy + dz * dz;
}

int main() {
  while (~scanf("%d %d", &n, &m)) {
    for (int i = 1; i <= n; i++) {
      scanf("%lf %lf %lf", &x[i], &y[i], &z[i]);
    }
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++)
        dis[i][j] = caldis(i, j);
    ans = 0;
    memset(arr, 0, sizeof(arr));
    memset(use, 0, sizeof(use));
    dfs(1);
    printf("%.6lf\\n", ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string.h>
using namespace std;
int n, m, cho[25], hh = 1048576;
double d[3][25], map[25][25] = {0};
double sum, ans = 0;
double getit(int a, int b) {
  int i, j;
  double s = 0, k;
  for (i = 0; i < 3; i++) {
    k = d[i][a] - d[i][b];
    k *= k;
    s += k;
  }
  return s;
}
int ok = 0;
void dfs(int a, int b) {
  int i, j;
  if (b > n)
    return;
  if (a > m) {

    sum = 0;
    for (i = 1; i <= m; i++) {
      for (j = i + 1; j <= m; j++) {
        sum += map[cho[i]][cho[j]];
      }
    }
    if (sum > ans)
      ans = sum;

  } else {
    for (i = b; n - i > m - a; i++) {
      cho[a] = i;
      dfs(a + 1, b + 1);
    }
  }
}

int main() {
  int i, j, k;
  scanf("%d%d", &n, &m);

  for (i = 0; i < n; i++) {
    scanf("%lf%lf%lf", &d[0][i], &d[1][i], &d[2][i]);
    for (j = 0; j < i; j++) {
      map[i][j] = getit(i, j);
      map[j][i] = map[i][j];
    }
  }
  // sort(sum,sum+n,cmp);
  dfs(1, 0);

  printf("%f\\n", ans);
  // system("pause");
  return 0;
}