alexjercan/bugnet huggingface dataset:
Problem p01416 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define MAX_H 500
#define MAX_W 500
#define F first
#define S second
typedef pair<int, int> pii;

int H, W;
char field[MAX_H][MAX_W];
vector<pii> v[26];

bool inField(int x, int y) { return (0 <= x && x < W && 0 <= y && y < H); }

int calc(int x, int y) {
  for (int i = 0; i < 26; i++) {
    v[i].clear();
  }
  const int dx[] = {-1, 0, 1, 0};
  const int dy[] = {0, -1, 0, 1};
  for (int i = 0; i < 4; i++) {
    int nx = x, ny = y;
    while (1) {
      nx += dx[i];
      ny += dy[i];
      if (!inField(nx, ny))
        break;
      if (field[ny][nx] != '.') {
        v[field[ny][nx] - 'A'].push_back(pii(nx, ny));
        break;
      }
    }
  }
  int res = 0;
  for (int i = 0; i < 26; i++) {
    if (v[i].size() == 2) {
      field[v[i][0].S][v[i][0].F] = '.';
      field[v[i][1].S][v[i][1].F] = '.';
      res += 2;
    }
  }
  return res;
}

int main() {
  cin >> H >> W;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> field[i][j];
    }
  }
  int res = 0;
  bool update = 1;
  while (update) {
    update = 0;
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        if (field[i][j] != '.')
          continue;
        int r = calc(j, i);
        if (r > 0) {
          res += r;
          update = 1;
        }
      }
    }
  }
  cout << res << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define MAX_H 500
#define MAX_W 500
#define F first
#define S second
typedef pair<int, int> pii;

int H, W;
char field[MAX_H][MAX_W];
vector<pii> v[26];

bool inField(int x, int y) { return (0 <= x && x < W && 0 <= y && y < H); }

int calc(int x, int y) {
  for (int i = 0; i < 26; i++) {
    v[i].clear();
  }
  const int dx[] = {-1, 0, 1, 0};
  const int dy[] = {0, -1, 0, 1};
  for (int i = 0; i < 4; i++) {
    int nx = x, ny = y;
    while (1) {
      nx += dx[i];
      ny += dy[i];
      if (!inField(nx, ny))
        break;
      if (field[ny][nx] != '.') {
        v[field[ny][nx] - 'A'].push_back(pii(nx, ny));
        break;
      }
    }
  }
  int res = 0;
  for (int i = 0; i < 26; i++) {
    if (v[i].size() == 2) {
      field[v[i][0].S][v[i][0].F] = '.';
      field[v[i][1].S][v[i][1].F] = '.';
      res += 2;
    }
  }
  return res;
}

int main() {
  cin >> H >> W;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> field[i][j];
    }
  }
  int res = 0;
  bool update = 1;
  while (update) {
    update = 0;
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        if (field[i][j] != '.')
          continue;
        int r = calc(j, i);
        if (r > 0) {
          res += r;
          update = 1;
          goto next;
        }
      }
    }
  next:;
  }
  cout << res << endl;
  return 0;
}
Problem p01416 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define int long long
#define r(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef pair<int, int> P;

int h, w;
string s[600];
int dy[] = {0, 1, 0, -1};
int dx[] = {1, 0, -1, 0};
int sum = 0;

bool ng[555][555];

void knock(int Y, int X) {

  map<char, vector<P>> M;
  r(i, 4) {
    int y = Y;
    int x = X;

    while (!isalpha(s[y][x])) {
      y += dy[i];
      x += dx[i];
      if (y < 0 || x < 0 || y >= h || x >= w)
        break;
    }

    if (y < 0 || x < 0 || y >= h || x >= w)
      continue;
    M[s[y][x]].push_back(P(y, x));
  }

  int TX = 0;
  for (auto &e : M) {
    if (e.second.size() != 2)
      continue;
    sum += 2;
    s[e.second[0].first][e.second[0].second] = '.';
    s[e.second[1].first][e.second[1].second] = '.';
    TX++;
  }

  if (!M.size())
    ng[Y][X] = 1;
}

