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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
typedef int64_t Int;

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

  vector<int> t(D);
  vector<vector<int>> last(26, vector<int>(1, -1));
  Int manzoku = 0;

  rep(j, 1) {
    int maxs = 0;
    rep(i, 26) maxs = max(maxs, s[j][i]);
    vector<int> v(0); // s[j][i]が最も大きくなるようなiを入れる
    rep(i, 26) {
      if (s[j][i] == maxs) {
        v.push_back(i + 1);
      }
    }
    if (v.size() == 1) {
      t[j] = v[0];
      last[t[j] - 1].push_back(j);
    } else { // 仮
      t[j] = v[0];
      last[t[j] - 1].push_back(j);
    }
  }
  int i = t[0] - 1;
  rep1(j, D - 1) {
    i = (i + 1) % 26;
    t[j] = i + 1;
    last[t[j] - 1].push_back(j);
  }

  rep(i, 26) last[i].push_back(D);

  vector<int> nums(26, 0);
  rep(i, 26) nums[i] = last[i].size();
  /*
      int M = 10000;
      rep(k,M) {
          int d, q;
          vector<pair<int,int>> vp(26);
          rep(i,26) vp[i] = make_pair(c[i] - 10 * nums[i],i);
          sort(vp.begin(),vp.end());
          q = vp[0].second;
          int p;
          for (int j = 25; j>=0; j--) {
              p = vp[j].second;
              if (last[p].size() == 2) continue;
              else {
                  break;
              }
          }
          if (last[p].size() <= 2) continue;
          int lpsz = last[p].size();
          for (int i = 1; i <= lpsz-2; i++) {
              d = last[p][i] + 1;
          manzoku = 0;

          manzoku -= s[d-1][t[d-1]-1];
          manzoku += s[d-1][q-1];

          auto iteold = find(last[t[d-1]-1].begin(), last[t[d-1]-1].end(), d-1);
          int old_last = *(iteold-1);
          manzoku -= c[t[d-1]-1] * (d-1 - old_last) * (*(iteold+1) - (d-1));

          auto itenew = upper_bound(last[q-1].begin(), last[q-1].end(), d-1);
          int new_last = *(itenew-1);
          manzoku += c[q-1] * (d-1 - new_last) * (*itenew - (d-1));

          if (manzoku > 0) {
              //更新したほうがよいならこれをやる
              last[t[d-1]-1].erase(iteold);
              last[q-1].insert(itenew,d-1);
              t[d-1] = q;
              nums[t[d-1]-1]--;
              nums[q-1]++;
          }
          }

          //cout << manzoku << endl;
      }
  */
  rep(j, D) cout << t[j] << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
typedef int64_t Int;

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

  vector<int> t(D);
  vector<vector<int>> last(26, vector<int>(1, -1));
  Int manzoku = 0;

  rep(j, D) {
    int maxs = 0;
    rep(i, 26) maxs = max(maxs, s[j][i]);
    vector<int> v(0); // s[j][i]が最も大きくなるようなiを入れる
    rep(i, 26) {
      if (s[j][i] == maxs) {
        v.push_back(i + 1);
      }
    }
    if (v.size() == 1) {
      t[j] = v[0];
      last[t[j] - 1].push_back(j);
    } else { // 仮
      t[j] = v[0];
      last[t[j] - 1].push_back(j);
    }
  }

  rep(i, 26) last[i].push_back(D);

  vector<int> nums(26, 0);
  rep(i, 26) nums[i] = last[i].size();

  int M = 10000;
  rep(k, M) {
    int d, q;
    vector<pair<int, int>> vp(26);
    rep(i, 26) vp[i] = make_pair(c[i] - 10 * nums[i], i);
    sort(vp.begin(), vp.end());
    q = vp[0].second;
    int p;
    for (int j = 25; j >= 0; j--) {
      p = vp[j].second;
      if (last[p].size() == 2)
        continue;
      else {
        break;
      }
    }
    if (last[p].size() <= 2)
      continue;
    int lpsz = last[p].size();
    for (int i = 1; i <= lpsz - 2; i++) {
      d = last[p][i] + 1;
      manzoku = 0;

      manzoku -= s[d - 1][t[d - 1] - 1];
      manzoku += s[d - 1][q - 1];

      auto iteold =
          find(last[t[d - 1] - 1].begin(), last[t[d - 1] - 1].end(), d - 1);
      int old_last = *(iteold - 1);
      manzoku -=
          c[t[d - 1] - 1] * (d - 1 - old_last) * (*(iteold + 1) - (d - 1));

      auto itenew = upper_bound(last[q - 1].begin(), last[q - 1].end(), d - 1);
      int new_last = *(itenew - 1);
      manzoku += c[q - 1] * (d - 1 - new_last) * (*itenew - (d - 1));

      if (manzoku > 0) {
        // 更新したほうがよいならこれをやる
        last[t[d - 1] - 1].erase(iteold);
        last[q - 1].insert(itenew, d - 1);
        t[d - 1] = q;
        nums[t[d - 1] - 1]--;
        nums[q - 1]++;
      }
    }

    // cout << manzoku << endl;
  }

  rep(j, D) cout << t[j] << endl;
}