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

#pragma GCC target("avx512f,avx512dq,avx512cd,avx512bw,avx512vl")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define ll long long

vector<int> A(10);
vector<vector<int>> L(50, vector<int>(4));
int N, M, Q, ans, point;

int dfs(int cn) {
  int i, j;
  for (i = A[cn - 1]; i <= M; i++) {
    A[cn] = i;
    if (cn == N - 1) {
      point = 0;
      for (j = 0; j < Q; j++) {
        if (A[L[j][1] - 1] - A[L[j][0] - 1] == L[j][2])
          point += L[j][3];
      }
      ans = max(ans, point);
      // return 0;
    } else {
      dfs(cn + 1);
    }
  }
  return 0;
}

int main() {
  int i, j;
  cin >> N >> M >> Q;
  ans = 0;
  point = 0;
  for (i = 0; i < Q; i++)
    for (j = 0; j < 4; j++)
      cin >> L[i][j];
  A[0] = 1;
  dfs(1);
  cout << ans << endl;
}

Error: -4

Fixed submission:

#pragma GCC target("avx512f,avx512dq,avx512cd,avx512bw,avx512vl")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define ll long long

vector<int> A(10);
vector<vector<int>> L(50, vector<int>(4));
int N, M, Q, ans, point;

int dfs(int cn) {
  int i, j;
  for (i = 1; i <= A[10 - cn]; i++) {
    A[9 - cn] = i;
    if (cn == N) {
      point = 0;
      for (j = 0; j < Q; j++) {
        if (A[10 - L[j][0]] - A[10 - L[j][1]] == L[j][2])
          point += L[j][3];
      }
      // cout<<point<<endl;
      ans = max(ans, point);
      return 0;
    } else {
      dfs(cn + 1);
    }
  }
  return 0;
}

