prithivMLmods/Coder-Stat huggingface dataset:
Problem p02695 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
using ll = long long;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;

ll ans = 0;
int a[20], b[20], c[20];
ll d[20];

void dfs(vector<int> A, int N, int M, int Q) {
  int P = A.size();
  // cout << P << endl;
  if (P - 1 == N) {
    ll tmp = 0;
    rep(i, Q) {
      if (A[b[i]] - A[a[i]] == c[i])
        tmp += d[i];
    }
    ans = max(ans, tmp);
    // cout << "OK" << endl;
    return;
  }
  for (int i = A[P - 1]; i <= M; i++) {
    A.push_back(i);
    dfs(A, N, M, Q);
    A.pop_back();
  }
}

int main() {
  int N, M, Q;
  cin >> N >> M >> Q;
  rep(i, Q) { cin >> a[i] >> b[i] >> c[i] >> d[i]; }
  vector<int> T;
  T.push_back(1);
  dfs(T, N, M, Q);
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
using ll = long long;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;

ll ans = 0;
int a[60], b[60], c[60];
ll d[60];

void dfs(vector<int> A, int N, int M, int Q) {
  int P = A.size();
  // cout << P << endl;
  if (P - 1 == N) {
    ll tmp = 0;
    rep(i, Q) {
      if (A[b[i]] - A[a[i]] == c[i])
        tmp += d[i];
    }
    ans = max(ans, tmp);
    // cout << "OK" << endl;
    return;
  }
  for (int i = A[P - 1]; i <= M; i++) {
    A.push_back(i);
    dfs(A, N, M, Q);
    A.pop_back();
  }
}

int main() {
  int N, M, Q;
  cin >> N >> M >> Q;
  rep(i, Q) { cin >> a[i] >> b[i] >> c[i] >> d[i]; }
  vector<int> T;
  T.push_back(1);
  dfs(T, N, M, Q);
  cout << ans << endl;
}
Problem p02695 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 20;
int a[N], b[N], c[N], d[N], p[N];
int ans = 0;
int n, m, q;
void dfs(int x) {
  int cnt = 0, i;
  if (x == n + 1) {
    for (i = 1; i <= q; i++)
      if (p[b[i]] - p[a[i]] == c[i])
        cnt += d[i];
    ans = max(cnt, ans);
    return;
  }
  for (p[x] = p[x - 1]; p[x] <= m; p[x]++)
    dfs(x + 1);
}
int main() {
  int i;
  cin >> n >> m >> q;
  for (i = 1; i <= q; i++)
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  p[0] = 1;
  dfs(1);
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 100;
int a[N], b[N], c[N], d[N], p[N];
int ans = 0;
int n, m, q;
void dfs(int x) {
  int cnt = 0, i;
  if (x == n + 1) {
    for (i = 1; i <= q; i++)
      if (p[b[i]] - p[a[i]] == c[i])
        cnt += d[i];
    ans = max(cnt, ans);
    return;
  }
  for (p[x] = p[x - 1]; p[x] <= m; p[x]++)
    dfs(x + 1);
}
int main() {
  int i;
  cin >> n >> m >> q;
  for (i = 1; i <= q; i++)
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  p[0] = 1;
  dfs(1);
  cout << ans << endl;
  return 0;
}
Problem p02695 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

ll N, M, Q;
vector<ll> A(10, 0), B(10, 0), C(10, 0), D(10, 0);
ll ans = 0;

ll dfs(vector<ll> V, int index) {
  vector<ll> V2 = V;
  ll ret = 0;
  if (index < N - 1) {
    for (ll i = V[index]; i <= M; i++) {
      V2[index + 1] = i;
      ret = max(ret, dfs(V2, index + 1));
    }
  } else {
    for (ll i = 0; i < Q; i++) {
      if (V[B[i]] - V[A[i]] == C[i])
        ret += D[i];
    }
  }
  return ret;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> N >> M >> Q;
  for (ll i = 0; i < Q; i++) {
    cin >> A[i] >> B[i] >> C[i] >> D[i];
    A[i]--, B[i]--;
  }
  vector<ll> V(N, 0);
  V[0] = 1;
  ans = dfs(V, 0);
  cout << ans << '\n';
  return 0;
}

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

ll N, M, Q;
vector<ll> A(50, 0), B(50, 0), C(50, 0), D(50, 0);
ll ans = 0;

ll dfs(vector<ll> V, int index) {
  vector<ll> V2 = V;
  ll ret = 0;
  if (index < N - 1) {
    for (ll i = V[index]; i <= M; i++) {
      V2[index + 1] = i;
      ret = max(ret, dfs(V2, index + 1));
    }
  } else {
    for (ll i = 0; i < Q; i++) {
      if (V[B[i]] - V[A[i]] == C[i])
        ret += D[i];
    }
  }
  return ret;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> N >> M >> Q;
  for (ll i = 0; i < Q; i++) {
    cin >> A[i] >> B[i] >> C[i] >> D[i];
    A[i]--, B[i]--;
  }
  vector<ll> V(N, 0);
  V[0] = 1;
  ans = dfs(V, 0);
  cout << ans << '\n';
  return 0;
}
Problem p02695 (C++)

Buggy submission (status: 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)
      break;
  }

  cout << ans << endl;

  return 0;
}


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 / 2))
      break;
  }

  cout << ans << endl;

  return 0;
}
Problem p02695 (C++)