signed main() {
  cin >> h >> w;
  r(i, h) cin >> s[i];

  r(tim, 26) r(i, h) r(j, w) if (!isalpha(s[i][j]) && !ng[i][j]) {
    knock(i, j);
  }

  cout << sum << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define int long long
#define r(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef pair<int, int> P;

int h, w;
string s[600];
int dy[] = {0, 1, 0, -1};
int dx[] = {1, 0, -1, 0};
int sum = 0;

void knock(int Y, int X) {

  map<char, vector<P>> M;
  r(i, 4) {
    int y = Y;
    int x = X;

    while (!isalpha(s[y][x])) {
      y += dy[i];
      x += dx[i];
      if (y < 0 || x < 0 || y >= h || x >= w)
        break;
    }

    if (y < 0 || x < 0 || y >= h || x >= w)
      continue;
    M[s[y][x]].push_back(P(y, x));
  }

  for (auto &e : M) {
    if (e.second.size() != 2)
      continue;
    sum += 2;
    s[e.second[0].first][e.second[0].second] = '.';
    s[e.second[1].first][e.second[1].second] = '.';
  }
}

signed main() {
  cin >> h >> w;
  r(i, h) cin >> s[i];

  r(tim, 30) r(i, h) r(j, w) if (!isalpha(s[i][j])) { knock(i, j); }

  cout << sum << endl;
}
Problem p01417 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define int long long // <-----!!!!!!!!!!!!!!!!!!!

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rrep2(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(a) (a).begin(), (a).end()

typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> TUPLE;
// typedef vector<int> V;
typedef vector<double> V;
typedef vector<V> VV;
typedef vector<VV> VVV;

signed main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);

  int N, M;
  cin >> N >> M;
  VV v(N, V(3));
  rep(i, N) cin >> v[i][0] >> v[i][1] >> v[i][2];
  VV d(N, V(N, 0));
  rep(i, N) rep(j, N) rep(k, 3) {
    d[i][j] += (v[i][k] - v[j][k]) * (v[i][k] - v[j][k]);
  }

  double mx = -1;
  rep(s, 1 << N) {
    if (__builtin_popcount(s) != M)
      continue;

    double sm = 0;
    rep(i, N) {
      if ((s >> i) % 2 == 0)
        continue;
      rep(j, i) {
        if ((s >> j) % 2 == 0)
          continue;
        sm += d[i][j];
      }
    }
    mx = max(mx, sm);
  }

  printf("%.10f\\n", mx);
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long // <-----!!!!!!!!!!!!!!!!!!!

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rrep2(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(a) (a).begin(), (a).end()

typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> TUPLE;
// typedef vector<int> V;

typedef vector<double> V;
typedef vector<V> VV;
typedef vector<VV> VVV;

inline double dist(const V &a, const V &b) {
  double ret;
  rep(i, 3) { ret += (a[i] - b[i]) * (a[i] - b[i]); }
  return ret;
}

signed main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);

  int n, m;
  cin >> n >> m;

  VV a(n, V(3));
  rep(i, n) { cin >> a[i][0] >> a[i][1] >> a[i][2]; }

  double ans = -1;
  rep(s, 1 << n) {
    if (__builtin_popcount(s) != m)
      continue;
    double sumD = 0;
    rep(i, n) {
      rep2(j, i + 1, n) {
        if (!(((s >> i) & 1) & ((s >> j) & 1)))
          continue;
        sumD += dist(a[i], a[j]);
        cerr << s << " " << i << " " << j << endl;
      }
    }
    ans = max(ans, sumD);
  }

  printf("%.10f\\n", ans);
}
Problem p01417 (C++) — buggy submission, verdict: Runtime Error

#include <cmath>
#include <cstdio>
#include <vector>
using namespace std;
struct node {
  double x, y, z;
};
struct node x[21];
vector<double> a;
int b[25], i, n, m, j;
double d[21][21], maxx = 0, ans;