int main() {
  int i, j;
  cin >> N >> M >> Q;
  ans = 0;
  point = 0;
  for (i = 0; i < Q; i++)
    for (j = 0; j < 4; j++)
      cin >> L[i][j];
  A[9] = M;
  dfs(1);
  cout << ans << endl;
}
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 ans = 0;
void dfs(vector<int> k) {
  if ((int)k.size() == n + 1) {
    int sco = 0;
    for (int i = 0; i < q; i++) {
      if (k.at(b.at(i)) - k.at(a.at(i)) == c.at(i))
        sco += d.at(i);
    }
    ans = max(ans, sco);
    return;
  }

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

int main() {
  cin >> n >> m >> q;
  a = b = c = d = vector<int>(q);
  for (int i = 0; i < q; i++)
    cin >> a.at(i) >> b.at(i) >> c.at(i) >> d.at(i);

  vector<int> sam(1, 1);
  dfs(sam);
  cout << ans << endl;
}

Error: 0

Fixed submission:

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

int n, m, q;
vector<int> a, b, c, d;
int ans = 0;
void dfs(vector<int> k) {
  if ((int)k.size() == m + 1) {
    int sco = 0;
    for (int i = 0; i < q; i++) {
      if (k.at(b.at(i)) - k.at(a.at(i)) == c.at(i))
        sco += d.at(i);
    }
    ans = max(ans, sco);
    return;
  }

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

int main() {
  cin >> n >> m >> q;
  a = b = c = d = vector<int>(q);
  for (int i = 0; i < q; i++)
    cin >> a.at(i) >> b.at(i) >> c.at(i) >> d.at(i);

  vector<int> sam(1, 1);
  dfs(sam);
  cout << ans << endl;
}
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
int N, M, Q;
vector<int> a, b, c, d;
ll ans;
// 参考:解説放送
void dfs(vector<int> A) {

  if (A.size() == N + 1) //([0]を1にしてi-indexedにしているため)
  {
    // calc ans
    ll ans2 = 0;
    rep(j, Q) // それぞれのクエリについて
    {
      if (A.at(b[j] - 1) - A.at(a[j] - 1) == c[j])
        ans2 += d[j];
    }
    ans = max(ans, ans2);
    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;
}
/*vector <int> a(3);
for (int i = 0; i < n; i++) {
    cin >> a.at(i);
  }*/

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
int N, M, Q;
vector<int> a, b, c, d;
ll ans;
// 参考:解説放送
void dfs(vector<int> A) {

  if (A.size() == M + 1) //([0]を1にしてi-indexedにしているため)
  {
    // calc ans
    ll ans2 = 0;
    rep(j, Q) // それぞれのクエリについて
    {
      if (A.at(b[j] - 1) - A.at(a[j] - 1) == c[j])
        ans2 += d[j];
    }
    ans = max(ans, ans2);
    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;
}
/*vector <int> a(3);
for (int i = 0; i < n; i++) {
    cin >> a.at(i);
  }*/
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <math.h>
#include <set>
#include <stdio.h>
#include <stdlib.h>

// などの必要なインクルードファイルすべて（3章の各標準ライブラリの説明でそれぞれ書いています）
using namespace std;

int N, M, Q;

int a[50][4];

int A[10];

int t, c;

void tokuten(int k) {

  int i, j;

  if (k == N) {

    c = 0;

    for (j = 0; j < Q; j++) {

      if ((A[a[j][1] - 1] - A[a[j][0] - 1]) == a[j][2]) {

        c = c + a[j][3];
      }
    }

    if ((t < c)) {
      t = c;
    } // 最高点dしたら入れ替え

  } else {

    if (k == 0) {

      for (i = 1; i < M + 1; i++) {

        A[k] = i;

        tokuten(k + 1);
      }

    } else {

      for (i = A[k - 1]; i < M + 1; i++) {

        A[k] = i;

        tokuten(k + 1);
      }
    }
  }
}

int main(void) {

  int i, j;

  t = 0;

  scanf("%d%d%d", &N, &M, &Q);

  for (i = 0; i < Q; i++) {

    scanf("%d%d%d%d", &a[i][0], &a[i][1], &a[i][2], &a[i][3]);
  }

  for (i = 0; i < N; i++) {

    A[i] = 1;
  }

  tokuten(0);

  printf("%d", t);
}

Error: WA

Fixed submission:

#include <iostream>
#include <math.h>
#include <set>
#include <stdio.h>
#include <stdlib.h>

// などの必要なインクルードファイルすべて（3章の各標準ライブラリの説明でそれぞれ書いています）
using namespace std;

int N, M, Q;

int a[50][4];

int A[10];

int t, c;

int tokuten(int k) {

  int i, j;

  if (k == N) {

    c = 0;

    for (j = 0; j < Q; j++) {

      if ((A[a[j][1] - 1] - A[a[j][0] - 1]) == a[j][2]) {

        c = c + a[j][3];
      }
    }

    if ((t < c)) {
      t = c;
    } // 最高点dしたら入れ替え

  } else {

    if (k == 0) {

      for (i = 1; i < M + 1; i++) {

        A[k] = i;

        tokuten(k + 1);
      }

    } else {

      for (i = A[k - 1]; i < M + 1; i++) {

        A[k] = i;

        tokuten(k + 1);
      }
    }
  }
}

int main(void) {

  int i, j;

  t = 0;

  scanf("%d%d%d", &N, &M, &Q);

  for (i = 0; i < Q; i++) {

    scanf("%d%d%d%d", &a[i][0], &a[i][1], &a[i][2], &a[i][3]);
  }

  for (i = 0; i < N; i++) {

    A[i] = 1;
  }

  tokuten(0);

  printf("%d", t);
}
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>

using namespace std;

#ifndef ONLINE_JUDGE
#include <fstream>
#define cin fin
#define cout fout
ifstream fin("test.in");
ofstream fout("test.out");
#endif

int p[11];
int n, m, q;
int a[60], b[60], c[60], d[60];

int f(int i, int fn) {
  if (i == n + 1) {
    int r = 0;
    /*for (int j = 1; j <= n; j++)
      cout << p[j] << ' ';*/
    for (int j = 1; j <= q; j++) {
      if (p[b[j]] - p[a[j]] == c[j])
        r += d[j];
    }
    // cout << r << endl;
    return r;
  }
  int r = 0;
  for (int j = fn; j <= m;
       j++) { // intram cu toate permutarile de lungime n cu valori de la 1 la m
    p[i] = j;
    r = max(r, f(i + 1, j));
  }
  return r;
}

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 = 0; i < q; i++) {
    a[i]--; b[i]--;
  }*/
  cout << f(1, 1) << endl;
  return 0;
}


Error: WA

Fixed submission:

#include <iostream>

using namespace std;

#ifndef ONLINE_JUDGE
#include <fstream>
#define cin fin
#define cout fout
ifstream fin("test.in");
ofstream fout("test.out");
#endif

int p[10];
int n, m, q;
int a[60], b[60], c[60], d[60];

int f(int i, int fn) {
  if (i == n + 1) {
    int r = 0;
    /*for (int j = 1; j <= n; j++)
      cout << p[j] << ' ';*/
    for (int j = 1; j <= q; j++) {
      if (p[b[j]] - p[a[j]] == c[j])
        r += d[j];
    }
    // cout << r << endl;
    return r;
  }
  int r = 0;
  for (int j = fn; j <= m;
       j++) { // intram cu toate permutarile de lungime n cu valori de la 1 la m
    p[i] = j;
    r = max(r, f(i + 1, j));
  }
  return r;
}

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 = 0; i < q; i++) {
    a[i]--; b[i]--;
  }*/
  cout << f(1, 1) << 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 pb push_back
#define all(v) (v).begin(), (v).end()
#define fi first
#define se second
#define sz(x) ((int)(x).size())
using ll = long long;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define MOD 1000000007
const ll INF = 1e18;
template <class T> void show(vector<T> v) {
  for (int i = 0; i < v.size(); i++) {
    cerr << v[i] << " ";
  }
  cerr << endl;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

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

// Aの末尾にM以下の数を追加していく
void dfs(vector<int> A) {
  if (sz(A) == n + 1) {
    int now = 0;
    rep(i, q) {
      if (A[b[i]] - A[a[i]] == c[i])
        now += d[i];
    }
    chmax(ans, now);
    return;
  }
  // Aの末尾に直前の数を基準に追加していく 入れられるのはm以下まで
  // そのためにdfsの最初にはvector<int>(1,1)をいれている
  // 1-indexにもなるしちょうど都合がいい
  A.pb(A.back());
  while (A.back() <= m) {
    dfs(A);
    // 末尾を+1して次の状態へ
    A.back()++;
  }
}

int main(int argc, char const *argv[]) {
  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>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define fi first
#define se second
#define sz(x) ((int)(x).size())
using ll = long long;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define MOD 1000000007
const ll INF = 1e18;
template <class T> void show(vector<T> v) {
  for (int i = 0; i < v.size(); i++) {
    cerr << v[i] << " ";
  }
  cerr << endl;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

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

// Aの末尾にM以下の数を追加していく
void dfs(vector<int> A) {
  if (sz(A) == n + 1) {
    int now = 0;
    rep(i, q) {
      if (A[b[i]] - A[a[i]] == c[i])
        now += d[i];
    }
    chmax(ans, now);
    return;
  }
  // Aの末尾に直前の数を基準に追加していく 入れられるのはm以下まで
  // そのためにdfsの最初にはvector<int>(1,1)をいれている
  // 1-indexにもなるしちょうど都合がいい
  A.pb(A.back());
  while (A.back() <= m) {
    dfs(A);
    A.back()++;
  }
}

int main(int argc, char const *argv[]) {
  cin >> n >> m >> q;
  a = b = c = d = vector<int>(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 <iostream>
#include <vector>

using namespace std;
using ll = long long int;

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];
  }
  int mx = 0, score;
  vector<int> s(n, 1);
  do {
    score = 0;
    // for (int i = 0; i < n; i++) cout << s[i];
    // cout << " ";
    for (int i = 0; i < q; i++) {
      if (s[b[i] - 1] - s[a[i] - 1] == c[i])
        score += d[i];
    }
    // cout << score << endl;
    mx = max(mx, score);
    s[n - 1]++;
    if (m != 1) {
      for (int i = 0; i < n; i++) {
        if (s[n - 1 - i] > m) {
          s[n - 2 - i]++;
          for (int j = 0; j <= i; j++) {
            s[n - 1 - i + j] = s[n - 2 - i];
          }
        }
      }
    }
  } while (s[0] != m);
  cout << mx << endl;
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;
using ll = long long int;

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];
  }
  int mx = 0, score;
  vector<int> s(n, 1);
  do {
    score = 0;
    // for (int i = 0; i < n; i++) cout << s[i];
    // cout << " ";
    for (int i = 0; i < q; i++) {
      if (s[b[i] - 1] - s[a[i] - 1] == c[i])
        score += d[i];
    }
    // cout << score << endl;
    mx = max(mx, score);
    for (int i = 0; i < n; i++) {
      if (i == 0)
        s[n - 1 - i]++;
      if (s[n - 1 - i] > m) {
        s[n - 2 - i]++;
        s[n - 1 - i] = s[n - 2 - i];
      }
    }
  } while (s[0] != m);
  cout << mx << 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 calc(vector<int> &A) {
  long long cur = 0;
  for (int i = 0; i < q; i++) {
    if (A[a[i] - 1] + c[i] == A[b[i] - 1])
      cur += d[i];
  }
  ans = max(ans, cur);
}

void rec(vector<int> &A, int idx) {
  if (idx < n) {
    for (int nx = A[idx - 1]; nx <= m; nx++) {
      vector<int> B(A.begin(), A.end());
      B[idx] = nx;
      rec(B, idx + 1);
    }
  } else {
    calc(A);
  }
}

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

  vector<int> A(n, 1);
  rec(A, 1);
  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

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

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

void calc(vector<int> &A) {
  long long cur = 0;
  for (int i = 0; i < q; i++) {
    if (A[a[i] - 1] + c[i] == A[b[i] - 1])
      cur += d[i];
  }
  ans = max(ans, cur);
}

void rec(vector<int> &A, int idx) {
  if (idx < n) {
    for (int nx = A[idx - 1]; nx <= m; nx++) {
      vector<int> B(A.begin(), A.end());
      B[idx] = nx;
      rec(B, idx + 1);
    }
  } else {
    calc(A);
  }
}

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

  vector<int> A(n, 1);
  rec(A, 1);
  cout << ans << endl;

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

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

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

void dfs(vector<int> A) {
  if (A.size() == n + 1) {
    // calc score
    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];
  }
  dfs(vector<int>(1, 1));
  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

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

void dfs(vector<int> A) {
  if (A.size() == n + 1) {
    // calc score
    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);
    }
  }

  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];
  }
  dfs(vector<int>(1, 1));
  cout << ans << endl;
}
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>

