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

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

ll N, M, Q;
ll ans = -1;
vector<ll> A;
void dfs(ll len_A, vector<vector<ll>> &vec, ll current) {
  if (len_A == N) {
    ll sub_ans = 0;
    rep(i, Q) {
      if (A[vec[i][1]] - A[vec[i][0]] == vec[i][2])
        sub_ans += vec[i][3];
    }
    ans = max(ans, sub_ans);
    return;
  }

  for (ll i = current; i <= M; i++) {
    A.push_back(i);
    dfs(len_A + 1, vec, i);
    A.pop_back();
  }
  return;
}

int main() {
  cin >> N >> M >> Q;
  vector<vector<ll>> vec(Q, vector<ll>(4));
  rep(i, Q) {
    rep(j, 4) { cin >> vec[i][j]; }
    vec[i][0]--;
    vec[i][1]--;
  }
  dfs(0, vec, 1);
  cout << ans << endl;
}


Error: -11

Fixed submission:

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

int ans(vector<vector<int>> vec, vector<int> A, int N, int Q) {
  bool check_1 = true;
  int sub_ans = 0;
  int sub_sub_ans = 0;
  rep(i, Q) {
    if (A[vec[i][1]] - A[vec[i][0]] == A[vec[i][2]]) {
      sub_ans += vec[i][3];
    }
  }
  rep(i, N) {
    if (A[i] != 1)
      check_1 = false;
  }
  if (check_1) {
    return sub_ans;
  }

  rep(i, N) {
    if (i == 0) {
      if (A[i] <= A[i + 1]) {
        vector<int> A_copy(N);
        A_copy[0] = A[0] - 1;
        for (int j = 1; j < N; j++) {
          A_copy[j] = A[j];
        }
        sub_sub_ans = max(sub_ans, ans(vec, A_copy, N, Q));
      }
    } else if (i == N - 1) {
      if (A[i - 1] < A[i]) {
        vector<int> A_copy(N);
        A_copy[i - 1] = A[i - 1] - 1;
        rep(j, N - 1) { A_copy[j] = A[j]; }
        sub_sub_ans = max(sub_ans, ans(vec, A_copy, N, Q));
      }
    } else {
      if (A[i - 1] < A[i] && A[i] <= A[i + 1]) {
        vector<int> A_copy(N);
        rep(j, N) {
          if (j == i)
            A_copy[j] = A[j] - 1;
          else
            A_copy[j] = A[j];
        }
        sub_sub_ans = max(sub_ans, ans(vec, A_copy, N, Q));
      }
    }
  }
  return max(sub_ans, sub_sub_ans);
}

int main() {
  int N, M, Q;
  cin >> N >> M >> Q;
  vector<vector<int>> vec(Q, vector<int>(4));
  rep(i, Q) {
    rep(j, 4) { cin >> vec[i][j]; }
  }
  vector<int> A(N);
  rep(i, N) { A[i] = M; }
  cout << ans(vec, A, N, Q) << endl;
}
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ios ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define debug freopen("in.txt", "r", stdin), freopen("out.txt", "w", stdout);
#define pb push_back
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int maxn = 1e6 + 10;
const int maxM = 1e6 + 10;
const int inf = 0x3f3f3f3f;

ll N, M, Q;
struct node {
  ll a, c, d;
};
struct node2 {
  ll a, b, c, d;
} qq[maxn];
vector<node> adj[maxn];
ll A[maxn];
ll res = 0;
void dfs(int id, int last = 1) {
  if (id > N) {
    ll sum = 0;
    for (int i = 1; i <= Q; i++) {
      if (A[qq[i].b] - A[qq[i].a] == qq[i].c)
        sum += qq[i].d;
    }
    res = max(res, sum);
    return;
  }
  A[id] = last;
  dfs(id + 1, last);
  for (auto no : adj[id]) {
    ll cur = A[no.a] + no.c;
    if (cur > M || cur < last)
      continue;
    else {
      A[id] = cur;
      dfs(id + 1, cur);
    }
  }
}

