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

#include <iostream>
#include <stdio.h>
// #include <bits/stdc++.h>
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cmath>
#include <cstdint>
#include <cstring>
#include <float.h>
#include <iomanip>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

#define INF 1e9
#define rep(i, n) for (int i = 0; (i) < (int)(n); i++)
#define REP(i, a, b) for (int i = (int)(a); (i) <= (int)(b); i++)
#define VEC(type, c, n)                                                        \\
  std::vector<type> c(n);                                                      \\
  for (auto &i : c)                                                            \\
    std::cin >> i;
#define vec(type, n) vector<type>(n)
#define vvec(m, n) vector<vector<int>>(int(m), vector<int>(n))
#define ALL(a) (a).begin(), (a).end()

using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
using P = pair<ll, ll>;

int n, m, q;
vector<int> a(100), b(100), c(100), d(100), A(20);

int res = 0;
void dfs(int dep, int x) {

  if (dep == n) {
    int sum = 0;
    rep(i, q) {
      if (A[b[i] - 1] - A[a[i] - 1] == c[i])
        sum += d[i];
    }
    res = max(res, sum);
    return;
  }

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

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

  dfs(0, 1);
  cout << res << endl;
}


Error: 0

Fixed submission:

#include <iostream>
#include <stdio.h>
// #include <bits/stdc++.h>
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cmath>
#include <cstdint>
#include <cstring>
#include <float.h>
#include <iomanip>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

#define INF 1e9
#define rep(i, n) for (int i = 0; (i) < (int)(n); i++)
#define REP(i, a, b) for (int i = (int)(a); (i) <= (int)(b); i++)
#define VEC(type, c, n)                                                        \\
  std::vector<type> c(n);                                                      \\
  for (auto &i : c)                                                            \\
    std::cin >> i;
#define vec(type, n) vector<type>(n)
#define vvec(m, n) vector<vector<int>>(int(m), vector<int>(n))
#define ALL(a) (a).begin(), (a).end()

using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
using P = pair<ll, ll>;

int n, m, q;
vector<int> a(20), b(20), c(20), d(20), A(20);

int res = 0;
void dfs(int dep, int x) {

  if (dep == n) {
    int sum = 0;
    rep(i, q) {
      if (A[b[i] - 1] - A[a[i] - 1] == c[i])
        sum += d[i];
    }
    res = max(res, sum);
    return;
  }

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

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

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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n, m, q;

vector<int> a, b, c, d, A;
int saiki(int k, int l, int p, vector<int> A);

int main() {
  int as;
  // cerr << "test";
  cin >> n >> m >> q;
  for (int i = 0; i < q; i++) {
    int e, f, g, h;
    cin >> e >> f >> g >> h;
    a.push_back(e);
    b.push_back(f);
    c.push_back(g);
    d.push_back(h);
  }
  // cerr << "test";
  as = saiki(n, m, q, A);
  cout << as << endl;
}
int saiki(int k, int l, int p, vector<int> A) {
  int ans = 0;
  vector<int> B = A;
  // cerr << k << l   << p  <<endl;
  if (k != 0) {
    for (int i = 1; i <= l; i++) {
      B.push_back(i);
      ans = max(ans, saiki(k - 1, i, p, B));
      B.pop_back();
    }
    return ans;
  } else {
    for (int i = 0; i < p; i++) {
      // for(int j=0;j<A.size();j++)cerr << A[j];
      // cerr << endl;
      if (A.at(A.size() - b.at(i)) - A.at(A.size() - a.at(i)) == c.at(i))
        ans += d.at(i);
    }
    return ans;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n, m, q;

vector<int> a, b, c, d, A;
int saiki(int k, int l, int p, vector<int> A);

int main() {
  int as;
  // cerr << "test";
  cin >> n >> m >> q;
  for (int i = 0; i < q; i++) {
    int e, f, g, h;
    cin >> e >> f >> g >> h;
    a.push_back(e);
    b.push_back(f);
    c.push_back(g);
    d.push_back(h);
  }
  // cerr << "test";
  as = saiki(n, m, q, A);
  cout << as << endl;
}
int saiki(int k, int l, int p, vector<int> A) {
  int ans = 0;
  vector<int> B = A;
  // cerr << k << l   << p  <<endl;
  if (k != 0) {
    for (int i = 1; i <= l; i++) {
      B.push_back(i);
      ans = max(ans, saiki(k - 1, i, p, B));
      B.pop_back();
    }
    return ans;
  } else {
    for (int i = 0; i < p; i++) {
      for (int j = 0; j < A.size(); j++)
        cerr << A[j];
      cerr << endl;
      if (A.at(A.size() - b.at(i)) - A.at(A.size() - a.at(i)) == c.at(i))
        ans += d.at(i);
    }
    return ans;
  }
}
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <utility>
#include <vector>
#define REP(i, n) for (ll i = 0; i < n; i++)
#define REPR(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, m, n) for (ll i = m; i <= n; i++)
#define FORR(i, m, n) for (ll i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n)
#define MAX 100000
#define inf 1000000007
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<ll, ll>;

ll n, m, q;
ll ans = 0;
vll a, b, c, d;

void dfs(vll A) { // 再起全探索
  if (A.size() ==
      n + 1) { //(ai, bi, ci, di)の組で与えられるAの添え字は1オリジン
    ll 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高速化
  cin.tie(0);
  ios::sync_with_stdio(false);
  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)); // 使わない0番目を埋めて、+1番目は1で考えて十分
  cout << ans << "\\n";
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <utility>
#include <vector>
#define REP(i, n) for (ll i = 0; i < n; i++)
#define REPR(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, m, n) for (ll i = m; i <= n; i++)
#define FORR(i, m, n) for (ll i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n)
#define MAX 100000
#define inf 1000000007
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<ll, ll>;

ll n, m, q;
ll ans = 0;
vll a, b, c, d;

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];
    }
    ans = max(ans, now);
    return;
  }

  while (A.back() <= m) {
    dfs(A);
    A.back()++;
  }
  A.push_back(A.back()); // 一番後ろに最後尾と同じ値をつける
}

