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

#include <algorithm> // require sort next_permutation count __gcd reverse etc.
#include <cctype>    // require tolower, toupper
#include <cfloat>
#include <climits>
#include <cmath>   // require fabs
#include <cstdio>  // require scanf printf
#include <cstdlib> // require abs exit atof atoi
#include <cstring> // require memset
#include <ctime>   // require srand
#include <deque>
#include <fstream> // require freopen
#include <functional>
#include <iomanip> // require setw
#include <iostream>
#include <limits>
#include <map>
#include <numeric> // require accumulate
#include <queue>
#include <set>
#include <sstream> // require stringstream
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(A) A.begin(), A.end()
#define INF 1 << 29

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

const int usage[] = {4, 1, 4, 1, 2, 1, 2, 1};

int main() {
  //	cut here before submit
  //	freopen ("testcase.flying_jenny", "r", stdin );

  vector<vector<int>> c(8, vector<int>(8, 0));
  rep(i, 8) c[0][i] = usage[i];
  for (int i = 1; i < 8; i++) {
    rep(j, 8) { c[i][j] = c[i - 1][(i + j) % 8]; } // end rep
  }                                                // end for
  sort(ALL(c));
  /*
          rep (i, 8 ){
                  rep (j, 8 ){
                          cerr << setw(2) << c[i][j];
                  } // end rep
                  cerr << endl;
          } // end rep
  */
  string str = "";
  while (getline(cin, str)) {
    stringstream ss(str);
    vector<int> p(8, 0);
    rep(i, 8) { ss >> p[i]; } // end rep

    int res = INF;
    int ans = -1;
    rep(i, 8) {
      int curr = 0;
      rep(j, 8) { curr += max(p[j] - c[i][j], 0); } // end rep
      if (res > curr) {
        res = curr;
        ans = i;
      }                                                       // end if
    }                                                         // end rep
    rep(i, 8) { cout << c[ans][i] << (i != 7 ? ' ' : '\\n'); } // end rep
  }                                                           // end loop

  return 0;
}

Error: 0
 1 2 1 2 1 4 1 4
 1 2 1 4 1 4 1 2
 1 4 1 2 1 2 1 4
 1 4 1 4 1 2 1 2
 2 1 2 1 4 1 4 1
 2 1 4 1 4 1 2 1
 4 1 2 1 2 1 4 1
 4 1 4 1 2 1 2 1


Fixed submission:

#include <algorithm> // require sort next_permutation count __gcd reverse etc.
#include <cctype>    // require tolower, toupper
#include <cfloat>
#include <climits>
#include <cmath>   // require fabs
#include <cstdio>  // require scanf printf
#include <cstdlib> // require abs exit atof atoi
#include <cstring> // require memset
#include <ctime>   // require srand
#include <deque>
#include <fstream> // require freopen
#include <functional>
#include <iomanip> // require setw
#include <iostream>
#include <limits>
#include <map>
#include <numeric> // require accumulate
#include <queue>
#include <set>
#include <sstream> // require stringstream
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(A) A.begin(), A.end()
#define INF 1 << 29

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

const int usage[] = {4, 1, 4, 1, 2, 1, 2, 1};

int main() {
  //	cut here before submit
  //	freopen ("testcase.flying_jenny", "r", stdin );

  vector<vector<int>> c(8, vector<int>(8, 0));
  rep(i, 8) c[0][i] = usage[i];
  for (int i = 1; i < 8; i++) {
    rep(j, 8) { c[i][j] = c[i - 1][(i + j) % 8]; } // end rep
  }                                                // end for
  sort(ALL(c));

  rep(i, 8) {
    rep(j, 8) { cerr << setw(2) << c[i][j]; } // end rep
    cerr << endl;
  } // end rep

  string str = "";
  while (getline(cin, str)) {
    stringstream ss(str);
    vector<int> p(8, 0);
    rep(i, 8) { ss >> p[i]; } // end rep

    int res = INF;
    int ans = -1;
    rep(i, 8) {
      int curr = 0;
      rep(j, 8) { curr += max(p[j] - c[i][j], 0); } // end rep
      if (res > curr) {
        res = curr;
        ans = i;
      }                                                       // end if
    }                                                         // end rep
    rep(i, 8) { cout << c[ans][i] << (i != 7 ? ' ' : '\\n'); } // end rep
  }                                                           // end loop

  return 0;
}