double dis(int i, int j) {
  return ((x[i].x - x[j].x) * (x[i].x - x[j].x) +
          (x[i].y - x[j].y) * (x[i].y - x[j].y) +
          (x[i].z - x[j].z) * (x[i].z - x[j].z));
}
void dfs(int v, int k) {
  int x, i, j, t;
  b[v] = 1;
  if (k == m) {
    ans = 0;
    for (i = 0; i < a.size(); i++) {
      x = a[i];
      for (j = 0; j < a.size(); j++)
        if (j != i) {
          t = a[j];
          ans = ans + d[x][t];
        }
    }
    if (ans / 2 > maxx)
      maxx = ans / 2;
    return;
  }
  a.push_back(v);
  k++;
  if (v + 1 <= n)
    dfs(v + 1, k);
  a.pop_back();
  k--;
  if (v + 1 <= n)
    dfs(v + 1, k);
  b[v] = 0;
}
int main() {
  scanf("%d%d", &n, &m);
  for (i = 0; i < n; i++)
    scanf("%lf%lf%lf", &x[i].x, &x[i].y, &x[i].z);
  for (i = 0; i < n; i++)
    for (j = 0; j < n; j++)
      d[i][j] = dis(i, j);
  dfs(0, 0);
  printf("%lf\\n", maxx);
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#define maxn 250
using namespace std;
struct colcor {
  double x, y, z;
} col[maxn];
double pow(double a) { return a * a; }
double dis[maxn][maxn];
double d[maxn] = {0};
bool f[maxn] = {0};
double ans;
int cnt, n, m;
void deal(int k) {
  for (int i = 0; i < n; i++) {
    if (f[i])
      continue;
    d[i] += dis[i][k];
  }
}
void work() {
  if (m < 2)
    ans = 0;
  while (cnt < m) {
    int maxp = -1;
    double maxd = -1;
    for (int i = 0; i < n; i++) {
      if (f[i])
        continue;
      if (d[i] > maxd) {
        maxd = d[i];
        maxp = i;
      }
    }
    ans += maxd;
    f[maxp] = 1;
    deal(maxp);
    cnt++;
  }
  printf("%.10lf", ans);
}
int main() {
  // freopen("in.txt","r",stdin);
  //   freopen("out.txt","w",stdout);
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) {
    scanf("%lf%lf%lf", &col[i].x, &col[i].y, &col[i].z);
  }
  double flag = 0;
  int a, b;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      dis[i][j] = pow(col[i].x - col[j].x) + pow(col[i].y - col[j].y) +
                  pow(col[i].z - col[j].z);
      dis[j][i] = dis[i][j];
      if (dis[i][j] > flag) {
        a = i;
        b = j;
        flag = dis[i][j];
      }
    }
  }
  f[a] = 1;
  f[b] = 1;
  ans = flag;
  cnt = 2;
  deal(a);
  deal(b);
  work();
  return 0;
}
Problem p01417 (C++) — buggy submission, verdict: Runtime Error

#include <iomanip>
#include <iostream>
#include <vector>

using namespace std;

typedef long long ll;

const int MAX_N = 30;
double L[MAX_N], a[MAX_N], b[MAX_N];
int N, M;

double max_comb(ll n) {
  vector<int> s;
  double ret = 0.0;

  for (int i = 0; i < N; i++) {
    if (n & 1 == 1)
      s.push_back(i);
    n >>= 1;
  }

  for (int i = 0; i < s.size(); i++)
    for (int j = i + 1; j < s.size(); j++)
      ret += (L[s[i]] - L[s[j]]) * (L[s[i]] - L[s[j]]) +
             (a[s[i]] - a[s[j]]) * (a[s[i]] - a[s[j]]) +
             (b[s[i]] - b[s[j]]) * (b[s[i]] - b[s[j]]);
  return ret;
}

int main() {
  double ans = 0.0;

  cout << setprecision(7) << fixed;
  cin >> N >> M;
  if (M == 0 || N == 0) {
    cout << 0 << endl;
    return 0;
  }
  for (int i = 0; i < N; i++)
    cin >> L[i] >> a[i] >> b[i];

  ll comb = (1 << M) - 1;
  while (comb < 1 << N) {
    ans = max(ans, max_comb(comb));

    int x = comb & -comb, y = comb + x;
    comb = ((comb & ~y) / x >> 1) | y;
  }

  cout << ans << endl;

  return 0;
}

Error: WA

