Problem p01417 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <sstream>
#include <string>
#include <typeinfo>
#include <utility>
#include <vector>
#define REP(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
using namespace std;
typedef vector<ll> vl;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef vector<string> vs;

int main() {
  int n, m;
  cin >> n >> m; // nは色の数 mは人の数
  double a[21][4] = {};
  REP(i, n) { cin >> a[i][0] >> a[i][1] >> a[i][2]; }

  double b[21][21] = {};
  REP(i, n) {
    REP(j, n) {
      b[i][j] += (a[i][0] - a[j][0]) * (a[i][0] - a[j][0]) +
                 (a[i][1] - a[j][1]) * (a[i][1] - a[j][1]) +
                 (a[i][2] - a[j][2]) * (a[i][2] - a[j][2]);
    }
  }

  int c[21] = {}; // i番目が選択済みなら1をたてる
  REP(i, m) { c[i] = 1; }
  sort(c, c + n);

  // m<2の時
  if (m < 2) {
    cout << 0 << endl;
  } else {
    // m=2の時
    double ans(0);

    do {
      double tmp(0);
      for (int i = 0; i < n; i++) {
        if (c[i] == 1) {
          for (int j = i + 1; j < n; j++) {
            if (c[j] == 1) {
              tmp += b[i][j];
            }
          }
        }
      }
      ans = max(ans, tmp);
    } while (next_permutation(c, c + n));

    cout << fixed << setprecision(7) << ans << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <sstream>
#include <string>
#include <typeinfo>
#include <utility>
#include <vector>
#define REP(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
using namespace std;
typedef vector<ll> vl;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef vector<string> vs;

int main() {
  int n, m;
  cin >> n >> m;
  double a[20][3] = {};
  REP(i, n) { cin >> a[i][0] >> a[i][1] >> a[i][2]; }

  double b[20][20] = {};
  REP(i, n) {
    REP(j, n) {
      b[i][j] += (a[i][0] - a[j][0]) * (a[i][0] - a[j][0]) +
                 (a[i][1] - a[j][1]) * (a[i][1] - a[j][1]) +
                 (a[i][2] - a[j][2]) * (a[i][2] - a[j][2]);
    }
  }

  int c[20] = {}; // i番目が選択済みなら1をたてる

  // m<2の時
  if (m < 2) {
    cout << 0 << endl;
  } else {
    // m=2の時
    double ans(0);
    for (int z = 2; z <= m; z++) {
      // cout<<"z= "<<z<<endl;
      //	cout <<fixed << setprecision(7) <<"ans= "<<  ans <<endl;
      if (z == 2) {
        int x, y;
        REP(i, n) {
          REP(j, n) {
            if (ans < b[i][j]) {
              ans = b[i][j];
              x = i, y = j;
            }
          }
        }
        c[x] = 1;
        c[y] = 1; // cout<<"x= "<<x<<endl<<"y= "<<y<<endl;
      } else {
        int p;
        double max(0);
        for (int i = 0; i < n; i++) {
          double tmp(0);
          REP(j, n) { tmp += b[i][j] * c[j] * (1 - c[i]); }
          // cout<<"i="<<i<<"の時、合計は"<<tmp<<endl;
          if (max < tmp) {
            max = tmp;
            p = i;
          }
        }
        c[p] = 1;
        ans += max; // cout<<"p= "<<p<<endl;//REP(i,n)cout<<c[i];cout<<endl;
      }
    }
    cout << fixed << setprecision(7) << ans << endl;
  }
  return 0;
}