int main() {
  // cin高速化
  cin.tie(0);
  ios::sync_with_stdio(false);
  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 << "\\n";
  return 0;
}
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define mp make_pair
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define pb push_back
#define fi first
#define se second
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
using namespace std;
int n, m, q;
int val[55];
int a[55], b[55], c[55], d[55];
int res;
int all[55];
vector<pair<pii, int>> str[55];
int suf[55];
void dfs(int now, int mn, int ans) {
  if (suf[now] + ans < res)
    return;
  if (now == n + 1) {
    res = max(res, ans);
    return;
  }
  for (int i = mn; i <= m; i++) {
    val[now] = i;
    int nowans = ans;
    for (int j = 0; j < (int)str[now].size(); j++)
      if (val[now] - val[str[now][j].fi.fi] == str[now][j].fi.se)
        nowans += str[now][j].se;
    dfs(now + 1, i, nowans);
  }
}
int main() {
  scanf("%d %d %d", &n, &m, &q);
  for (int i = 1; i <= q; i++)
    scanf("%d %d %d %d", &a[i], &b[i], &c[i], &d[i]),
        str[b[i]].pb(mp(mp(a[i], c[i]), d[i])), all[b[i]] += d[i];
  for (int i = n; i >= 1; i--)
    suf[i] = suf[i + 1] + all[i];
  dfs(1, 1, 0);
  printf("%d\\n", res);
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define mp make_pair
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define pb push_back
#define fi first
#define se second
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
using namespace std;
int n, m, q;
int val[15];
int a[55], b[55], c[55], d[55];
int res;
int all[15];
vector<pair<pii, int>> str[15];
int suf[15];
void dfs(int now, int mn, int ans, int lft) {
  if (lft + ans < res)
    return;
  if (now == n + 1) {
    res = max(res, ans);
    return;
  }
  lft -= all[now];
  for (int i = mn; i <= m; i++) {
    val[now] = i;
    int nowans = ans;
    for (int j = 0; j < str[now].size(); j++)
      if (val[now] - val[str[now][j].fi.fi] == str[now][j].fi.se)
        nowans += str[now][j].se;
    dfs(now + 1, (i == mn) ? mn : mn + 1, nowans, lft);
  }
}
int main() {
  scanf("%d %d %d", &n, &m, &q);
  for (int i = 1; i <= q; i++)
    scanf("%d %d %d %d", &a[i], &b[i], &c[i], &d[i]),
        str[b[i]].pb(mp(mp(a[i], c[i]), d[i])), all[b[i]] += d[i];
  for (int i = q; i >= 1; i--)
    suf[i] = suf[i + 1] + all[i];
  dfs(1, 1, 0, suf[1]);
  cout << res << endl;
  return 0;
}
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
int N, M, Q;
vector<int> a, b, c, d;
long long ans;
long long res, res2;
vector<int> num;
long long dfs(int i, int u) {
  long long t2 = 0;
  if (u == N) {
    long long t = 0;
    for (int j = 0; j < Q; j++) {
      if (num[b[j] - 1] - num[a[j] - 1] == c[j])
        t += d[j];
    }
    return t;
  }
  num[u] = i;
  for (int j = 0; j + i <= M; j++) {
    t2 = max(t2, dfs(i + j, u + 1));
  }
  return t2;
}
int main() {
  cin >> N >> M >> Q;
  a.resize(Q);
  b.resize(Q);
  c.resize(Q);
  d.resize(Q);
  for (int i = 0; i < Q; i++) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  }
  ans = 0;
  num.resize(N);
  for (int i = 1; i <= M; i++) {
    ans = max(ans, dfs(i, 0));
  }
  cout << ans << endl;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
int N, M, Q;
vector<int> a, b, c, d;
long long ans;
long long res, res2;
vector<int> num;
long long dfs(int i, int u) {
  long long t2 = 0;
  if (u == N) {
    long long t = 0;
    for (int j = 0; j < Q; j++) {
      if (num[b[j] - 1] - num[a[j] - 1] == c[j])
        t += d[j];
    }
    return t;
  }
  num[u] = i;
  for (int j = 0; j + i <= M; j++) {
    t2 = max(t2, dfs(i + j, u + 1));
  }
  return t2;
}
int main() {
  cin >> N >> M >> Q;
  a.resize(Q);
  b.resize(Q);
  c.resize(Q);
  d.resize(Q);
  for (int i = 0; i < Q; i++) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  }
  ans = 0;
  for (int i = 1; i <= M; i++) {
    ans = max(ans, dfs(i, 0));
  }
  cout << ans << endl;
}
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#define ll long long
using namespace std;

