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

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
int save[100000][20];
int now[20];
int n, m, Q, tot;
void dfs(int index, int p) {
  if (index == n) {
    for (int i = 0; i < n; i++) {
      save[tot][i] = now[i];
    }
    tot++;
    return;
  }
  if (index > n) {
    return;
  }
  for (int i = p; i <= m; i++) {
    now[index] = i;
    dfs(index + 1, i);
  }
}
void init() {
  tot = 0;
  dfs(0, 1);
}

int qu[60][10];

int get_sum(int index) {
  int sum = 0;
  for (int i = 0; i < Q; i++) {
    if (save[index][qu[i][1] - 1] - save[index][qu[i][0] - 1] == qu[i][2]) {
      sum += qu[i][3];
    }
  }
  return sum;
}

int main() {
  cin >> n >> m >> Q;
  init();
  for (int i = 0; i < Q; i++) {
    cin >> qu[i][0] >> qu[i][1] >> qu[i][2] >> qu[i][3];
  }
  int maxx = 0;
  for (int i = 0; i < tot; i++) {
    int now = get_sum(i);
    if (now >= maxx) {
      maxx = now;
    }
  }
  cout << maxx << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
int save[1050][20];
int now[20];
int n, m, Q, tot;
void dfs(int index, int p) {
  if (index == n) {
    for (int i = 0; i < n; i++) {
      save[tot][i] = now[i];
    }
    tot++;
    return;
  }
  if (index > n) {
    return;
  }
  for (int i = p; i <= m; i++) {
    now[index] = i;
    dfs(index + 1, i);
  }
}
void init() {
  tot = 0;
  dfs(0, 1);
}

int qu[60][10];

int get_sum(int index) {
  int sum = 0;
  for (int i = 0; i < Q; i++) {
    if (save[index][qu[i][1] - 1] - save[index][qu[i][0] - 1] == qu[i][2]) {
      sum += qu[i][3];
    }
  }
  return sum;
}

int main() {
  cin >> n >> m >> Q;
  init();
  for (int i = 0; i < Q; i++) {
    cin >> qu[i][0] >> qu[i][1] >> qu[i][2] >> qu[i][3];
  }
  int maxx = 0;
  for (int i = 0; i < tot; i++) {
    int now = get_sum(i);
    if (now >= maxx) {
      maxx = now;
    }
  }
  cout << maxx << endl;
  return 0;
}
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

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

int ans = 0;
int N, M, Q;
int a[51], b[51], c[51], d[51];
int A[11];
void sum_d(int A[], int n) {

  if (n == N) {
    int d_sum = 0;
    for (int i = 0; i < Q; i++) {
      if (A[b[i] - 1] - A[a[i] - 1] == c[i]) {
        d_sum += d[i];
      }
    }
    ans = max(ans, d_sum);
  } else {
    for (int i = A[n - 1]; i <= M; i++) {
      A[n] = i;
      sum_d(A, n + 1);
    }
  }
}

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


Error: 0

Fixed submission:

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

int ans = 0;
int N, M, Q;
int a[51], b[51], c[51], d[51];

void sum_d(int A[], int n) {

  if (n - 1 == N - 1) {
    int d_sum = 0;
    for (int i = 0; i < Q; i++) {
      if (A[b[i] - 1] - A[a[i] - 1] == c[i]) {
        d_sum += d[i];
      }
    }
    ans = max(ans, d_sum);
  } else {
    for (int i = A[n - 1]; i <= M; i++) {
      A[n] = i;
      sum_d(A, n + 1);
    }
  }
}

int main() {
  int A[N];
  A[0] = 1;
  cin >> N >> M >> Q;
  for (int i = 0; i < Q; i++) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  }
  sum_d(A, 1);
  cout << ans << endl;
}
Problem p02695 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define int long long
#define ll long long
#define pb push_back
#define mp make_pair
#define f(i, a, n) for (int i = a; i < (int)n; i++)
#define rf(i, n, a) for (int i = n; i >= a; i--)
#define F first
#define S second
#define all(c) (c).begin(), (c).end()
#define sz(v) (int)(v).size()
#define fast ios::sync_with_stdio(false), cin.tie(nullptr);

typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair<pii, int> ppi;
typedef vector<pii> vpi;

const int inf = 1e9;
const int inf64 = 1e18;
const int MOD = inf + 7;
int n, m, q;
int a[55], b[55], c[55], d[55];
int ans = 0;

void solve(vector<int> v, int mx = 1) {
  if (sz(v) == n + 1) {
    int cur = 0;
    f(i, 0, q) if (v[b[i]] - v[a[i]] == c[i]) cur += d[i];
    ans = max(ans, cur);
    return;
  }

  for (int i = mx; i <= m; i++) {
    v.push_back(i);
    solve(v, i);
    v.pop_back();
  }
}
int32_t main() {
  fast;
  cin >> n >> m >> q;
  f(i, 0, q) cin >> a[i] >> b[i] >> c[i] >> d[i];
  vector<int> v = {1};
  solve(v);
  cout << ans;
}

Error: TLE

Fixed submission:

#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define pb push_back
#define mp make_pair
#define f(i, a, n) for (int i = a; i < (int)n; i++)
#define rf(i, n, a) for (int i = n; i >= a; i--)
#define F first
#define S second
#define all(c) (c).begin(), (c).end()
#define sz(v) (int)(v).size()
#define fast ios::sync_with_stdio(false), cin.tie(nullptr);

typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair<pii, int> ppi;
typedef vector<pii> vpi;

