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

#include <algorithm>
#include <array>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define vi vector<int>
#define vvi vector<vector<int>>
#define ll long long int
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
#define ld long double
#define INF 1e9
#define EPS 0.0000000001
#define rep(i, n) for (int i = 0; i < n; i++)
#define loop(i, s, n) for (int i = s; i < n; i++)
#define all(in) in.begin(), in.end()
template <class T, class S> void cmin(T &a, const S &b) {
  if (a > b)
    a = b;
}
template <class T, class S> void cmax(T &a, const S &b) {
  if (a < b)
    a = b;
}
#define MAX 9999999
using namespace std;
typedef pair<double, double> pii;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
/*next_combination*/
#define it vector<int>::iterator
bool next_combination(it first1, it last1, it first2, it last2) {
  if ((first1 == last1) || (first2 == last2))
    return false;
  it m1 = last1, m2 = last2;
  m2--;
  while (--m1 != first1 && !(*m1 < *m2))
    ;
  bool result = (m1 == first1) && !(*first1 < *m2);
  if (!result) {
    while (first2 != m2 && !(*m1 < *first2))
      first2++;
    first1 = m1;
    iter_swap(first1, first2);
    first1++;
    first2++;
  }
  if ((first1 != last1) && (first2 != last2)) {
    m1 = last1;
    m2 = first2;
    while ((m1 != first1) && (m2 != last2))
      iter_swap(--m1, m2++);
    reverse(first1, m1);
    reverse(first1, last1);
    reverse(m2, last2);
    reverse(first2, last2);
  }
  return !result;
}
double p(double temp) { return temp * temp; }
typedef pair<pii, double> piii;
double Power(piii a, piii b) {
  return (p(a.first.first - b.first.first) +
          p(a.first.second - b.first.second) + p(a.second - b.second));
}
bool next_combination(it first, it middle, it last) {
  return next_combination(first, middle, middle, last);
}
int main() {
  int n, m;
  cin >> n >> m;
  vi v(n);
  rep(i, n) v[i] = i;
  vector<piii> u;
  rep(i, n) {
    piii temp;
    cin >> temp.first.first >> temp.first.second >> temp.second;
    u.push_back(temp);
  }
  double ans = 0;
  do {
    double t = 0;
    // cout<<v[0]<<" "<<v[1]<<" "<<v[2]<<endl;
    rep(i, m) {
      for (int j = i + 1; j < m; j++) {
        t += Power(u[v[i]], u[v[j]]);
      }
    }
    // cout<<t<<endl;
    cmax(ans, t);
  } while (next_combination(v.begin(), v.begin() + m, v.end()));
  printf("%.10f\\n", ans);
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define vi vector<int>
#define vvi vector<vector<int>>
#define ll long long int
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
#define ld long double
#define INF 1e9
#define EPS 0.0000000001
#define rep(i, n) for (int i = 0; i < n; i++)
#define loop(i, s, n) for (int i = s; i < n; i++)
#define all(in) in.begin(), in.end()
template <class T, class S> void cmin(T &a, const S &b) {
  if (a > b)
    a = b;
}
template <class T, class S> void cmax(T &a, const S &b) {
  if (a < b)
    a = b;
}
#define MAX 9999999
using namespace std;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
int n, m;
double _ans = 0;
double Power(double temp) { return temp * temp; }
typedef pair<pdd, double> piii;
vector<piii> v;
void dfs(vb flag, int cnt) {
  if (cnt == m) {
    vector<piii> cal;
    for (int i = 0; i < flag.size(); i++)
      if (!flag[i])
        cal.push_back(v[i]);
    double ans = 0;
    for (int i = 0; i < cal.size(); i++) {
      for (int j = i + 1; j < cal.size(); j++) {
        ans += Power(cal[i].first.first - cal[j].first.first) +
               Power(cal[i].first.second - cal[j].first.second) +
               Power(cal[i].second - cal[j].second);
      }
    }
    cmax(_ans, ans);
    return;
  }
  for (int i = 0; i < v.size(); i++)
    if (!flag[i])
      continue;
    else {
      vb tiya = flag;
      tiya[i] = false;
      dfs(tiya, cnt + 1);
    }
}
int main() {
  vb flag(21, true);
  cin >> n >> m;
  rep(i, n) {
    piii temp;
    cin >> temp.first.first >> temp.first.second >> temp.second;
    v.push_back(temp);
  }
  for (int i = 0; i < n; i++) {
    vb tiya = flag;
    tiya[i] = false;
    dfs(tiya, 1);
  }
  printf("%.10lf\\n", _ans);
}