int main() {
  int N, M, Q;
  int ans = 0;
  cin >> N >> M >> Q;
  vector<int> A(N + 5);
  vector<int> v(4 + 5, 0);
  vector<vector<int>> S(Q + 5, v);
  for (int i = 0; i < Q; i++) {
    cin >> S[i][0] >> S[i][1] >> S[i][2] >> S[i][3];
  }
  for (A[0] = 1; A[0] <= M; A[0]++) {
    for (A[1] = A[0]; A[1] <= M; A[1]++) {
      for (A[2] = A[1]; A[2] <= M; A[2]++) {
        for (A[3] = A[2]; A[3] <= M; A[3]++) {
          for (A[4] = A[3]; A[4] <= M; A[4]++) {
            for (A[5] = A[4]; A[5] <= M; A[5]++) {
              for (A[6] = A[5]; A[6] <= M; A[6]++) {
                for (A[7] = A[6]; A[7] <= M; A[7]++) {
                  for (A[8] = A[7]; A[8] <= M; A[8]++) {
                    for (A[9] = A[8]; A[9] <= M; A[9]++) {
                      int tmp = 0;
                      for (int i = 0; i < Q; i++) {
                        int e = S[i][0];
                        int f = S[i][1];
                        int g = S[i][2];
                        int h = S[i][3];
                        if (A[f - 1] - A[e - 1] == g) {
                          tmp += h;
                        }
                      }
                      ans = max(ans, tmp);
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
  cout << ans << endl;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#define ll long long
using namespace std;

int main() {
  int N, M, Q;
  int ans = 0;
  cin >> N >> M >> Q;
  vector<int> A(N + 2);
  vector<int> v(4 + 2, 0);
  vector<vector<int>> S(Q + 2, v);
  for (int i = 0; i < Q; i++) {
    cin >> S[i][0] >> S[i][1] >> S[i][2] >> S[i][3];
  }
  for (A[0] = 1; A[0] <= M; A[0]++) {
    for (A[1] = A[0]; A[1] <= M; A[1]++) {
      for (A[2] = A[1]; A[2] <= M; A[2]++) {
        for (A[3] = A[2]; A[3] <= M; A[3]++) {
          for (A[4] = A[3]; A[4] <= M; A[4]++) {
            for (A[5] = A[4]; A[5] <= M; A[5]++) {
              for (A[6] = A[5]; A[6] <= M; A[6]++) {
                for (A[7] = A[6]; A[7] <= M; A[7]++) {
                  for (A[8] = A[7]; A[8] <= M; A[8]++) {
                    for (A[9] = A[8]; A[9] <= M; A[9]++) {
                      int tmp = 0;
                      for (int i = 0; i < Q; i++) {
                        int e = S[i][0];
                        int f = S[i][1];
                        int g = S[i][2];
                        int h = S[i][3];
                        if (A[f - 1] - A[e - 1] == g) {
                          tmp += h;
                        }
                      }
                      ans = max(ans, tmp);
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
  cout << ans << endl;
}
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define MOD (1000000007)

using namespace std;

typedef long long int Int;

constexpr Int TEN(int n) { return n == 0 ? 1 : 10 * TEN(n - 1); }

const int max_n = 100;
int A[max_n];
int B[max_n];
int C[max_n];
int D[max_n];
int N, M, Q;
int G[max_n];

Int rec(int cp, int num) {
  Int ans = 0;
  if (cp == N) {
    for (int i = 0; i < Q; i++) {
      if (G[B[i]] - G[A[i]] == C[i])
        ans += D[i];
    }
  } else {
    for (int j = num; j <= M; j++) {
      G[cp] = j;
      ans = max(ans, rec(cp + 1, j));
    }
  }

  return ans;
}

Int solve(void) {
  Int ans = 0;
  for (int i = 1; i <= M; i++) {
    G[0] = i;
    ans = max(ans, rec(1, i));
  }

  return ans;
}

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

  for (int i = 0; i < Q; i++) {
    cin >> A[i] >> B[i] >> C[i] >> D[i];
    A[i]--;
    B[i]--;
  }

  cout << solve() << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define MOD (1000000007)

using namespace std;

typedef long long int Int;

constexpr Int TEN(int n) { return n == 0 ? 1 : 10 * TEN(n - 1); }

const int max_n = 13;
int A[max_n];
int B[max_n];
int C[max_n];
int D[max_n];
int N, M, Q;
int G[max_n];

Int rec(int cp, int num) {
  Int ans = 0;
  if (cp == N) {
    for (int i = 0; i < Q; i++) {
      if (G[B[i]] - G[A[i]] == C[i])
        ans += D[i];
    }
  } else {
    for (int j = num; j <= M; j++) {
      G[cp] = j;
      ans = max(ans, rec(cp + 1, j));
    }
  }

  return ans;
}

Int solve(void) {
  Int ans = 0;
  for (int i = 1; i <= M; i++) {
    G[0] = i;
    ans = max(ans, rec(1, i));
  }

  return ans;
}

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

  for (int i = 0; i < Q; i++) {
    cin >> A[i] >> B[i] >> C[i] >> D[i];
    A[i]--;
    B[i]--;
  }

  cout << solve() << 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)++)
#define rep2(i, a, n) for (int i = a; i < n; i++)
#define been(ix) (ix).begin(), (ix).end()
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<long long> vl;
const ll INFL = 1e18;
const int INF = 1001001001;
typedef pair<int, int> P;
#define foreach(ix, i) for (auto &(ix) : (i))
typedef long double ld;
const int mod = 1000000007;
vector<vi> res;
int n, m, q;
int cnt = 0;
void dfs(vi a, int y) {
  if (a.size() == n) {
    res.push_back(a);
    cnt++;
    return;
  }
  if (y > m)
    return;
  rep2(i, y, m + 1) {
    a.push_back(i);
    dfs(a, i);
    a.pop_back();
  }
}
int main() {
  cin >> n >> m >> q;
  ll a[q], b[q], c[q], d[q];
  rep(i, q) { cin >> a[i] >> b[i] >> c[i] >> d[i]; }
  ll ans = 0;
  rep(i, m) {
    string s = to_string(i + 1);
    vi x;
    x.push_back(i + 1);
    dfs(x, i + 1);
  }
  // cout<<cnt<<endl;
  rep(i, res.size()) {
    // int v = res[i];
    vi s = res[i];
    ll sum = 0;

    rep(j, q) {
      if (s[b[j] - 1] - s[a[j] - 1] == c[j])
        sum += d[j];
    }
    ans = max(ans, sum);
  }

  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rep2(i, a, n) for (int i = a; i < n; i++)
#define been(ix) (ix).begin(), (ix).end()
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<long long> vl;
const ll INFL = 1e18;
const int INF = 1001001001;
typedef pair<int, int> P;
#define foreach(ix, i) for (auto &(ix) : (i))
typedef long double ld;
const int mod = 1000000007;
vector<ll> res;
int n, m, q;
int cnt = 0;
void dfs(int a, int y) {
  string s = to_string(a);
  if (s.length() == n) {
    res.push_back(a);
    cnt++;
    return;
  }
  if (y > m)
    return;

  rep2(i, y, m + 1) { dfs(a * 10 + i, i); }
}
int main() {
  cin >> n >> m >> q;
  ll a[q], b[q], c[q], d[q];
  rep(i, q) { cin >> a[i] >> b[i] >> c[i] >> d[i]; }
  ll ans = 0;
  rep(i, m) {
    string s = to_string(i + 1);
    dfs(i + 1, i + 1);
  }
  // cout<<cnt<<endl;
  rep(i, res.size()) {
    int v = res[i];
    string s = to_string(v);
    ll sum = 0;

    rep(j, q) {
      if (s[b[j] - 1] - s[a[j] - 1] == c[j])
        sum += d[j];
    }
    ans = max(ans, sum);
  }

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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
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;
    rep(i, q) {
      if (A[b[i]] - A[a[i]] == c[i]) {
        now += d[i];
      }
    }
    ans = max(ans, now);
    return;
  }
  int prev = A.back();
  rep2(v, prev, m + 1) {
    A.push_back(v);
    dfs(A);
    A.pop_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); }
  dfs(vector<int>(1, 1));
  cout << ans << endl;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
using namespace std;
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.at(i) >> b.at(i) >> c.at(i) >> d.at(i);
    a.at(i)--;
    b.at(i)--;
  }
  int ans = 0;
  for (int tmp = 0; tmp < (1 << m); tmp++) {
    bitset<10> s(tmp);
    vector<bool> e(m, false);
    rep(i, m) {
      if (s.test(i)) {
        e.at(i) = true;
      }
    }

    vector<int> f(m, 0);
    int p = 0;
    rep(i, m) {
      if (e.at(i)) {

        f.at(i) = p;
        p++;
      } else {
        f.at(i) = 0;
      }
    }

    int co = 0;
    rep(i, q) {

      if (e.at(a.at(i))) {
        if (e.at(b.at(i))) {

          if (f.at(b.at(i)) - f.at(a.at(i)) == c.at(i)) {
            co = co + d.at(i);
          }
        }
      }
    }

    ans = max(ans, co);
  }

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

#include <algorithm>
#include <bits/stdc++.h>
#include <cstring>
#include <iostream>
#include <math.h>
#include <vector>

using namespace std;
typedef long long ll;

void dfs(int i, const int size, const int end, const int start, vector<int> &A,
         vector<int> a, vector<int> b, vector<int> c, vector<int> d, int &max) {
  if (i == size) {
    int sum = 0;

    for (int k = 1; k < a.size(); k++) {
      if (A[b[k] - 1] - A[a[k] - 1] == c[k]) {
        sum += d[k];
      }
    }

    if (max < sum) {
      max = sum;
    }
  } else {
    for (int j = start; j <= end; j++) {
      A[i] = j;
      dfs(i + 1, size, end, j, A, a, b, c, d, max);
    }
  }
}

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

  cin >> N >> M >> Q;

  vector<int> a(Q + 1), b(Q + 1), c(Q + 1), d(Q + 1);

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

  vector<int> A(N);

  int start = 1;
  int end = M;
  int size = N;
  int max = -1;

  dfs(0, size, end, start, A, a, b, c, d, max);

  cout << max << endl;

  return 0;
}

Error: TLE

Fixed submission:

#pragma GCC optimize("Ofast")
#include <algorithm>
#include <bits/stdc++.h>
#include <cstring>
#include <iostream>
#include <math.h>
#include <vector>
using namespace std;
typedef long long ll;

void dfs(int i, const int size, const int end, const int start, vector<int> &A,
         vector<int> a, vector<int> b, vector<int> c, vector<int> d, int &max) {
  if (i == size) {
    int sum = 0;

    for (int k = 1; k < a.size(); i++) {
      if (A[b[i] - 1] - A[a[i] - 1] == c[i]) {
        sum += d[i];
      }
    }

    if (max < sum) {
      max = sum;
    }
  } else {
    for (int j = start; j <= end; j++) {
      A[i] = j;
      dfs(i + 1, size, end, j, A, a, b, c, d, max);
    }
  }
}

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

  cin >> N >> M >> Q;

  vector<int> a(Q + 1), b(Q + 1), c(Q + 1), d(Q + 1);

  vector<int> A(N);

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

  int start = 1;
  int end = M;
  int size = N;
  int max = -1;

  dfs(0, size, end, start, A, a, b, c, d, max);

  cout << max << endl;

  return 0;
}