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

#define _CRT_SECURE_NO_WARNINGS 1
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <forward_list>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
// # define M_PI 3.141592
#define INF ((int)(1 << 30))
#define FOR(i, n) for (int i = 0; i < (int)n; i++)
#define FORI(i, k, n) for (int i = k; i < (int)n; i++)
#define toRad 2.0 * M_PI / 360.0
#define inin(x)                                                                \\
  int x;                                                                       \\
  cin >> x;
#define all(x) x.begin(), x.end()
#define debug(x) cout << #x << " :" << x << endl;
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define EPS 1e-12
#define CHECK(i, a)                                                            \\
  FOR(i, a.size()) cout << #a << "[" << i << "] : " << a[i] << endl;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
int dx[4] = {0, -1, 1, 0}, dy[4] = {1, 0, 0, -1};

int n, m;
double d[22][22];
double memo[1 << 20];
double dp(int bit) {
  int t = 0;
  for (int i = 0; i < n; i++)
    if (bit & 1 << i)
      t++;
  if (t == m)
    return 0.0;
  if (memo[bit] >= 0)
    return memo[bit];
  double ans = 0.0;
  for (int i = 0; i < n; i++) {
    if (!(bit & 1 << i)) {
      double temp = 0.0;
      for (int j = 0; j < n; j++) {
        if (bit & 1 << j) {
          temp += d[i][j];
        }
      }
      ans = max(ans, temp + dp(bit | 1 << i));
    }
  }
  return memo[bit] = ans;
}

int main() {
  cin >> n >> m;
  FOR(i, 1 << 20) memo[i] = -1;
  double L[22], A[22], B[22];
  FOR(i, n) { cin >> L[i] >> A[i] >> B[i]; }
  FOR(i, n) {
    FOR(j, n) {
      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]);
    }
  }
  printf("%.8f\\n", dp(0));
}

Error: TLE

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS 1
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <forward_list>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
// # define M_PI 3.141592
#define INF ((int)(1 << 30))
#define FOR(i, n) for (int i = 0; i < (int)n; i++)
#define FORI(i, k, n) for (int i = k; i < (int)n; i++)
#define toRad 2.0 * M_PI / 360.0
#define inin(x)                                                                \\
  int x;                                                                       \\
  cin >> x;
#define all(x) x.begin(), x.end()
#define debug(x) cout << #x << " :" << x << endl;
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define EPS 1e-12
#define CHECK(i, a)                                                            \\
  FOR(i, a.size()) cout << #a << "[" << i << "] : " << a[i] << endl;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
int dx[4] = {0, -1, 1, 0}, dy[4] = {1, 0, 0, -1};

int n, m;
float d[22][22];
float memo[1 << 20];
float dp(int bit) {
  int t = 0;
  for (int i = 0; i < n; i++)
    if (bit & 1 << i)
      t++;
  if (t == m)
    return 0.0;
  if (memo[bit] >= 0)
    return memo[bit];
  float ans = 0.0;
  for (int i = 0; i < n; i++) {
    if (!(bit & 1 << i)) {
      float temp = 0.0;
      for (int j = 0; j < n; j++) {
        if (bit & 1 << j) {
          temp += d[i][j];
        }
      }
      ans = max(ans, temp + dp(bit | 1 << i));
    }
  }
  return ans;
}

int main() {
  cin >> n >> m;
  FOR(i, 1 << 20) memo[i] = -1;
  float L[22], A[22], B[22];
  FOR(i, n) { cin >> L[i] >> A[i] >> B[i]; }
  FOR(i, n) {
    FOR(j, n) {
      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]);
    }
  }
  printf("%.8f\\n", dp(0));
}