Fixed submission:

#include <iomanip>
#include <iostream>
#include <vector>

using namespace std;

const int MAX_N = 25;
double L[MAX_N], a[MAX_N], b[MAX_N];
int N, M;

double max_comb(long long n) {
  vector<int> s;
  double ret = 0.0;

  for (int i = 0; i < N; i++) {
    if (n & 1 == 1)
      s.push_back(i);
    n >>= 1;
  }

  for (int i = 0; i < s.size(); i++)
    for (int j = i + 1; j < s.size(); j++)
      ret += (L[s[i]] - L[s[j]]) * (L[s[i]] - L[s[j]]) +
             (a[s[i]] - a[s[j]]) * (a[s[i]] - a[s[j]]) +
             (b[s[i]] - b[s[j]]) * (b[s[i]] - b[s[j]]);
  return ret;
}

int main() {
  double ans = 0.0;

  cout << setprecision(7) << fixed;
  cin >> N >> M;
  for (int i = 0; i < N; i++)
    cin >> L[i] >> a[i] >> b[i];

  long long comb = (1 << M) - 1;
  while (comb < 1 << N) {
    ans = max(ans, max_comb(comb));

    int x = comb & -comb, y = comb + x;
    comb = ((comb & ~y) / x >> 1) | y;
  }

  cout << ans << endl;

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

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

int main() {
  double dist[20][20];
  double L[20], a[20], b[20];
  int N, M;
  cin >> N >> M;
  if (M <= 1) {
    cout << 0 << endl;
    return 0;
  }
  for (int i = 0; i < N; i++) {
    cin >> L[i] >> a[i] >> b[i];
  }
  for (int i = 0; i < N; i++) {
    for (int j = i; j < N; j++) {
      dist[i][j] = dist[j][i] =
          pow(L[i] - L[j], 2) + pow(a[i] - a[j], 2) + pow(b[i] - b[j], 2);
    }
  }
  int comb = (1 << M) - 1;
  double ans = 0;
  while (comb < (1 << N)) {
    double D = 0;
    for (int i = 0; (1 << i) <= comb; i++) {
      if ((1 << i) & comb)
        for (int j = 0; j < i; j++) {
          if ((1 << j) & comb)
            D += dist[i][j];
        }
    }
    ans = max(ans, D);
    int x = comb & -comb, y = comb + x;
    comb = ((comb & ~y) / x >> 1) | y;
  }
  cout << fixed;
  cout.precision(6);
  cout << ans << endl;

  return 0;
}

Error: WA

Fixed submission:

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

int main() {
  double dist[20][20];
  double L[20], a[20], b[20];
  int N, M;
  cin >> N >> M;
  for (int i = 0; i < N; i++) {
    cin >> L[i] >> a[i] >> b[i];
  }
  for (int i = 0; i < N; i++) {
    for (int j = i; j < N; j++) {
      dist[i][j] = dist[j][i] =
          pow(L[i] - L[j], 2) + pow(a[i] - a[j], 2) + pow(b[i] - b[j], 2);
    }
  }
  int comb = (1 << M) - 1;
  double ans = 0;
  while (comb < 1 << N) {
    double D = 0;
    for (int i = 0; (1 << i) <= comb; i++) {
      if ((1 << i) & comb)
        for (int j = 0; j < i; j++) {
          if ((1 << j) & comb)
            D += dist[i][j];
        }
    }
    ans = max(ans, D);
    int x = comb & -comb, y = comb + x;
    comb = ((comb & ~y) / x >> 1) | y;
  }
  cout << fixed;
  cout.precision(6);
  cout << ans << endl;

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

#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;

const int N_MAX = 20;
int n, m;
double l[N_MAX], a[N_MAX], b[N_MAX];
vector<int> selected;
double result;

void combinations(int c) {
  if (c == 0) {
    double sum = 0;
    for (int i = 0; i < m; i++) {
      for (int j = i + 1; j < m; j++) {
        int s = selected[i];
        int t = selected[j];
        double x = l[s] - l[t];
        double y = a[s] - a[t];
        double z = b[s] - b[t];
        sum += x * x + y * y + z * z;
      }
    }
    result = max(result, sum);
  } else {
    int start = 0;
    if (c != m) {
      start = selected.back() + 1;
    }
    for (int i = start; i < n; i++) {
      selected.push_back(i);
      combinations(c - 1);
      selected.pop_back();
    }
  }
}

int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> l[i] >> a[i] >> b[i];
  }
  result = 0.;
  combinations(m);
  printf("%.6lf\\n", result);
  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
#include <vector>
using namespace std;

const int N_MAX = 20;
int n, m;
double l[N_MAX], a[N_MAX], b[N_MAX];
vector<int> selected;
double result;

void combinations(int c) {
  if (c == 0) {
    double sum = 0;
    for (int i = 0; i < m; i++) {
      for (int j = i + 1; j < m; j++) {
        int s = selected[i];
        int t = selected[j];
        double x = l[s] - l[t];
        double y = a[s] - a[t];
        double z = b[s] - b[t];
        sum += x * x + y * y + z * z;
      }
    }
    result = max(result, sum);
  } else {
    int start = 0;
    if (!selected.empty())
      start = *selected.rend() + 1;
    for (int i = start; i < n; i++) {
      selected.push_back(i);
      combinations(c - 1);
      selected.pop_back();
    }
  }
}

int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> l[i] >> a[i] >> b[i];
  }
  result = 0.;
  combinations(m);
  cout << result << endl;
  return 0;
}
Problem p01417 (C++) — buggy submission, verdict: Time Limit Exceeded