Buggy submission (status: 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 < 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;
}

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 <= 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;
}
Problem p02695 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

bool nextvect(vector<int> &A, int N, int M) {
  if (A[N - 1] < M)
    A[N - 1]++;
  else {
    int i = N - 2;
    while (i >= 0 && A[i] == M)
      i--;
    if (i == -1)
      return false;
    A[i]++;
    for (int j = i + 1; j < N; j++)
      A[j] = A[i];
  }
}

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

  vector<vector<int>> Queries(Q, vector<int>(4));
  for (int i = 0; i < Q; i++)
    cin >> Queries[i][0] >> Queries[i][1] >> Queries[i][2] >> Queries[i][3];
  vector<int> A(N, 1);
  int MAX = 0;
  while (1) {
    int sum = 0;
    for (int i = 0; i < Q; i++)
      if (A[Queries[i][1] - 1] - A[Queries[i][0] - 1] == Queries[i][2])
        sum += Queries[i][3];
    MAX = max(MAX, sum);
    if (!nextvect(A, N, M))
      break;
  }
  cout << MAX << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

bool nextvect(vector<int> &A, int N, int M) {
  if (A[N - 1] < M)
    A[N - 1]++;
  else {
    int i = N - 2;
    while (i >= 0 && A[i] == M)
      i--;
    if (i == -1)
      return false;
    A[i]++;
    for (int j = i + 1; j < N; j++)
      A[j] = A[i];
  }
  return true;
}

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

  vector<vector<int>> Queries(Q, vector<int>(4));
  for (int i = 0; i < Q; i++)
    cin >> Queries[i][0] >> Queries[i][1] >> Queries[i][2] >> Queries[i][3];
  vector<int> A(N, 1);
  int MAX = 0;
  while (1) {
    int sum = 0;
    for (int i = 0; i < Q; i++)
      if (A[Queries[i][1] - 1] - A[Queries[i][0] - 1] == Queries[i][2])
        sum += Queries[i][3];
    MAX = max(MAX, sum);
    if (!nextvect(A, N, M))
      break;
  }
  cout << MAX << endl;
}
Problem p02695 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

#define INF 1e9
#define MAXN 100005
#define MAXM 100005
#define MOD 1000000007
#define ll long long
#define vi vector<int>
#define vll vector<long long>
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define pii pair<int, int>

int ans = 0, N, M, Q;
vi A, B, C, D, num;