const int inf = 1e9;
const int MOD = inf + 7;
int n, m, q;
vi a;
int v[55][4];

bool pos(vi &a, int id, int val) {
  bool res = true;
  if (val < 0 || val > m)
    res = false;
  for (int i = 1; i <= id - 1; i++) {
    if (a[i] != -1 && a[i] > val)
      res = false;
  }
  for (int i = id + 1; i <= n; i++) {
    if (a[i] != -1 && a[i] < val)
      res = false;
  }
  return res;
}

map<pair<vi, int>, int> dp;

int solve(vi &a, int id) {
  if (dp.find({a, id}) != dp.end())
    return (dp[{a, id}]);
  if (id == q)
    return 0;
  int res = solve(a, id + 1);
  int l = v[id][0];
  int r = v[id][1];
  int c = v[id][2];
  int d = v[id][3];

  for (int i = 1; i <= m; i++) {
    vi v = a;
    if (v[l] == i || (pos(v, l, i) && v[l] == -1)) {
      v[l] = i;
      if (v[r] == i + c || (pos(v, r, i + c) && v[r] == -1)) {
        v[r] = i + c;
        res = max(res, d + solve(v, id + 1));
      }
    }
  }
  dp[{a, id}] = res;
  return res;
}
int32_t main() {
  fast;
  cin >> n >> m >> q;
  f(i, 0, q) f(j, 0, 4) cin >> v[i][j];

  a.resize(n + 1, -1);
  int ans = solve(a, 0);
  cout << ans << "\\n";
}
Problem p02695 (C++) — buggy submission, verdict: 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;
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";
}


Error: TLE

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;
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";
}
Problem p02695 (C++) — buggy submission, verdict: 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;
    }
    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;
}

Error: TLE

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;
    }
    num_array[index] = 1;
    return array_next(index - 1);
  }

  num_array[index] += 1;
  return true;
}
Problem p02695 (C++) — buggy submission, verdict: 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]; }

  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;
}

Error: 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]; }

  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, verdict: 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);
    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;
}


Error: TLE

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);
  }
  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;
}
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int A[12], a[52], b[52], c[52], d[52];
int n, m, q, ans = -1;
void score() {
  int sum = 0;
  for (int j = 1; j <= q; j++)
    if (A[b[j]] - A[a[j]] == c[j])
      sum += d[j];
  ans = max(ans, sum);
}
void solve(int k, int q) {
  for (int i = q; i <= m; i++) {
    A[k] = i;
    if (k == n)
      score();
    if (n == k && i == m)
      break;
    //		k++;
    if (k + 1 <= n)
      solve(k + 1, i);
  }
}
int main() {
  cin >> n >> m >> q;
  for (int j = 1; j <= q; j++)
    cin >> a[j] >> b[j] >> c[j] >> d[j];
  solve(1, 1);
  cout << ans << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int A[12], a[52], b[52], c[52], d[52];
int n, m, q, ans = -1;
void score() {
  int sum = 0;
  for (int j = 1; j <= q; j++)
    if (A[b[j]] - A[a[j]] == c[j])
      sum += d[j];
  ans = max(ans, sum);
}
void solve(int k) {
  for (int i = 1; i <= m; i++) {
    A[k] = i;
    if (k == n)
      score();
    if (n == k && i == m)
      break;
    solve(++k);
  }
}
int main() {
  cin >> n >> m >> q;
  for (int j = 1; j <= q; j++)
    cin >> a[j] >> b[j] >> c[j] >> d[j];
  solve(1);
  cout << ans << 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 < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b - 1); i >= (a); i--)
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
#define pb push_back
using ll = long long;
using D = double;
using LD = long double;
using P = pair<int, int>;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  int n, m, q;
  cin >> n >> m >> q;
  vector<int> a(q), b(q), c(q), d(q);
  rep(i, q) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
    a[i]--;
    b[i]--;
  }
  vector<int> v(n, 1);
  int ans = 0;
  rep(i, q) {
    if (v[b[i]] - v[a[i]] == c[i])
      ans += d[i];
  }
  auto next = [&] {
    if (v[0] == m)
      return false;
    int num = 0, counter = n;
    rrep(i, n) {
      if (v[i] < m) {
        num = v[i] + 1;
        counter = i;
        break;
      }
    }
    FOR(i, counter, n) { v[i] = num; }
    return true;
  };

  while (next()) {
    int tmp = 0;
    rep(i, q) {
      if (v[b[i]] - v[a[i]] == c[i])
        tmp += d[i];
    }
    chmax(ans, tmp);
  }
  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b - 1); i >= (a); i--)
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
#define pb push_back
using ll = long long;
using D = double;
using LD = long double;
using P = pair<int, int>;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  int n, m, q;
  cin >> n >> m >> q;
  vector<int> a(q), b(q), c(q), d(q);
  rep(i, q) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
    a[i]--;
    b[i]--;
  }
  vector<int> v(n, 1);
  int ans = 0;
  rep(i, q) {
    if (v[b[i]] - v[a[i]] == c[i])
      ans += d[i];
  }
  auto next = [&] {
    if (v[0] == m)
      return false;
    int num, counter;
    rrep(i, n) {
      if (v[i] < m) {
        num = v[i] + 1;
        counter = i;
        break;
      }
    }
    FOR(i, counter, n) { v[i] = num; }
  };

  while (next()) {
    int tmp = 0;
    rep(i, q) {
      if (v[b[i]] - v[a[i]] == c[i])
        tmp += d[i];
    }
    chmax(ans, tmp);
  }
  cout << ans << endl;
}