Problem p01417 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = 1e12;
const ll MOD = 1e9 + 7;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, N) for (int i = 0; i < (N); i++)
#define ALL(s) (s).begin(), (s).end()
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))
#define fi first
#define se second
#define N_SIZE (1LL << 20)
#define NIL -1

ll sq(ll num) { return num * num; }
ll mod_pow(ll x, ll n) {
  if (n == 0)
    return 1;
  if (n == 1)
    return x % MOD;
  ll res = sq(mod_pow(x, n / 2));
  res %= MOD;
  if (n % 2 == 1) {
    res *= x;
    res %= MOD;
  }
  return res;
}
ll mod_add(ll a, ll b) { return (a + b) % MOD; }
ll mod_sub(ll a, ll b) { return (a - b + MOD) % MOD; }
ll mod_mul(ll a, ll b) { return a * b % MOD; }

typedef tuple<double, double, double> T;
int n, m;
double ans = 0;
vector<T> vt;

vector<vector<int>> vvi;

double dist_d(double a, double b) { return (a - b) * (a - b); }
double dist(T a, T b) {
  return dist_d(get<0>(a), get<0>(b)) + dist_d(get<1>(a), get<1>(b)) +
         dist_d(get<2>(a), get<2>(b));
}

int main() {
  cin >> n >> m;
  rep(i, n) {
    double l, a, b;
    cin >> l >> a >> b;
    vt.push_back(T(l, a, b));
  }
  rep(i, (1 << n)) {
    int cnt = 0;
    rep(j, n) {
      if (i & (1 << j))
        cnt++;
    }
    if (cnt == m) {
      double sum = 0;
      rep(j, n) {
        FOR(k, j + 1, n) {
          if (i & (1 << j) && i & (1 << k)) {
            sum += dist(vt[j], vt[k]);
          }
        }
      }
      ans = max(ans, sum);
    }
  }
  printf("%.20lf\\n", ans);
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = 1e12;
const ll MOD = 1e9 + 7;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, N) for (int i = 0; i < (N); i++)
#define ALL(s) (s).begin(), (s).end()
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))
#define fi first
#define se second
#define N_SIZE (1LL << 20)
#define NIL -1

ll sq(ll num) { return num * num; }
ll mod_pow(ll x, ll n) {
  if (n == 0)
    return 1;
  if (n == 1)
    return x % MOD;
  ll res = sq(mod_pow(x, n / 2));
  res %= MOD;
  if (n % 2 == 1) {
    res *= x;
    res %= MOD;
  }
  return res;
}
ll mod_add(ll a, ll b) { return (a + b) % MOD; }
ll mod_sub(ll a, ll b) { return (a - b + MOD) % MOD; }
ll mod_mul(ll a, ll b) { return a * b % MOD; }

typedef tuple<double, double, double> T;
int n, m;
double ans = 0;
vector<T> vt;
double d[100][100];
bool used[100];

vector<vector<int>> vvi;

double dist_d(double a, double b) { return (a - b) * (a - b); }
double dist(T a, T b) {
  return dist_d(get<0>(a), get<0>(b)) + dist_d(get<1>(a), get<1>(b)) +
         dist_d(get<2>(a), get<2>(b));
}

void solve(int now, vector<int> &vi) {
  if (vi.size() == m) {
    double num = 0;
    rep(i, vi.size()) {
      FOR(j, i + 1, vi.size()) {
        // cout << vi[i] << " " << vi[j] << endl;
        num += dist(vt[vi[i]], vt[vi[j]]);
      }
    }
    // cout << endl;
    ans = max(ans, num);
  }
  FOR(i, now, n) {
    vi.push_back(i);
    solve(now + 1, vi);
    vi.pop_back();
  }
}
vector<int> v;

int main() {
  cin >> n >> m;
  rep(i, n) {
    double l, a, b;
    cin >> l >> a >> b;
    vt.push_back(T(l, a, b));
  }
  solve(0, v);
  printf("%.20lf\\n", ans);
}