Problem p02619 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("O3")
// #pragma GCC target ("avx")
#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>

#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define NDEBUG
#define eprintf(...)                                                           \
  do {                                                                         \
  } while (0)
#endif
#include <cassert>

using namespace std;

typedef long long LL;
typedef vector<int> VI;

#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define EACH(i, c)                                                             \
  for (__typeof((c).begin()) i = (c).begin(), i##_end = (c).end();             \
       i != i##_end; ++i)

template <class T> inline void amin(T &x, const T &y) {
  if (y < x)
    x = y;
}
template <class T> inline void amax(T &x, const T &y) {
  if (x < y)
    x = y;
}
template <class Iter> void rprintf(const char *fmt, Iter begin, Iter end) {
  for (bool sp = 0; begin != end; ++begin) {
    if (sp)
      putchar(' ');
    else
      sp = true;
    printf(fmt, *begin);
  }
  putchar('\n');
}

int D;
int C[26];
int S[356][26];
int T[356];

int last[26];

int calc() {
  int cur = 0;
  REP(c, 26) last[c] = -1;

  REP(i, D) {
    int t = T[i];
    last[t] = i;

    cur += S[i][t];
    REP(c, 26) cur -= C[c] * (i - last[c]);

    printf("%d\n", cur);
  }
  return cur;
}

void MAIN() {
  scanf("%d", &D);
  REP(c, 26) scanf("%d", C + c);
  REP(i, D) REP(c, 26) scanf("%d", S[i] + c);
  REP(i, D) scanf("%d", T + i), T[i]--;

  calc();
}

int main() {
  int TC = 1;
  //    scanf("%d", &TC);
  REP(tc, TC) MAIN();
  return 0;
}


Fixed submission:
#pragma GCC optimize("O3")
// #pragma GCC target ("avx")
#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>

#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define NDEBUG
#define eprintf(...)                                                           \
  do {                                                                         \
  } while (0)
#endif
#include <cassert>

using namespace std;

typedef long long LL;
typedef vector<int> VI;

#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define EACH(i, c)                                                             \
  for (__typeof((c).begin()) i = (c).begin(), i##_end = (c).end();             \
       i != i##_end; ++i)

template <class T> inline void amin(T &x, const T &y) {
  if (y < x)
    x = y;
}
template <class T> inline void amax(T &x, const T &y) {
  if (x < y)
    x = y;
}
template <class Iter> void rprintf(const char *fmt, Iter begin, Iter end) {
  for (bool sp = 0; begin != end; ++begin) {
    if (sp)
      putchar(' ');
    else
      sp = true;
    printf(fmt, *begin);
  }
  putchar('\n');
}

int D;
int C[26];
int S[365][26];
int T[365];

int last[26];

int calc() {
  int cur = 0;
  REP(c, 26) last[c] = -1;

  REP(i, D) {
    int t = T[i];
    last[t] = i;

    cur += S[i][t];
    REP(c, 26) cur -= C[c] * (i - last[c]);

    printf("%d\n", cur);
  }
  return cur;
}

void MAIN() {
  scanf("%d", &D);
  REP(c, 26) scanf("%d", C + c);
  REP(i, D) REP(c, 26) scanf("%d", S[i] + c);
  REP(i, D) scanf("%d", T + i), T[i]--;

  calc();
}

int main() {
  int TC = 1;
  //    scanf("%d", &TC);
  REP(tc, TC) MAIN();
  return 0;
}