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

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

int N, M, Q;
vector<int> a, b, c, d;

int compute(int prev_pos, vector<int> prev_val) {
  // cout << prev_pos << " / ";
  // for (int i = 0;i < N;i++) {
  //     cout << prev_val[i];
  // }cout << endl;

  if (prev_pos == -1) {
    prev_val[0] = 1;
    return compute(0, prev_val);
  }
  if (prev_pos == N - 1) {
    return 0;
  }
  vector<int> val_to_score(M + 1, 0);
  for (int i = 0; i < Q; i++) {
    if (b[i] == prev_pos + 1) {
      int next_value = prev_val[a[i]] + c[i];
      if (next_value <= M) {
        val_to_score[next_value] += d[i];
      }
    }
  }
  int max_score = 0;
  for (int i = prev_val[prev_pos]; i <= M; i++) {
    if (val_to_score[i] == 0 && i != prev_val[prev_pos])
      continue;
    prev_val[prev_pos + 1] = i;
    int score = val_to_score[i] + compute(prev_pos + 1, prev_val);
    if (max_score < score) {
      max_score = score;
    }
  }
  return max_score;
}

int main() {
  cin >> N >> M >> Q;
  for (int i = 0; i < Q; i++) {
    int ia, ib, ic, id;
    cin >> ia >> ib >> ic >> id;
    a.push_back(ia - 1);
    b.push_back(ib - 1);
    c.push_back(ic);
    d.push_back(id);
  }
  cout << compute(-1, vector<int>(N, -1)) << endl;
}


Error: 0

Fixed submission:

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

int N, M, Q;
vector<int> a, b, c, d;

int compute(int prev_pos, vector<int> prev_val) {
  // cout << prev_pos << " " << prev_val[prev_pos] << endl;
  if (prev_pos == -1) {
    prev_val[0] == 1;
    return compute(0, prev_val);
  }
  if (prev_pos == M - 1) {
    return 0;
  }
  vector<int> val_to_score(M + 1, 0);
  for (int i = 0; i < Q; i++) {
    if (b[i] == prev_pos + 1) {
      int next_value = prev_val[a[i]] + c[i];
      if (next_value <= M) {
        val_to_score[next_value] += d[i];
      }
    }
  }
  int max_score = 0;
  for (int i = prev_val[prev_pos]; i <= M; i++) {
    if (val_to_score[i] == 0 && i != prev_val[prev_pos])
      continue;
    prev_val[prev_pos + 1] = i;
    int score = val_to_score[i] + compute(prev_pos + 1, prev_val);
    if (max_score < score) {
      max_score = score;
    }
  }
  return max_score;
}

int main() {
  cin >> N >> M >> Q;
  for (int i = 0; i < Q; i++) {
    int ia, ib, ic, id;
    cin >> ia >> ib >> ic >> id;
    a.push_back(ia - 1);
    b.push_back(ib - 1);
    c.push_back(ic);
    d.push_back(id);
  }
  cout << compute(-1, vector<int>(N, 0)) << endl;
}
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++)
const int MOD = 1e9 + 7;
using ll = long long;

