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

#include <bits/stdc++.h>
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimization("Ofast")
#pragma GCC optimization("unroll-loops")
using namespace std;
int D;
int c[27], s[400][27];
int L[27];
int last[27];
long long ANS = 0;
long long ans = 0;
int T[400];
long long test() {
  for (int i = 1; i <= 26; i++) {
    last[i] = 0;
  }
  long long ans = 0;
  for (int i = 1; i <= D; i++) {
    ans += s[i][T[i]];
    last[T[i]] = i;
    for (int j = 1; j <= 26; j++) {
      ans -= c[j] * (i - last[j]);
    }
  }
  return ans;
}
int main() {
  auto begin_time = chrono::steady_clock::now().time_since_epoch().count();
  scanf("%d", &D);
  for (int i = 1; i <= 26; i++)
    scanf("%d", &c[i]);
  for (int i = 1; i <= D; i++) {
    for (int j = 1; j <= 26; j++)
      scanf("%d", &s[i][j]);
  }
  for (int i = 1; i <= D; i++) {
    int b1 = -1;
    long long ok = 0;
    for (int t = 1; t <= 26; t++) {
      for (int x = 1; x <= 26; x++) {
        for (int y = 1; y <= 26; y++) {
          ans = ANS;
          for (int j = 1; j <= 26; j++) {
            last[j] = L[j];
          }

          ans += s[i][t];
          last[t] = i;

          for (int j = 1; j <= 26; j++) {
            ans -= c[j] * (i - last[j]);
          }

          if (i != D) {
            ans += s[i + 1][x];
            last[x] = i + 1;
            for (int j = 1; j <= 26; j++) {
              ans -= c[j] * (i + 1 - last[j]);
            }
          }

          if (i < D - 1) {
            ans += s[i + 2][y];
            last[y] = i + 2;
            for (int j = 1; j <= 26; j++) {
              ans -= c[j] * (i + 2 - last[j]);
            }
          }

          if (b1 == -1 || ans > ok) {
            b1 = t;
            ok = ans;
          }
        }
      }
      // printf("for %d: %lld\\n",t,ans);
    }
    T[i] = b1;
    ANS += s[i][b1];
    L[b1] = i;
    for (int j = 1; j <= 26; j++) {
      ANS -= c[j] * (i - L[j]);
    }
    // printf("%d\\n",b1);
    // printf("score %lld\\n",ANS);
  }
  long long best = ANS;
  // printf("from %lld\\n",best);
  srand(time(NULL));
  while (chrono::steady_clock::now().time_since_epoch().count() - begin_time <
         1950000000) {
    int rD = rand() % 365 + 1;
    int rV = rand() % 26 + 1;
    int oldV = T[rD];
    T[rD] = rV;
    long long cur = test();
    if (cur > best) {
      best = cur;
    } else {
      T[rD] = oldV;
    }
  }
  // printf("to %lld\\n",best);
  for (int i = 1; i <= D; i++) {
    printf("%d\\n", T[i]);
  }
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimization("Ofast")
#pragma GCC optimization("unroll-loops")
using namespace std;
int D;
int c[27], s[400][27];
int L[27];
int last[27];
long long ANS = 0;
long long ans = 0;
int T[27];
long long test() {
  for (int i = 1; i <= 26; i++) {
    last[i] = 0;
  }
  long long ans = 0;
  for (int i = 1; i <= D; i++) {
    ans += s[i][T[i]];
    last[T[i]] = i;
    for (int j = 1; j <= 26; j++) {
      ans -= c[j] * (i - last[j]);
    }
  }
  return ans;
}
int main() {
  auto begin_time = chrono::steady_clock::now().time_since_epoch().count();
  scanf("%d", &D);
  for (int i = 1; i <= 26; i++)
    scanf("%d", &c[i]);
  for (int i = 1; i <= D; i++) {
    for (int j = 1; j <= 26; j++)
      scanf("%d", &s[i][j]);
  }
  for (int i = 1; i <= D; i++) {
    int b1 = -1;
    long long ok = 0;
    for (int t = 1; t <= 26; t++) {
      for (int x = 1; x <= 26; x++) {
        for (int y = 1; y <= 26; y++) {
          ans = ANS;
          for (int j = 1; j <= 26; j++) {
            last[j] = L[j];
          }

          ans += s[i][t];
          last[t] = i;

          for (int j = 1; j <= 26; j++) {
            ans -= c[j] * (i - last[j]);
          }

          if (i != D) {
            ans += s[i + 1][x];
            last[x] = i + 1;
            for (int j = 1; j <= 26; j++) {
              ans -= c[j] * (i + 1 - last[j]);
            }
          }

          if (i < D - 1) {
            ans += s[i + 2][y];
            last[y] = i + 2;
            for (int j = 1; j <= 26; j++) {
              ans -= c[j] * (i + 2 - last[j]);
            }
          }

          if (b1 == -1 || ans > ok) {
            b1 = t;
            ok = ans;
          }
        }
      }
      // printf("for %d: %lld\\n",t,ans);
    }
    T[i] = b1;
    ANS += s[i][b1];
    L[b1] = i;
    for (int j = 1; j <= 26; j++) {
      ANS -= c[j] * (i - L[j]);
    }
    // printf("%d\\n",b1);
    // printf("score %lld\\n",ANS);
  }
  long long best = ANS;
  // printf("from %lld\\n",best);
  srand(time(NULL));
  while (chrono::steady_clock::now().time_since_epoch().count() - begin_time <
         1950000000) {
    int rD = rand() % 365 + 1;
    int rV = rand() % 26 + 1;
    int oldV = T[rD];
    T[rD] = rV;
    long long cur = test();
    if (cur > best) {
      best = cur;
    } else {
      T[rD] = oldV;
    }
  }
  // printf("to %lld\\n",best);
  for (int i = 1; i <= D; i++) {
    printf("%d\\n", T[i]);
  }
}