int main() {
  // debug;
  ios;

  cin >> N >> M >> Q;
  for (int i = 1; i <= Q; i++) {
    ll a, b, c, d;
    cin >> a >> b >> c >> d;
    adj[b].pb({a, c, d});
    qq[i] = {a, b, c, d};
  }
  dfs(1);
  cout << res << '\\n';
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;

int s[20];
int a[20], b[20], c[20], d[20];

int sum = -inf;
int n, m, q;

void check() {
  int now = 0;
  for (int i = 0; i < q; i++) {
    if (s[b[i]] - s[a[i]] == c[i]) {
      now += d[i];
    }
  }
  sum = max(sum, now);
  return;
}

void dfs(int now, int val) {
  s[now] = val;
  if (now == n) {
    check();
  }
  for (int i = val; i <= m; i++) {
    dfs(now + 1, i);
  }
  return;
}

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

  dfs(1, 1);

  cout << sum << 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 repit(it, li) for (auto it = li.begin(); it != li.end(); it++)
#define ll long long int

int n, m;
int g[10];

int inc(int i) {
  if (i < 0)
    return -1;
  if (g[i] == m) {
    g[i] = inc(i - 1);
  } else
    g[i]++;
  return g[i];
}

int main() {
  int q;
  int a[50], b[50], c[50], d[50];
  cin >> n >> m >> q;
  rep(i, q) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
    a[i]--;
    b[i]--;
  }

  rep(i, n) g[i] = 1;
  int mx = 0;
  while (1) {
    int s = 0;
    rep(i, q) if (g[b[i]] - g[a[i]] == c[i]) s += d[i];
    mx = max(mx, s);
    if (inc(n - 1) == -1)
      break;
  }
  cout << mx << endl;
  return 0;
}


Error: 0

Fixed submission:

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

#define rep(i, n) for (int i = 0; i < n; ++i)
#define repit(it, li) for (auto it = li.begin(); it != li.end(); it++)
#define ll long long int

int n = 10, m = 10;
int g[10];

int inc(int i) {
  if (i < 0)
    return -1;
  if (g[i] == m) {
    g[i] = inc(i - 1);
  } else
    g[i]++;
  return g[i];
}

int main() {
  int q;
  int a[10], b[10], c[10], d[10];
  cin >> n >> m >> q;
  rep(i, q) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
    a[i]--;
    b[i]--;
  }

  rep(i, n) g[i] = 1;
  int mx = 0;
  while (1) {
    int s = 0;
    rep(i, q) if (g[b[i]] - g[a[i]] == c[i]) s += d[i];
    mx = max(mx, s);
    if (inc(n - 1) == -1)
      break;
  }
  cout << mx << endl;
  return 0;
}
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

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

Error: 0

Fixed submission:

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

#include <algorithm>
#include <iostream>

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

int ans;

int n, m, q;

void dfs(int k) {

  if (k == n) {
    int sum = 0;
    for (int i = 0; i < q; ++i) {
      if (A[b[i]] - A[a[i]] == c[i])
        sum += d[i];
    }
    ans = std::max(ans, sum);
    return;
  }

  for (int i = A[k - 1]; i <= m; ++i) {
    A[k] = i;
    dfs(k + 1);
  }
}

