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

#include <bits/stdc++.h>

#define VARNAME(x) #x
#define show(x) cerr << #x << " = " << x << endl

using namespace std;
using ll = long long;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "sz:" << v.size() << "\\n[";
  for (const auto &p : v) {
    os << p << ",";
  }
  os << "]\\n";
  return os;
}

template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &e : v) {
    is >> e;
  }
  return is;
}

template <typename S, typename T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}

constexpr ll MOD = (ll)1e9 + 7LL;

template <typename T> constexpr T INF = numeric_limits<T>::max() / 10;

using V7 = vector<vector<vector<vector<vector<vector<vector<ll>>>>>>>;
using V6 = vector<vector<vector<vector<vector<vector<ll>>>>>>;
using V5 = vector<vector<vector<vector<vector<ll>>>>>;
using V4 = vector<vector<vector<vector<ll>>>>;
using V3 = vector<vector<vector<ll>>>;
using V2 = vector<vector<ll>>;
using V1 = vector<ll>;

struct Data {
  vector<int> ind;
  ll x;
};

constexpr int MAX = 300;
constexpr int SQRT = 18;
constexpr int LP = 7;
constexpr int prime[LP] = {2, 3, 5, 7, 11, 13, 17};
constexpr int maxind[LP] = {9, 6, 4, 3, 3, 3, 3};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int pnum = 0;
  auto isprime = [](const int n) {
    for (int i = 2; i * i <= n; i++) {
      if (n % i == 0) {
        return false;
      }
    }
    return true;
  };
  map<int, int> mp;
  mp[1] = 0;
  for (int i = SQRT; i <= MAX; i++) {
    if (isprime(i)) {
      pnum++;
      mp[i] = pnum;
    }
  }

  vector<V7> value(
      pnum + 1, V7(maxind[0],
                   V6(maxind[1],
                      V5(maxind[2],
                         V4(maxind[3],
                            V3(maxind[4], V2(maxind[5], V1(maxind[6], 0))))))));

  int Q;
  cin >> Q;
  vector<vector<Data>> data(pnum + 1);
  for (int i = 0; i < Q; i++) {
    ll m, x;
    cin >> m >> x;
    vector<int> num(LP, 0);
    for (int j = 0; j < LP; j++) {
      while (m % prime[j] == 0) {
        num[j]++;
        m /= prime[j];
      }
    }
    data[mp[m]].push_back(Data{num, x});
  }

  auto div = [](const int n0, const int n1, const int n2, const int n3,
                const int n4, const int n5, const int n6,
                const vector<int> &ind) {
    return n0 >= ind[0] and n1 >= ind[1] and n2 >= ind[2] and n3 >= ind[3] and
           n4 >= ind[4] and n5 >= ind[5] and n6 >= ind[6];
  };
  for (int i = 0; i <= pnum; i++) {
    for (int n0 = 0; n0 < maxind[0]; n0++) {
      for (int n1 = 0; n1 < maxind[1]; n1++) {
        for (int n2 = 0; n2 < maxind[2]; n2++) {
          for (int n3 = 0; n3 < maxind[3]; n3++) {
            for (int n4 = 0; n4 < maxind[4]; n4++) {
              for (int n5 = 0; n5 < maxind[5]; n5++) {
                for (int n6 = 0; n6 < maxind[6]; n6++) {
                  for (const auto &d : data[i]) {
                    const auto &ind = d.ind;
                    if (div(n0, n1, n2, n3, n4, n5, n6, ind)) {
                      value[i][n0][n1][n2][n3][n4][n5][n6] += d.x;
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }

  ll maximum = 0;
  for (int n0 = 0; n0 < maxind[0]; n0++) {
    for (int n1 = 0; n1 < maxind[1]; n1++) {
      for (int n2 = 0; n2 < maxind[2]; n2++) {
        for (int n3 = 0; n3 < maxind[3]; n3++) {
          for (int n4 = 0; n4 < maxind[4]; n4++) {
            for (int n5 = 0; n5 < maxind[5]; n5++) {
              for (int n6 = 0; n6 < maxind[6]; n6++) {
                ll sum = value[0][n0][n1][n2][n3][n4][n5][n6];
                for (int i = 1; i <= pnum; i++) {
                  sum += max(0LL, value[i][n0][n1][n2][n3][n4][n5][n6]);
                }
                maximum = max(maximum, sum);
              }
            }
          }
        }
      }
    }
  }
  cout << maximum << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define VARNAME(x) #x
#define show(x) cerr << #x << " = " << x << endl

using namespace std;
using ll = long long;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "sz:" << v.size() << "\\n[";
  for (const auto &p : v) {
    os << p << ",";
  }
  os << "]\\n";
  return os;
}

template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &e : v) {
    is >> e;
  }
  return is;
}

template <typename S, typename T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}

constexpr ll MOD = (ll)1e9 + 7LL;

template <typename T> constexpr T INF = numeric_limits<T>::max() / 10;

using V7 = vector<vector<vector<vector<vector<vector<vector<ll>>>>>>>;
using V6 = vector<vector<vector<vector<vector<vector<ll>>>>>>;
using V5 = vector<vector<vector<vector<vector<ll>>>>>;
using V4 = vector<vector<vector<vector<ll>>>>;
using V3 = vector<vector<vector<ll>>>;
using V2 = vector<vector<ll>>;
using V1 = vector<ll>;

struct Data {
  vector<int> ind;
  ll x;
};

constexpr int MAX = 300;
constexpr int SQRT = 18;
constexpr int LP = 7;
constexpr int prime[LP] = {2, 3, 5, 7, 11, 13, 17};
constexpr int maxind[LP] = {9, 6, 4, 3, 3, 3, 3};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int pnum = 0;
  auto isprime = [](const int n) {
    for (int i = 2; i * i <= n; i++) {
      if (n % i == 0) {
        return false;
      }
    }
    return true;
  };
  map<int, int> mp;
  mp[1] = 0;
  for (int i = SQRT; i <= MAX; i++) {
    if (isprime(i)) {
      pnum++;
      mp[i] = pnum;
    }
  }

  vector<V7> value(
      pnum + 1, V7(maxind[0],
                   V6(maxind[1],
                      V5(maxind[2],
                         V4(maxind[3],
                            V3(maxind[4], V2(maxind[5], V1(maxind[6], 0))))))));
  for (int i = 0; i <= pnum; i++) {
    value[i][0][0][0][0][0][0][0] = 0;
  }

  int Q;
  cin >> Q;
  vector<vector<Data>> data(pnum + 1);
  for (int i = 0; i < Q; i++) {
    int m, x;
    cin >> m >> x;
    vector<int> num(LP, 0);
    for (int j = 0; j < LP; j++) {
      while (m % prime[j] == 0) {
        num[j]++;
        m /= prime[j];
      }
    }
    data[m].push_back(Data{num, x});
  }

  auto div = [](const int n0, const int n1, const int n2, const int n3,
                const int n4, const int n5, const int n6,
                const vector<int> &ind) {
    return n0 >= ind[0] and n1 >= ind[1] and n2 >= ind[2] and n3 >= ind[3] and
           n4 >= ind[4] and n5 >= ind[5] and n6 >= ind[6];
  };
  ll maximum = 0;
  for (int i = 0; i <= pnum; i++) {
    for (int n0 = 0; n0 < maxind[0]; n0++) {
      for (int n1 = 0; n1 < maxind[1]; n1++) {
        for (int n2 = 0; n2 < maxind[2]; n2++) {
          for (int n3 = 0; n3 < maxind[3]; n3++) {
            for (int n4 = 0; n4 < maxind[4]; n4++) {
              for (int n5 = 0; n5 < maxind[5]; n5++) {
                for (int n6 = 0; n6 < maxind[6]; n6++) {
                  for (const auto &d : data[i]) {
                    const auto &ind = d.ind;
                    if (div(n0, n1, n2, n3, n4, n5, n6, ind)) {
                      value[i][n0][n1][n2][n3][n4][n5][n6] += d.x;
                    }
                  }
                  if (i == 0) {
                    maximum =
                        max(maximum, value[0][n0][n1][n2][n3][n4][n5][n6]);
                  } else {
                    maximum =
                        max(maximum, value[i][n0][n1][n2][n3][n4][n5][n6] +
                                         value[0][n0][n1][n2][n3][n4][n5][n6]);
                  }
                }
              }
            }
          }
        }
      }
    }
  }
  cout << maximum << endl;

  return 0;
}