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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1 << 30;
const int EPS = 1e-8;

typedef pair<int, int> pii;

map<pii, double> mp;

double calc(int r1, int r2) {
  if (mp[pii(r1, r2)])
    return mp[pii(r1, r2)];
  return mp[pii(r1, r2)] = sqrt((r1 + r2) * (r1 + r2) - (r1 - r2) * (r1 - r2));
}

int main() {
  string s;
  while (getline(cin, s)) {
    stringstream ss(s);
    double x;
    ss >> x;
    int r;
    vector<int> v;
    while (ss >> r) {
      v.push_back(r);
    }
    sort(ALL(v));
    int n = v.size();
    if (n == 0) {
      cout << "OK" << endl;
      continue;
    }
    deque<int> dq;
    REP(i, n / 2) {
      if (i % 2 == 0) {
        dq.push_front(v[i]);
        dq.push_back(v[n - i - 1]);
      } else {
        dq.push_front(v[n - i - 1]);
        dq.push_back(v[i]);
      }
    }
    if (n % 2) {
      if (n == 1)
        dq.push_back(v[0]);
      else if (dq[n - 2] + calc(dq[0], v[n / 2]) <
               dq[0] + calc(dq[n - 2], v[n / 2]))
        dq.push_front(v[n / 2]);
      else
        dq.push_back(v[n / 2]);
    }
    // FOR(it, dq) {
    //   cout << *it << " ";
    // }
    // cout << endl;

    double xx = dq[0] + dq[n - 1];
    REP(i, n - 1) { xx += calc(dq[i], dq[i + 1]); }
    if (xx <= x)
      cout << "OK";
    else
      cout << "NA";
    cout << endl;
  }
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1 << 30;
const int EPS = 1e-8;

typedef pair<int, int> pii;

map<pii, double> mp;

double calc(int r1, int r2) {
  if (mp[pii(r1, r2)])
    return mp[pii(r1, r2)];
  return mp[pii(r1, r2)] = sqrt((r1 + r2) * (r1 + r2) - (r1 - r2) * (r1 - r2));
}

int main() {
  string s;
  while (getline(cin, s)) {
    stringstream ss(s);
    double x;
    ss >> x;
    int r;
    vector<int> v;
    while (ss >> r) {
      v.push_back(r);
    }
    sort(ALL(v));
    int n = v.size();
    if (n == 0) {
      cout << "OK" << endl;
      continue;
    }
    deque<int> dq;
    REP(i, n / 2) {
      if (i % 2 == 0) {
        dq.push_front(v[i]);
        dq.push_back(v[n - i - 1]);
      } else {
        dq.push_front(v[n - i - 1]);
        dq.push_back(v[i]);
      }
    }
    if (n % 2) {
      if (dq[n - 2] + calc(dq[0], v[n / 2]) < dq[0] + calc(dq[n - 2], v[n / 2]))
        dq.push_front(v[n / 2]);
      else
        dq.push_back(v[n / 2]);
    }
    FOR(it, dq) { cout << *it << " "; }
    cout << endl;

    double xx = dq[0] + dq[n - 1];
    REP(i, n - 1) { xx += calc(dq[i], dq[i + 1]); }
    if (xx <= x)
      cout << "OK";
    else
      cout << "NA";
    cout << endl;
  }
}