int checkReq(vi tmp) {
  int res = 0;

  rep(i, Q) {
    if (tmp[B[i]] - tmp[A[i]] == C[i])
      res += D[i];
  }

  return res;
}

bool dfs(vi tmp) {
  int n = tmp.size();

  if (n == N) {
    // rep(i, N)
    //     cout << tmp[i+1] << " ";
    // cout << endl;
    ans = max(ans, checkReq(tmp));
  } else {
    tmp.push_back(tmp[n - 1]);
    while (tmp[n] <= M) {
      dfs(tmp);
      tmp[n]++;
    }
    tmp.pop_back();
  }
}

void solve() {

  rep(i, M) {
    num.push_back(i + 1);
    dfs(num);
    num.pop_back();
  }

  cout << ans << endl;
}

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];
    --A[i], --B[i];
  }

  solve();
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

#define INF 1e9
#define MAXN 100005
#define MAXM 100005
#define MOD 1000000007
#define ll long long
#define vi vector<int>
#define vll vector<long long>
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define pii pair<int, int>

int ans = 0, N, M, Q;
vi A, B, C, D, num;

int checkReq(vi tmp) {
  int res = 0;

  rep(i, Q) {
    if (tmp[B[i]] - tmp[A[i]] == C[i])
      res += D[i];
  }

  return res;
}

void dfs(vi tmp) {
  int n = tmp.size();

  if (n == N) {
    // rep(i, N)
    //     cout << tmp[i+1] << " ";
    // cout << endl;
    ans = max(ans, checkReq(tmp));
  } else {
    tmp.push_back(tmp[n - 1]);
    while (tmp[n] <= M) {
      dfs(tmp);
      tmp[n]++;
    }
    tmp.pop_back();
  }
}

void solve() {

  rep(i, M) {
    num.push_back(i + 1);
    dfs(num);
    num.pop_back();
  }

  cout << ans << endl;
}

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];
    --A[i], --B[i];
  }

  solve();
}
Problem p02695 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

typedef struct QRY {
  int a;
  int b;
  int c;
  int d;
} qry_t;

