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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define foreach(c, i)                                                          \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)

template <class Itr> Itr get_adv_itr(Itr _itr, int n) {
  Itr itr(_itr);
  advance(itr, n);
  return itr;
}

string in;
int L, temp, N;

int main() {
  while (getline(cin, in)) {
    istringstream is(in);
    is >> L;
    vector<int> cakes;
    while (is >> temp)
      cakes.push_back(temp);
    N = cakes.size();
    string ans = "NA";

    rep(i, N) {
      vector<int> cp_cakes(cakes);
      double cost = cp_cakes[i];
      cp_cakes.erase(get_adv_itr(cp_cakes.begin(), i));
      int last = (int)cost;
      while (cp_cakes.size()) {
        vector<int> dists;
        for (int j = 0; j < cp_cakes.size(); ++j)
          dists.push_back(pow(last - cp_cakes[j], 2));
        int id =
            distance(dists.begin(), max_element(dists.begin(), dists.end()));
        cost += sqrt(pow(last + cp_cakes[id], 2) - pow(last - cp_cakes[id], 2));
        last = cp_cakes[id];
        cp_cakes.erase(get_adv_itr(cp_cakes.begin(), id));
      }
      cost += last;
      if (L >= cost) {
        ans = "OK";
        break;
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define foreach(c, i)                                                          \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
typedef pair<int, int> P;

string in;
int L, temp, N;

int main() {
  while (getline(cin, in)) {
    istringstream is(in);
    is >> L;
    vector<int> cakes;
    vector<int> temps;
    while (is >> temp) {
      temps.push_back(temp);
    }
    N = temps.size();
    sort(temps.begin(), temps.end());

    for (int i = 0; i < N / 2; ++i) {
      cakes.push_back(temps[i]);
      cakes.push_back(temps[N - 1 - i]);
    }
    if (N % 2) {
      int e = temps[(N - 1) / 2];
      if (abs(e - cakes.front()) < abs(e - cakes.back()))
        cakes.push_back(e);
      else {
        cakes.push_back(0);
        for (int i = cakes.size() - 2; i >= 0; --i)
          cakes[i + 1] = cakes[i];
        cakes[0] = e;
        // cakes.push_back(e);
      }
    }
    // rep(i,cakes.size()) printf("%4d", cakes[i]); puts("");
    // puts("OK"); continue;

    double current_length = cakes.front();
    for (int i = 0; i < cakes.size() - 1; ++i) {
      current_length += 2 * sqrt(cakes[i] * cakes[i + 1]);
    }
    current_length += cakes.back();
    // printf("%lf\\n", current_length);

    printf("%s\\n", (L + 1e-10 > current_length) ? "OK" : "NA");
  }
  return 0;
}