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


#include <algorithm>
#include <functional>
#include <iomanip> //! for setprecision(10)
#include <iostream>
#include <math.h>
#include <string>
#include <vector>

#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <utility>

using namespace std;

typedef long long LL;

#define dump(c)                                                                \\
  {                                                                            \\
    for (auto it = c.begin(); it != c.end(); ++it)                             \\
      if (it == c.begin())                                                     \\
        cout << *it;                                                           \\
      else                                                                     \\
        cout << ' ' << *it;                                                    \\
    cout << endl;                                                              \\
  }
#define rep(i, n) for (int i = 0; i < (n); ++i)

const int MOD = 1000000007;

int ans = 0;
int N, M, Q;
vector<int> a, b, c, d;

void dfs(vector<int> A) {
  if (A.size() == N + 1) {
    int tmp = 0;
    rep(q, Q) {
      if (A[b[q]] - A[a[q]] == c[q]) {
        tmp += d[q];
      }
      ans = max(ans, tmp);
    }
  } else {
    int b = A.back();
    for (int a = b; a <= M; ++a) {
      vector<int> B = A;
      B.push_back(a);
      dfs(B);
    }
  }
}

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

  cin >> N >> M >> Q;
  a.resize(Q);
  b.resize(Q);
  c.resize(Q);
  d.resize(Q);
  rep(i, Q) cin >> a[i] >> b[i] >> c[i] >> d[i];

  vector<int> A;
  A.push_back(1);
  dfs(A);

  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:


#include <algorithm>
#include <functional>
#include <iomanip> //! for setprecision(10)
#include <iostream>
#include <math.h>
#include <string>
#include <vector>

#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <utility>

using namespace std;

typedef long long LL;

#define dump(c)                                                                \\
  {                                                                            \\
    for (auto it = c.begin(); it != c.end(); ++it)                             \\
      if (it == c.begin())                                                     \\
        cout << *it;                                                           \\
      else                                                                     \\
        cout << ' ' << *it;                                                    \\
    cout << endl;                                                              \\
  }
#define rep(i, n) for (int i = 0; i < (n); ++i)

const int MOD = 1000000007;

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

  int N, M, Q;
  cin >> N >> M >> Q;
  vector<int> a(Q);
  vector<int> b(Q);
  vector<int> c(Q);
  vector<int> d(Q);
  rep(i, Q) cin >> a[i] >> b[i] >> c[i] >> d[i];

  LL ans = 0;
  for (int i = 1; i < 1024; ++i) {
    bool ok = true;
    LL tmp = 0;
    vector<vector<int>> cand;
    rep(q, Q) {
      if (i & (1 << q)) {
        int mn = a[q];
        int mx = b[q];
        int diff = c[q];
        tmp += d[q];
        if (cand.empty()) {
          for (int minV = 1; minV + diff <= M; ++minV) {
            vector<int> A(N);
            A[mn] = minV;
            A[mx] = minV + diff;
            cand.push_back(A);
          }
        } else {
          vector<vector<int>> newCand;
          for (int j = cand.size() - 1; 0 <= j; --j) {
            for (int minV = 1; minV + diff <= M; ++minV) {
              vector<int> A = cand[j];
              bool ng = false;
              rep(k, N) {
                if ((k <= mn && A[k] && minV < A[k]) ||
                    (mx <= k && A[k] < minV + diff)) {
                  ng = true;
                  break;
                }
              }
              if (!ng) {
                A[mn] = minV;
                A[mx] = minV + diff;
                newCand.push_back(A);
              }
            }
          }
          cand.swap(newCand);
        }
        ok = !cand.empty();
      }
      if (!ok) {
        break;
      }
    }
    if (ok) {
      ans = max(ans, tmp);
    }
  }

  cout << ans << endl;

  return 0;
}