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

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

const int WIDTH = 300;
const int DEBUG = 0;

struct State {
  int score;
  int prevs;
  int sousa;
  int Last[29];
};

bool operator<(const State &a1, const State &a2) {
  if (a1.score < a2.score)
    return true;
  return false;
}

int N;
int C[29], S[409][29];
int cnts[409];
State G[409][WIDTH + 9];
State H[(WIDTH * 26) + 9];

void solve(int pos, int num) {
  for (int i = 1; i <= 26; i++) {
    int NewScore = G[pos][num].score;
    NewScore += S[pos + 1][i];
    for (int j = 1; j <= 26; j++) {
      if (i != j)
        NewScore -= C[j] * ((pos + 1) - G[pos][num].Last[j]);
    }
    H[cnts[pos + 1]].score = NewScore;
    H[cnts[pos + 1]].prevs = num;
    H[cnts[pos + 1]].sousa = i;
    for (int j = 1; j <= 26; j++)
      H[cnts[pos + 1]].Last[j] = G[pos][num].Last[j];
    H[cnts[pos + 1]].Last[i] = pos + 1;
    cnts[pos + 1]++;
  }
}

int main() {
  // 入力
  if (DEBUG == 0) {
    cin >> N;
    for (int i = 1; i <= 26; i++)
      cin >> C[i];
    for (int i = 1; i <= N; i++) {
      for (int j = 1; j <= 26; j++)
        cin >> S[i][j];
    }
  } else {
    N = 365;
    for (int i = 1; i <= 26; i++)
      C[i] = rand() % 101;
    for (int i = 1; i <= N; i++) {
      for (int j = 1; j <= 26; j++)
        S[i][j] = ((rand() % 32768) * 32768 + (rand() % 32768)) % 20001;
    }
  }

  // ビームサーチ
  G[0][0].prevs = -1;
  cnts[0] = 1;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < cnts[i]; j++)
      solve(i, j);
    sort(H, H + cnts[i + 1]);
    reverse(H, H + cnts[i + 1]);
    cnts[i + 1] = min(cnts[i + 1], WIDTH);
    for (int j = 0; j < cnts[i + 1]; j++)
      G[i + 1][j] = H[j];
  }

  // 復元
  vector<int> Answer;
  int cx = 0;
  for (int i = N; i >= 1; i--) {
    Answer.push_back(G[i][cx].sousa);
    cx = G[i][cx].prevs;
  }
  reverse(Answer.begin(), Answer.end());
  for (int i = 0; i < Answer.size(); i++)
    cout << Answer[i] << endl;
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

const int WIDTH = 300;
const int DEBUG = 0;

struct State {
  int score;
  int prevs;
  int sousa;
  int Last[29];
};

bool operator<(const State &a1, const State &a2) {
  if (a1.score < a2.score)
    return true;
  return false;
}

int N;
int C[29], S[409][29];
int cnts[409];
State G[409][WIDTH + 9];
State H[(WIDTH * 26) + 9];

void solve(int pos, int num) {
  for (int i = 1; i <= 26; i++) {
    int NewScore = G[pos][num].score;
    NewScore += S[pos + 1][i];
    for (int j = 1; j <= 26; j++) {
      if (i != j)
        NewScore -= C[j] * ((pos + 1) - G[pos][num].Last[j]);
    }
    H[cnts[pos + 1]].score = NewScore;
    H[cnts[pos + 1]].prevs = pos;
    H[cnts[pos + 1]].sousa = i;
    for (int j = 1; j <= 26; j++)
      H[cnts[pos + 1]].Last[j] = G[pos][num].Last[j];
    H[cnts[pos + 1]].Last[i] = pos + 1;
    cnts[pos + 1]++;
  }
}

int main() {
  // 入力
  if (DEBUG == 0) {
    cin >> N;
    for (int i = 1; i <= N; i++)
      cin >> C[i];
    for (int i = 1; i <= N; i++) {
      for (int j = 1; j <= 26; j++)
        cin >> S[i][j];
    }
  } else {
    N = 365;
    for (int i = 1; i <= N; i++)
      C[i] = rand() % 101;
    for (int i = 1; i <= N; i++) {
      for (int j = 1; j <= 26; j++)
        S[i][j] = ((rand() % 32768) * 32768 + (rand() % 32768)) % 20001;
    }
  }

  // ビームサーチ
  G[0][0].prevs = -1;
  cnts[0] = 1;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < cnts[i]; j++)
      solve(i, j);
    sort(H, H + cnts[i + 1]);
    reverse(H, H + cnts[i + 1]);
    cnts[i + 1] = min(cnts[i + 1], WIDTH);
    for (int j = 0; j < cnts[i + 1]; j++)
      G[i + 1][j] = H[j];
  }

  // 復元
  vector<int> Answer;
  int cx = 0;
  for (int i = N; i >= 1; i--) {
    Answer.push_back(G[i][cx].sousa);
    cx = G[i][cx].prevs;
  }
  reverse(Answer.begin(), Answer.end());
  for (int i = 0; i < Answer.size(); i++)
    cout << Answer[i] << endl;
  return 0;
}