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

#include <bits/stdc++.h>
#pragma GCC optimize("O3")

#define fi first
#define se second
#define em emplace
#define eb emplace_back
#define mp make_pair
#define pi pair<int, int>
#define vi vector<int>
#define vpi vector<pair<int, int>>
#define vll vector<ll>
#define vpll vector<pair<ll, ll>>
#define endl '\\n'

#define si(v) int((v).size())
#define all(v) (v).begin(), (v).end()
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define rep2(i, n, m) for (ll i = n; i <= (ll)(m); ++i)
#define rep3(i, n, m) for (ll i = n; i >= (ll)(m); --i)

template <class T, class U> bool chmax(T &a, U b) {
  if (a < b)
    a = b;
  return true;
}
template <class T, class U> bool chmin(T &a, U b) {
  if (a > b)
    a = b;
  return true;
}

using ll = long long;
using ld = long double;
using namespace std;

constexpr ll MOD = 1000000007;

int n, m, q, ans;
vector<vi> a(60, vi(4));

void dfs(int cnt = 0, vi v = {}, int last = 1) {
  if (cnt == n) {
    int sum = 0;
    rep(i, q) if (v[a[i][1] - 1] - v[a[i][0] - 1] == a[i][2]) sum += a[i][3];

    chmax(ans, sum);
    return;
  }

  rep2(i, last, m) {
    v.eb(i);

    dfs(cnt + 1, v, last = i);

    v.pop_back();
  }
}