#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;

#define pb push_back
typedef long long ll;
typedef complex<int> P;

int n, m;
double ans = 0;
double x[21], y[21], z[21];

double f(int i, int j) {
  return (x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]) +
         (z[i] - z[j]) * (z[i] - z[j]);
}

void dfs(int p, vector<int> &v) {
  if (p == n) {
    if (v.size() == m) {
      double sum = 0.0;
      for (int i = 0; i < v.size(); i++) { //
        for (int j = 0; j < i; j++) {      //
          sum += f(v[i], v[j]);
        }
      }
      ans = max(ans, sum);
      return;
    }
  } else {
    dfs(p + 1, v);
    v.push_back(p);
    dfs(p + 1, v);
    v.pop_back();
  }
}

bool solve() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> x[i] >> y[i] >> z[i];
  }

  ans = 0.0;
  vector<int> v;
  dfs(0, v);
  cout << ans << endl;
  return true;
}

int main() {
  cout.setf(ios::fixed);
  cout.precision(10);

  solve();

  return 0;
}


Error: TLE

Fixed submission:


#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;

#define pb push_back
typedef long long ll;
typedef complex<int> P;

int n, m;
double ans = 0;
double x[21], y[21], z[21];

double f(int i, int j) {
  return (x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]) +
         (z[i] - z[j]) * (z[i] - z[j]);
}

void dfs(int p, vector<int> &v) {
  if (p == n) {
    if (v.size() == m) {
      double sum = 0.0;
      for (int i = 0; i < v.size() - 1; i++) {
        for (int j = i + 1; j < v.size(); j++) {
          sum += f(v[i], v[j]);
        }
      }
      ans = max(ans, sum);
      return;
    }
  } else {
    dfs(p + 1, v);
    v.push_back(p);
    dfs(p + 1, v);
    v.pop_back();
  }
}

bool solve() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> x[i] >> y[i] >> z[i];
  }

  ans = 0.0;
  vector<int> v;
  dfs(0, v);
  cout << ans << endl;
  return true;
}

int main() {
  cout.setf(ios::fixed);
  cout.precision(10);

  solve();

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

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
int N, M;
double diffSum[20][20];
double ans = 0.0;
double calc(double a, double b) { return (a - b) * (a - b); }
void dfs(int ite, vector<int> &v) {
  if (ite >= N && v.size() == M) {
    double sum = 0.0;
    for (int i = 0; i < v.size() - 1; i++) {
      for (int j = i + 1; j < v.size(); j++) {
        sum += diffSum[v[i]][v[j]];
      }
    }
    ans = max(ans, sum);
    return;
  }

  if (ite >= N)
    return;

  dfs(ite + 1, v);
  v.push_back(ite);
  dfs(ite + 1, v);
  v.pop_back();
}
int main() {
  cin >> N >> M;
  double L[20];
  double a[20];
  double b[20];
  for (int i = 0; i < N; i++) {
    cin >> L[i] >> a[i] >> b[i];
  }
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      if (i == j)
        continue;
      diffSum[i][j] = calc(L[i], L[j]) + calc(a[i], a[j]) + calc(b[i], b[j]);
      diffSum[j][i] = diffSum[i][j];
    }
  }
  vector<int> v;
  if (M != 0)
    dfs(0, v);
  printf("%.6f\\n", ans);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