using namespace std;
int maxv = 0;
struct node {
  int a, b, c, d;
};
node tmp[100];
int arr[20];
int n, m, q;
void dfs(int u, int last) {
  if (u == n + 1) {
    int sum = 0;
    for (int i = 1; i <= q; i++) {
      if (arr[tmp[i].b] - arr[tmp[i].a] == tmp[i].c) {
        sum += tmp[i].d;
      }
    }
    maxv = max(maxv, sum);
    return;
  }
  for (int i = last; i <= m; i++) {
    arr[u] = i;
    dfs(u + 1, i);
  }
}
int main() {
  cin >> n >> m >> q;
  for (int i = 1; i <= q; i++) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    tmp[i] = {a, b, c, d};
  }
  dfs(1, 1);
  cout << maxv << endl;
}

Error: 0

Fixed submission:

#include <iostream>

using namespace std;
int maxv = 0;
struct node {
  int a, b, c, d;
};
node tmp[20];
int arr[20];
int n, m, q;
void dfs(int u, int last) {
  if (u == n + 1) {
    int sum = 0;
    for (int i = 1; i <= q; i++) {
      if (arr[tmp[i].b] - arr[tmp[i].a] == tmp[i].c) {
        sum += tmp[i].d;
      }
    }
    maxv = max(maxv, sum);
    return;
  }
  for (int i = last; i <= m; i++) {
    arr[u] = i;
    dfs(u + 1, i);
  }
}
int main() {
  cin >> n >> m >> q;
  for (int i = 1; i <= q; i++) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    tmp[i] = {a, b, c, d};
  }
  dfs(1, 1);
  cout << maxv << endl;
}
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>
#define ll long long
using namespace std;
vector<int> buf;
vector<vector<int>> a_s;
int vectorrow = 0;