int main(void) {
  int N, M, Q;
  cin >> N >> M >> Q;
  vector<qry_t> qry(Q);
  for (int i = 0; i < Q; ++i) {
    cin >> qry.at(i).a >> qry.at(i).b >> qry.at(i).c >> qry.at(i).d;
  }

  vector<vector<int>> A(1, vector<int>(M)), Am(0, vector<int>(M));
  A.at(0).at(0) = 1;

  /* 数列をDFSで列挙 */
  for (int i = 1; i < N; i++) {
    Am = A;
    A.clear();

    for (auto it = Am.begin(); it != Am.end(); ++it) {
      for (int j = it->at(i - 1); j <= M; ++j) {
        vector<int> a = *it;
        a[i] = j;
        A.push_back(a);
      }
    }
  }

  int ans = 0;
  for (auto it = A.begin(); it != A.end(); ++it) {
    int tmp = 0;
    for (int i = 0; i < Q; ++i) {
      if (it->at(qry.at(i).b - 1) - it->at(qry.at(i).a - 1) == qry.at(i).c) {
        tmp += qry.at(i).d;
      }
    }
    ans = max(tmp, ans);
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

typedef struct QRY {
  int a;
  int b;
  int c;
  int d;
} qry_t;

int main(void) {
  int N, M, Q;
  cin >> N >> M >> Q;
  vector<qry_t> qry(Q);
  for (int i = 0; i < Q; ++i) {
    cin >> qry.at(i).a >> qry.at(i).b >> qry.at(i).c >> qry.at(i).d;
  }

  vector<vector<int>> A(1, vector<int>(N)), Am(0, vector<int>(N));
  A.at(0).at(0) = 1;

  /* 数列をDFSで列挙 */
  for (int i = 1; i < N; i++) {
    Am = A;
    A.clear();

    for (auto it = Am.begin(); it != Am.end(); ++it) {
      for (int j = it->at(i - 1); j <= M; ++j) {
        vector<int> a = *it;
        a[i] = j;
        A.push_back(a);
      }
    }
  }

  int ans = 0;
  for (auto it = A.begin(); it != A.end(); ++it) {
    int tmp = 0;
    for (int i = 0; i < Q; ++i) {
      if (it->at(qry.at(i).b - 1) - it->at(qry.at(i).a - 1) == qry.at(i).c) {
        tmp += qry.at(i).d;
      }
    }
    ans = max(tmp, ans);
  }

  cout << ans << endl;

  return 0;
}
Problem p02695 (C++)

Buggy submission (status: 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(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;
}

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

Buggy submission (status: 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[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);
}


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[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);
}
Problem p02695 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <unordered_map>
#include <vector>

using namespace std;

int n, m, q;
vector<vector<unordered_map<int, int>>> v;

void dfs(vector<int> &cur, int score, int &res) {
  if ((int)cur.size() == n) {
    res = max(res, score);
    return;
  }
  int i = cur.empty() ? 1 : cur.back();
  for (; i <= m; i++) {
    cur.push_back(i);
    int ns = score;
    for (int j = 0; j < (int)cur.size() - 1; j++) {
      ns += v[cur.size()][j + 1][i - cur[j]];
    }
    dfs(cur, ns, res);
    cur.pop_back();
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin >> n >> m >> q;
  v = vector<vector<unordered_map<int, int>>>(
      m + 1, vector<unordered_map<int, int>>(m + 1, unordered_map<int, int>()));
  for (int i = 0; i < q; i++) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    v[b][a][c] = d;
  }

  int res = 0;
  vector<int> cur;
  dfs(cur, 0, res);
  cout << res << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <unordered_map>
#include <vector>

using namespace std;

int n, m, q;
vector<vector<unordered_map<int, int>>> v;

void dfs(vector<int> &cur, int score, int &res) {
  if ((int)cur.size() == n) {
    res = max(res, score);
    return;
  }
  int i = cur.empty() ? 1 : cur.back();
  for (; i <= m; i++) {
    cur.push_back(i);
    int ns = score;
    for (int j = 0; j < (int)cur.size() - 1; j++) {
      ns += v[cur.size()][j + 1][i - cur[j]];
    }
    dfs(cur, ns, res);
    cur.pop_back();
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin >> n >> m >> q;
  v = vector<vector<unordered_map<int, int>>>(
      n + 1, vector<unordered_map<int, int>>(n + 1, unordered_map<int, int>()));
  for (int i = 0; i < q; i++) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    v[b][a][c] = d;
  }

  int res = 0;
  vector<int> cur;
  dfs(cur, 0, res);
  cout << res << endl;
  return 0;
}
Problem p02695 (C++)

Buggy submission (status: 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(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;
}


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

Buggy submission (status: 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(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;
}

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

Buggy submission (status: 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);
  }
  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;
}

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

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <vector>
using namespace std;

int n, m, q;
vector<int> a, b, c, d;
int ans;
void dfs(vector<int> A) {
  if (A.size() == n + 1) {
    int now = 0;
    for (int i = 0; i < q; i++) {
      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);
  for (int i = 0; i < q; i++) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  }
  vector<int> A(1, 1);
  dfs(A);
  cout << ans << endl;
}

Fixed submission:
#include <iostream>
#include <vector>
using namespace std;

int n, m, q;
vector<int> a, b, c, d;
int ans;
void dfs(vector<int> A) {
  if (A.size() == n + 1) {
    int now = 0;
    for (int i = 0; i < q; i++) {
      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);
  for (int i = 0; i < q; i++) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  }
  vector<int> A(1, 1);
  dfs(A);
  cout << ans << endl;
}
Problem p02695 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;

int a[10], b[10], c[10], d[10], e[10];
int n, m, q;
int ret = 0;

void dfs(int pos, int val) {
  if (pos == n) {
    int tmp = 0;
    for (int i = 0; i < q; i++) {
      tmp += d[i] * (e[b[i]] - e[a[i]] == c[i]);
    }
    ret = max(ret, tmp);
    return;
  }
  for (int i = val; i <= m; i++) {
    e[pos] = i;
    dfs(pos + 1, i);
  }
}

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

Fixed submission:
#include <iostream>
using namespace std;

int a[50], b[50], c[50], d[50], e[50];
int n, m, q;
int ret = 0;

void dfs(int pos, int val) {
  if (pos == n) {
    int tmp = 0;
    for (int i = 0; i < q; i++) {
      tmp += d[i] * (e[b[i]] - e[a[i]] == c[i]);
    }
    ret = max(ret, tmp);
    return;
  }
  for (int i = val; i <= m; i++) {
    e[pos] = i;
    dfs(pos + 1, i);
  }
}

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

Buggy submission (status: 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(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;
}

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
int q, ans = 0;
vector<int> e;
vector<int> f;
struct matter {
  int a, b, c, d;
};
matter p[50];
void check() {
  int val = 0;
  for (int i = 0; i < q; i++) {
    if ((f[p[i].b - 1] - f[p[i].a - 1]) == p[i].c)
      val = val + p[i].d;
  }
  ans = max(ans, val);
}
void solve(int init, int k) {
  if (k == 0) {
    check();
    return;
  }
  int prev = 0;
  if (init != 0)
    prev = init - 1;
  for (int i = prev; i <= (e.size() - k); i++) {
    f.push_back(e[i]);
    solve(i + 1, k - 1);
    f.pop_back();
  }
}
int main() {
  int n, m;
  scanf("%d%d%d", &n, &m, &q);
  for (int i = 0; i < q; i++)
    scanf("%d%d%d%d", &p[i].a, &p[i].b, &p[i].c, &p[i].d);
  for (int i = 0; i < m; i++)
    e.push_back(i + 1);
  solve(0, n);
  printf("%d\n", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
int q, ans = 0;
vector<int> e;
vector<int> f;
struct matter {
  int a, b, c, d;
};
matter p[50];
void check() {
  int val = 0;
  for (int i = 0; i < q; i++) {
    if ((f[p[i].b - 1] - f[p[i].a - 1]) == p[i].c)
      val = val + p[i].d;
  }
  ans = max(ans, val);
}
void solve(int init, int k) {
  if (k == 0) {
    check();
    return;
  }
  int prev = 0;
  if (init != 0)
    prev = init - 1;
  for (int i = prev; i < e.size(); i++) {
    f.push_back(e[i]);
    solve(i + 1, k - 1);
    f.pop_back();
  }
}
int main() {
  int n, m;
  scanf("%d%d%d", &n, &m, &q);
  for (int i = 0; i < q; i++)
    scanf("%d%d%d%d", &p[i].a, &p[i].b, &p[i].c, &p[i].d);
  for (int i = 0; i < m; i++)
    e.push_back(i + 1);
  solve(0, n);
  printf("%d\n", ans);
  return 0;
}
Problem p02695 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

long long int x[15], n, m, i, ans, a[15], b[15], c[15], d[15], q;

void bt(long long int p) {
  long long int t, s;
  if (p <= n) {
    for (t = x[p - 1]; t < m + 1; t++) {
      x[p] = t;
      bt(p + 1);
    }
    return;
  }
  s = 0;
  for (t = 1; t < q + 1; t++)
    if (x[b[t]] - x[a[t]] == c[t])
      s = s + d[t];
  ans = max(ans, s);
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  cin >> n >> m >> q;
  for (i = 1; i < q + 1; i++)
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  ans = 0;
  x[0] = 1;
  bt(1);
  cout << ans;
}


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

long long int x[15], n, m, i, ans, a[55], b[55], c[55], d[55], q;

void bt(long long int p) {
  long long int t, s;
  if (p <= n) {
    for (t = x[p - 1]; t < m + 1; t++) {
      x[p] = t;
      bt(p + 1);
    }
    return;
  }
  s = 0;
  for (t = 1; t < q + 1; t++)
    if (x[b[t]] - x[a[t]] == c[t])
      s = s + d[t];
  ans = max(ans, s);
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  cin >> n >> m >> q;
  for (i = 1; i < q + 1; i++)
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  ans = 0;
  x[0] = 1;
  bt(1);
  cout << ans;
}
Problem p02695 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define loop(i, a, n) for (ll i = a; i < n; i++)
#define loopm(i, a, n) for (ll i = n - 1; i >= a; i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define Graph(n) vector<vector<ll>>(n, vector<ll>(0, 0))
#define tr(container, it)                                                      \
  for (auto it = container.begin(); it != container.end(); it++)
#define SZ(x) ((int)(x).size())
typedef vector<long long> VI;
typedef long long ll;
typedef vector<vector<ll>> VII;
typedef pair<ll, ll> PII;
typedef double db;
struct quad {
  ll a, b, c, d;
};
vector<quad> conditions;
ll m, n;
vector<ll> perm;
ll ans = 0;
ll rec(ll i, ll lvl) {

  loop(j, i, m + 1) {
    perm[lvl] = j;
    if (lvl != n - 1) {
      rec(j, lvl + 1);
    } else {
      ll thi = 0;
      loop(f, 0, conditions.size()) {
        if (perm[conditions[f].b - 1] - perm[conditions[f].a - 1] ==
            conditions[f].c)
          thi += conditions[f].d;
      }
      ans = max(ans, thi);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ll q;
  cin >> n >> m >> q;
  conditions = vector<quad>(q);
  perm = VI(n, 0);
  loop(i, 0, q) {
    cin >> conditions[i].a;
    cin >> conditions[i].b;
    cin >> conditions[i].c;
    cin >> conditions[i].d;
  }
  rec(1, 0);
  cout << ans << "\n";
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define loop(i, a, n) for (ll i = a; i < n; i++)
#define loopm(i, a, n) for (ll i = n - 1; i >= a; i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define Graph(n) vector<vector<ll>>(n, vector<ll>(0, 0))
#define tr(container, it)                                                      \
  for (auto it = container.begin(); it != container.end(); it++)
#define SZ(x) ((int)(x).size())
typedef vector<long long> VI;
typedef long long ll;
typedef vector<vector<ll>> VII;
typedef pair<ll, ll> PII;
typedef double db;
struct quad {
  ll a, b, c, d;
};
vector<quad> conditions;
ll m, n;
vector<ll> perm;
ll ans = 0;
void rec(ll i, ll lvl) {

  loop(j, i, m + 1) {
    perm[lvl] = j;
    if (lvl != n - 1) {
      rec(j, lvl + 1);
    } else {
      ll thi = 0;
      loop(f, 0, conditions.size()) {
        if (perm[conditions[f].b - 1] - perm[conditions[f].a - 1] ==
            conditions[f].c)
          thi += conditions[f].d;
      }
      ans = max(ans, thi);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ll q;
  cin >> n >> m >> q;
  conditions = vector<quad>(q);
  perm = VI(n, 0);
  loop(i, 0, q) {
    cin >> conditions[i].a;
    cin >> conditions[i].b;
    cin >> conditions[i].c;
    cin >> conditions[i].d;
  }
  rec(1, 0);
  cout << ans << "\n";
}
Problem p02695 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <vector>

using namespace std;

int *num_array;
int array_len;
int num_max;
int score_len;

vector<int> as;
vector<int> bs;
vector<int> cs;
vector<int> ds;

bool array_next(int index);

int main(void) {
  cin >> array_len >> num_max >> score_len;

  for (int i = 0; i < score_len; i++) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    as.push_back(a - 1);
    bs.push_back(b - 1);
    cs.push_back(c);
    ds.push_back(d);
  }

  num_array = new int[array_len];

  for (int i = 0; i < array_len; i++) {
    num_array[i] = 1;
  }
  num_array[array_len - 1] = 0;

  int max_score = 0;
  while (array_next(array_len - 1)) {
    bool ascend = true;
    for (int i = 0; i < array_len - 1; i++) {
      if (num_array[i] > num_array[i + 1]) {
        ascend = false;
        break;
      }
    }

    if (ascend) {
      int current_sum = 0;
      for (int i = 0; i < score_len; i++) {
        if (num_array[bs[i]] - num_array[as[i]] == cs[i]) {
          current_sum += ds[i];
        }
      }

      if (current_sum > max_score) {
        max_score = current_sum;
      }
    }
  }

  cout << max_score << endl;

  return 0;
}

bool array_next(int index) {
  if (num_array[index] == num_max) {
    if (index == 0) {
      return false;
    }
    num_array[index] = 1;
    return array_next(index - 1);
  }

  num_array[index] += 1;
  return true;
}

Fixed submission:
#include <iostream>
#include <vector>

using namespace std;

int *num_array;
int array_len;
int num_max;
int score_len;

vector<int> as;
vector<int> bs;
vector<int> cs;
vector<int> ds;

bool array_next(int index);

int main(void) {
  cin >> array_len >> num_max >> score_len;

  for (int i = 0; i < score_len; i++) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    as.push_back(a - 1);
    bs.push_back(b - 1);
    cs.push_back(c);
    ds.push_back(d);
  }

  num_array = new int[array_len];

  for (int i = 0; i < array_len; i++) {
    num_array[i] = 1;
  }
  num_array[array_len - 1] = 0;

  int max_score = 0;
  while (array_next(array_len - 1)) {
    bool ascend = true;
    for (int i = 0; i < array_len - 1; i++) {
      if (num_array[i] > num_array[i + 1]) {
        ascend = false;
        break;
      }
    }

    if (ascend) {
      int current_sum = 0;
      for (int i = 0; i < score_len; i++) {
        if (num_array[bs[i]] - num_array[as[i]] == cs[i]) {
          current_sum += ds[i];
        }
      }

      if (current_sum > max_score) {
        max_score = current_sum;
      }
    }
  }

  cout << max_score << endl;

  return 0;
}

bool array_next(int index) {
  if (num_array[index] == num_max) {
    if (index == 0) {
      return false;
    }
    bool cont = array_next(index - 1);
    if (cont) {
      for (int i = index; i < array_len; i++) {
        num_array[i] = num_array[i - 1];
      }
    }
    return cont;
  }

  num_array[index] += 1;
  return true;
}
Problem p02695 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef vector<ll> vl;
typedef vector<string> vs;
typedef vector<char> vc;
typedef queue<ll> ql;
typedef deque<ll> dql;
typedef priority_queue<ll, vl /*, greater<ll>*/> pql; // 降順(/*昇順*/)
typedef set<ll> sl;
#define rep(i, n) for (ll i = 0; i < ll(n); ++i)
#define rep2(i, n) for (ll i = 1; i <= ll(n); ++i)
// #define rep(i, k, n) for(ll i = k-1; i < ll(n); ++i)
// #define rep2(i, k, n) for(ll i = k; i <= ll(n); ++i)
#define all(v) (v).begin(), (v).end()
bool chmin(ll &a, ll b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
bool chmax(ll &a, ll b) {
  if (b > a) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1LL << 60;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const ll MAX = 1e9;
const char newl = '\n';
ll num = 1, ans = 0, cnt = 0, sum = 0;

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

  ll n, m, q, watch;
  cin >> n >> m >> q;
  vl a(q), b(q), c(q), d(q);
  rep(i, q) { cin >> a[i] >> b[i] >> c[i] >> d[i]; }

  vl A(n + 1, 1);
  while (A[0] == 1) {
    sum = 0;
    rep(i, q) if (A[b[i]] - A[a[i]] == c[i]) sum += d[i];
    chmax(ans, sum);

    watch = n;
    while (A[watch] == m)
      watch--;
    A[watch]++;
    for (ll i = n; i > watch; i--)
      A[i] = A[watch];
  }

  cout << ans << newl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef vector<ll> vl;
typedef vector<string> vs;
typedef vector<char> vc;
typedef queue<ll> ql;
typedef deque<ll> dql;
typedef priority_queue<ll, vl /*, greater<ll>*/> pql; // 降順(/*昇順*/)
typedef set<ll> sl;
#define rep(i, n) for (ll i = 0; i < ll(n); ++i)
#define rep2(i, n) for (ll i = 1; i <= ll(n); ++i)
// #define rep(i, k, n) for(ll i = k-1; i < ll(n); ++i)
// #define rep2(i, k, n) for(ll i = k; i <= ll(n); ++i)
#define all(v) (v).begin(), (v).end()
bool chmin(ll &a, ll b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
bool chmax(ll &a, ll b) {
  if (b > a) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1LL << 60;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const ll MAX = 1e9;
const char newl = '\n';
ll num = 1, ans = 0, cnt = 0, sum = 0;

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

  ll n, m, q, watch;
  cin >> n >> m >> q;
  vl a(q), b(q), c(q), d(q);
  rep(i, q) { cin >> a[i] >> b[i] >> c[i] >> d[i]; }

  if (m == 1) {
    rep(i, q) if (!c[i]) ans += d[i];
    cout << ans << newl;
    return 0;
  }

  vl A(n + 1, 1);
  while (A[0] == 1) {
    sum = 0;
    rep(i, q) if (A[b[i]] - A[a[i]] == c[i]) sum += d[i];
    chmax(ans, sum);

    watch = n;
    while (A[watch] == m)
      watch--;
    A[watch]++;
    for (ll i = n; i > watch; i--)
      A[i] = A[watch];
  }

  cout << ans << newl;
  return 0;
}
Problem p02695 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cctype>    // isupper, islower, isdigit, toupper, tolower
#include <climits>
#include <cstdint> // int64_t, int*_t
#include <cstdio>  // printf
#include <deque>   // deque
#include <functional>
#include <iomanip> //setprecision
#include <iostream>
#include <map> // map
#include <math.h>
#include <queue>         // queue, priority_queue
#include <set>           // set
#include <stack>         // stack
#include <string>        // string, to_string, stoi
#include <tuple>         // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
int n, m, p, ans = 0;
vector<int> e, b, c, d;
void dfs(vector<int> a) {
  if (a.size() == n + 1) {
    int now = 0;
    rep(i, p) {
      if (a[b[i]] - a[e[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 >> p;
  e = b = c = d = vector<int>(p);
  rep(i, p) { cin >> e[i] >> b[i] >> c[i] >> d[i]; }
  dfs(vector<int>(1, 1));
  cout << ans;
}


Fixed submission:
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cctype>    // isupper, islower, isdigit, toupper, tolower
#include <climits>
#include <cstdint> // int64_t, int*_t
#include <cstdio>  // printf
#include <deque>   // deque
#include <functional>
#include <iomanip> //setprecision
#include <iostream>
#include <map> // map
#include <math.h>
#include <queue>         // queue, priority_queue
#include <set>           // set
#include <stack>         // stack
#include <string>        // string, to_string, stoi
#include <tuple>         // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
int n, m, p, ans = 0;
vector<int> e, b, c, d;
void dfs(vector<int> a) {
  if (a.size() == n + 1) {
    int now = 0;
    rep(i, p) {
      if (a[b[i]] - a[e[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 >> p;
  e = b = c = d = vector<int>(p);
  rep(i, p) { cin >> e[i] >> b[i] >> c[i] >> d[i]; }
  dfs(vector<int>(1, 1));
  cout << ans;
}