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

#include <bits/stdc++.h>

#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;

#define FOR(i, r, n) for (ll i = (ll)(r); i < (ll)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
using ll = long long int;
using vi = vector<ll>;

ll n, m, k;
ll ans = 0, sum = 0;

/*--------------------template--------------------*/

vi combs;
vi a(50), b(50), c(50), d(50);

void dfs(ll j, const ll &n, const ll &m, const ll &k, ll &ans) {
  if (combs.size() == n) {
    sum = 0;
    REP(i, k) {
      if (combs[b[i] - 1] - combs[a[i] - 1] == c[i])
        sum += d[i];
    }
    ans = max(ans, sum);
    combs.pop_back();
    return;
  }
  for (ll jj = j; jj <= m; ++jj) {
    combs.push_back(jj);
    dfs(jj, n, m, k, ans);
  }
  combs.pop_back();
  return;
}

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

  cin >> n >> m >> k;

  REP(i, k) { cin >> a[i] >> b[i] >> c[i] >> d[i]; }
  dfs(1, n, m, k, ans);

  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;

#define FOR(i, r, n) for (ll i = (ll)(r); i < (ll)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
using ll = long long int;
using vi = vector<ll>;

ll n, m, k;
ll ans = 0, sum = 0;

/*--------------------template--------------------*/

vi combs;
vi a(10), b(10), c(10), d(10);

void dfs(ll j, const ll &n, const ll &m, const ll &k, ll &ans) {
  if (combs.size() == n) {
    sum = 0;
    REP(i, k) {
      if (combs[b[i] - 1] - combs[a[i] - 1] == c[i])
        sum += d[i];
    }
    ans = max(ans, sum);
    combs.pop_back();
    return;
  }
  for (ll jj = j; jj <= m; ++jj) {
    combs.push_back(jj);
    dfs(jj, n, m, k, ans);
  }
  combs.pop_back();
  return;
}

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

  cin >> n >> m >> k;

  REP(i, k) { cin >> a[i] >> b[i] >> c[i] >> d[i]; }
  dfs(1, n, m, k, ans);

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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, m, n) for (ll i = (ll)(m); i < (ll)(n); i++)
long long mo = 1e9 + 7;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<ll, ll> Pll;
template <class T, class S> void cmin(T &a, const S &b) {
  if (a > b)
    a = b;
}
template <class T, class S> void cmax(T &a, const S &b) {
  if (a < b)
    a = b;
}

vector<ll> a(60), b(60), c(60), d(60);
ll N, M, Q;
vector<ll> A(15);
ll best = 0;

ll solve(ll cur, ll prev) {
  if (cur == N) {
    ll tmp = 0;
    /*rep(i,N){
        cout << A[i] << " ";
    }
    cout << endl;*/
    rep(i, Q) {
      if (A[b[i] - 1] - A[a[i] - 1] == c[i]) {
        tmp += d[i];
      }
    }
    cmax(best, tmp);
    return 0;
  } else {
    REP(i, prev, M + 1) {
      A[cur] = i;
      solve(ll(cur + 1), ll(i));
    }
    return 0;
  }
}

int main() {
  // ll N,M,Q;
  string S;
  cin >> N >> M >> Q;
  rep(i, Q) { cin >> a[i] >> b[i] >> c[i] >> d[i]; }
  solve(ll(0), ll(1));
  cout << best << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, m, n) for (ll i = (ll)(m); i < (ll)(n); i++)
long long mo = 1e9 + 7;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<ll, ll> Pll;
template <class T, class S> void cmin(T &a, const S &b) {
  if (a > b)
    a = b;
}
template <class T, class S> void cmax(T &a, const S &b) {
  if (a < b)
    a = b;
}

vector<ll> a(15), b(15), c(15), d(15);
ll N, M, Q;
vector<ll> A(15);
ll best = 0;

ll solve(ll cur, ll prev) {
  if (cur == N) {
    ll tmp = 0;
    /*rep(i,N){
        cout << A[i] << " ";
    }
    cout << endl;*/
    rep(i, Q) {
      if (A[b[i] - 1] - A[a[i] - 1] == c[i]) {
        tmp += d[i];
      }
    }
    cmax(best, tmp);
    return 0;
  } else {
    REP(i, prev, M + 1) {
      A[cur] = i;
      solve(ll(cur + 1), ll(i));
    }
    return 0;
  }
}