void dfs(int i, const int size, const int range_start, const int range_end) {
  if (i == size) {
    a_s.push_back(buf);
    vectorrow++;
  } else {
    int lastd = range_start;
    if (i > 0) {
      lastd = buf[i - 1];
    }
    for (int j = lastd; j <= range_end; ++j) {
      buf[i] = j;
      dfs(i + 1, size, range_start, range_end);
    }
  }
}

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++) {
    int ai, bi;
    cin >> ai >> bi >> c[i] >> d[i];
    a[i] = ai - 1;
    b[i] = bi - 1;
  }
  buf.resize(n);
  dfs(0, n, 1, m);
  int max = 0;
  for (int i = 0; i < a_s.size(); i++) {
    int point = 0;
    for (int j = 0; j < q; j++) {
      if (a_s[i][b[j]] - a_s[i][a[j]] == c[j]) {
        point += d[j];
      }
    }
    if (max < point) {
      max = point;
    }
  }
  cout << max << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
#define ll long long
using namespace std;
vector<int> buf;
vector<vector<int>> a_s;
int vectorrow = 0;

void dfs(int i, const int size, const int range_start, const int range_end) {
  if (i == size) {
    a_s.push_back(buf);
    vectorrow++;
  } else {
    int lastd = range_start;
    if (i > 0) {
      lastd = buf[i - 1];
    }
    for (int j = lastd; j <= range_end; ++j) {
      buf[i] = j;
      dfs(i + 1, size, range_start, range_end);
    }
  }
}