int main() {

  A[0] = 1;

  std::cin >> n >> m >> q;

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

  dfs(1);

  std::cout << ans << std::endl;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>

int A[10], a[10], b[10], c[10], d[10];

int ans;

int n, m, q;

void dfs(int k) {

  if (k == n) {
    int sum = 0;
    for (int i = 0; i < q; ++i) {
      if (A[b[i]] - A[a[i]] == c[i])
        sum += d[i];
    }
    ans = std::max(ans, sum);
    return;
  }

  for (int i = A[k - 1]; i <= m; ++i) {
    A[k] = i;
    dfs(k + 1);
  }
}

int main() {

  A[0] = 1;

  std::cin >> n >> m >> q;

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

  dfs(1);

  std::cout << ans << std::endl;

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

#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define INF (1 << 30) - 1
#define LINF 1LL << 60
const int MOD = 1000000007;
using ll = long long;
using P = pair<int, int>;

struct Req {
  int a, b, c, d;
  Req(int a, int b, int c, int d) : a(a), b(b), c(c), d(d) {}
};

vector<Req> query;

int n, m, q;
vector<int> perm;
vector<int> perm2;

ll calc() {
  ll ret = 0;
  for (int i = 0; i < q; ++i) {
    int a = query[i].a;
    int b = query[i].b;
    int c = query[i].c;
    int d = query[i].d;
    if (perm2[b] - perm2[a] == c)
      ret += d;
  }

  return ret;
}

int main() {
  cin >> n >> m >> q;
  perm.resize(n + m);
  perm2.resize(n);
  for (int i = 0; i < n; ++i)
    perm[i] = 0;
  for (int i = n; i < n + m; ++i)
    perm[i] = 1;
  for (int i = 0; i < q; ++i) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    --a;
    --b;
    query.push_back(Req(a, b, c, d));
  }

  ll ans = 0;
  do {
    if (perm[0] == 0)
      continue;

    int zero_cnt = 0;
    int cnt = 0;
    for (int i = 0; i < n + m; ++i) {
      if (perm[i])
        ++cnt;
      else {
        perm2[zero_cnt] = cnt;
        ++zero_cnt;
      }
    }

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

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


Error: 0

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define INF (1 << 30) - 1
#define LINF 1LL << 60
const int MOD = 1000000007;
using ll = long long;
using P = pair<int, int>;

struct Req {
  int a, b, c, d;
  Req(int a, int b, int c, int d) : a(a), b(b), c(c), d(d) {}
};

vector<Req> query;

int n, m, q;
vector<int> perm;
vector<int> used;

ll ans = 0;
void calc() {
  ll tmp = 0;
  for (int i = 0; i < q; ++i) {
    int a = query[i].a;
    int b = query[i].b;
    int c = query[i].c;
    int d = query[i].d;
    if (perm[b] - perm[a] == c)
      tmp += d;
  }
  ans = max(ans, tmp);
}

void dfs(int pos) {
  if (pos == n) {
    calc();
    return;
  }

  for (int i = 0; i < m; ++i) {
    if (used[i])
      continue;
    fill(used.begin(), used.begin() + i, true);
    perm[pos] = i;
    dfs(pos + 1);
    fill(used.begin(), used.begin() + i, false);
  }
}

int main() {
  cin >> n >> m >> q;
  for (int i = 0; i < q; ++i) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    --a;
    --b;
    query.push_back(Req(a, b, c, d));
  }

  perm.resize(n);
  used.resize(n, 0);
  dfs(0);

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

#include <bits/stdc++.h>
using namespace std;
vector<int> a, b, c, d;
int n, m, q;
int ans;

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

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
vector<int> a, b, c, d;
int n, m, q;
int ans;

void dfs(vector<int> A) {
  if (A.size() == n + 1) {
    int sum = 0;
    for (int i = 0; i < q; i++) {
      if (A[b[i]] - A[a[i]] == c[i]) {
        sum += d[i];
      }
    }
    ans = max(ans, sum);
    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;
  return 0;
}
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

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

int n, m, q;
vec a, b, c, d;
int ans;

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

  if (e.empty()) {
    e = {1};
  } else {
    e.push_back(e.back());
  }
  while (e.back() <= m) {
    dfs(e);
    e.back()++;
  }
}

int main() {
  cin >> n >> m >> q;
  a = b = c = d = vec(q);

  rep(i, q) { cin >> a[i] >> b[i] >> c[i] >> d[i]; }

  dfs(vec(0));

  cout << ans << endl;
}


Error: -11

Fixed submission:

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

int n, m, q;
vec a, b, c, d;
int ans;

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

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

int main() {
  cin >> n >> m >> q;
  a = b = c = d = vec(q);

  rep(i, q) { cin >> a[i] >> b[i] >> c[i] >> d[i]; }

  dfs(vec(0, 0));

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

#include <bits/stdc++.h>
using namespace std;
vector<long long> factorial;
long long fact(long long n) {
  if (factorial[n] == -1)
    factorial[n] = n * fact(n - 1);
  return factorial[n];
}
long long binom(long long n, long long r) {
  return fact(n) / fact(r) / fact(n - r);
}
int main() {
  int n, m, q;
  cin >> n >> m >> q;
  vector<int> a(q), b(q), c(q), d(q);
  for (int i = 0; i < q; ++i)
    cin >> a[i] >> b[i] >> c[i] >> d[i];

  vector<int> B(n + m, 0);
  for (int i = 1; i <= n; ++i)
    B[i] = 1;

  factorial.assign(n + m, -1);
  factorial[0] = 1;
  int ans = 0;
  long long t = binom(n + m - 1, n);
  for (long long j = 0; j < t; ++j) {
    int cnt = 1;
    vector<int> A(n + 1, 0);
    for (int i = 0; i < n + m; ++i) {
      if (B[i] == 0) {
        ++A[cnt];
      } else {
        if (cnt == n)
          break;
        A[cnt + 1] = A[cnt];
        ++cnt;
      }
    }

    int tmp = 0;
    for (int i = 0; i < q; ++i) {
      if (A[b[i]] - A[a[i]] == c[i])
        tmp += d[i];
    }
    ans = max(ans, tmp);
    next_permutation(B.begin() + 1, B.end());
  }

  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
vector<long long> factorial;
long long fact(long long n) {
  if (factorial[n] == -1)
    factorial[n] = n * fact(n - 1);
  return factorial[n];
}
long long binom(long long n, long long r) {
  return fact(n) / fact(r) / fact(n - r);
}
int main() {
  int n, m, q;
  cin >> n >> m >> q;
  vector<int> a(q), b(q), c(q), d(q);
  for (int i = 0; i < q; ++i)
    cin >> a[i] >> b[i] >> c[i] >> d[i];

  vector<int> B(n + m, 0);
  for (int i = 1; i <= n; ++i)
    B[i] = 1;

  factorial.assign(n + m, -1);
  factorial[0] = 1;
  int ans = 0;
  long long t = binom(n + m - 1, n);
  for (long long j = 0; j < t; ++j) {
    int cnt = 1;
    vector<int> A(n + 1, 0);
    for (int i = 0; i < n + m; ++i) {
      if (B[i] == 0) {
        ++A[cnt];
      } else {
        A[cnt + 1] = A[cnt];
        ++cnt;
      }
    }

    int tmp = 0;
    for (int i = 0; i < q; ++i) {
      if (A[b[i]] - A[a[i]] == c[i])
        tmp += d[i];
    }
    ans = max(ans, tmp);
    next_permutation(B.begin() + 1, B.end());
  }

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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using vc = vector<char>;
using vvc = vector<vc>;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
const ll INF = 1LL << 60;
const ll MOD = 1e9 + 7;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define debug(x) cerr << #x << ": " << x << endl;
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

ll N, M, Q;
vll a, b, c, d;
ll ans = 0;

void dfs(vll A) {
  if (A.size() == N + 1) {
    ll now = 0;
    rep(i, Q) if (A[b[i]] - A[a[i]] == c[i]) now += d[i];
    chmax(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 = vll(Q);
  rep(i, Q) cin >> a[i] >> b[i] >> c[i] >> d[i];
  dfs(vll(1, 1));
  cout << ans << endl;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using vc = vector<char>;
using vvc = vector<vc>;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
const ll INF = 1LL << 60;
const ll MOD = 1e9 + 7;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define debug(x) cerr << #x << ": " << x << endl;
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

ll N, M, Q;
vll a(Q), b(Q), c(Q), d(Q);
ll ans = 0;

void dfs(vll A) {
  if (A.size() == N + 1) {
    ll now = 0;
    rep(i, Q) if (A[b[i]] - A[a[i]] == c[i]) now += d[i];
    chmax(ans, now);
    return;
  }

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

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

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

int ans = 0;
int N, M, Q;
vector<int> a, b, c, d;
void dfs(vector<int> vec) {
  int now = 0;
  if (vec.size() == N + 1) {
    rep(i, Q) {
      if (vec.at(b.at(i)) - vec.at(a.at(i)) == c.at(i))
        now += d.at(i);
    }
    ans = max(ans, now);
    return;
  }
  vec.push_back(vec.back());
  while (vec.back() <= M) {
    dfs(vec);
    vec.back()++;
  }
}

int main() {

  cin >> N >> M >> Q;
  a = b = c = d = vector<int>(Q);
  rep(i, Q) cin >> a.at(i) >> b.at(i) >> c.at(i) >> d.at(i);
  vector<int> vec(1, 1);
  dfs(vec);
  cout << ans << endl;
}


Error: TLE

Fixed submission:

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

int ans = 0;
int N, M, Q;
vector<int> a, b, c, d;
void dfs(vector<int> vec) {
  int now = 0;
  if (vec.size() == N + 1) {
    rep(i, Q) {
      if (vec.at(b.at(i)) - vec.at(a.at(i)) == c.at(i))
        now += d.at(i);
    }
    ans = max(ans, now);
  }
  vec.push_back(vec.back());
  while (vec.back() <= M) {
    dfs(vec);
    vec.back()++;
  }
}

int main() {

  cin >> N >> M >> Q;
  a = b = c = d = vector<int>(Q);
  rep(i, Q) cin >> a.at(i) >> b.at(i) >> c.at(i) >> d.at(i);
  vector<int> vec(1, 1);
  dfs(vec);
  cout << ans << endl;
}
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

void print(const std::vector<int> &v) {
  std::for_each(v.begin(), v.end(), [](int x) { std::cout << x << " "; });
  std::cout << std::endl;
}

int main(void) {
  int N, M, Q;
  cin >> N >> M >> Q;
  vector<int> a(Q);
  vector<int> b(Q);
  vector<int> c(Q);
  vector<int> d(Q);

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

  vector<int> v;
  if (M == 1) {
    int sum = 0;
    for (int i = 0; i < Q; i++) {
      if (0 == c[i]) {
        sum += d[i];
      }
    }
    cout << sum << endl;
    return 0;
  } else {
    for (int i = 0; i < M - 1; i++) {
      v.push_back(0);
    }
  }
  for (int i = 0; i < N; i++) {
    v.push_back(1);
  }

  vector<int> result;

  while (next_permutation(v.begin(), v.end())) {
    int count = 1;
    vector<int> A;
    for (int i = 0; i < (int)v.size(); i++) {
      if (v[i] == 1) {
        A.push_back(count);
      } else {
        count++;
      }
    }
    long long sum = 0;
    for (int i = 0; i < Q; i++) {
      if (A[b[i] - 1] - A[a[i] - 1] == c[i]) {
        sum += d[i];
      }
    }
    result.push_back(sum);
  }

  cout << *max_element(result.begin(), result.end()) << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

void print(const std::vector<int> &v) {
  std::for_each(v.begin(), v.end(), [](int x) { std::cout << x << " "; });
  std::cout << std::endl;
}

int main(void) {
  int N, M, Q;
  cin >> N >> M >> Q;
  vector<int> a(Q);
  vector<int> b(Q);
  vector<int> c(Q);
  vector<int> d(Q);

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

  vector<int> v;
  if (M == 1) {

  } else {
    for (int i = 0; i < M - 1; i++) {
      v.push_back(0);
    }
  }
  for (int i = 0; i < N; i++) {
    v.push_back(1);
  }
  vector<int> result;
  while (next_permutation(v.begin(), v.end())) {
    int count = 1;
    vector<int> A;
    for (int i = 0; i < (int)v.size(); i++) {
      if (v[i] == 1) {
        A.push_back(count);
      } else {
        count++;
      }
    }
    long long sum = 0;
    for (int i = 0; i < Q; i++) {
      if (A[b[i] - 1] - A[a[i] - 1] == c[i]) {
        sum += d[i];
      }
    }
    result.push_back(sum);
  }

  cout << *max_element(result.begin(), result.end()) << endl;

  return 0;
}