int main() {
  // ll N,M,Q;
  string S;
  cin >> N >> M >> Q;
  rep(i, Q) { cin >> a[i] >> b[i] >> c[i] >> d[i]; }
  solve(ll(0), ll(1));
  cout << best << endl;
}
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main() {
  int A, B, N, M, Q, ii, f, max, MAX;
  f = 0;
  max = 0;
  MAX = 0;
  int a[100], b[100], c[100], d[100], An[20];
  cin >> N >> M >> Q;
  for (int i = 1; i <= Q; i++) {
    cin >> a[i];
    cin >> b[i];
    cin >> c[i];
    cin >> d[i];
  }
  for (int i = 0; i <= 11; i++) {
    An[i] = 1;
  }
  while (An[0] == 1) {
    max = 0;
    for (int i = 1; i <= Q; i++) {
      A = a[i];
      B = b[i];
      if (An[B] - An[A] == c[i]) {
        max = max + d[i];
      }
    }

    ii = N;
    f = 0;
    while (f == 0) {
      if (An[ii] != M) {
        An[ii]++;
        for (int iii = ii + 1; iii <= 11; iii++) {
          An[iii] = An[iii - 1];
        }
        f = 1;
      } else {
        ii = ii - 1;
      }
    }
    if (MAX < max) {
      MAX = max;
    }
  }
  cout << MAX << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int A, B, N, M, Q, ii, f, ff, max, MAX;
  f = 0;
  ff = 0;
  max = 0;
  MAX = 0;
  int a[50], b[50], c[50], d[50], An[11];
  cin >> N >> M >> Q;
  for (int i = 1; i <= Q; i++) {
    cin >> a[i];
    cin >> b[i];
    cin >> c[i];
    cin >> d[i];
  }
  for (int i = 0; i <= 11; i++) {
    An[i] = 1;
  }
  while (An[0] == 1) {
    max = 0;
    for (int i = 1; i <= Q; i++) {
      A = a[i];
      B = b[i];
      if (An[B] - An[A] == c[i]) {
        max = max + d[i];
      }
    }

    ii = N;
    f = 0;
    while (f == 0) {
      if (An[ii] != M) {
        An[ii]++;
        for (int iii = ii + 1; iii <= 11; iii++) {
          An[iii] = An[iii - 1];
        }
        f = 1;
      } else {
        ii = ii - 1;
      }
    }
    if (MAX < max) {
      MAX = max;
    }
  }
  cout << MAX << endl;
  return 0;
}
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define call(var) cout << #var << "=" << var << endl;
#define dup(x, y) (((x) + (y)-1) / (y))
using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using vb = vector<bool>;
using Graph = vector<vi>;
using P = pair<int, int>;
const int MOD = (int)1e9 + 7;
const int INF = (int)1e9;
const ll LINF = (ll)1e18;
template <class t, class u> inline bool chmax(t &a, u b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class t, class u> inline bool chmin(t &a, u b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

int n, m, q;
vi nums;
vi a, b, c, d;

int calc2() {
  int ret = 0;
  rep(i, q) {
    if (nums[b[i] - 1] - nums[a[i] - 1] == c[i])
      ret += d[i];
  }
  return ret;
}

int ans = INT_MIN;
void calc(int depth, int mix) {
  if (depth == n - 1) {
    for (int i = mix; i <= m; ++i) {
      nums[depth] = i;
      chmax(ans, calc2());
    }
    return;
  } else {
    for (int i = mix; i <= m; ++i) {
      nums[depth] = i;
      calc(depth + 1, i);
    }
  }
  return;
}

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

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define call(var) cout << #var << "=" << var << endl;
#define dup(x, y) (((x) + (y)-1) / (y))
using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using vb = vector<bool>;
using Graph = vector<vi>;
using P = pair<int, int>;
const int MOD = (int)1e9 + 7;
const int INF = (int)1e9;
const ll LINF = (ll)1e18;
template <class t, class u> inline bool chmax(t &a, u b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class t, class u> inline bool chmin(t &a, u b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
int n, m, q;
vi nums;
vi a, b, c, d;
int calc2() {
  int ret = 0;
  rep(i, q) {
    if (nums[b[i] - 1] - nums[a[i] - 1] == c[i])
      ret += d[i];
  }
  return ret;
}
int ans = INT_MIN;
void calc(int depth, int mix) {
  if (depth == m - 1) {
    for (int i = mix; i <= m; ++i) {
      nums[depth] = i;
      chmax(ans, calc2());
    }
    return;
  } else {
    for (int i = mix; i <= m; ++i) {
      nums[depth] = i;
      calc(depth + 1, i);
    }
  }
  return;
}

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

#include <cmath>
#include <iostream>
#include <vector>
#define pb push_back
#define For(i, x, y) for (register int i = (x); i <= (y); i++)
using namespace std;
int n, m, q, ans;
int a[100], b[100], c[100], d[100];
vector<int> v;
void dfs(int idx) {
  if (idx > n) {
    int now = 0;
    For(i, 1, q) if (v[b[i]] - v[a[i]] == c[i]) now += d[i];
    ans = max(ans, now);
    return;
  }
  // cout<<v[idx-1];
  // cout<<idx-1<<endl;
  For(i, v[idx - 1], m) {
    v.pb(i);
    dfs(idx + 1);
    v.pop_back();
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m >> q;
  v.pb(1);
  for (int i = 1; i <= q; i++)
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  // cout<<"ok"<<endl;
  dfs(1);
  cout << ans << endl;
  return 0;
}


Error: -11

Fixed submission:

#include <cmath>
#include <iostream>
#include <vector>
#define pb push_back
#define For(i, x, y) for (register int i = (x); i <= (y); i++)
using namespace std;
int n, m, q, ans;
int a[100], b[100], c[100], d[100];
vector<int> v;
void dfs(int idx) {
  if (idx > n) {
    int now = 0;
    For(i, 1, q) if (v[b[i]] - v[a[i]] == c[i]) now += d[i];
    ans = max(ans, now);
    return;
  }
  For(i, v[idx - 1], m) {
    v.pb(i);
    dfs(idx + 1);
    v.pop_back();
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m >> q;
  v[0] = 1;
  for (int i = 1; i <= q; i++)
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  dfs(1);
  cout << ans << endl;
  return 0;
}
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

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

struct info {
  int a, b, c, d;
};

int main() {
  int n, m, q;
  cin >> n >> m >> q;
  vector<info> v(q);
  for (int i = 0; i < q; i++) {
    info in;
    cin >> in.a >> in.b >> in.c >> in.d;
    v[i] = in;
  }
  queue<vector<int>> que;
  que.push({1});
  int cur_mx = 0;
  while (!que.empty()) {
    vector<int> front = que.front();
    que.pop();
    int sz = front.size();
    if (sz == n) {
      int score = 0;
      for (int i = 0; i < q; i++) {
        int a = v[i].a;
        int b = v[i].b;
        int c = v[i].c;
        int d = v[i].d;
        if (front[b - 1] - front[a - 1] == c) {
          score += d;
        }
      }
      cur_mx = max(cur_mx, score);
    } else {
      int last = front[sz - 1];
      for (int i = last; i <= m; i++) {
        vector<int> nw = front;
        nw.push_back(i);
        que.push(nw);
      }
    }
  }
  cout << cur_mx;
  return 0;
}

Error: WA

Fixed submission:

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

struct info {
  int a, b, c, d;
};

int main() {
  int n, m, q;
  cin >> n >> m >> q;
  vector<info> v(n);
  for (int i = 0; i < q; i++) {
    info in;
    cin >> in.a >> in.b >> in.c >> in.d;
    v[i] = in;
  }
  queue<vector<int>> que;
  que.push({1});
  int cur_mx = 0;
  while (!que.empty()) {
    vector<int> front = que.front();
    que.pop();
    int sz = front.size();
    if (sz == n) {
      int score = 0;
      for (int i = 0; i < q; i++) {
        int a = v[i].a;
        int b = v[i].b;
        int c = v[i].c;
        int d = v[i].d;
        if (front[b - 1] - front[a - 1] == c) {
          score += d;
        }
      }
      cur_mx = max(cur_mx, score);
    } else {
      int last = front[sz - 1];
      for (int i = last; i <= m; i++) {
        vector<int> nw = front;
        nw.push_back(i);
        que.push(nw);
      }
    }
  }
  cout << cur_mx;
  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++)
typedef long long ll;

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

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()++;
  }
}

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]; }
  dfs(vector<int>(1, 1));
  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

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

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);
  }
  A.push_back(A.back());
  while (A.back() <= m) {
    dfs(A);
    A.back()++;
  }
}

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]; }
  dfs(vector<int>(1, 1));
  cout << ans << endl;
}
Problem p02695 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

