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

#include <bits/stdc++.h>
#define LOCAL
using namespace std;
template <typename A, typename B>
ostream &operator<<(ostream &out, const pair<A, B> &a) {
  out << "(" << a.first << "," << a.second << ")";
  return out;
}
template <typename T, size_t N>
ostream &operator<<(ostream &out, const array<T, N> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T> ostream &operator<<(ostream &out, const vector<T> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T, class Cmp>
ostream &operator<<(ostream &out, const set<T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "}";
  return out;
}
template <typename U, typename T, class Cmp>
ostream &operator<<(ostream &out, const map<U, T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &p : a) {
    out << (first ? "" : ", ");
    out << p.first << ":" << p.second;
    first = 0;
  }
  out << "}";
  return out;
}
#ifdef LOCAL
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 42
#endif
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
#define int long long
using P = pair<int, int>;
// #########################################
int last[40];
int s[400][400];
int c[26];
int d;
int calc(vector<int> &t) {
  int ret = 0;

  rep(i, d) {
    // use t[i];
    ret += s[i][t[i] - 1];
    last[t[i] - 1] = i + 1;
    rep(j, 26) { ret -= c[j] * (i + 1 - last[j]); }
  }
  return ret;
}
signed main() {
  cin >> d;
  rep(i, 26) cin >> c[i];
  vector<int> ans(d);
  vector<int> t(d);
  rep(i, d) rep(j, 26) cin >> s[i][j];
  int cnt = 0;
  int res = 0;
  int mx[400];
  rep(i, d) {
    P v = P(0, 0);
    rep(j, 26) { v = max(v, P(s[i][j], j)); }
    mx[i] = v.second + 1;
  }
  while (cnt < 10000) {
    rep(i, d) {
      if (i >= cnt % d && i <= cnt % d + 30)
        t[i] = mx[i];
      else
        t[i] = (cnt + i) % 26 + 1;
    }

    if (res < calc(t)) {
      res = max(res, calc(t));
      rep(i, d) ans[i] = t[i];
    }
    cnt++;
  }
  rep(i, d) cout << ans[i] << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define LOCAL
using namespace std;
template <typename A, typename B>
ostream &operator<<(ostream &out, const pair<A, B> &a) {
  out << "(" << a.first << "," << a.second << ")";
  return out;
}
template <typename T, size_t N>
ostream &operator<<(ostream &out, const array<T, N> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T> ostream &operator<<(ostream &out, const vector<T> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T, class Cmp>
ostream &operator<<(ostream &out, const set<T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "}";
  return out;
}
template <typename U, typename T, class Cmp>
ostream &operator<<(ostream &out, const map<U, T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &p : a) {
    out << (first ? "" : ", ");
    out << p.first << ":" << p.second;
    first = 0;
  }
  out << "}";
  return out;
}
#ifdef LOCAL
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 42
#endif
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
#define int long long
using P = pair<int, int>;
// #########################################
int last[40];
int s[400][400];
int c[26];
int d;
int calc(vector<int> &t) {
  int ret = 0;

  rep(i, d) {
    // use t[i];
    ret += s[i][t[i] - 1];
    last[t[i] - 1] = i + 1;
    rep(j, 26) { ret -= c[j] * (i + 1 - last[j]); }
  }
  return ret;
}
signed main() {
  cin >> d;
  rep(i, 26) cin >> c[i];
  vector<int> ans(d);
  vector<int> t(d);
  rep(i, d) rep(j, 26) cin >> s[i][j];
  int cnt = 0;
  int res = 0;
  int mx[400];
  rep(i, d) {
    P v = P(0, 0);
    rep(j, 26) { v = max(v, P(s[i][j], j)); }
    mx[i] = v.second + 1;
  }
  // rep(i,d)cout << mx[i] << endl;
  while (cnt < 10000) {
    rep(i, d) {
      if (i <= 100)
        t[i] = mx[i];
      else
        t[i] = (cnt + i % 26) + 1;
    }

    if (res < calc(t)) {
      res = max(res, calc(t));
      rep(i, d) ans[i] = t[i];
    }
    cnt++;
  }
  rep(i, d) cout << ans[i] << endl;
  // cout << calc(ans) << endl;
  return 0;
}