signed main() {
  cout << fixed << setprecision(20);
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> n >> m >> q;
  rep(i, q) rep(j, 4) cin >> a[i][j];

  dfs();
  cout << ans << endl;

  return (0);
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#pragma GCC optimize("O3")

#define fi first
#define se second
#define em emplace
#define eb emplace_back
#define mp make_pair
#define pi pair<int, int>
#define vi vector<int>
#define vpi vector<pair<int, int>>
#define vll vector<ll>
#define vpll vector<pair<ll, ll>>
#define endl '\\n'

#define si(v) int((v).size())
#define all(v) (v).begin(), (v).end()
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define rep2(i, n, m) for (ll i = n; i <= (ll)(m); ++i)
#define rep3(i, n, m) for (ll i = n; i >= (ll)(m); --i)

template <class T, class U> bool chmax(T &a, U b) {
  if (a < b)
    a = b;
  return true;
}
template <class T, class U> bool chmin(T &a, U b) {
  if (a > b)
    a = b;
  return true;
}

using ll = long long;
using ld = long double;
using namespace std;

constexpr ll MOD = 1000000007;

int n, m, q, ans;
vector<vi> a(10, vi(4));

void dfs(int cnt = 0, vi v = {}, int last = 1) {
  if (cnt >= n) {
    int sum = 0;
    rep(i, q) if (v[a[i][1] - 1] - v[a[i][0] - 1] == a[i][2]) sum += a[i][3];

    chmax(ans, sum);
    return;
  }

  rep2(i, last, m) {
    v.eb(i);

    dfs(cnt + 1, v, last = i);

    v.pop_back();
  }
}

signed main() {
  cout << fixed << setprecision(20);
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> n >> m >> q;
  rep(i, q) rep(j, 4) cin >> a[i][j];

  dfs();
  cout << ans << endl;

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

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

typedef long long ll;
typedef pair<ll, ll> pi;
typedef vector<ll> vi;
typedef vector<pi> vpi;
#define f first
#define s second
#define FOR(i, s, e) for (ll i = s; i <= ll(e); ++i)
#define DEC(i, s, e) for (ll i = s; i >= ll(e); --i)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
#define aFOR(i, x) for (auto i : x)
#define mem(x, i) memset(x, i, sizeof x)
#define fast ios_base::sync_with_stdio(false), cin.tie(0)
#define maxn 20
#define maxq 60
#define int ll
int A[maxq], B[maxq], C[maxq], D[maxq];

int N, M, Q;
int ans = 0;

vi vect;
void gen(int idx) {

  if (idx == N + 1) {
    int res = 0;

    FOR(i, 1, Q) {
      if (vect[B[i] - 1] - vect[A[i] - 1] == C[i])
        res += D[i];
    }

    ans = max(ans, res);
    return;
  }

  int lim = 1;
  if (!vect.empty())
    lim = max(lim, vect.back());

  FOR(i, lim, M) {
    vect.pb(i);
    gen(idx + 1);
    vect.pop_back();
  }
}
int32_t main() {
  fast;

  cin >> N >> M >> Q;

  FOR(i, 1, Q) cin >> A[i] >> B[i] >> C[i] >> D[i];

  gen(1);

  cout << ans;
}


Error: TLE

Fixed submission:

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

typedef long long ll;
typedef pair<ll, ll> pi;
typedef vector<ll> vi;
typedef vector<pi> vpi;
#define f first
#define s second
#define FOR(i, s, e) for (ll i = s; i <= ll(e); ++i)
#define DEC(i, s, e) for (ll i = s; i >= ll(e); --i)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
#define aFOR(i, x) for (auto i : x)
#define mem(x, i) memset(x, i, sizeof x)
#define fast ios_base::sync_with_stdio(false), cin.tie(0)
#define maxn 12

int A[maxn], B[maxn], C[maxn], D[maxn];

vpi adj[maxn];
bool vis[maxn];

bool yes;
int N, M, Q;
int ans = 0;

void gen(int idx, int a[maxn]) {
  if (idx == N + 1) {
    int res = 0;

    FOR(i, 1, Q) {
      if (a[B[i]] - a[A[i]] == C[i])
        res += D[i];
    }
    ans = max(ans, res);
    return;
  }

  if (idx == 0) {
    FOR(i, 0, M) {
      int new_[maxn];
      FOR(i, 1, N) new_[i] = a[i];

      new_[idx] = i;
      gen(idx + 1, new_);
    }
  } else {
    FOR(i, a[idx - 1], M) {
      int new_[maxn];
      FOR(i, 1, N) new_[i] = a[i];
      new_[idx] = i;
      gen(idx + 1, new_);
    }
  }
}
int main() {
  fast;

  cin >> N >> M >> Q;

  FOR(i, 1, Q) cin >> A[i] >> B[i] >> C[i] >> D[i];

  int init[maxn];

  gen(1, init);

  cout << ans;
}
Problem p02695 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

#define MOD (long long)(1e9 + 7)
#define REP(i, n) for (ll i = 0; i < n; i++)
#define REP1(i, n) for (ll i = 1; i <= n; i++)

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

int main(void) {
  ll n, m, q;
  cin >> n >> m >> q;
  vector<ll> a(q), b(q), c(q), d(q);
  REP(i, q) cin >> a[i] >> b[i] >> c[i] >> d[i];
  vector<ll> A(n);
  REP(i, n) A[i] = 1;
  ll maxans = 0;
  REP(i, q) maxans += d[i];
  ll ans = 0;
  ll ma = 1;
  REP(i, n) ma *= m;

  REP(i, ma) {
    ll i0 = i;
    /*
    REP(j, n) {
      A[n - j - 1] = (i0 % m) + 1;
      i0 /= m;
    }
    */
    ll point = 0;
    bool ok = true;
    REP(j, n - 1) {
      if (A[j + 1] < A[j])
        ok = false;
    }
    if (!ok)
      continue;

    //        REP(j, n) cout << A[j] << " ";

    REP(k, q) {
      if (A[b[k] - 1] - A[a[k] - 1] == c[k])
        point += d[k];
    }
    //        cout << " : " << point << endl;
    if (point > ans)
      ans = point;
    if (ans == maxans)
      break;

    A[n - 1]++;
    for (ll k = n - 1; k >= 0; k--) {
      if (A[k] > m) {
        A[k - 1]++;
        for (ll l = k; l < n; l++) {
          A[l] = A[k - 1];
        }
      }
    }
    bool ok2 = true;
    REP(i, n) {
      if (A[i] != m)
        ok2 = false;
    }
    if (ok2)
      break;
  }

  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

#define MOD (long long)(1e9 + 7)
#define REP(i, n) for (ll i = 0; i < n; i++)
#define REP1(i, n) for (ll i = 1; i <= n; i++)

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

int main(void) {
  ll n, m, q;
  cin >> n >> m >> q;
  vector<ll> a(q), b(q), c(q), d(q);
  REP(i, q) cin >> a[i] >> b[i] >> c[i] >> d[i];
  vector<ll> A(n);
  ll maxans = 0;
  REP(i, q) maxans += d[i];
  ll ans = 0;
  ll ma = 1;
  REP(i, n) ma *= m;

  REP(i, ma) {
    ll i0 = i;
    REP(j, n) {
      A[n - j - 1] = (i0 % m) + 1;
      i0 /= m;
    }
    ll point = 0;
    bool ok = true;
    REP(j, n - 1) {
      if (A[j + 1] < A[j])
        ok = false;
    }
    if (!ok)
      continue;

    //    REP(j, n) cout << A[j] << " ";

    REP(k, q) {
      if (A[b[k] - 1] - A[a[k] - 1] == c[k])
        point += d[k];
    }
    //    cout << " : " << point << endl;
    if (point > ans)
      ans = point;
    if (ans == maxans)
      break;
  }

  cout << ans << endl;
}
Problem p02695 (C++) — buggy submission, verdict: Time Limit Exceeded

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

long long N, M, Q;

long long ans[15];

long long a[55];
long long b[55];
long long c[55];
long long d[55];

long long maxi;

long long dfs(long long pa, long long dep) {
  if (dep == N) {
    long long tmp = 0;
    for (long long i = 0; i < Q; i++) {
      if (ans[b[i]] - ans[a[i]] == c[i]) {
        tmp += d[i];
      }
    }
    maxi = max(maxi, tmp);
    return 0;
  }

  for (int i = pa; i <= M; i++) {
    ans[dep] = i;
    dfs(i, dep + 1);
  }
  return 0;
}

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

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

  dfs(1, 0);
  cout << maxi << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

long long N, M, Q;

long long ans[15];

long long a[55];
long long b[55];
long long c[55];
long long d[55];

long long maxi;

long long dfs(long long pa, long long dep) {
  if (dep == N) {
    long long tmp = 0;
    for (long long i = 0; i < Q; i++) {
      if (ans[b[i]] - ans[a[i]] == c[i]) {
        tmp += d[i];
      }
    }
    maxi = max(maxi, tmp);
    return 0;
  }

  for (int i = pa; i <= M; i++) {
    ans[dep] = i;
    dfs(i, dep + 1);
  }
}

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

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

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

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

Error: 0

Fixed submission:

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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <tuple>
#include <vector>
using namespace std;
vector<tuple<int, int, int, int>> vec;

long long calAns(vector<int> &A) {
  long long ans = 0;
  for (auto it : vec) {
    int a, b, c, d;
    tie(a, b, c, d) = it;
    if (A[b - 1] - A[a - 1] == c)
      ans += d;
  }
  return ans;
}

long long sol(vector<int> &A, int cur, int end, int m) {
  if (cur == end) {
    return calAns(A);
  }
  long long res = 0;
  int s = A.size() ? A[cur - 1] : 1;

  for (int i = s; i <= m; ++i) {
    A.push_back(i);
    res = max(res, sol(A, cur + 1, end, m));
    A.pop_back();
  }
  return res;
}

int main() {
  int n, m, q;
  cin >> n >> m >> q;
  for (int i = 0, a, b, c, d; i < q; ++i) {
    cin >> a >> b >> c >> d;
    vec.emplace_back(a, b, c, d);
  }
  vector<int> A;
  cout << sol(A, 0, n, m);

  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <tuple>
#include <vector>
using namespace std;
vector<tuple<int, int, int, int>> vec;
int dist[11][11] = {};

int calAns(vector<int> &A) {
  int ans = 0;
  for (auto it : vec) {
    int a, b, c, d;
    tie(a, b, c, d) = it;
    if (A[b - 1] - A[a - 1] == c)
      ans += d;
  }
  return ans;
}

int sol(vector<int> &A, int cur, int end) {
  if (cur == end) {
    return calAns(A);
  }
  int res = 0;
  int s = A.size() ? A[cur - 1] : 1;
  int e = 0;
  for (int i = 0; i <= end; ++i) {
    e = max(e, dist[s][i]);
  }

  for (int i = s; i <= s + e; ++i) {
    A.push_back(i);
    res = max(res, sol(A, cur + 1, end));
    A.pop_back();
  }
  return res;
}

int main() {
  int n, m, q;
  cin >> n >> m >> q;
  for (int i = 0, a, b, c, d; i < q; ++i) {
    cin >> a >> b >> c >> d;
    vec.emplace_back(a, b, c, d);
    dist[a][b] = max(dist[a][b], c);
    dist[b][a] = max(dist[b][a], c);
  }
  vector<int> A;
  cout << sol(A, 0, n);

  return 0;
}
Problem p02695 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(nullptr);                                                            \\
  cin.tie(nullptr);
#define int long long
#define ld long double
#define pb emplace_back
#define range(v) v.begin(), v.end()
#define mod (int)(1e9 + 7)
#define inf (int)1e15
#define pii pair<int, int>
#define ff first
#define ss second
// #define N --

using namespace std;

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

int n, m, q, ans = -inf;
vector<query> v;
deque<int> s;

int maxAns() {
  int ans = 0;
  for (query &q : v)
    if (s[q.b - 1] - s[q.a - 1] == q.c)
      ans += q.d;
  return ans;
}

void require(int i = 1) {
  if (s.size() == n) {
    ans = max(ans, maxAns());
    return;
  }

  if (i > m)
    return;

  require(i + 1);
  s.pb(i);
  require(i);
  s.pop_back();
}

int32_t main() {
  fast int t_c = 1;
  //	cin >> t_c;
  while (t_c--) {
    //		cout << "#: ";
    cin >> n >> m >> q;
    v.resize(q);
    for (query &q : v)
      cin >> q.a >> q.b >> q.c >> q.d;
    require();
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(nullptr);                                                            \\
  cin.tie(nullptr);
#define int long long
#define ld long double
#define pb emplace_back
#define range(v) v.begin(), v.end()
#define mod (int)(1e9 + 7)
#define inf (int)1e15
#define pii pair<int, int>
#define ff first
#define ss second
// #define N --

using namespace std;

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

int n, m, q, ans = -inf;
vector<query> v;

int maxAns(string &s) {
  int ans = 0;
  for (query &q : v)
    if (s[q.b - 1] - s[q.a - 1] == q.c)
      ans += q.d;
  return ans;
}

void require(int i = 1, string s = "") {
  if (s.length() == n) {
    ans = max(ans, maxAns(s));
    return;
  }

  for (int j = i; j <= m; j++)
    require(j, s + to_string(j));
}

int32_t main() {
  fast int t_c = 1;
  //	cin >> t_c;
  while (t_c--) {
    //		cout << "#: ";
    cin >> n >> m >> q;
    v.resize(q);
    for (query &q : v)
      cin >> q.a >> q.b >> q.c >> q.d;
    require();
    cout << ans << endl;
  }
}
Problem p02695 (C++) — buggy submission, verdict: 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[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;
}

Error: 0

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

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

typedef long long ll;

ll a[50], b[50], c[50], d[50];
int N, M, Q;
ll ans;

void dfs(vector<int> cur) {
  if (cur.size() == N) {
    ll sum = 0;
    for (int i = 0; i < Q; i++) {
      if (a[i] < cur.size() && b[i] < cur.size()) {
        if ((cur[b[i]] - cur[a[i]]) == c[i]) {
          sum += d[i];
        }
      }
    }

    ans = max(sum, ans);
    return;
  }

  if (cur.size() == 0) {
    for (int i = 1; i <= M; i++) {
      cur.push_back(i);
      dfs(cur);
      cur.pop_back();
    }
  }

  else {
    for (int i = cur.back(); i <= M; i++) {
      cur.push_back(i);
      dfs(cur);
      cur.pop_back();
    }
  }
}

int main() {
  cin >> N >> M >> Q;
  ans = 0;

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

  vector<int> cur;

  dfs(cur);

  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

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

typedef long long ll;

int main() {
  int N, M, Q;
  cin >> N >> M >> Q;
  ll num[4][50];
  ll ans = 0;

  for (int i = 0; i < Q; i++) {
    for (int j = 0; j < 4; j++) {
      cin >> num[i][j];
    }
  }

  int lim = M + 1;

  vector<vector<ll>> lis((1 << lim));

  for (int bit = 0; bit < (1 << lim); bit++) {
    for (int i = 0; i <= M; i++) {
      if (i == 0)
        continue;
      if (bit & (1 << i))
        lis[bit].push_back(i);
    }
  }

  for (int bit = 0; bit < (1 << lim); bit++) {
    ll sum = 0;
    if ((int)lis[bit].size() != N)
      continue;
    for (int i = 0; i < Q; i++) {
      int a = num[i][0] - 1;
      int b = num[i][1] - 1;
      if ((a < (int)lis[bit].size()) && (b < (int)lis[bit].size())) {
        if ((lis[bit][b] - lis[bit][a]) == num[i][2])
          sum += num[i][3];
      }
    }
    ans = max(ans, sum);
  }

  cout << ans << endl;

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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn = 50 + 5;
int a[maxn], b[maxn], c[maxn], d[maxn];
int n, m, Q;
int ans = 0;
int res[maxn];
void dfs(int step, int last) {
  if (step == n + 1) {
    int cost = 0;
    for (int i = 1; i <= Q; i++) {
      if (res[b[i]] - res[a[i]] == c[i])
        cost += d[i];
    }
    ans = max(ans, cost);
    return;
  }
  for (int i = last; i <= m; i++) {
    res[step] = i;
    dfs(step + 1, i);
    res[step] = 0;
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> m >> Q;
  for (int i = 1; i <= Q; i++) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  }
  dfs(1, 1);
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn = 10 + 5;
int a[maxn], b[maxn], c[maxn], d[maxn];
int n, m, Q;
int ans = 0;
int res[maxn];
void dfs(int step, int last) {
  if (step == n + 1) {
    int cost = 0;
    for (int i = 1; i <= Q; i++) {
      if (res[b[i]] - res[a[i]] == c[i])
        cost += d[i];
    }
    ans = max(ans, cost);
    return;
  }
  for (int i = last; i <= m; i++) {
    res[step] = i;
    dfs(step + 1, i);
    res[step] = 0;
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> m >> Q;
  for (int i = 1; i <= Q; i++) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  }
  dfs(1, 1);
  cout << ans << endl;
  return 0;
}
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define LL long long
#define rep(i, a, b) for (int i = a; i <= b; ++i)
using namespace std;

const int mn = 57;
int a[mn], b[mn], c[mn], d[mn], v[mn] = {1};
int n, m, q, ans;

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

int main() {
  scanf("%d%d%d", &n, &m, &q);
  rep(i, 1, q) scanf("%d%d%d%d", &a[i], &b[i], &c[i], &d[i]);
  dfs(1);
  printf("%d", ans);
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define LL long long
#define rep(i, a, b) for (int i = a; i <= b; ++i)
using namespace std;

const int mn = 17;
int a[mn], b[mn], c[mn], d[mn], v[mn] = {1};
int n, m, q, ans;

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

int main() {
  scanf("%d%d%d", &n, &m, &q);
  rep(i, 1, q) scanf("%d%d%d%d", &a[i], &b[i], &c[i], &d[i]);
  dfs(1);
  printf("%d", ans);
}
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef pair<int, int> P;
typedef tuple<int, int, int, int> T;
#define PI 3.14159265359
#define MOD 1000000007
const int MAX = 510000;
int maxd = 0;
vector<int> A(11);
int N, M, Q;
vector<int> a(51);
vector<int> b(51);
vector<int> c(51);
vector<int> d(51);
void dfs(int n, int m, int x) {
  int dn = N - n;
  A[dn] = x;
  if (n != 1) {
    for (int i = x; i <= m; i++) {
      dfs(n - 1, m, i);
    }
  } else {
    int memo = 0;
    rep(i, Q) {
      if (A[b[i]] == (A[a[i]] + c[i]))
        memo += d[i];
    }
    ::maxd = max(memo, ::maxd);
  }
}
int main() {
  cin >> N >> M >> Q;
  rep(i, Q) {
    int x, y, z, w;
    cin >> x >> y >> z >> w;
    a[i] = x - 1;
    b[i] = y - 1;
    c[i] = z;
    d[i] = w;
  }
  dfs(N, M, 1);
  cout << maxd;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef pair<int, int> P;
typedef tuple<int, int, int, int> T;
#define PI 3.14159265359
#define MOD 1000000007
const int MAX = 510000;
int maxd = 0;
vector<int> A(11);
int N, M, Q;
vector<int> a(51);
vector<int> b(51);
vector<int> c(51);
vector<int> d(51);
int dfs(int n, int m, int x) {
  int dn = N - n;
  A[dn] = x;
  if (n != 1) {
    for (int i = x; i <= m; i++) {
      dfs(n - 1, m, i);
    }
  } else {
    int memo = 0;
    rep(i, Q) {
      if (A[b[i]] == (A[a[i]] + c[i]))
        memo += d[i];
    }
    ::maxd = max(memo, ::maxd);
  }
}
int main() {
  cin >> N >> M >> Q;
  rep(i, Q) {
    int x, y, z, w;
    cin >> x >> y >> z >> w;
    a[i] = x - 1;
    b[i] = y - 1;
    c[i] = z;
    d[i] = w;
  }
  dfs(N, M, 1);
  cout << maxd;
}