int n, m, q;
vector<vector<int>> Q;
int ans = 0;
vector<int> A;

void dfs(int ptr = 0, int before = 1) {
  if (ptr == n) {
    int tmp = 0;
    rep(i, q) {
      int a = A[Q[i][0]];
      int b = A[Q[i][1]];
      if (b - a == Q[i][2])
        tmp += Q[i][3];
    }
    ans = max(ans, tmp);
  } else {
    for (int i = before; i <= m; i++) {
      A[ptr] = i;
      dfs(ptr + 1, i);
    }
  }
}

int main() {
  cin >> n >> m >> q;
  A.resize(n);
  A[0] = 1;
  Q.resize(q);
  rep(i, q) Q[i].resize(4);
  rep(i, q) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    a--;
    b--;
    Q[i][0] = a;
    Q[i][1] = b;
    Q[i][2] = c;
    Q[i][3] = d;
  }
  dfs();
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

int main() {
  int n, m, q;
  cin >> n >> m >> q;
  vector<int[4]> Q(q);
  rep(i, q) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    a--;
    b--;
    Q[i][0] = a;
    Q[i][1] = b;
    Q[i][2] = c;
    Q[i][3] = d;
  }
  int ans = 0;
  stack<string> S;
  S.push("");
  while (!S.empty()) {
    string now = S.top();
    S.pop();
    if (now.size() == n) {
      int tmp = 0;
      rep(i, q) {
        int Ab = now[Q[i][1]] - '0';
        int Aa = now[Q[i][0]] - '0';
        if (Ab - Aa == Q[i][2])
          tmp += Q[i][3];
      }
      ans = max(ans, tmp);
      /*
       * debug
       */
      // cout << now << endl;
      // cout << ans << endl << endl;;
    } else {
      int last = now[now.size() - 1] - '0';
      for (int i = 1; i <= m; i++) {
        if (i >= last) {
          string next = now + to_string(i);
          S.push(next);
        }
      }
    }
  }
  cout << ans << endl;
}
Problem p02695 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

  do {
    vector<int> box;
    int sum = 0;
    int zerocnt = 1;
    for (int i = 0; i < m + n - 1; i++) {
      if (perm[i] == 0)
        zerocnt++;
      else
        box.push_back(zerocnt);
    }
    for (int i = 0; i < q; i++) {
      if (box[b[i]] - box[a[i]] == c[i])
        sum += d[i];
    }

    ans = max(ans, sum);
  } while (next_permutation(perm.begin(), perm.end()));

  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  ll n, m, q;
  cin >> n >> m >> q;
  vector<vector<ll>> box(q, vector<ll>(4, 0));
  for (int i = 0; i < q; i++) {
    for (int j = 0; j < 4; j++) {
      cin >> box[i][j];
    }
  }

  ll ans = 0;

  for (int i = 1; i < (1 << 10); i++) {
    if (__builtin_popcount(i) != n) {
      continue;
    } else {
      ll sum = 0;
      vector<ll> a;
      for (int j = 0; j < 10; i++) {
        if (((1 << j) & (i)) != 0) {
          a.push_back(j + 1);
        }
      }

      if (a[n - 1] > m) {
        continue;
      }
      for (int i = 0; i < q; i++) {
        if (a[box[i][1] - 1] - a[box[i][0] - 1] == box[i][2]) {
          sum += box[i][3];
        }
      }
      ans = max(sum, ans);
    }
  }
  cout << ans << endl;
}
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

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

