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

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define P(p) cout << (p) << endl
#define rep(i, m, n) for (int i = (m); i < (int)(n); i++)
#define rrep(i, m, n) for (int i = (int)(m); i >= (int)(n); i--)
#define vsort(v) sort(v.begin(), v.end());
#define rvsort(v) sort(v.begin(), v.end(), greater<int>());
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define yes cout << "yes" << endl
#define no cout << "no" << endl
#define ret return
#define lb(v, n) lower_bound(v.begin(), v.end(), n)
#define ub(v, n) upper_bound(v.begin(), v.end(), n)
#define mae(v) max_element(v.begin(), v.end())
#define mie(v) min_element(v.begin(), v.end())

////////////////////////////////////////////////////////////

int main() {
  int p[8];
  while (cin >> p[0]) {
    rep(i, 1, 8) cin >> p[i];

    queue<int> q;
    q.push(4);
    q.push(1);
    q.push(4);
    q.push(1);
    q.push(2);
    q.push(1);
    q.push(2);
    q.push(1);

    queue<int> q2 = q;

    int ans = 0x7FFFFFFF;
    vector<int> idx;
    rep(i, 0, 8) {

      int sum = 0;
      rep(j, 0, 8) {
        int num = q.front();
        q.pop();
        // cout << "test:" << p[j] - num << " num:" << num << endl;
        sum += max(p[j] - num, 0);
        q.push(num);
      }

      // cout << "sum:" << sum << endl;
      if (ans > sum) {
        ans = sum;
        vector<int> tmp2;
        idx = tmp2;
        idx.push_back(i);
      } else if (ans == sum) {
        idx.push_back(i);
      }

      // cout << "ans:" << ans << " idx:" << idx[0] << endl;

      q.push(q.front());
      q.pop();
    }

    stringstream ss;
    long out = 99999999;
    string outS;
    rep(i, 0, idx.size()) {
      q = q2;
      rep(j, 0, idx[i]) {
        q.push(q.front());
        q.pop();
      }

      rep(i, 0, 8) {
        ss << q.front();
        q.pop();
      }

      string s = ss.str();

      ss.str("");
      long che;
      sscanf(s.c_str(), "%ld", &che);
      // cout << "che:" << che << endl;
      if (out > che) {
        outS = s;
        out = che;
      }
    }

    rep(i, 0, 8) {
      cout << outS[i];
      if (i != 7)
        cout << " ";
      else
        cout << endl;
    }
  }

  ret 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define P(p) cout << (p) << endl
#define rep(i, m, n) for (int i = (m); i < (int)(n); i++)
#define rrep(i, m, n) for (int i = (int)(m); i >= (int)(n); i--)
#define vsort(v) sort(v.begin(), v.end());
#define rvsort(v) sort(v.begin(), v.end(), greater<int>());
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define yes cout << "yes" << endl
#define no cout << "no" << endl
#define ret return
#define lb(v, n) lower_bound(v.begin(), v.end(), n)
#define ub(v, n) upper_bound(v.begin(), v.end(), n)
#define mae(v) max_element(v.begin(), v.end())
#define mie(v) min_element(v.begin(), v.end())

////////////////////////////////////////////////////////////

int main() {
  int p[8];
  while (cin >> p[0]) {
    rep(i, 1, 8) cin >> p[i];

    queue<int> q;
    q.push(4);
    q.push(1);
    q.push(4);
    q.push(1);
    q.push(2);
    q.push(1);
    q.push(2);
    q.push(1);

    queue<int> q2 = q;

    int ans = 0x7FFFFFFF;
    vector<int> idx;
    rep(i, 0, 8) {

      int sum = 0;
      rep(j, 0, 8) {
        int num = q.front();
        q.pop();
        // cout << "test:" << p[j] - num << " num:" << num << endl;
        sum += max(p[j] - num, 0);
        q.push(num);
      }

      // cout << "sum:" << sum << endl;
      if (ans > sum) {
        ans = sum;
        vector<int> tmp2;
        idx = tmp2;
        idx.push_back(i);
      } else if (ans == sum) {
        idx.push_back(i);
      }

      // cout << "ans:" << ans << " idx:" << idx[0] << endl;

      q.push(q.front());
      q.pop();
    }

    stringstream ss;
    long out = 99999999;
    string outS;
    rep(i, 0, idx.size()) {
      q = q2;
      rep(j, 0, idx[i]) {
        q.push(q.front());
        q.pop();
      }

      rep(i, 0, 8) {
        ss << q.front();
        q.pop();
      }

      string s = ss.str();
      if (out > stoi(s)) {
        outS = s;
        out = stoi(s);
      }
    }

    rep(i, 0, 8) {
      cout << outS[i];
      if (i != 7)
        cout << " ";
      else
        cout << endl;
    }
  }

  ret 0;
}