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

#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC target("avx")
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int(i) = 0; i < (n); i++)
#define rrep(i, n) for (int(i) = 1; i <= (n); i++)
#define REP(i, m, n) for (int(i) = (m); (i) < (n); (i)++)
#define MOD 1000000007
#define INF 1e18
#define int long long
#define endl "\\n"
#define yorn(f) puts((f) ? "Yes" : "No")
#define YORN(f) puts((f) ? "YES" : "NO")
typedef long long ll;
typedef pair<int, int> P;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; };
int lcm(int a, int b) { return a / gcd(a, b) * b; };
int mod(int a, int b) { return (a + b - 1) / b; };
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
};
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
};

int D;
vector<int> c(26);
vector<vector<int>> s(365, vector<int>(26));
vector<int> last(26);

void input() {
  cin >> D;
  rep(i, 26) cin >> c[i];
  rep(i, D) { rep(j, 26) cin >> s[i][j]; }
}

void output(vector<int> &v) {
  rep(i, v.size()) { cout << v[i] + 1 << endl; }
}

int calc_score(vector<int> &t) {
  int score = 0;
  rep(i, t.size()) {
    last[t[i]] = i + 1;
    rep(j, 26) { score -= (i + 1 - last[i]) * c[i]; }
    score += s[i][t[i]];
  }
  return score;
}

signed main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);
  // cout << fixed << setprecision(15);

  input();
  vector<int> res;
  rep(i, D) {
    int maxscore = LONG_MIN;
    int bestId = 0;
    rep(j, 26) {
      res.push_back(j);
      int now = calc_score(res);
      if (maxscore < now) {
        maxscore = now;
        bestId = j;
      }
      res.pop_back();
    }
    res.push_back(bestId);
  }
  output(res);
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC target("avx")
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int(i) = 0; i < (n); i++)
#define rrep(i, n) for (int(i) = 1; i <= (n); i++)
#define REP(i, m, n) for (int(i) = (m); (i) < (n); (i)++)
#define MOD 1000000007
#define INF 1e18
#define int long long
#define endl "\\n"
#define yorn(f) puts((f) ? "Yes" : "No")
#define YORN(f) puts((f) ? "YES" : "NO")
typedef long long ll;
typedef pair<int, int> P;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; };
int lcm(int a, int b) { return a / gcd(a, b) * b; };
int mod(int a, int b) { return (a + b - 1) / b; };
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
};
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
};

int D;
vector<int> c(26); // コンテスト
vector<vector<int>> s(365, vector<int>(26));
vector<int> last(26, -1); // 最後に出た日

void input() {
  cin >> D;
  rep(i, 26) cin >> c[i];
  rep(i, D) { rep(j, 26) cin >> s[i][j]; }
}

signed main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);
  // cout << fixed << setprecision(15);

  input();
  int t[D];
  rep(i, D) {
    cin >> t[i];
    t[i]--;
  }

  int score = 0;
  rep(i, D) {
    score += s[i][t[i]];
    last[t[i]] = i;
    rep(j, 26) { score -= c[j] * (i - last[j]); }
    cout << score << endl;
  }
  return 0;
}