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

#include <algorithm>
#include <array>
#include <cmath>
#include <iostream>
#include <set>
#include <vector>

const uint32_t D = 365 + 1;
const uint32_t contests = 26 + 1;

int main() {
  {
    uint32_t Dtmp = 0;
    std::cin >> Dtmp;
  }

  std::array<uint32_t, contests> cost;
  for (size_t c = 1; c < contests; c++)
    std::cin >> cost[c];

  std::array<std::array<uint32_t, contests>, D> sat;
  for (size_t d = 1; d < D; d++)
    for (size_t c = 1; c < contests; c++)
      std::cin >> sat[d][c];

  // solve problem
  std::array<uint32_t, D> test;
  for (size_t d = 1; d < D; d++)
    std::cin >> test[d];
  // for (size_t d = 1; d < D; d++)
  // {
  //   uint32_t max_val = 0, max_ind = 0;
  //   for (size_t c = 1; c < contests; c++){
  //     if(sat[d][c] > max_val){
  //       max_val = sat[d][c];
  //       max_ind = i;
  //     }
  //   }
  //   test[d] = max_ind + 1;
  // }

  // for(size_t d = 1; d < D; d++) std::cout << test[d] << std::endl;

  // calc satisfaction
  std::array<int64_t, D> value;
  value[0] = 0;
  std::array<std::set<uint32_t>, contests> history;
  for (auto &&h : history)
    h.insert(0), h.insert(D);
  for (size_t d = 1; d < D; d++) {
    value[d] = value[d - 1] + sat[d][test[d]];
    history[test[d]].insert(d);

    int64_t c_sum = 0;
    for (size_t c = 1; c < contests; c++)
      c_sum += cost[c] * (d - *std::next(history[c].crbegin()));
    value[d] -= c_sum;
  }

  // query
  int64_t v = value.back();
  uint32_t M = 0;
  std::cin >> M;
  for (size_t m = 0; m < M; m++) {
    uint32_t d = 0, c = 0;
    std::cin >> d >> c;

    v += sat[d][c];
    v -= sat[d][test[d]];

    auto it_before = history[test[d]].find(d);
    v -= cost[test[d]] * (d - *std::prev(it_before)) *
         (*std::next(it_before) - d);
    history[test[d]].erase(it_before);

    history[c].insert(d);
    auto it_after = history[c].find(d);
    v += cost[c] * (d - *std::prev(it_after)) * (*std::next(it_after) - d);

    test[d] = c;

    std::cout << v << std::endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <array>
#include <cmath>
#include <iostream>
#include <set>
#include <vector>

// const uint32_t D = 365 + 1;
const uint32_t D = 5 + 1;
const uint32_t contests = 26 + 1;

int main() {
  {
    uint32_t Dtmp = 0;
    std::cin >> Dtmp;
  }

  std::array<uint32_t, contests> cost;
  for (size_t c = 1; c < contests; c++)
    std::cin >> cost[c];

  std::array<std::array<uint32_t, contests>, D> sat;
  for (size_t d = 1; d < D; d++)
    for (size_t c = 1; c < contests; c++)
      std::cin >> sat[d][c];

  // solve problem
  std::array<uint32_t, D> test;
  for (size_t d = 1; d < D; d++)
    std::cin >> test[d];
  // for (size_t d = 1; d < D; d++)
  // {
  //   uint32_t max_val = 0, max_ind = 0;
  //   for (size_t c = 1; c < contests; c++){
  //     if(sat[d][c] > max_val){
  //       max_val = sat[d][c];
  //       max_ind = i;
  //     }
  //   }
  //   test[d] = max_ind + 1;
  // }

  // for(size_t d = 1; d < D; d++) std::cout << test[d] << std::endl;

  // calc satisfaction
  std::array<int64_t, D> value;
  value[0] = 0;
  std::array<std::set<uint32_t>, contests> history;
  for (auto &&h : history)
    h.insert(0), h.insert(D);
  for (size_t d = 1; d < D; d++) {
    value[d] = value[d - 1] + sat[d][test[d]];
    history[test[d]].insert(d);

    int64_t c_sum = 0;
    for (size_t c = 1; c < contests; c++)
      c_sum += cost[c] * (d - *std::next(history[c].crbegin()));
    value[d] -= c_sum;
  }

  // query
  int64_t v = value.back();
  uint32_t M = 0;
  std::cin >> M;
  for (size_t m = 0; m < M; m++) {
    uint32_t d = 0, c = 0;
    std::cin >> d >> c;

    v += sat[d][c];
    v -= sat[d][test[d]];

    auto it_before = history[test[d]].find(d);
    v -= cost[test[d]] * (d - *std::prev(it_before)) *
         (*std::next(it_before) - d);
    history[test[d]].erase(it_before);

    history[c].insert(d);
    auto it_after = history[c].find(d);
    v += cost[c] * (d - *std::prev(it_after)) * (*std::next(it_after) - d);

    test[d] = c;

    std::cout << v << std::endl;
  }

  return 0;
}