int main(void) {
  int size = 3;
  int range_start = 1;
  int range_end = 5;

  int n, m, q;
  cin >> n >> m >> q;
  vector<int> a(n), b(n), c(n), d(n);
  for (int i = 0; i < q; i++) {
    int ai, bi;
    cin >> ai >> bi >> c[i] >> d[i];
    a[i] = ai - 1;
    b[i] = bi - 1;
  }
  buf.resize(n);
  dfs(0, n, 1, m);
  int max = 0;
  for (int i = 0; i < a_s.size(); i++) {
    int point = 0;
    for (int j = 0; j < q; j++) {
      if (a_s[i][b[j]] - a_s[i][a[j]] == c[j]) {
        point += d[j];
      }
    }
    if (max < point) {
      max = point;
    }
  }
  cout << max << endl;
  return 0;
}
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <unordered_map>
#include <vector>

using namespace std;

int n, m, q;
vector<vector<unordered_map<int, int>>> v;

void dfs(vector<int> &cur, int score, int &res) {
  if ((int)cur.size() == n) {
    res = max(res, score);
    return;
  }
  int i = cur.empty() ? 1 : cur.back();
  for (; i <= m; i++) {
    cur.push_back(i);
    int ns = score;
    for (int j = 0; j < (int)cur.size() - 1; j++) {
      ns += v[cur.size()][j + 1][i - cur[j]];
    }
    dfs(cur, ns, res);
    cur.pop_back();
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin >> n >> m >> q;
  v = vector<vector<unordered_map<int, int>>>(
      n + 1, vector<unordered_map<int, int>>(n + 1, unordered_map<int, int>()));
  for (int i = 0; i < q; i++) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    v[b][a][c] = d;
  }

  int res = 0;
  vector<int> cur;
  dfs(cur, 0, res);
  cout << res << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <unordered_map>
#include <vector>

using namespace std;

int n, m, q;
vector<vector<unordered_map<int, int>>> v;

void dfs(vector<int> &cur, int score, int &res) {
  if ((int)cur.size() == n) {
    res = max(res, score);
    return;
  }
  int i = cur.empty() ? 1 : cur.back();
  for (; i <= m; i++) {
    cur.push_back(i);
    int ns = score;
    for (int j = 0; j < (int)cur.size() - 1; j++) {
      ns += v[cur.size()][j + 1][i - cur[j]];
    }
    dfs(cur, ns, res);
    cur.pop_back();
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin >> n >> m >> q;
  v = vector<vector<unordered_map<int, int>>>(
      m + 1, vector<unordered_map<int, int>>(m + 1, unordered_map<int, int>()));
  for (int i = 0; i < q; i++) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    v[b][a][c] = d;
  }

  int res = 0;
  vector<int> cur;
  dfs(cur, 0, res);
  cout << res << endl;
  return 0;
}
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
#include <stdlib.h>
#define rep(i, N) for (int i = 0; i < (int)N; i++)
int N, M, Q, answer, a[51], b[51], c[51], d[51], array[10] = {};
void DFS(int x) {
  if (x >= N) {
    int point = 0;
    rep(i, Q) if (array[b[i]] - array[a[i]] == c[i]) point += d[i];
    answer = (answer < point) ? point : answer;
    return;
  } else {
    int j = array[x - 1];
    while (j <= M)
      array[x] = j, DFS(x + 1), j++;
  }
}
int main(void) {
  answer = scanf("%d %d %d", &N, &M, &Q);
  rep(i, Q) {
    answer = scanf("%d %d %d %d", &a[i], &b[i], &c[i], &d[i]);
    a[i]--, b[i]--;
  }
  array[0] = 1, answer = 0, DFS(1);
  return printf("%d", answer), 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, N) for (int i = 0; i < (int)N; i++)
int main() {
  int N, M, Q;
  int64_t S, point = 0;
  cin >> N >> M >> Q;
  vector<int> a(Q, 0), b(Q, 0), c(Q, 0);
  vector<int64_t> d(Q, 0), p;
  rep(i, M) p.push_back(i + 1);
  rep(i, Q) cin >> a.at(i) >> b.at(i) >> c.at(i) >> d.at(i);
  do {
    S = 0;
    rep(i, Q) if (p.at(b.at(i) - 1) - p.at(a.at(i) - 1) == c.at(i)) S +=
        d.at(i);
    point = max(S, point);
  } while (next_permutation(p.begin(), p.end()));
  return cout << point << endl, 0;
}