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

#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <sstream>
#include <stack>
#include <stdio.h>

using namespace std;

double dp[1 << 13][13];
int n;

int main() {

  string input;
  while (getline(cin, input)) {
    stringstream ss;
    ss << input;
    double box;
    ss >> box;

    double cake[12] = {0};
    n = 0;
    while (ss >> cake[n]) {
      n++;
    }

    double v[13][13] = {0};
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (i == j) {
          v[i][j] = 0;
          v[j][i] = v[i][j];
        } else {
          double x = (cake[i] + cake[j]) * (cake[i] + cake[j]) -
                     (cake[i] - cake[j]) * (cake[i] - cake[j]);
          v[i + 1][j + 1] = sqrt((fabs(x)));
          // cout<<i<<" "<<j<<" "<<v[i][j]<<endl;
          v[j][i] = v[i][j];
        }
      }
    }

    for (int i = 1; i <= n; i++) {
      v[0][i] = cake[i - 1];
      v[i][0] = cake[i - 1];
    }

    n++;
    // memset(dp, 999999.9999, sizeof(dp));
    for (int i = 0; i < (1 << 13); i++) {
      for (int j = 0; j < 13; j++) {
        dp[i][j] = 100000000;
      }
    }

    dp[(1 << n) - 1][0] = 0;

    for (int i = (1 << n) - 2; i >= 0; i--) {
      for (int j = 0; j < n; j++) {
        for (int k = 0; k < n; k++) {
          if (!((i >> k) & 1)) {

            dp[i][j] = min(dp[i][j], dp[i | (1 << k)][k] + v[j][k]);
          }
        }
      }
    }

    // cout<<dp[0][0]<<endl;
    if (dp[0][0] <= box)
      cout << "OK" << endl;
    else
      cout << "NA" << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <sstream>
#include <stack>
#include <stdio.h>

using namespace std;

double dp[1 << 12][12];
int n;

int main() {

  string input;
  while (getline(cin, input)) {
    stringstream ss;
    ss << input;
    double box;
    ss >> box;

    double cake[12] = {0};
    n = 0;
    cake[n] = 0;
    n++;
    while (ss >> cake[n]) {
      n++;
    }

    double v[13][13] = {0};
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (i == j) {
          v[i][j] = 0;
          v[j][i] = v[i][j];
        } else {
          double x = (cake[i] + cake[j]) * (cake[i] + cake[j]) -
                     (cake[i] - cake[j]) * (cake[i] - cake[j]);
          v[i][j] = sqrt((fabs(x)));
          // cout<<i<<" "<<j<<" "<<v[i][j]<<endl;
          v[j][i] = v[i][j];
        }
      }
    }

    // memset(dp, 999999.9999, sizeof(dp));
    for (int i = 0; i < (1 << 12); i++) {
      for (int j = 0; j < 12; j++) {
        dp[i][j] = 100000000;
      }
    }

    dp[(1 << n) - 1][0] = 0;

    for (int i = (1 << n) - 2; i >= 0; i--) {
      for (int j = 0; j < n; j++) {

        for (int k = 0; k < n; k++) {
          if (!((i >> k) & 1)) {
            if (j == 0) {
              dp[i][j] = min(dp[i][j], dp[i | (1 << k)][k] + cake[k]);
            } else if (k == 0) {
              dp[i][j] = min(dp[i][j], dp[i | (1 << k)][k] + cake[j]);
            } else {
              /*
              int m=i;
              for(int l=0; l<n; l++){
                  cout<<m%2;
                  m/=2;
              }
              cout<<endl;
              cout<<"j  "<<j<<endl;
              cout<<"k  "<<k<<endl;
               */
              dp[i][j] = min(dp[i][j], dp[i | (1 << k)][k] + v[j][k]);
              /*
              cout<<"dp "<<dp[i][j]<<endl;
              cout<<"v  "<<v[j][k]<<endl;
              cout<<"-----------"<<endl;
               */
            }
          }
        }
      }
    }

    // cout<<dp[0][0]<<endl;
    if (dp[0][0] <= box)
      cout << "OK" << endl;
    else
      cout << "NA" << endl;
  }
}