int N, M;
double diffSum[20][20];
double ans = 0.0;
double calc(double a, double b) { return (a - b) * (a - b); }
void dfs(int ite, vector<int> &v) {
  if (ite == N && v.size() == M) {
    double sum = 0.0;
    for (int i = 0; i < v.size() - 1; i++) {
      for (int j = i + 1; j < v.size(); j++) {
        sum += diffSum[v[i]][v[j]];
      }
    }
    ans = max(ans, sum);
  }

  if (ite >= N)
    return;

  dfs(ite + 1, v);
  v.push_back(ite);
  dfs(ite + 1, v);
  v.pop_back();
}
int main() {
  cin >> N >> M;
  double L[20];
  double a[20];
  double b[20];
  for (int i = 0; i < N; i++) {
    cin >> L[i] >> a[i] >> b[i];
  }
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      if (i == j)
        continue;
      diffSum[i][j] = calc(L[i], L[j]) + calc(a[i], a[j]) + calc(b[i], b[j]);
      diffSum[j][i] = diffSum[i][j];
    }
  }
  vector<int> v;
  dfs(0, v);
  printf("%.6f\\n", ans);
  return 0;
}
Problem p01417 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); ++i)

template <typename T> inline T sqr(T x) { return x * x; }
template <typename T> inline void chmax(T &t, T f) {
  if (t < f)
    t = f;
}

int N, M;

int next_combination(int x) {
  int l = x & -x;
  int m = x + l;
  int ls = x & ~m;
  return m | ((ls / l) >> 1);
}

double solve(vector<double> &L, vector<double> &a, vector<double> &b) {
  if (M == 0)
    return 0.0;
  double res = 0.0;
  for (int S = (1 << M) - 1; S < (1 << N); S = next_combination(S)) {
    double total = 0.0;
    rep(i, N) if (S >> i & 1) {
      rep(j, i) if (S >> j & 1) {
        total += sqr(L[i] - L[j]) + sqr(a[i] - a[j]) + sqr(b[i] - b[j]);
      }
    }
    chmax(res, total);
  }
  return res;
}

int main() {
  while (cin >> N >> M) {
    vector<double> L(N), a(N), b(N);
    rep(i, N) cin >> L[i] >> a[i] >> b[i];
    cout << fixed << setprecision(16) << solve(L, a, b) << '\\n';
  }
  return 0;
}

Error: WA

Fixed submission:

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

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); ++i)

template <typename T> inline T sqr(T x) { return x * x; }
template <typename T> inline void chmax(T &t, T f) {
  if (t < f)
    t = f;
}

int N, M;

int next_combination(int x) {
  int l = x & -x;
  int m = x + l;
  int ls = x & ~m;
  return m | ((ls / l) >> 1);
}

double solve(vector<double> &L, vector<double> &a, vector<double> &b) {
  double res = 0.0;
  for (int S = (1 << M) - 1; S < (1 << N); S = next_combination(S)) {
    double total = 0.0;
    rep(i, N) if (S >> i & 1) {
      rep(j, i) if (S >> j & 1) {
        total += sqr(L[i] - L[j]) + sqr(a[i] - a[j]) + sqr(b[i] - b[j]);
      }
    }
    chmax(res, total);
  }
  return res;
}

int main() {
  while (cin >> N >> M) {
    vector<double> L(N), a(N), b(N);
    rep(i, N) cin >> L[i] >> a[i] >> b[i];
    cout << fixed << setprecision(16) << solve(L, a, b) << '\\n';
  }
  return 0;
}