int N, M, Q;
int a[50], b[50], c[50], d[50];
int ans = 0;
int C[20];
void recursive_comb(int s, int rest) {
  if (rest == 0) {
    int A[N];
    for (int i = 0; i < N; i++) {
      A[i] = C[i] - i + 1;
      // cout << A[i] << ":";
    }
    // cout << endl;
    int tmp = 0;
    for (int i = 0; i < Q; i++) {
      if (A[b[i] - 1] - A[a[i] - 1] == c[i])
        tmp += d[i];
    }
    ans = max(tmp, ans);
    return;
  } else {
    if (s < 0)
      return;
    recursive_comb(s - 1, rest);
    C[rest - 1] = s;
    recursive_comb(s - 1, rest - 1);
  }
}

int main() {
  cin >> N >> M >> Q;
  for (int i = 0; i < Q; i++) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  }
  recursive_comb(N + M - 2, N);
  cout << ans << endl;
}


Error: 0

Fixed submission:

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

int main() {
  int N, M, Q;
  cin >> N >> M >> Q;
  int a[Q], b[Q], c[Q], d[Q];
  for (int i = 0; i < Q; i++) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  }
  int A[N];
  for (int i = 0; i < N; i++) {
    A[i] = 1;
  }
  int j = N - 1;
  int ans = 0;
  for (int i = 0; i < N; i++) {
    if (A[b[i] - 1] - A[a[i]] == c[i])
      ans += d[i];
  }
  while (j >= 0) {
    while (A[j] < M) {
      A[j]++;
      int tmp = 0;
      for (int i = 0; i < Q; i++) {
        if (A[b[i] - 1] - A[a[i] - 1] == c[i])
          tmp += d[i];
      }
      ans = max(tmp, ans);
    }
    j--;
  }
  cout << ans << endl;
}
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

