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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a) for (int i = 0; i < (a); i++)
typedef long long ll;
template <typename T> struct edge {
  int src, to;
  T cost;
  edge(int to, T cost) : src(-1), to(to), cost(cost) {}
  edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}
  edge &operator=(const int &x) {
    to = x;
    return *this;
  }
  operator int() const { return to; }
};
template <typename T> using Edges = vector<edge<T>>;
template <typename T> using WeightedGraph = vector<Edges<T>>;
using UnWeightedGraph = vector<vector<int>>;
template <typename T> using Matrix = vector<vector<T>>;

/////////////////////////////////////////////////////////////////////
const ll inf = 1LL << 60;

ll d;
vector<ll> c(26);
vector<vector<ll>> s(365, vector<ll>(26));
vector<ll> t(365);

vector<ll> calc() {
  vector<ll> nsc(26);
  vector<ll> score(d);
  vector<ll> last(26, -1);
  rep(i, d) {
    last[t[i]] = i;
    rep(j, 26) {
      if (t[i] == j)
        nsc[j] += s[i][j];
      else
        nsc[j] -= c[j] * (i - last[j]);
    }
    ll sum = 0;
    rep(j, 26) sum += nsc[j];
    score[i] = sum;
  }
  return score;
}

int main() {
  cin >> d;
  rep(i, 26) cin >> c[i];
  rep(i, d) rep(j, 26) cin >> s[i][j];

  rep(i, d) {
    ll tmp = 0;
    ll ma = -inf;
    rep(j, 26) {
      if (s[i][j] > ma) {
        ma = s[i][j];
        tmp = j;
      } else if (s[i][j] == ma) {
        if (c[j] < c[tmp]) {
          tmp = j;
        }
      }
    }
    t[i] = tmp;
  }
  vector<ll> fsc = calc();
  // for (auto v:fsc) cout << v << endl;
  // cout << endl;

  {
    vector<ll> last(26, -1);
    vector<ll> nsc(26);
    rep(i, d) {
      ll idx = 0;
      ll sum = 0;
      rep(j, 26) {
        nsc[j] -= c[j] * (i - last[j]);
        sum += nsc[j];
      }
      rep(j, 26) {
        ll aa = sum + c[j] * (i - last[j]) + s[i][j];
        ll bb = sum + c[idx] * (i - last[idx]) + s[i][idx];
        if (aa > bb && aa >= fsc[i]) {
          idx = j;
        }
      }
      nsc[idx] += c[idx] * (i - last[idx]) + s[i][idx];
      last[idx] = i;
      t[i] = idx;
    }
  }
  rep(i, d) cout << t[i] + 1 << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a) for (int i = 0; i < (a); i++)
typedef long long ll;
template <typename T> struct edge {
  int src, to;
  T cost;
  edge(int to, T cost) : src(-1), to(to), cost(cost) {}
  edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}
  edge &operator=(const int &x) {
    to = x;
    return *this;
  }
  operator int() const { return to; }
};
template <typename T> using Edges = vector<edge<T>>;
template <typename T> using WeightedGraph = vector<Edges<T>>;
using UnWeightedGraph = vector<vector<int>>;
template <typename T> using Matrix = vector<vector<T>>;

/////////////////////////////////////////////////////////////////////
const ll inf = 1LL << 60;

ll d;
vector<ll> c(26);
vector<vector<ll>> s(365, vector<ll>(26));
vector<ll> t(365);

vector<ll> calc() {
  vector<ll> nsc(26);
  vector<ll> score(d);
  vector<ll> last(26, -1);
  rep(i, d) {
    last[t[i]] = i;
    rep(j, 26) {
      if (t[i] == j)
        nsc[j] += s[i][j];
      else
        nsc[j] -= c[j] * (i - last[j]);
    }
    ll sum = 0;
    rep(j, 26) sum += nsc[j];
    score[i] = sum;
  }
  return score;
}

int main() {
  cin >> d;
  rep(i, 26) cin >> c[i];
  rep(i, d) rep(j, 26) cin >> s[i][j];

  // rep(i, d) {
  //   ll tmp = 0;
  //   ll ma = -inf;
  //   rep(j, 26) {
  //     if (s[i][j] > ma) {
  //       ma = s[i][j];
  //       tmp = j;
  //     } else if (s[i][j] == ma) {
  //       if (c[j] < c[tmp]) {
  //         tmp = j;
  //       }
  //     }
  //   }
  //   t[i] = tmp;
  // }
  // vector<ll> nt = t;
  // vector<ll> vvv = calc();
  // rep(i, d) cout << t[i]+1 << endl;
  // for (auto v:vvv) cout << v << endl;
  // cout << endl;

  {
    vector<ll> last(26, -1);
    vector<ll> fsc = calc();
    vector<ll> nsc(26);
    rep(i, d) {
      ll idx = 0;
      ll sum = 0;
      rep(j, 26) { sum += c[j] * (i - last[j]); }
      rep(j, 26) {
        ll aa = sum - c[j] * (i - last[j]) - s[i][j];
        ll bb = sum - c[idx] * (i - last[idx]) - s[i][idx];
        if (aa < bb) {
          idx = j;
        }
      }
      last[i] = i;
      t[i] = idx;
    }
  }
  // vector<ll> vvvv = calc();
  rep(i, d) cout << t[i] + 1 << endl;
  // for (auto v:vvvv) cout << v << endl;
  // cout << endl;

  return 0;
}