alexjercan/bugnet huggingface dataset:
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, M, Q;
  cin >> N >> M >> Q;
  vector<int> a(Q);
  vector<int> b(Q);
  vector<int> c(Q);
  vector<int> d(Q);

  for (int i = 0; i < Q; i++) {
    cin >> a.at(i) >> b.at(i) >> c.at(i) >> d.at(i);
    a.at(i)--;
    b.at(i)--;
  }

  vector<int> A(10);
  long long maximum = 0;

  for (A.at(9) = 1; A.at(9) <= M; A.at(9)++) {
    for (A.at(8) = 1; A.at(8) <= A.at(9); A.at(8)++) {
      for (A.at(7) = 1; A.at(7) <= A.at(8); A.at(7)++) {
        for (A.at(6) = 1; A.at(6) <= A.at(7); A.at(6)++) {
          for (A.at(5) = 1; A.at(5) <= A.at(6); A.at(5)++) {
            for (A.at(4) = 1; A.at(4) <= A.at(5); A.at(4)++) {
              for (A.at(3) = 1; A.at(3) <= A.at(4); A.at(3)++) {
                for (A.at(2) = 1; A.at(2) <= A.at(3); A.at(2)++) {
                  for (A.at(1) = 1; A.at(1) <= A.at(2); A.at(1)++) {
                    for (A.at(0) = 1; A.at(0) <= A.at(1); A.at(0)++) {
                      long long sum = 0;
                      for (int i = 0; i < Q; i++)
                        if (A.at(b.at(i)) - A.at(a.at(i)) == c.at(i))
                          sum += d.at(i);
                      if (sum > maximum)
                        maximum = sum;
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
  cout << maximum << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, M, Q;
  cin >> N >> M >> Q;
  vector<int> a(Q);
  vector<int> b(Q);
  vector<int> c(Q);
  vector<int> d(Q);

  for (int i = 0; i < Q; i++)
    cin >> a.at(i) >> b.at(i) >> c.at(i) >> d.at(i);

  vector<int> A(10);
  long long maximum = 0;

  for (A.at(9) = 1; A.at(9) <= 9; A.at(9)++) {
    for (A.at(8) = 1; A.at(8) <= A.at(9); A.at(8)++) {
      for (A.at(7) = 1; A.at(7) <= A.at(8); A.at(7)++) {
        for (A.at(6) = 1; A.at(6) <= A.at(7); A.at(6)++) {
          for (A.at(5) = 1; A.at(5) <= A.at(6); A.at(5)++) {
            for (A.at(4) = 1; A.at(4) <= A.at(5); A.at(4)++) {
              for (A.at(3) = 1; A.at(3) <= A.at(4); A.at(3)++) {
                for (A.at(2) = 1; A.at(2) <= A.at(3); A.at(2)++) {
                  for (A.at(1) = 1; A.at(1) <= A.at(2); A.at(1)++) {
                    for (A.at(0) = 1; A.at(0) <= A.at(1); A.at(0)++) {
                      long long sum = 0;
                      for (int i = 0; i < Q; i++)
                        if (A.at(b.at(i)) - A.at(a.at(i)) == c.at(i))
                          sum += d.at(i);
                      maximum = max(sum, maximum);
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
  cout << maximum << endl;
  return 0;
}
Problem p02695 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int n, m, q;
vector<int> a, b, c, d;
int ans = 0;

void dfs(vector<int> A) {
  if (A.size() == n + 1) {
    int now = 0;
    rep(i, q) {
      if (A[b[i]] - A[a[i]] == c[i]) {
        now += d[i];
      }
    }
    ans = max(ans, now);

    return;
  }
  A.push_back(A.back());
  while (A.back() <= m) {
    dfs(A);
    A.back()++;
  }

  // cout << now << endl;
}

int main() {

  cin >> n >> m >> q;
  a = b = c = d = vector<int>(q);
  rep(i, q) { cin >> a[i] >> b[i] >> c[i] >> d[i]; }
  vector<int> A(1);
  A[0] = 1;
  dfs(A);
  cout << ans;
}


Error: TLE

Fixed submission:

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

class Radix {
private:
  const char *s;
  int a[128];

public:
  Radix(const char *s = "0123456789ABCDEF") : s(s) {
    int i;
    for (i = 0; s[i]; ++i)
      a[(int)s[i]] = i;
  }
  std::string to(long long p, int q) {
    int i;
    if (!p)
      return "0";
    char t[64] = {};
    for (i = 62; p; --i) {
      t[i] = s[p % q];
      p /= q;
    }
    return std::string(t + i + 1);
  }
  std::string to(const std::string &t, int p, int q) { return to(to(t, p), q); }
  long long to(const std::string &t, int p) {
    int i;
    long long sm = a[(int)t[0]];
    for (i = 1; i < (int)t.length(); ++i)
      sm = sm * p + a[(int)t[i]];
    return sm;
  }
};

int main() {
  Radix r;
  int N, M, Q;
  cin >> N >> M >> Q;
  vector<vector<int>> data(Q, vector<int>(4));
  for (int i = 0; i < Q; i++) {
    for (int j = 0; j < 4; j++) {
      cin >> data.at(i).at(j);
    }
  }
  int max_score;
  for (int i = 0; i < Q; i++) {
    max_score += data.at(i).at(3);
  }

  string max_num;
  for (int i = 0; i < N; i++) {
    max_num += to_string(M);
  }

  int score = 0;
  for (int i = 0; i < pow(N, M); i++) {
    // int tmp = r.to(i, M);
    string tmp(r.to(i, M));
    while (tmp.length() < N) {
      tmp = "0" + tmp;
    }

    sort(tmp.begin(), tmp.end());

    if (tmp == max_num) {
      break;
    }

    vector<int> an(N);

    for (int i = 0; i < N; i++) {
      char t = tmp[i];
      int tmp = (int)(t - '0') + 1;
      an.at(i) = tmp;
    }

    int score_tmp = 0;
    for (int q = 0; q < Q; q++) {
      int aa = an.at(data.at(q).at(0) - 1);
      int ab = an.at(data.at(q).at(1) - 1);
      if (ab - aa == data.at(q).at(2)) {
        score_tmp += data.at(q).at(3);
      }
    }
    if (score < score_tmp) {
      score = score_tmp;
    }
    if (score == max_score) {
      break;
    }
  }
  cout << score;
}
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <string>
#define ft first
#define sc second
#define pt(sth) cout << sth << "\\n"
#define chmax(a, b) (a) = max(a, b)
#define chmin(a, b) (a) = min(a, b)
#define moC(a, s, b) (a) = ((a)s(b) + MOD) % MOD
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
static const ll INF = 1e18;
static const ll MAX = 101010;
static const ll MOD = 1e9 + 7;

/*
 for(i=0; i<N; i++)
   cin >> a[i];
*/

int main(void) {
  ll i, j, k;

  ll N, M, Q;
  cin >> N >> M >> Q;

  ll a[55], b[55], c[55], d[55];

  for (i = 0; i < Q; i++) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  }

  ll ans = 0;
  for (ll S = 0; S < 1 << M; S++) {

    vector<ll> va;
    for (i = 0; i < M; i++) {
      if (S >> i & 1)
        va.push_back(i);
    }

    for (ll T = 0; T < 1 << (N - 1); T++) {

      vector<ll> vp;
      vp.push_back(-1);
      for (i = 0; i < N - 1; i++) {
        if (T >> i & 1)
          vp.push_back(i);
      }
      vp.push_back(N - 1);

      if (vp.size() != va.size() + 1)
        continue;

      ll u[11] = {};
      for (i = 0; i < vp.size() - 1; i++) {
        for (j = vp[i] + 1; j <= vp[i + 1]; j++) {
          u[j] = va[i];
        }
      }

      ll t = 0;
      for (i = 0; i < Q; i++) {
        ll l = a[i] - 1;
        ll r = b[i] - 1;
        if (u[r] - u[l] == c[i])
          t += d[i];
      }

      chmax(ans, t);
    }
  }

  pt(ans);
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <string>
#define ft first
#define sc second
#define pt(sth) cout << sth << "\\n"
#define chmax(a, b) (a) = max(a, b)
#define chmin(a, b) (a) = min(a, b)
#define moC(a, s, b) (a) = ((a)s(b) + MOD) % MOD
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
static const ll INF = 1e18;
static const ll MAX = 101010;
static const ll MOD = 1e9 + 7;

/*
 for(i=0; i<N; i++)
   cin >> a[i];
*/

int main(void) {
  ll i, j, k;

  ll N, M, Q;
  cin >> N >> M >> Q;

  ll a[11], b[11], c[11], d[11];

  for (i = 0; i < Q; i++) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  }

  ll ans = 0;
  for (ll S = 0; S < 1 << M; S++) {

    vector<ll> va;
    for (i = 0; i < M; i++) {
      if (S >> i & 1)
        va.push_back(i);
    }

    for (ll T = 0; T < 1 << (N - 1); T++) {

      vector<ll> vp;
      vp.push_back(-1);
      for (i = 0; i < N - 1; i++) {
        if (T >> i & 1)
          vp.push_back(i);
      }
      vp.push_back(N - 1);

      if (vp.size() != va.size() + 1)
        continue;

      ll u[11] = {};
      for (i = 0; i < vp.size() - 1; i++) {
        for (j = vp[i] + 1; j <= vp[i + 1]; j++) {
          u[j] = va[i];
        }
      }

      ll t = 0;
      for (i = 0; i < Q; i++) {
        ll l = a[i] - 1;
        ll r = b[i] - 1;
        if (u[r] - u[l] == c[i])
          t += d[i];
      }

      chmax(ans, t);
    }
  }

  pt(ans);
}
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

int a[50], b[50], c[50], d[50];
int n, m, q;
int ans = 0;
int A[10];

void dfs(int dep, int pre) {
  if (dep == n) {
    int sum = 0;
    for (int i = 0; i < q; i++) {
      if (A[b[i] - 1] - A[a[i] - 1] == c[i])
        sum += d[i];
    }
    ans = max(sum, ans);
    return;

  } else {
    for (int i = pre; i <= m; i++) {
      A[dep] = i;
      dfs(dep + 1, i);
    }
  }
}

int main() {
  cin >> n >> m >> q;
  for (int i = 0; i < q; i++) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  }
  dfs(0, 1);
  cout << ans << endl;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int a[50], b[50], c[50], d[50];
int n, m, q;
int ans = 0;
int A[10];

void dfs(int dep, int pre) {
  if (dep == n) {
    int sum = 0;
    for (int i = 0; i < q; i++) {
      if (A[b[i] - 1] - A[a[i] - 1] == c[i])
        sum += d[i];
    }
    ans = max(sum, ans);
    return;

  } else {
    for (int i = pre; i < n - 1; i++) {
      A[i] = i;
      dfs(i + 1, pre);
    }
  }
}

int main() {
  cin >> n >> m >> q;
  for (int i = 0; i < q; i++) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  }
  dfs(0, 1);
  cout << ans << endl;
}
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

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

int n, m, q;
int ans = 0;
vector<int> a(50), b(50), c(50), d(50), e(10);

void dfs(int dep, int curr) {
  if (dep == n) {
    int tmp = 0;
    for (int i = 0; i < q; i++) {
      if (e.at(b.at(i) - 1) - e.at(a.at(i) - 1) == c.at(i))
        tmp += d.at(i);
    }
    ans = max(ans, tmp);
    return;
  }
  for (int i = curr; i <= m; i++) {
    e.at(dep) = i;
    dfs(dep + 1, i);
  }
}

int main() {
  cin >> n >> m >> q;
  for (int i = 0; i < q; i++) {
    cin >> a.at(i) >> b.at(i) >> c.at(i) >> d.at(i);
  }

  dfs(0, 1);
  cout << ans << endl;
}


Error: -6
terminate called after throwing an instance of 'std::out_of_range'
  what():  vector::_M_range_check: __n (which is 10) >= this->size() (which is 10)


Fixed submission:

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

int n, m, q;
int ans = 0;
vector<int> a(50), b(50), c(50), d(50), e(10);

void dfs(int dep, int curr) {
  if (dep == n - 1) {
    int tmp = 0;
    for (int i = 0; i < q; i++) {
      if (e.at(b.at(i) - 1) - e.at(a.at(i) - 1) == c.at(i))
        tmp += d.at(i);
    }
    ans = max(ans, tmp);
  }
  for (int i = curr; i <= m; i++) {
    e.at(dep) = i;
    dfs(dep + 1, i);
  }
}

int main() {
  cin >> n >> m >> q;
  for (int i = 0; i < q; i++) {
    cin >> a.at(i) >> b.at(i) >> c.at(i) >> d.at(i);
  }

  dfs(0, 1);
  cout << ans << endl;
}
Problem p02695 (C++) — buggy submission, verdict: Runtime Error


#include <algorithm>
#include <assert.h>
#include <climits>
#include <cmath>
#include <iostream>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
constexpr int64_t MOD = 1e9 + 7;

void rec(int i, int prev, int64_t &max_res, vector<int64_t> &fixed,
         vector<int64_t> &C, vector<int64_t> &D, vector<pair<int, int>> &AB,
         int32_t &N, int32_t &M, int32_t &Q) {
  if (i == N) {
    int64_t cur_sum = 0;
    for (int k = 0; k < Q; k++) {
      int a = AB[k].first;
      int b = AB[k].second;
      if (fixed[b] - fixed[a] == C[k]) {
        cur_sum += D[k];
      }
    }
    max_res = max(max_res, cur_sum);
    return;
  }
  for (int j = prev; j <= M; j++) {
    fixed[i] = j;
    rec(i + 1, j, max_res, fixed, C, D, AB, N, M, Q);
  }
  return;
}

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

  int32_t N, M, Q;
  cin >> N >> M >> Q;
  vector<int64_t> fixed(N, 0);
  fixed[0] = 1;
  vector<int64_t> D(Q);
  vector<int64_t> C(Q);
  vector<pair<int, int>> AB(Q);
  for (int i = 0; i < Q; i++) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    AB[i] = make_pair(a - 1, b - 1);
    C[i] = c;
    D[i] = d;
  }
  int64_t max_res = 0;
  rec(1, 1, max_res, fixed, C, D, AB, N, M, Q);
  cout << max_res << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <climits>
#include <cmath>
#include <iostream>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
constexpr int64_t MOD = 1e9 + 7;
int32_t N, M, Q;

void rec(int i, int64_t prev, int64_t &max_res, vector<int64_t> &fixed,
         vector<int64_t> &C, vector<int64_t> &D, vector<pair<int, int>> &AB) {
  if (i == N) {
    int64_t cur_sum = 0;
    for (int k = 0; k < Q; k++) {
      int a = AB[k].first;
      int b = AB[k].second;
      if (fixed[b] - fixed[a] == C[k]) {
        cur_sum += D[k];
      }
    }
    max_res = max(max_res, cur_sum);
    return;
  }
  for (int j = prev; j <= M; j++) {
    fixed[i] = j;
    rec(i + 1, j, max_res, fixed, C, D, AB);
  }
  return;
}

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

  cin >> N >> M >> Q;
  vector<int64_t> fixed(10, 0);
  fixed[0] = 1;
  vector<int64_t> D(M);
  vector<int64_t> C(M);
  vector<pair<int, int>> AB(M);
  for (int i = 0; i < Q; i++) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    AB[i] = make_pair(a - 1, b - 1);
    C[i] = c;
    D[i] = d;
  }
  int64_t max_res = 0;
  rec(1, 1, max_res, fixed, C, D, AB);
  cout << max_res << endl;
  return 0;
}
Problem p02695 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
// #include <limits>
// #include <type_traits>
// #include <boost/integer/common_factor_rt.hpp>
using namespace std;

int main() {

  long ans, sum;
  int i, j, N, M, Q;
  int a[50], b[50], c[50];
  long d[50] = {};
  int e[10] = {};
  bool can;
  can = true;
  // using T = int32_t;
  ans = 0;
  sum = 0;
  cin >> N >> M >> Q;
  for (i = 0; i < Q; i++) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  }

  for (i = 0; i < N; i++) {
    e[i] = 1;
  }

  for (;;) {
    e[N - 1] += 1;
    for (i = N - 1; i > -1; i--) {
      if (e[i] == M + 1)
        e[i - 1] += 1;
    }
    for (i = 1; i < N; i++) {
      if (e[i] == M + 1)
        e[i] = e[i - 1];
    }
    for (j = 0; j < Q; j++) {
      if ((e[b[j] - 1] - e[a[j] - 1]) == c[j])
        sum += d[j];
    }
    if (ans < sum)
      ans = sum;
    sum = 0;
    // cout<<e[0]<<" "<<e[1]<<" "<<e[2]<<" "<<e[3]<<" "<<e[4]<<" "<<e[5]<<"
    // "<<e[6]<<" "<<e[7]<<" "<<e[8]<<" "<<e[9]<<endl;
    if (e[0] > (M / 2))
      break;
  }

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
// #include <limits>
// #include <type_traits>
// #include <boost/integer/common_factor_rt.hpp>
using namespace std;

int main() {

  long ans, sum;
  int i, j, N, M, Q;
  int a[50], b[50], c[50];
  long d[50] = {};
  int e[10] = {};
  bool can;
  can = true;
  // using T = int32_t;
  ans = 0;
  sum = 0;
  cin >> N >> M >> Q;
  for (i = 0; i < Q; i++) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  }

  for (i = 0; i < N; i++) {
    e[i] = 1;
  }

  for (;;) {
    e[N - 1] += 1;
    for (i = N - 1; i > -1; i--) {
      if (e[i] == M + 1)
        e[i - 1] += 1;
    }
    for (i = 1; i < N; i++) {
      if (e[i] == M + 1)
        e[i] = e[i - 1];
    }
    for (j = 0; j < Q; j++) {
      if ((e[b[j] - 1] - e[a[j] - 1]) == c[j])
        sum += d[j];
    }
    if (ans < sum)
      ans = sum;
    sum = 0;
    // cout<<e[0]<<" "<<e[1]<<" "<<e[2]<<" "<<e[3]<<" "<<e[4]<<" "<<e[5]<<"
    // "<<e[6]<<" "<<e[7]<<" "<<e[8]<<" "<<e[9]<<endl;
    if (e[0] == M)
      break;
  }

  cout << ans << endl;

  return 0;
}
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
using namespace std;
// #define int int64_t
#define pcnt __builtin_popcount
#define pb push_back
#define ALL(x) (x).begin(), (x).end()
#define vec vector<int>
#define mat vector<vector<int>>    // mat データ名(縦,vec(横));
int64_t INF = 1000000000000000001; // 10^18+1
int MOD = 1000000007;              // 10^9+7
// cout << fixed << setprecision(15); (小数点以下表示)

void next(vec &A, int M, int i) {
  if (A[i] >= M) {
    next(A, M, i - 1);
    int siz = A.size();
    for (int j = i; j < siz; j++) {
      if (j > 0) {
        A[j] = A[j - 1];
      } else {
        A[0]++;
      }
    }
  } else {
    A[i]++;
  }
}

signed main() {
  int N, M, Q, score = 0, ans = 0;
  cin >> N >> M >> Q;
  vec A(N, 1), a(Q), b(Q), c(Q), d(Q);
  for (int i = 0; i < Q; i++) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  }
  for (int i = 0; i < INF; i++) {
    if (i != 0)
      next(A, M, N - 1);
    score = 0;
    for (int j = 0; j < Q; j++) {
      if (A[b[j] - 1] - A[a[j] - 1] == c[j])
        score += d[j];
    }
    ans = max(ans, score);
    if (A[0] == M)
      break;
  }
  cout << ans << endl;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
using namespace std;
// #define int int64_t
#define pcnt __builtin_popcount
#define pb push_back
#define ALL(x) (x).begin(), (x).end()
#define vec vector<int>
#define mat vector<vector<int>>    // mat データ名(縦,vec(横));
int64_t INF = 1000000000000000001; // 10^18+1
int MOD = 1000000007;              // 10^9+7
// cout << fixed << setprecision(15); (小数点以下表示)

void next(vec &A, int M, int i) {
  if (A[i] >= M) {
    next(A, M, i - 1);
    int siz = A.size();
    for (int j = i; j < siz; j++) {
      if (j > 0) {
        A[j] = A[j - 1];
      } else {
        A[0]++;
      }
    }
  } else {
    A[i]++;
  }
}

signed main() {
  int N, M, Q, score = 0, ans = 0;
  cin >> N >> M >> Q;
  vec A(N, 1), a(N), b(N), c(N), d(N);
  for (int i = 0; i < Q; i++) {
    cin >> a.at(i) >> b.at(i) >> c.at(i) >> d.at(i);
  }
  for (int i = 0; i < INF; i++) {
    if (i != 0)
      next(A, M, N - 1);
    score = 0;
    for (int j = 0; j < Q; j++) {
      if (A[b[j] - 1] - A[a[j] - 1] == c[j])
        score += d[j];
    }
    ans = max(ans, score);
    if (A[0] == M)
      break;
  }
  // cout << ans << endl;
}
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

int dfs(int n, int N, int M, vector<int> &a, vector<int> &b, vector<int> &c,
        vector<int> &d, vector<int> &A) {
  if (n == N) {
    int Q = a.size();

    int score = 0;
    for (int i = 0; i < Q; i++) {
      if (A[b[i]] - A[a[i]] == c[i])
        score += d[i];
    }

    return score;
  }

  int ret = 0;
  for (int i = A[n]; i <= M; i++) {
    A[n + 1] = i;
    ret = max(dfs(n + 1, N, M, a, b, c, d, A), ret);
  }

  return ret;
}

int main() {
  int N, M, Q;
  cin >> N >> M >> Q;

  vector<int> a(Q), b(Q), c(Q), d(Q), A(N + 1, 1);
  for (int i = 0; i < Q; i++)
    cin >> a[i] >> b[i] >> c[i] >> d[i];

  int s = dfs(0, N, M, a, b, c, d, A);
  cout << s << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int M;

int dfs(int n, int N, vector<int> &a, vector<int> &b, vector<int> &c,
        vector<int> &d, vector<int> &A) {
  if (n == N) {
    int Q = a.size();

    int score = 0;
    for (int i = 0; i < Q; i++) {
      if (A[b[i]] - A[a[i]] == c[i])
        score += d[i];
    }

    return score;
  }

  int ret = 0;
  for (int i = a[n]; i <= M; i++) {
    A[n + 1] = i;
    ret = max(dfs(n + 1, N, a, b, c, d, A), ret);
  }

  return ret;
}

int main() {
  int N, Q;
  cin >> N >> M >> Q;

  vector<int> a(Q), b(Q), c(Q), d(Q), A(N, 1);
  for (int i = 0; i < Q; i++)
    cin >> a[i] >> b[i] >> c[i] >> d[i];

  cout << dfs(0, N, a, b, c, d, A) << endl;
  ;

  return 0;
}
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

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

int n, m, q;
vector<int> sum, ans, a, b, c, d;

void dfs(int cnt) {
  if (cnt == n - 1) {
    int S = 0;
    for (int i = 0; i < q; ++i) {
      if (sum[b[i]] - sum[a[i]] == c[i])
        S += d[i];
    }
    ans.push_back(S);
  } else if (cnt < n) {
    for (int i = 0; i < m - sum[cnt]; ++i) {
      sum[cnt + 1] = sum[cnt] + i;
      dfs(cnt + 1);
    }
  }
}

int main() {
  cin >> n >> m >> q;
  sum.resize(n, 0);
  a.resize(q);
  b.resize(q);
  c.resize(q);
  d.resize(q);
  for (int i = 0; i < q; ++i) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
    a[i]--;
    b[i]--;
  }
  dfs(0);
  sort(ans.begin(), ans.end());
  reverse(ans.begin(), ans.end());
  cout << ans[0] << endl;
}


Error: 0

Fixed submission:

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

int n, m, q;
vector<int> sum, ans, a, b, c, d;

void dfs(int cnt) {
  if (cnt == m - 1) {
    int S = 0;
    for (int i = 0; i < q; ++i) {
      if (sum[b[i]] - sum[a[i]] == c[i])
        S += d[i];
    }
    ans.push_back(S);
  } else {
    for (int i = 0; i < m - sum[cnt]; ++i) {
      sum[cnt + 1] = sum[cnt] + i;
      dfs(cnt + 1);
    }
  }
}

int main() {
  cin >> n >> m >> q;
  sum.resize(n, 0);
  a.resize(q);
  b.resize(q);
  c.resize(q);
  d.resize(q);
  for (int i = 0; i < q; ++i) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
    a[i]--;
    b[i]--;
  }
  dfs(0);
  sort(ans.begin(), ans.end());
  reverse(ans.begin(), ans.end());
  cout << ans[0] << endl;
}
Problem p02695 (C++) — buggy submission, verdict: Time Limit Exceeded

#pragma GCC optimize(2)
#include <bits/stdc++.h>
#define ll long long
#define maxn 1000005
#define inf 1e9
#define pb push_back
#define per(i, a, b) for (int i = a; i >= b; i--)
using namespace std;

ll ans, n, m, Q, a[maxn], b[maxn], c[maxn], q[maxn], x[maxn];

inline void jud() {
  ll tmp = 0;
  for (int i = 1; i <= Q; i++)
    if (x[b[i]] - x[a[i]] == c[i])
      tmp += q[i];
  ans = max(ans, tmp);
}

inline void dfs(int u) {
  if (u == n + 1) {
    jud();
    return;
  }
  for (int i = x[u - 1]; i <= m; i++) {
    x[u] = i;
    dfs(u + 1);
  }
}

int main() {
  cin >> n >> m >> Q;
  for (int i = 1; i <= Q; i++)
    cin >> a[i] >> b[i] >> c[i] >> q[i];
  x[0] = 1;
  dfs(1);
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#pragma GCC optimize(2)
#include <bits/stdc++.h>
#define ll long long
#define maxn 1000005
#define inf 1e9
#define pb push_back
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define per(i, a, b) for (int i = a; i >= b; i--)
using namespace std;

inline int read() {
  int x = 0, w = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      w = -1;
    c = getchar();
  }
  while (c <= '9' && c >= '0') {
    x = (x << 1) + (x << 3) + c - '0';
    c = getchar();
  }
  return w == 1 ? x : -x;
}

ll ans, n, m, Q, a[maxn], b[maxn], c[maxn], q[maxn], x[maxn];

inline bool jud() {
  ll tmp = 0;
  for (int i = 1; i <= Q; i++) {
    if (x[b[i]] - x[a[i]] == c[i])
      tmp += q[i];
  }
  ans = max(ans, tmp);
}

inline void dfs(int u) {
  if (u == n + 1) {
    jud();
    return;
  }
  for (int i = x[u - 1]; i <= m; i++) {
    x[u] = i;
    dfs(u + 1);
  }
}

int main() {
  cin >> n >> m >> Q;
  rep(i, 1, Q) cin >> a[i] >> b[i] >> c[i] >> q[i];
  x[0] = 1;
  dfs(1);
  cout << ans << endl;
  return 0;
}