int main() {
  int n, m, q;
  cin >> n >> m >> q;
  vector<ll> a(q), b(q), c(q), d(q);
  rep(i, q) {
    int A, B, C, D;
    cin >> A >> B >> C >> D;
    A--;
    B--;
    a[i] = A;
    b[i] = B;
    c[i] = C;
    d[i] = D;
  }
  vector<vector<ll>> memo;
  for (int A = 1; A <= m; A++) {
    for (int B = A; B <= m; B++) {
      for (int C = B; C <= m; C++) {
        for (int D = C; D <= m; D++) {
          for (int E = D; E <= m; E++) {
            for (int F = E; F <= m; F++) {
              for (int G = F; G <= m; G++) {
                for (int H = G; H <= m; H++) {
                  for (int I = H; I <= m; I++) {
                    for (int J = I; J <= m; J++) {
                      memo.push_back({A, B, C, D, E, F, G, H, I, J});
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
  ll ans = 0;
  ll l = memo.size();
  for (ll i = 0; i < l; i++) {
    const vector<ll> test = memo[i];
    if (test.size() == 0)
      continue;
    ll res = 0;
    rep(j, q) {
      if (test[b[j]] - test[a[j]] == c[j])
        res += d[j];
    }
    ans = max(ans, res);
  }
  cout << ans << endl;
}


Error: 0

Fixed submission:

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

int main() {
  int n, m, q;
  cin >> n >> m >> q;
  vector<ll> a(n), b(n), c(n), d(n);
  rep(i, q) {
    int A, B, C, D;
    cin >> A >> B >> C >> D;
    A--;
    B--;
    a[i] = A;
    b[i] = B;
    c[i] = C;
    d[i] = D;
  }
  vector<vector<ll>> memo;
  for (int A = 1; A <= m; A++) {
    for (int B = A; B <= m; B++) {
      for (int C = B; C <= m; C++) {
        for (int D = C; D <= m; D++) {
          for (int E = D; E <= m; E++) {
            for (int F = E; F <= m; F++) {
              for (int G = F; G <= m; G++) {
                for (int H = G; H <= m; H++) {
                  for (int I = H; I <= m; I++) {
                    for (int J = I; J <= m; J++) {
                      memo.push_back({A, B, C, D, E, F, G, H, I, J});
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
  ll ans = 0;
  ll l = memo.size();
  for (ll i = 0; i < l; i++) {
    const vector<ll> test = memo[i];
    if (test.size() == 0)
      continue;
    ll res = 0;
    rep(j, q) {
      if (test[b[j]] - test[a[j]] == c[j])
        res += d[j];
    }
    ans = max(ans, res);
  }
  cout << ans << endl;
}
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

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

int N, M, Q;
long long ans = 0;
vector<int> a(50), b(50), c(50), d(50);

void dfs(vector<int> v, int depth, int lastNum) {
  if (depth == N) {
    // 数列が完成したらスコア計算
    long long score = 0;
    for (int i = 0; i < Q; i++) {
      if (v[b[i] - 1] - v[a[i] - 1] == c[i])
        score += d[i];
    }
    ans = max(ans, score);
  } else {
    for (int i = lastNum; i < M + 1; i++) {
      // 末尾の数字以上の数字を結合したパターン分岐
      vector<int> nw = v;
      nw.push_back(i);
      dfs(nw, depth + 1, i);
    }
  }
}

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

Error: -11

Fixed submission:

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

int N, M, Q;
long long ans = 0;
vector<int> a(M), b(M), c(M), d(M);

void dfs(vector<int> v, int depth, int lastNum) {
  if (depth == N) {
    // 数列が完成したらスコア計算
    long long score = 0;
    for (int i = 0; i < Q; i++) {
      if (v[b[i] - 1] - v[a[i] - 1] == c[i])
        score += d[i];
    }
    ans = max(ans, score);
  } else {
    for (int i = lastNum; i < M + 1; i++) {
      // 末尾の数字以上の数字を結合したパターン分岐
      vector<int> nw = v;
      nw.push_back(i);
      dfs(nw, depth + 1, i);
    }
  }
}

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

#include <algorithm>
#include <cmath>
#include <deque>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

#include <boost/multiprecision/cpp_int.hpp>

using namespace std;
using ll = long long;
namespace mp = boost::multiprecision;
using mpint = mp::cpp_int; // 多倍長整数（任意精度）

void dfs(int i, const int size, const int range_start, const int range_end,
         vector<int> &A, int &max, vector<int> a, vector<int> b, vector<int> c,
         vector<int> d) {
  if (i == size) {
    // for(int i = 0; i < A.size(); ++i){
    //   cout << A[i] << " ";
    // }

    int sum = 0;

    for (int i = 1; i < 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 = range_start; j <= range_end; ++j) {
      A[i] = j;
      dfs(i + 1, size, j, range_end, A, max, a, b, c, d);
    }
  }
}

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 max = -1;

  int size = N;
  int range_start = 1;
  int range_end = M;

  dfs(0, size, range_start, range_end, A, max, a, b, c, d);

  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, int N, int M, int start, vector<int> &A, vector<int> a,
         vector<int> b, vector<int> c, vector<int> d, int &max) {
  if (i == N) {
    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 <= M; j++) {
      A[i] = j;
      dfs(i + 1, N, M, j, A, a, b, c, d, max);
    }
  }
}

int main() {
  int N, M, Q;
  int i;
  int start = 1;
  int max = -1;

  cin >> N >> M >> Q;

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

  vector<int> A(N);

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

  dfs(0, N, M, start, A, a, b, c, d, max);

  cout << max << endl;

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

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

#define int long long

const int N = 55;
int a[N], b[N], c[N], d[N];
int n, m, q;
int ans = 0;

void rec(vector<int> temp) {
  if ((int)temp.size() == n) {
    int tot = 0;
    for (int i = 1; i <= q; i++) {
      if (temp[b[i] - 1] - temp[a[i] - 1] == c[i]) {
        tot += d[i];
      }
    }
    ans = max(ans, tot);
    return;
  }

  int back = (temp.size()) ? temp.back() : 1;
  for (int i = back; i <= m; i++) {
    temp.push_back(i);
    rec(temp);
    temp.pop_back();
  }
}

int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

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

  vector<int> temp;
  rec(temp);
  cout << ans << '\\n';
  return 0;
}


Error: 0

Fixed submission:

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

#define int long long

const int N = 12;
int a[N], b[N], c[N], d[N];
int n, m, q;
int ans = 0;

void rec(vector<int> temp) {
  if (temp.size() == n) {
    int tot = 0;
    for (int i = 1; i <= q; i++) {
      if (temp[b[i] - 1] - temp[a[i] - 1] == c[i]) {
        tot += d[i];
      }
    }
    ans = max(ans, tot);
    return;
  }

  int back = (temp.size()) ? temp.back() : 1;
  for (int i = back; i <= m; i++) {
    temp.push_back(i);
    rec(temp);
    temp.pop_back();
  }
}

int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

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

  vector<int> temp;
  rec(temp);
  cout << ans << '\\n';
  return 0;
}
Problem p02695 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>
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;
    for (int i = 0; i < q; i++) {
      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);
  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;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
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;
    for (int i = 0; i < q; i++) {
      if (A[b[i]] - A[a[i]] == c[i])
        now += d[i];
    }
    ans = max(ans, now);
  }
  A.push_back(A.back());
  while (A.back() <= m) {
    dfs(A);
    A.back()++;
  }
}
int main() {
  cin >> n >> m >> 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;
}
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define repo(i, n) for (int i = 1; i <= n; i++)
#define INF 1001001001
#define INFll 100100100100100
// debug用
#define PrintVec(x)                                                            \\
  for (auto elementPrintVec : x) {                                             \\
    cout << elementPrintVec << " ";                                            \\
  }                                                                            \\
  cout << endl;

using namespace std;
using ull = unsigned long long;
using ll = long long;
using P = pair<int, int>;
const int mod = 1000000007;

vector<int> a;
vector<int> b;
vector<int> c;
vector<int> d;
int ans = 0;
int n, m, q;

void dfs(vector<int> A) {
  if (A.size() == n + 1) {
    int score = 0;
    rep(i, q) {
      if (A[b[i]] - A[a[i]] == c[i]) {
        score += d[i];
      }
    }
    ans = max(ans, score);
    return;
  }
  A.push_back(A.back());
  while (A.back() <= m) {
    dfs(A);
    A.back()++;
  }
}

int main() {
  cin >> n >> m >> q;
  a.resize(q);
  b.resize(q);
  c.resize(q);
  d.resize(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 = 0; i < n; i++)
#define repo(i, n) for (int i = 1; i <= n; i++)
#define INF 1001001001
#define INFll 100100100100100
// debug用
#define PrintVec(x)                                                            \\
  for (auto elementPrintVec : x) {                                             \\
    cout << elementPrintVec << " ";                                            \\
  }                                                                            \\
  cout << endl;

using namespace std;
using ull = unsigned long long;
using ll = long long;
using P = pair<int, int>;
const int mod = 1000000007;

vector<int> a;
vector<int> b;
vector<int> c;
vector<int> d;
int ans = 0;
int n, m, q;

void dfs(vector<int> A) {
  if (A.size() == n + 1) {
    int score = 0;
    rep(i, q) {
      if (A[b[i]] - A[a[i]] == c[i]) {
        score += d[i];
      }
    }
    ans = max(ans, score);
    return;
  }
  A.push_back(A.back());
  while (A.back() <= m) {
    dfs(A);
    A.back()++;
  }
}

int main() {
  cin >> n >> m >> q;
  a.resize(n);
  b.resize(n);
  c.resize(n);
  d.resize(n);
  rep(i, q) { cin >> a[i] >> b[i] >> c[i] >> d[i]; }
  dfs(vector<int>(1, 1));
  cout << ans << endl;
  return 0;
}
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm> //sort,reverse
#include <deque>     //double_ended queue
#include <iostream>
#include <map>   //map
#include <queue> //queue,priority_queue
#include <string>
#include <utility> //pair
#include <vector>
using namespace std;
typedef long long LL;
typedef pair<LL, LL> P; // Pでpair<-,->を表す。
const LL MAX = 2000010;
const LL MOD = 1000000007;
typedef map<LL, LL>::iterator Itr;

int main() {
  LL n, m, q;
  cin >> n >> m >> q;
  LL a[q];
  LL b[q];
  LL c[q];
  LL d[q];
  for (int i = 0; i < q; i++) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  }
  LL ans = 0;
  vector<int> vec;
  vec.push_back(1);
  while (!vec.empty()) {
    LL cnt = 0;
    int S = vec.size();
    if (vec[S - 1] > m) {
      vec.pop_back();
      if (S != 1) {
        vec[S - 2]++;
      }
      continue;
    }
    if (S < n) {
      vec.push_back(vec[S - 1]);
      continue;
    }

    for (int i = 0; i < q; i++) {
      if (vec[b[i] - 1] - vec[a[i] - 1] == c[i]) {
        cnt = cnt + d[i];
      }
    }
    if (ans < cnt) {
      ans = cnt;
    }
    /*for(int i=0;i<S;i++){cout<<vec[i]<<' ';}*/
    vec[S - 1]++;
    // cout<<endl;
  }
  cout << ans << endl;
  return 0;
}

Error: -6
double free or corruption (out)


Fixed submission:

#include <algorithm> //sort,reverse
#include <deque>     //double_ended queue
#include <iostream>
#include <map>   //map
#include <queue> //queue,priority_queue
#include <string>
#include <utility> //pair
#include <vector>
using namespace std;
typedef long long LL;
typedef pair<LL, LL> P; // Pでpair<-,->を表す。
const LL MAX = 2000010;
const LL MOD = 1000000007;
typedef map<LL, LL>::iterator Itr;

int main() {
  LL n, m, q;
  cin >> n >> m >> q;
  LL a[q];
  LL b[q];
  LL c[q];
  LL d[q];
  for (int i = 0; i < q; i++) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  }
  LL ans = 0;
  vector<int> vec;
  vec.push_back(1);
  while (!vec.empty()) {
    LL cnt = 0;
    int S = vec.size();
    if (vec[S - 1] > m) {
      vec.pop_back();
      vec[S - 2]++;
      continue;
    }
    if (S < n) {
      vec.push_back(vec[S - 1]);
      continue;
    }

    for (int i = 0; i < q; i++) {
      if (vec[b[i] - 1] - vec[a[i] - 1] == c[i]) {
        cnt = cnt + d[i];
      }
    }
    if (ans < cnt) {
      ans = cnt;
    }
    /*for(int i=0;i<S;i++){cout<<vec[i]<<' ';}*/
    vec[S - 1]++;
    cout << endl;
  }
  cout << ans << endl;
  return 0;
}
Problem p02695 (C++) — buggy submission, verdict: Time Limit Exceeded

// https://atcoder.jp/contests/abc165/tasks/abc165_c
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define REP(i, n) FOR(i, 0, n)
#define REPR(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, s, n) for (int i = (s), i##_len = (n); i < i##_len; ++i)
#define ALL(obj) (obj).begin(), (obj).end()
#define ALLR(obj) (obj).rbegin(), (obj).rend()
#define DIV(a, b) ((a - 1) / b + 1)

struct FOUR {
  int a, b, c, d;
};
int N, M, Q, ans;
vector<FOUR> D;
void dfs(vector<int> &A) {
  if ((int)A.size() == N) {
    int sum = 0;
    for (auto &x : D) {
      if (A[x.b - 1] - A[x.a - 1] == x.c) {
        sum += x.d;
      }
    }
    ans = max(ans, sum);
    return;
  }

  FOR(i, (A.size() > 0 ? A.back() : 0), M) {
    A.push_back(i);
    dfs(A);
    A.pop_back();
  }
}
signed main() {
  cin >> N >> M >> Q;
  D.resize(Q);
  for (auto &x : D) {
    cin >> x.a >> x.b >> x.c >> x.d;
  }
  vector<int> A;
  dfs(A);
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

// https://atcoder.jp/contests/abc165/tasks/abc165_c
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define REP(i, n) FOR(i, 0, n)
#define REPR(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, s, n) for (int i = (s), i##_len = (n); i < i##_len; ++i)
#define ALL(obj) (obj).begin(), (obj).end()
#define ALLR(obj) (obj).rbegin(), (obj).rend()
#define DIV(a, b) ((a - 1) / b + 1)

class Radix {
private:
  const char *s;
  int a[128];

public:
  Radix(const char *s = "0123456789ABCDEF") : s(s) {
    int i;
    for (i = 0; s[i]; ++i)
      a[(int)s[i]] = i;
  }
  std::string to(long long p, int q) {
    int i;
    if (!p)
      return "0";
    char t[64] = {};
    for (i = 62; p; --i) {
      t[i] = s[p % q];
      p /= q;
    }
    return std::string(t + i + 1);
  }
  std::string to(const std::string &t, int p, int q) { return to(to(t, p), q); }
  long long to(const std::string &t, int p) {
    int i;
    long long sm = a[(int)t[0]];
    for (i = 1; i < (int)t.length(); ++i)
      sm = sm * p + a[(int)t[i]];
    return sm;
  }
};

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

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N, M, Q;
  cin >> N >> M >> Q;
  vector<FOUR> v(Q);
  for (auto &x : v) {
    cin >> x.a >> x.b >> x.c >> x.d;
  }

  int ans = 0;
  REP(i, pow(M, N)) {
    Radix r;
    string t = r.to(i, M);
    int plen = N - t.length();
    REP(j, plen) { t = "0" + t; }
    bool judge = true;
    FOR(j, 1, t.length()) {
      if (t[j - 1] > t[j]) {
        judge = false;
        break;
      }
    }
    if (judge) {
      int sum = 0;
      for (auto &x : v) {
        if (t[x.b - 1] - t[x.a - 1] == x.c) {
          sum += x.d;
        }
      }
      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 n, m, q, a[55], b[55], c[55], d[55], slot[20], has, flag[15];
void f(int k, int las) {
  if (k > n) {
    int now = 0;
    // for(int i=1;i<=n;i++) cout<<slot[i]<<" ";
    for (int i = 1; i <= q; i++) {
      if (slot[b[i]] - slot[a[i]] == c[i])
        now += d[i];
    }
    // cout<<" , now= "<<now<<endl<<"========"<<endl;
    has = max(has, now);
    return;
  }
  for (int i = las; i <= m; i++) {
    slot[k] = i;
    f(k + 1, i);
  }
}
int main() {
  cin >> n >> m >> q;
  for (int i = 1; i <= q; i++)
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  f(1, 1);
  cout << has << endl;
}

Error: 0

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();
    return;
  }
  for (int i = val; i <= m; i++) {
    dfs(now + 1, i);
  }
}

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;

int main(void) {
  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];
    a[i]--, b[i]--;
  }

  vector<int> A(N);
  string nm = "";
  for (int i = 0; i < M; i++) {
    nm += "0";
  }
  for (int i = 0; i < N; i++) {
    nm += "1";
  }

  int ans = 0;
  do {
    int count = 0;
    int j = 0;
    if (nm[0] == '1')
      continue;
    for (int i = 0; i < N + M; i++) {
      if (nm[i] == '0')
        count++;
      if (nm[i] == '1') {
        A[j] = count;
        j++;
      }
    }

    int score = 0;
    for (int q = 0; q < Q; q++) {
      if (A[b[q]] - A[a[q]] == c[q]) {
        score += d[q];
      }
    }
    ans = max(score, ans);

  } while (std::next_permutation(nm.begin(), nm.end()));

  cout << ans << endl;
}

Error: 0

Fixed submission:

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

int main(void) {
  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];
    a[i]--, b[i]--;
  }

  vector<int> A(N);
  string nm = "";
  for (int i = 0; i < N; i++) {
    nm += "0";
  }
  for (int i = 0; i < M - 1; i++) {
    nm += "1";
  }

  int ans = 0;
  do {
    int count = 0;
    int j = 0;

    for (int i = 0; i < N + M; i++) {
      if (nm[i] == '0')
        count++;
      if (nm[i] == '1') {
        A[j] = count + 1;
        j++;
      }
    }

    int score = 0;
    for (int q = 0; q < Q; q++) {
      if (A[b[q]] - A[a[q]] == c[q]) {
        score += d[q];
      }
    }
    ans = max(score, ans);

  } while (std::next_permutation(nm.begin(), nm.end()));

  cout << ans << endl;
}