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

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

int main() {
  int n, m, q;
  cin >> n >> m >> q;
  int ans = 0;
  vector<int> s(q), b(q), c(q), d(q);
  rp(i, q) { cin >> s.at(i) >> b.at(i) >> c.at(i) >> d.at(i); }
  vector<int> a(10, 0);
  for (a.at(0) = 0; a.at(0) < m; a.at(0)++) {
    for (a.at(1) = a.at(0); a.at(1) < m; a.at(1)++) {
      for (a.at(2) = a.at(1); a.at(2) < m; a.at(2)++) {
        for (a.at(3) = a.at(2); a.at(3) < m; a.at(3)++) {
          for (a.at(4) = a.at(3); a.at(4) < m; a.at(4)++) {
            for (a.at(5) = a.at(4); a.at(5) < m; a.at(5)++) {
              for (a.at(6) = a.at(5); a.at(6) < m; a.at(6)++) {
                for (a.at(7) = a.at(6); a.at(7) < m; a.at(7)++) {
                  for (a.at(8) = a.at(7); a.at(8) < m; a.at(8)++) {
                    for (a.at(9) = a.at(8); a.at(9) < m; a.at(9)++) {
                      int sum = 0;
                      rp(i, q) {
                        if (a.at(b[i] - 1) - a.at(s[i] - 1) == c[i])
                          sum += d[i];
                      }
                      ans = max(ans, sum);
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

vector<G> points(11, G(11, vector<int>(11, 0)));
int main() {
  int n, m, q;
  cin >> n >> m >> q;
  int ans = 0;
  vector<int> s(q), b(q), c(q), d(q);
  rp(i, q) { cin >> s.at(i) >> b.at(i) >> c.at(i) >> d.at(i); }
  vector<int> a(10);
  for (a[0] = 0; 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[7] < m; a[8]++) {
                    for (a[9] = a[8]; a[8] < m; a[9]++) {
                      int sum = 0;
                      rp(i, q) {
                        if (a.at(b[i]) - a.at(s[i]) == c[i])
                          sum += d[i];
                      }
                      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 fast_io                                                                \\
  {                                                                            \\
    cin.sync_with_stdio(0);                                                    \\
    cout.sync_with_stdio(0);                                                   \\
  }
#define out(x, y)                                                              \\
  { cout << x << y; }
using ll = int;
using pa = pair<ll, ll>;
using vll = deque<ll>;
using vvll = vector<vll>;

ll a[50], b[50], c[50], d[50];
ll ans = 0;
ll n, m, q;

vvll rest;

void give(ll start, ll len) {
  vvll final;
  if (len == 0)
    return;
  for (ll j = start; j <= m; j++) {
    give(j, len - 1);
    if (rest.empty() && len == 1) {
      vll v;
      v.push_back(j);
      final.push_back(v);
      continue;
    }
    for (ll i = 0; i < rest.size(); i++) {
      rest[i].push_front(j);
      final.push_back(rest[i]);
    }
    rest.clear();
  }
  for (ll i = 0; i < final.size(); i++) {
    rest.push_back(final[i]);
  }
}

int main() {
  fast_io;
  cin >> n >> m >> q;
  for (ll i = 0; i < q; i++) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
    a[i]--;
    b[i]--;
  }
  give(1, n);
  for (ll i = 0; i < rest.size(); i++) {
    vll v = rest[i];
    ll sum = 0;
    for (ll j = 0; j < q; j++) {
      if (v[b[j]] - v[a[j]] == c[j])
        sum += d[j];
    }
    ans = max(ans, sum);
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define fast_io                                                                \\
  {                                                                            \\
    cin.sync_with_stdio(0);                                                    \\
    cout.sync_with_stdio(0);                                                   \\
  }
#define out(x, y)                                                              \\
  { cout << x << y; }
using ll = int;
using pa = pair<ll, ll>;
using vll = deque<ll>;
using vvll = vector<vll>;

ll a[10], b[10], c[10], d[10];
ll ans = 0;
ll n, m, q;

vvll rest;

void give(ll start, ll len) {
  vvll final;
  if (start > m || len == 0)
    return;
  for (ll j = start; j <= m; j++) {
    give(j, len - 1);
    if (rest.empty() && len == 1) {
      vll v;
      v.push_back(j);
      final.push_back(v);
    }
    for (ll i = 0; i < rest.size(); i++) {
      rest[i].push_front(j);
      final.push_back(rest[i]);
    }
    rest.clear();
  }
  for (ll i = 0; i < final.size(); i++) {
    rest.push_back(final[i]);
  }
}

int main() {
  fast_io;
  cin >> n >> m >> q;
  for (ll i = 0; i < q; i++) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
    a[i]--;
    b[i]--;
  }
  give(1, n);
  for (ll i = 0; i < rest.size(); i++) {
    vll v = rest[i];
    ll sum = 0;
    for (ll j = 0; j < q; j++) {
      if (v[b[j]] - v[a[j]] == c[j])
        sum += d[j];
    }
    ans = max(ans, sum);
  }
  cout << ans << endl;
}
Problem p02695 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define SIZE_OF_ARRAY(array) sizeof(array) / sizeof(array[0])

struct ten {
  int list[10];
};

int main() {
  int N, M, Q;
  cin >> N >> M >> Q;
  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<ten> v;
  for (int aa = 1; aa <= M; aa++) {
    for (int bb = aa; bb <= M; bb++) {
      for (int cc = bb; cc <= M; cc++) {
        for (int dd = cc; dd <= M; dd++) {
          for (int ee = dd; ee <= M; ee++) {
            for (int ff = ee; ff <= M; ff++) {
              for (int gg = ff; gg <= M; gg++) {
                for (int hh = gg; hh <= M; hh++) {
                  for (int ii = hh; ii <= M; ii++) {
                    for (int jj = ii; jj <= M; jj++) {
                      v.push_back({aa, bb, cc, dd, ee, ff, gg, hh, ii, jj});
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
  int maxscore = 0;
  for (int vl = 0; vl < v.size(); vl++) {
    int score = 0;
    // for(int i = 0; i < 10; i++){
    //     cout << v.at(vl).list[i] << " ";
    // }
    // cout << endl;
    for (int q = 0; q < Q; q++) {
      if (v.at(vl).list[b[q] - 1] - v.at(vl).list[a[q] - 1] == c[q]) {
        score += d[q];
      }
    }
    maxscore = std::max(score, maxscore);
  }
  cout << maxscore;
}

Error: TLE

Fixed submission:

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

#define SIZE_OF_ARRAY(array) sizeof(array) / sizeof(array[0])

int main() {
  int N, M, Q;
  cin >> N >> M >> Q;
  int A[Q];
  int B[Q];
  int C[Q];
  int D[Q];
  for (int i = 0; i < Q; i++) {
    cin >> A[i] >> B[i] >> C[i] >> D[i];
  }
  int score = 0;
  int l[10];
  for (int a = 0; a < 10; a++) {
    l[0] = a;
    for (int b = a; b < 10; b++) {
      l[1] = b;
      for (int c = b; c < 10; c++) {
        l[2] = c;
        for (int d = c; c < 10; d++) {
          l[3] = d;
          for (int e = d; e < 10; e++) {
            l[4] = e;
            for (int f = e; f < 10; f++) {
              l[5] = f;
              for (int g = f; g < 10; g++) {
                l[6] = g;
                for (int h = g; h < 10; h++) {
                  l[7] = h;
                  for (int i = h; i < 10; i++) {
                    l[8] = i;
                    for (int j = i; j < 10; j++) {
                      l[9] = j;
                      int qscore = 0;
                      for (int q = 0; q < Q; q++) {
                        int aaa = A[q];
                        int bbb = B[q];
                        int ccc = C[q];
                        int ddd = D[q];
                        if (l[bbb] - l[aaa] == ccc) {
                          score += ddd;
                        }
                      }
                      if (qscore > score) {
                        score = qscore;
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
  cout << score;
}
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>

using namespace std;

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

int n, m, q;
vector<dat> input(50);
vector<int> cur_v(10);

int f() {
  int out = 0;
  for (int i = 0; i != q; i++) {
    if (cur_v.at(input.at(i).b - 1) - cur_v.at(input.at(i).a - 1) ==
        input.at(i).c)
      out += input.at(i).d;
  }
  return out;
}

int dfs(int max_num, int next_ind) {
  if (n == next_ind)
    return f();
  else {
    max_num++;
    if (max_num <= m) {
      int out = dfs(max_num, next_ind);
      for (int i = next_ind; i != n; i++) {
        cur_v.at(i) = max_num;
        out = max(out, dfs(max_num, i + 1));
      }
      return out;
    } else {
      return 0;
    }
  }
}

int main() {
  cin >> n >> m >> q;
  input.resize(q);
  cur_v.resize(n);
  for (auto &in : input)
    cin >> in.a >> in.b >> in.c >> in.d;

  int out = 0;
  out = dfs(0, 0);
  cout << out << endl;
  return 0;
}

Error: WA

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

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

int n, m, q;
vector<dat> input(50);
vector<int> cur_v(10);

int f() {
  int out = 0;
  for (int i = 0; i != q; i++) {
    if (cur_v.at(input.at(i).b - 1) - cur_v.at(input.at(i).a - 1) ==
        input.at(i).c)
      out += input.at(i).d;
  }
  for (const auto ci : cur_v)
    cout << ci;
}

int dfs(int max_num, int next_ind) {
  if (n == next_ind)
    return f();
  else {
    max_num++;
    if (max_num <= m) {
      int out = dfs(max_num, next_ind);
      for (int i = next_ind; i != n; i++) {
        cur_v.at(i) = max_num;
        out = max(out, dfs(max_num, i + 1));
      }
      return out;
    } else {
      return 0;
    }
  }
}

int main() {
  cin >> n >> m >> q;
  input.resize(q);
  cur_v.resize(n);
  for (auto &in : input)
    cin >> in.a >> in.b >> in.c >> in.d;

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

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define ull unsigned long long

using namespace std;

set<vector<int>> seqs;

int n, m, q;

void createSeq(vector<int> seq, int pos) {
  if (pos >= n - 1) {
    seqs.insert(seq);
    return;
  }

  for (int i = seq[pos]; i <= m; i++) {
    seq[pos + 1] = i;
    createSeq(seq, pos + 1);
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  cin >> n >> m >> q;

  vector<vector<int>> nums(q);

  for (int i = 0; i < q; i++) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;

    nums[i].pb(a);
    nums[i].pb(b);
    nums[i].pb(c);
    nums[i].pb(d);
  }

  vector<int> a(n, 1);

  createSeq(a, 0);

  int res = 0;

  for (auto seq : seqs) {
    int ans = 0;
    for (auto y : nums) {
      if (seq[y[1] - 1] - seq[y[0] - 1] == y[2]) {
        ans += y[3];
      }
    }
    res = max(ans, res);
  }

  cout << res << '\\n';

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define ull unsigned long long

using namespace std;

set<vector<int>> seqs;
int n, m, q;
void createSeq(vector<int> seq, int pos) {
  if (pos >= n) {
    seqs.insert(seq);
    return;
  }

  for (int i = seq[pos]; i <= m; i++) {
    seq[pos + 1] = i;
    createSeq(seq, pos + 1);
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  cin >> n >> m >> q;

  vector<vector<int>> nums(q);

  for (int i = 0; i < q; i++) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;

    nums[i].pb(a);
    nums[i].pb(b);
    nums[i].pb(c);
    nums[i].pb(d);
  }

  vector<int> a(n, 1);

  createSeq(a, 0);

  int res = 0;

  for (auto seq : seqs) {
    int ans = 0;
    for (auto y : nums) {
      if (seq[y[1] - 1] - seq[y[0] - 1] == y[2]) {
        ans += y[3];
      }
    }
    res = max(ans, res);
  }

  cout << res << '\\n';

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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
int A[15], n, m, q, a[55], b[55], c[55], d[55], ans;
int main() {
  cin >> n >> m >> q;
  for (int i = 1; i <= q; i++) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  }
  for (int i = 1; i <= n; i++)
    A[i] = 1;   // 1  1 1
  while (!A[0]) // A[0]=0是标志位   不等于0时说明序列生成结束
  {
    int res = 0;
    for (int i = 1; i <= q; i++) {
      if ((A[b[i]] - A[a[i]]) == c[i])
        res += d[i];
    }
    ans = max(res, ans);
    for (int i = n; i >= 0; i--) {
      if (A[i] < m) {
        A[i]++;
        for (int j = i + 1; j <= n; j++)
          A[j] = A[i];
        break;
      }
    }
  }
  cout << ans;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
int A[15], n, m, q, a[55], b[55], c[55], d[55], ans;
int main() {
  cin >> n >> m >> q;
  for (int i = 1; i <= q; i++) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  }
  for (int i = 1; i <= n; i++)
    A[i] = 1;   // 1  1 1
  while (!a[0]) // a[0]=0是标志位   不等于0时说明序列生成结束
  {
    int res = 0;
    for (int i = 1; i <= q; i++) {
      if ((A[b[i]] - A[a[i]]) == c[i])
        res += d[i];
    }
    ans = max(res, ans);
    for (int i = n; i >= 0; i--) {
      if (A[i] < m) {
        A[i]++;
        for (int j = i + 1; j <= n; j++)
          A[j] = A[i];
        break;
      }
    }
  }
  cout << ans;

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

#include <bits/stdc++.h>
using namespace std;
long long a[55], b[55], c[55], d[55], n, m, q, ans1, ans, dl[15];
int dg(int tms, int lst) {
  //	cout<<tms<<endl;
  ans1 = 0;
  if (tms == n) {
    //	for(int i=0;i<n;i++)
    // cout<<dl[i]<<" ";
    // cout<<endl;
    for (int i = 0; i < q; i++) {
      if (dl[b[i] - 1] - dl[a[i] - 1] == c[i])
        ans1 += d[i];
    }
    ans = max(ans, ans1);
    return 0;
  }
  for (int i = lst; i <= m; i++) {
    dl[tms] = i;
    dg(tms + 1, i);
    dl[tms] = 0;
  }
  return 0;
}
int main() {
  cin >> n >> m >> q;
  for (int i = 0; i < q; i++)
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  dg(0, 1);
  cout << ans << endl;
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
long long a[55], b[55], c[55], d[55], n, m, q, ans1, ans, dl[15];
int dg(int tms, int lst) {
  ans1 = 0;
  if (tms == n) {
    for (int i = 0; i < q; i++) {
      if (dl[b[i]] - dl[a[i]] == dl[c[i]])
        ans1 += dl[d[i]];
    }
    ans = max(ans, ans1);
    return 0;
  }
  for (int i = lst; i <= m; i++) {
    dl[tms] = i;
    dg(tms + 1, i);
    dl[tms] = 0;
  }
  return 0;
}
int main() {
  cin >> n >> m >> q;
  for (int i = 0; i < q; i++)
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  dg(0, 1);
  cout << ans << endl;
  return 0;
}
Problem p02695 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define MODNum 1000000007
#define REP(type, i, a, b) for (type i = a; i < b; i++)
typedef long long ll;
using namespace std;
ll N, M, Q, ans;
vector<ll> NumArray(10);
vector<ll> A(50), B(50), C(50), d(50);

void dfs(int start, int n) {
  if (n == N) {
    ll Score = 0;
    REP(ll, k, 0, Q) {
      if (NumArray.at(B.at(k) - 1) - NumArray.at(A.at(k) - 1) == C.at(k)) {
        Score += d.at(k);
      }
    }
    ans = max(ans, Score);
    return;
  }
  for (int i = start; i <= M; i++) {
    NumArray.at(n) = i;
    dfs(i, n + 1);
  }
}

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

  REP(ll, i, 0, Q) { cin >> A.at(i) >> B.at(i) >> C.at(i) >> d.at(i); }
  ans = 0;
  dfs(1, 0);
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define MODNum 1000000007
#define REP(type, i, a, b) for (type i = a; i < b; i++)
typedef long long ll;

using namespace std;

int main(void) {
  ll N, M, Q, ans;
  cin >> N >> M >> Q;
  vector<ll> A(Q), B(Q), C(Q), d(Q);
  vector<ll> NumArray(N);
  REP(ll, i, 0, Q) { cin >> A.at(i) >> B.at(i) >> C.at(i) >> d.at(i); }
  ans = 0;
  for (ll i = 0; i < pow(M, N); i++) {
    ll Number = i;
    ll j = 0;
    while (j < N) {
      NumArray.at(j) = Number % M + 1;
      Number /= M;
      j++;
    }
    sort(NumArray.begin(), NumArray.end());
    ll Score = 0;
    REP(ll, k, 0, Q) {
      if (NumArray.at(B.at(k) - 1) - NumArray.at(A.at(k) - 1) == C.at(k)) {
        Score += d.at(k);
      }
    }
    ans = max(ans, Score);
  }
  cout << ans << endl;
  return 0;
}
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
using ll = long long;
queue<ll> que;
int n, m, q;
vector<int> a(100), b(100), c(100), d(100);
// vector<int> e(10, 1);
ll ans = 0;
void dfs(int k, int mini, vector<int> e) {
  if (k == n + 2) {
    ll sum = 0;
    for (int i = 0; i < q; i++) {
      if (e[b[i]] - e[a[i]] == c[i]) {
        sum += d[i];
      }
    }
    ans = max(ans, sum);
    return;
  }

  e.push_back(mini);
  for (int i = mini; i <= m; i++) {
    dfs(k + 1, i, e);
  }
}

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, vector<int>(0));

  // cout << que.front() << endl;

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

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
using ll = long long;
int main() {
  int n, m, q;
  cin >> n >> m >> q;
  m--;
  vector<int> a(100), b(100), c(100), d(100);
  for (int i = 0; i < q; i++) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
    a[i]--;
    b[i]--;
  }
  queue<ll> que;
  for (int i = 0; i < m; i++) {
    que.push(i + 1);
  }
  ll limit = 0;
  for (int i = 0; i < n; i++) {
    limit += m * pow(10, i);
  }
  for (int i = 0; i <= 99999; i++) {
    ll tmp = que.front();
    que.pop();
    ll in = tmp;
    for (int j = in % 10; j <= m + 1; j++) {
      que.push(in * 10 + j);
    }
  }
  // cout << que.front() << endl;

  ll ans = 0;
  while (!que.empty()) {
    ll sum = 0;
    ll tmp = que.front();
    que.pop();
    vector<ll> e(100, 0);
    bool ok = true;
    for (int i = n - 1; i >= 0; i--) {
      if (tmp % 10 > m + 1) {
        ok = false;
        break;
      }
      e[i] = tmp % 10;
      tmp /= 10;
    }
    if (ok == false) {
      continue;
    }
    for (int i = 0; i < q; i++) {
      if (e[b[i]] - e[a[i]] == c[i]) {
        sum += d[i];
      }
    }
    ans = max(ans, sum);
  }

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

#include <bits/stdc++.h>

using namespace std;

int a[100], b[100], c[100], d[100];
long long ans = 0;
int l[20];
int n, m, q;
void dfs(int x, int y) // x当前第几位,这一位是几
{
  l[x] = y;
  if (x == n) {
    long long sum = 0;
    for (int i = 1; i <= q; i++)
      if (l[b[i]] - l[a[i]] == c[i])
        sum += d[i];
    ans = max(ans, sum);
    return;
  } else {
    for (int i = y; i <= m; i++) {
      dfs(x + 1, i);
    }
  }
}
int main() {
  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;

int a[20], b[20], c[20], d[20];
long long ans = 0;
int l[20];
int n, m, q;
void dfs(int x, int y) // x当前第几位,这一位是几
{
  l[x] = y;
  if (x == n) {
    long long sum = 0;
    for (int i = 1; i <= q; i++)
      if (l[b[i]] - l[a[i]] == c[i])
        sum += d[i];
    ans = max(ans, sum);
    return;
  } else {
    for (int i = y; i <= m; i++) {
      dfs(x + 1, i);
    }
  }
}
int main() {
  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: Time Limit Exceeded

#include <bits/stdc++.h>
#define ll long long
using namespace std;
int A[15], n, m, q, a[55], b[55], c[55], d[55], res, ans;
int main() {
  cin >> n >> m >> q;
  for (int i = 1; i <= q; i++) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  }
  for (int i = 1; i <= n; i++)
    A[i] = 1;
  while (!A[0]) {
    int res = 0;
    for (int i = 1; i <= q; i++) {
      if (A[b[i]] - A[a[i]] == c[i])
        res += d[i];
    }
    ans = max(res, ans);
    for (int i = n; i >= 0; i--) {
      if (A[i] < m) {
        A[i]++;
        for (int j = i + 1; j <= n; j++)
          A[j] = A[i];
        break;
      }
    }
  }
  cout << ans;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
using namespace std;
int A[15], n, m, q, a[55], b[55], c[55], d[55], res, ans;
int main() {
  cin >> n >> m >> q;
  for (int i = 1; i <= q; i++) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  }
  for (int i = 1; i <= n; i++)
    A[i] = 1;
  while (!a[0]) {
    int res = 0;
    for (int i = 1; i <= q; i++) {
      if (A[b[i]] - A[a[i]] == c[i])
        res += d[i];
    }
    ans = max(res, ans);
    for (int i = n; i >= 0; i--) {
      if (A[i] < m) {
        A[i]++;
        for (int j = i + 1; j <= n; j++)
          A[j] = A[i];
        break;
      }
    }
  }
  cout << ans;
  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 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;
  }

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

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

  dfs(vector<int>(1, 1));

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


Error: 0

Fixed submission:

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

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

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

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

  dfs(vector<int>(1, 1));

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