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

#include <bits/stdc++.h>
using namespace std;

#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define print(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define print(x)
#endif

const int inf = 1e9;
const int64_t inf64 = 1e18;
const double eps = 1e-9;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

int64_t gcd(int64_t a, int64_t b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

int64_t lcm(int64_t a, int64_t b) { return a / gcd(a, b) * b; }

void solve(int n) {
  map<int, vector<int>> x;
  rep(i, 0, n) {
    int d, t;
    cin >> d >> t;
    if (x.find(d) == x.end())
      x[d].resize(d);
    rep(j, 0, d) {
      int a;
      cin >> a;
      x[d][(j - t + d) % d] += a;
    }
  }

  for (;;) {
    bool update = false;
    for (auto &p : x) {
      for (auto &q : x) {
        const int a = p.first, b = q.first;
        if (a == b)
          continue;
        const int c = lcm(a, b), d = gcd(a, b);
        if (d == 1)
          continue;
        vector<int> v = x[c];
        if (v.empty())
          v.resize(c);
        rep(i, 0, c) v[i] += p.second[i % a] + q.second[i % b];
        if (a == c)
          rep(i, 0, c) v[i] -= p.second[i];
        if (b == c)
          rep(i, 0, c) v[i] -= q.second[i];
        x.erase(a);
        x.erase(b);
        x[c] = v;

        update = true;
        break;
      }
      if (update)
        break;
    }
    if (!update)
      break;
  }

  int ans = 0;
  for (auto &p : x) {
    int ma = 0;
    for (int a : p.second)
      ma = max(ma, a);
    ans += ma;
  }
  cout << ans << endl;
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  cout.setf(ios::fixed);
  cout.precision(10);
  for (;;) {
    int n;
    cin >> n;
    if (!n)
      break;
    solve(n);
  }
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define print(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define print(x)
#endif

const int inf = 1e9;
const int64_t inf64 = 1e18;
const double eps = 1e-9;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

int64_t gcd(int64_t a, int64_t b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

int64_t lcm(int64_t a, int64_t b) { return a / gcd(a, b) * b; }

void solve(int n) {
  map<int, vector<int>> x;
  rep(i, 0, n) {
    int d, t;
    cin >> d >> t;
    if (x.find(d) == x.end())
      x[d].resize(d);
    rep(j, 0, d) {
      int a;
      cin >> a;
      x[d][(j + t) % d] += a;
    }
  }

  for (;;) {
    bool update = false;
    for (auto &p : x) {
      for (auto &q : x) {
        if (p.first == q.first)
          continue;
        int c = lcm(p.first, q.first), d = gcd(p.first, q.first);
        if (d == 1)
          continue;
        vector<int> v(c);
        rep(i, 0, c) v[i] = p.second[i % p.first] + q.second[i % q.first];
        x.erase(p.first);
        x.erase(q.first);
        x[c] = v;

        update = true;
      }
    }
    if (!update)
      break;
  }

  int ans = 0;
  for (auto &p : x) {
    int ma = 0;
    for (int a : p.second)
      ma = max(ma, a);
    ans += ma;
  }
  cout << ans << endl;
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  cout.setf(ios::fixed);
  cout.precision(10);
  for (;;) {
    int n;
    cin >> n;
    if (!n)
      break;
    solve(n);
  }
  return 0;
}