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

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

//// < "D:\\D_Download\\Visual Studio
///2015\\Projects\\programing_contest_c++\\Debug\\a.txt" > "D:\\D_Download\\Visual
///Studio 2015\\Projects\\programing_contest_c++\\Debug\\b.txt"

int main() {
  while (1) {
    vector<int> ps(8), cs{1, 4, 1, 4, 1, 2, 1, 2};
    for (int i = 0; i < 8; ++i) {
      if (!(cin >> ps[i])) {
        return 0;
      }
    }
    int ride_max = -1;
    vector<int> ans;
    for (int off = 0; off < 8; ++off) {
      rotate(cs.begin(), cs.begin() + off, cs.end());
      int canride = 0;
      for (int i = 0; i < 8; ++i) {
        canride += max(0, min(cs[i], ps[i]));
      }
      if (canride > ride_max) {

        ride_max = canride;
        ans = cs;
      } else if (canride == ride_max) {
        bool ok = false;
        for (int i = 0; i < 8; ++i) {
          if (ans[i] > cs[i]) {
            ok = true;
            break;
          } else if (ans[i] < cs[i]) {
            ok = false;
            break;
          } else {
            continue;
          }
        }
        if (ok) {
          ride_max = canride;
          ans = cs;
        }
      }
      rotate(cs.begin(), cs.begin() + 8 - off, cs.end());
    }
    for (int i = 0; i < 8; ++i) {
      cout << ans[i];
      if (i == 7)
        cout << endl;
      else
        cout << " ";
    }
  }

  return 0;
}

Error: 1

Fixed submission:

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

//// < "D:\\D_Download\\Visual Studio
///2015\\Projects\\programing_contest_c++\\Debug\\a.txt" > "D:\\D_Download\\Visual
///Studio 2015\\Projects\\programing_contest_c++\\Debug\\b.txt"

int main() {
  while (1) {

    vector<int> cs(8);
    vector<int> ps{4, 1, 4, 1, 2, 1, 2, 1};
    for (int i = 0; i < 8; ++i) {
      if (!(cin >> cs[i])) {
        exit(true);
      }
    }
    int ans = 10000000;
    int aa = 0;
    vector<int> ansv(8);
    for (int off = 0; off < 8; ++off) {
      int nans = 0;
      vector<int> nansv;
      for (int k = 0; k < 8; ++k) {
        nans += max(0, cs[(k + off) % 8] - ps[k]);
        nansv.push_back(max(0, cs[(k + off) % 8] - ps[k]));
      }
      if (ans > nans) {
        ans = nans;
        ansv = nansv;
        aa = off;
      } else if (ans == nans) {
        bool ok = false;
        for (int i = 0; i < 8; ++i) {
          if (ansv[i] < nansv[i]) {
            ok = true;
            break;
          } else if (ansv[i] > nansv[i]) {
            ok = false;
            break;
          } else {
          }
        }
        if (ok) {
          ans = nans;
          ansv = nansv;
          aa = off;
        }
      }
    }
    for (int i = 0; i < 8; ++i) {
      cout << ps[(i + 8 - aa) % 8];
      if (i == 7)
        cout << endl;
      else
        cout << " ";
    }
  }

  return 0;
}