// #include <bits/stdc++.h>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef vector<int> vint;
typedef vector<vector<int>> vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long>> vvll, vvLL;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = 0; i < n; ++i)
#define mod (ll)(1e9 + 7)
#define FIX(a) ((a) % mod + mod) % mod
#define ALL(obj) (obj).begin(), (obj).end()
#define rALL(obj) (obj).rbegin(), (obj).rend()
#define INF 1000000000              // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define fi first
#define se second
#define pb push_back
int dy[] = {0, 0, 1, -1};
int dx[] = {1, -1, 0, 0};
vll sq(10, 1);
vll a(50), b(50), c(50), d(50);
ll solve(ll n, ll m, ll id, ll q) {
  ll ret = 0;
  if (id == n) {

    REP(i, q) {
      if (sq[b[i]] - sq[a[i]] == c[i]) {
        ret += d[i];
      }
    }
    return ret;
  }
  if (id == 0) {
    ret = solve(n, m, 1, q);
  } else {
    for (ll i = sq[id - 1]; i <= m; i++) {
      sq[id] = i;
      ret = max(ret, solve(n, m, id + 1, q));
    }
  }
  return ret;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n, m, q;
  cin >> n >> m >> q;
  ll ans = 0;
  REP(i, q) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
    a[i]--;
    b[i]--;
  }
  ans = solve(n, m, 0, q);
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

// #include <bits/stdc++.h>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef vector<int> vint;
typedef vector<vector<int>> vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long>> vvll, vvLL;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = 0; i < n; ++i)
#define mod (ll)(1e9 + 7)
#define FIX(a) ((a) % mod + mod) % mod
#define ALL(obj) (obj).begin(), (obj).end()
#define rALL(obj) (obj).rbegin(), (obj).rend()
#define INF 1000000000              // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define fi first
#define se second
#define pb push_back
int dy[] = {0, 0, 1, -1};
int dx[] = {1, -1, 0, 0};
vll sq(10, 1);
vll a(10), b(10), c(10), d(10);
ll solve(ll n, ll m, ll id, ll q) {
  ll ret = 0;
  if (id == n) {

    REP(i, q) {
      if (sq[b[i]] - sq[a[i]] == c[i]) {
        ret += d[i];
      }
    }
    return ret;
  }
  if (id == 0) {
    ret = solve(n, m, 1, q);
  } else {
    for (ll i = sq[id - 1]; i <= m; i++) {
      sq[id] = i;
      ret = max(ret, solve(n, m, id + 1, q));
    }
  }
  return ret;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n, m, q;
  cin >> n >> m >> q;
  ll ans = 0;
  REP(i, q) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
    a[i]--;
    b[i]--;
  }
  ans = solve(n, m, 0, q);
  cout << ans << endl;
  return 0;
}
Problem p02695 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
const int INF = 1e9;

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

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

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

signed main() {
  cin >> n >> m >> q;
  rep(i, q) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
    a[i]--;
    b[i]--;
  }
  dfs(0, 1);
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
const int INF = 1e9;

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

void dfs(int i, const int size, const int range_start, const int range_end) {
  if (i == size) {
    // ここで所望の作業を行う
    rep(i, buf.size()) buf2[i] = buf[i];
    sort(buf2.begin(), buf2.end());
    ll t = 0;
    rep(i, q) {
      if (buf2[b[i] - 1] - buf2[a[i] - 1] == c[i])
        t += d[i];
    }
    ans = max(ans, t);
    // for (int i = 0; i < buf.size(); ++i) {
    //   cout << buf2[i] << " ";
    // }
    // cout << endl;
  } else {
    for (int j = range_start; j <= range_end; ++j) {
      buf[i] = j;
      dfs(i + 1, size, range_start, range_end);
    }
  }
}

int main() {
  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];

  int size = n;
  int range_start = 1;
  int range_end = m;

  buf.resize(size);
  buf2.resize(size);
  dfs(0, size, range_start, range_end);

  cout << ans << endl;
  return 0;
}